2
0

or-proof-progress.spec.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. var OrProofTestData = require('../data/or-proof-data');
  11. var ProgressMeterTestData = require('../data/progress-meter-data');
  12. var zkProof = require('../../lib/index');
  13. var elGamal = require('scytl-elgamal');
  14. describe('The zero-knowledge proof module should be able to ...', function() {
  15. var proofService_;
  16. var progressMeterTestData_;
  17. var group_;
  18. var secret_;
  19. var ciphertext_;
  20. var publicKey_;
  21. var elements_;
  22. var numElements_;
  23. var index_;
  24. var proofHandler_;
  25. var proof_;
  26. var numProgressChecks_;
  27. var callback_;
  28. var defaultMinCheckInterval_;
  29. var customMinCheckInterval_;
  30. beforeAll(function() {
  31. proofService_ = zkProof.newService();
  32. progressMeterTestData_ = new ProgressMeterTestData();
  33. var proofTestData = new OrProofTestData();
  34. var elGamalService = elGamal.newService();
  35. group_ = proofTestData.getGroup();
  36. var encryptedElements = proofTestData.getEncryptedElements();
  37. secret_ = encryptedElements.secret;
  38. ciphertext_ = elGamalService.newEncryptedElements(
  39. encryptedElements.gamma, encryptedElements.phis);
  40. publicKey_ = proofTestData.getPublicKey();
  41. elements_ = proofTestData.getElements();
  42. numElements_ = elements_.length;
  43. index_ = proofTestData.getIndex();
  44. proofHandler_ = proofService_.newOrProofHandler(group_).init(publicKey_);
  45. proof_ = proofHandler_.generate(secret_, elements_, index_, ciphertext_);
  46. numProgressChecks_ = proofTestData.getNumProgressChecks();
  47. callback_ = progressMeterTestData_.progressCallback;
  48. defaultMinCheckInterval_ =
  49. progressMeterTestData_.getDefaultProgressPercentMinCheckInterval();
  50. customMinCheckInterval_ =
  51. progressMeterTestData_.getCustomProgressPercentMinCheckInterval();
  52. });
  53. beforeEach(function() {
  54. progressMeterTestData_.initializeProgressMeterData();
  55. });
  56. describe('create a zero-knowledge proof service that should be able to ..', function() {
  57. describe('create an OR proof handler that should be able to', function() {
  58. it('measure progress when generating an OR proof', function() {
  59. var proof = proofHandler_.measureProgress(callback_).generate(
  60. secret_, elements_, index_, ciphertext_);
  61. checkVerifiedAndProgressMeasured(proof);
  62. });
  63. it('measure progress when pre-computing an OR proof', function() {
  64. var preComputation =
  65. proofHandler_.measureProgress(callback_).preCompute(numElements_);
  66. var proof = proofHandler_.generate(
  67. secret_, elements_, index_, ciphertext_,
  68. {preComputation: preComputation});
  69. checkVerifiedAndProgressMeasured(proof);
  70. });
  71. it('measure progress when verifying an OR proof', function() {
  72. var verified = proofHandler_.measureProgress(callback_).verify(
  73. proof_, elements_, ciphertext_);
  74. expect(verified).toBeTruthy();
  75. checkProgressMeasured();
  76. });
  77. it('not measure progress when generating an OR proof and the progress meter is not used',
  78. function() {
  79. var proof =
  80. proofHandler_.generate(secret_, elements_, index_, ciphertext_);
  81. checkVerifiedAndProgressNotMeasured(proof);
  82. });
  83. it('not measure progress when pre-computing an OR proof and the progress meter is not used',
  84. function() {
  85. var preComputation = proofHandler_.preCompute(numElements_);
  86. var proof = proofHandler_.generate(
  87. secret_, elements_, index_, ciphertext_,
  88. {preComputation: preComputation});
  89. checkVerifiedAndProgressNotMeasured(proof);
  90. });
  91. it('not measure progress when verifying an OR proof and the progress meter is not used',
  92. function() {
  93. var verified = proofHandler_.verify(proof_, elements_, ciphertext_);
  94. expect(verified).toBeTruthy();
  95. checkProgressNotMeasured();
  96. });
  97. it('measure progress when generating an OR proof, using a custom minimum check interval',
  98. function() {
  99. var proof =
  100. proofHandler_.measureProgress(callback_, customMinCheckInterval_)
  101. .generate(secret_, elements_, index_, ciphertext_);
  102. checkVerifiedAndProgressMeasured(proof, customMinCheckInterval_);
  103. });
  104. it('measure progress when pre-computing an OR proof, using a custom minimum check interval',
  105. function() {
  106. var preComputation =
  107. proofHandler_.measureProgress(callback_, customMinCheckInterval_)
  108. .preCompute(numElements_);
  109. var proof = proofHandler_.generate(
  110. secret_, elements_, index_, ciphertext_,
  111. {preComputation: preComputation});
  112. checkVerifiedAndProgressMeasured(proof, customMinCheckInterval_);
  113. });
  114. it('measure progress when verifying an OR proof, using a custom minimum check interval',
  115. function() {
  116. var verified =
  117. proofHandler_.measureProgress(callback_, customMinCheckInterval_)
  118. .verify(proof_, elements_, ciphertext_);
  119. expect(verified).toBeTruthy();
  120. checkProgressMeasured(customMinCheckInterval_);
  121. });
  122. it('throw an error when measuring OR proof progress and not enough input arguments are provided',
  123. function() {
  124. expect(function() {
  125. proofHandler_.measureProgress();
  126. }).toThrow();
  127. });
  128. it('throw an error when measuring OR proof progress with null input arguments',
  129. function() {
  130. expect(function() {
  131. proofHandler_.measureProgress(null);
  132. }).toThrow();
  133. expect(function() {
  134. proofHandler_.measureProgress(callback_, null);
  135. }).toThrow();
  136. });
  137. it('throw an error when measuring OR proof progress with empty input arguments',
  138. function() {
  139. expect(function() {
  140. proofHandler_.measureProgress('');
  141. }).toThrow();
  142. expect(function() {
  143. proofHandler_.measureProgress(callback_, '');
  144. }).toThrow();
  145. });
  146. it('throw an error when measuring OR proof progress with a progress callback function that is not of type \'function\'',
  147. function() {
  148. expect(function() {
  149. proofHandler_.measureProgress(customMinCheckInterval_);
  150. }).toThrow();
  151. });
  152. it('throw an error when measuring OR proof progress with a progress percent minimum check interval that is not of type \'number\'',
  153. function() {
  154. var percentMeasuredLatest = 0;
  155. var progressCallback = function(progressPercent) {
  156. percentMeasuredLatest = progressPercent;
  157. };
  158. expect(function() {
  159. proofHandler_.measureProgress(progressCallback, progressCallback);
  160. }).toThrow();
  161. });
  162. });
  163. });
  164. function checkVerifiedAndProgressMeasured(proof, minCheckInterval) {
  165. checkVerified(proof);
  166. checkProgressMeasured(minCheckInterval);
  167. }
  168. function checkVerifiedAndProgressNotMeasured(proof) {
  169. checkVerified(proof);
  170. checkProgressNotMeasured();
  171. }
  172. function checkVerified(proof) {
  173. var verified = proofHandler_.verify(proof, elements_, ciphertext_);
  174. expect(verified).toBeTruthy();
  175. }
  176. function checkProgressMeasured(minCheckInterval) {
  177. if (typeof minCheckInterval === 'undefined') {
  178. minCheckInterval = defaultMinCheckInterval_;
  179. }
  180. expect(progressMeterTestData_.getProgressPercentLatest()).toBe(100);
  181. expect(progressMeterTestData_.getProgressPercentSum())
  182. .toBe(progressMeterTestData_.calculateProgressPercentSum(
  183. numProgressChecks_, minCheckInterval));
  184. }
  185. function checkProgressNotMeasured() {
  186. expect(progressMeterTestData_.getProgressPercentLatest()).toBe(0);
  187. expect(progressMeterTestData_.getProgressPercentSum()).toBe(0);
  188. }
  189. });