2
0

plaintext-equality-proof-progress.spec.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 PlaintextEqualityProofTestData =
  11. require('../data/plaintext-equality-proof-data');
  12. var ProgressMeterTestData = require('../data/progress-meter-data');
  13. var zkProof = require('../../lib/index');
  14. var elGamal = require('scytl-elgamal');
  15. describe('The zero-knowledge proof module should be able to ...', function() {
  16. var proofService_;
  17. var progressMeterTestData_;
  18. var group_;
  19. var primarySecret_;
  20. var primaryCiphertext_;
  21. var secondarySecret_;
  22. var secondaryCiphertext_;
  23. var primaryPublicKey_;
  24. var secondaryPublicKey_;
  25. var proofHandler_;
  26. var proof_;
  27. var numProgressChecks_;
  28. var callback_;
  29. var defaultMinCheckInterval_;
  30. var customMinCheckInterval_;
  31. beforeAll(function() {
  32. proofService_ = zkProof.newService();
  33. progressMeterTestData_ = new ProgressMeterTestData();
  34. var proofTestData = new PlaintextEqualityProofTestData();
  35. var elGamalService = elGamal.newService();
  36. group_ = proofTestData.getGroup();
  37. var primaryEncryptedElements = proofTestData.getPrimaryEncryptedElements();
  38. primarySecret_ = primaryEncryptedElements.secret;
  39. primaryCiphertext_ = elGamalService.newEncryptedElements(
  40. primaryEncryptedElements.gamma, primaryEncryptedElements.phis);
  41. var secondaryEncryptedElements =
  42. proofTestData.getSecondaryEncryptedElements();
  43. secondarySecret_ = secondaryEncryptedElements.secret;
  44. secondaryCiphertext_ = elGamalService.newEncryptedElements(
  45. secondaryEncryptedElements.gamma, secondaryEncryptedElements.phis);
  46. primaryPublicKey_ = proofTestData.getPrimaryPublicKey();
  47. secondaryPublicKey_ = proofTestData.getSecondaryPublicKey();
  48. proofHandler_ = proofService_.newPlaintextEqualityProofHandler(group_).init(
  49. primaryPublicKey_, secondaryPublicKey_);
  50. proof_ = proofHandler_.generate(
  51. primarySecret_, secondarySecret_, primaryCiphertext_,
  52. secondaryCiphertext_);
  53. numProgressChecks_ = proofTestData.getNumProgressChecks();
  54. callback_ = progressMeterTestData_.progressCallback;
  55. defaultMinCheckInterval_ =
  56. progressMeterTestData_.getDefaultProgressPercentMinCheckInterval();
  57. customMinCheckInterval_ =
  58. progressMeterTestData_.getCustomProgressPercentMinCheckInterval();
  59. });
  60. beforeEach(function() {
  61. progressMeterTestData_.initializeProgressMeterData();
  62. });
  63. describe('create a zero-knowledge proof service that should be able to ..', function() {
  64. describe('create a plaintext equality proof handler that should be able to', function() {
  65. it('measure progress when generating a plaintext equality proof',
  66. function() {
  67. var proof = proofHandler_.measureProgress(callback_).generate(
  68. primarySecret_, secondarySecret_, primaryCiphertext_,
  69. secondaryCiphertext_);
  70. checkVerifiedAndProgressMeasured(proof);
  71. });
  72. it('measure progress when pre-computing a plaintext equality proof',
  73. function() {
  74. var preComputation =
  75. proofHandler_.measureProgress(callback_).preCompute();
  76. var proof = proofHandler_.generate(
  77. primarySecret_, secondarySecret_, primaryCiphertext_,
  78. secondaryCiphertext_, {preComputation: preComputation});
  79. checkVerifiedAndProgressMeasured(proof);
  80. });
  81. it('measure progress when verifying a plaintext equality proof',
  82. function() {
  83. var verified = proofHandler_.measureProgress(callback_).verify(
  84. proof_, primaryCiphertext_, secondaryCiphertext_);
  85. expect(verified).toBeTruthy();
  86. checkProgressMeasured();
  87. });
  88. it('not measure progress when generating a plaintext equality proof and the progress meter is not used',
  89. function() {
  90. var proof = proofHandler_.generate(
  91. primarySecret_, secondarySecret_, primaryCiphertext_,
  92. secondaryCiphertext_);
  93. checkVerifiedAndProgressNotMeasured(proof);
  94. });
  95. it('not measure progress when pre-computing a plaintext equality proof and the progress meter is not used',
  96. function() {
  97. var preComputation = proofHandler_.preCompute();
  98. var proof = proofHandler_.generate(
  99. primarySecret_, secondarySecret_, primaryCiphertext_,
  100. secondaryCiphertext_, {preComputation: preComputation});
  101. checkVerifiedAndProgressNotMeasured(proof);
  102. });
  103. it('not measure progress when verifying a plaintext equality proof and the progress meter is not used',
  104. function() {
  105. var verified = proofHandler_.verify(
  106. proof_, primaryCiphertext_, secondaryCiphertext_);
  107. expect(verified).toBeTruthy();
  108. checkProgressNotMeasured();
  109. });
  110. it('measure progress when generating a plaintext equality proof, using a custom minimum check interval',
  111. function() {
  112. var proof =
  113. proofHandler_.measureProgress(callback_, customMinCheckInterval_)
  114. .generate(
  115. primarySecret_, secondarySecret_, primaryCiphertext_,
  116. secondaryCiphertext_);
  117. checkVerifiedAndProgressMeasured(proof, customMinCheckInterval_);
  118. });
  119. it('measure progress when pre-computing a plaintext equality proof, using a custom minimum check interval',
  120. function() {
  121. var preComputation =
  122. proofHandler_.measureProgress(callback_, customMinCheckInterval_)
  123. .preCompute();
  124. var proof = proofHandler_.generate(
  125. primarySecret_, secondarySecret_, primaryCiphertext_,
  126. secondaryCiphertext_, {preComputation: preComputation});
  127. checkVerifiedAndProgressMeasured(proof, customMinCheckInterval_);
  128. });
  129. it('measure progress when verifying a plaintext equality proof, using a custom minimum check interval',
  130. function() {
  131. var verified =
  132. proofHandler_.measureProgress(callback_, customMinCheckInterval_)
  133. .verify(proof_, primaryCiphertext_, secondaryCiphertext_);
  134. expect(verified).toBeTruthy();
  135. checkProgressMeasured(customMinCheckInterval_);
  136. });
  137. it('throw an error when measuring plaintext equality proof and not enough input arguments are provided',
  138. function() {
  139. expect(function() {
  140. proofHandler_.measureProgress();
  141. }).toThrow();
  142. });
  143. it('throw an error when measuring plaintext equality proof with null input arguments',
  144. function() {
  145. expect(function() {
  146. proofHandler_.measureProgress(null);
  147. }).toThrow();
  148. expect(function() {
  149. proofHandler_.measureProgress(callback_, null);
  150. }).toThrow();
  151. });
  152. it('throw an error when measuring plaintext equality proof with empty input arguments',
  153. function() {
  154. expect(function() {
  155. proofHandler_.measureProgress('');
  156. }).toThrow();
  157. expect(function() {
  158. proofHandler_.measureProgress(callback_, '');
  159. }).toThrow();
  160. });
  161. it('throw an error when measuring plaintext equality proof with a progress callback function that is not of type \'function\'',
  162. function() {
  163. expect(function() {
  164. proofHandler_.measureProgress(customMinCheckInterval_);
  165. }).toThrow();
  166. });
  167. it('throw an error when measuring plaintext equality proof with a progress percent minimum check interval that is not of type \'number\'',
  168. function() {
  169. var percentMeasuredLatest = 0;
  170. var progressCallback = function(progressPercent) {
  171. percentMeasuredLatest = progressPercent;
  172. };
  173. expect(function() {
  174. proofHandler_.measureProgress(progressCallback, progressCallback);
  175. }).toThrow();
  176. });
  177. });
  178. });
  179. function checkVerifiedAndProgressMeasured(proof, minCheckInterval) {
  180. checkVerified(proof);
  181. checkProgressMeasured(minCheckInterval);
  182. }
  183. function checkVerifiedAndProgressNotMeasured(proof) {
  184. checkVerified(proof);
  185. checkProgressNotMeasured();
  186. }
  187. function checkVerified(proof) {
  188. var verified =
  189. proofHandler_.verify(proof, primaryCiphertext_, secondaryCiphertext_);
  190. expect(verified).toBeTruthy();
  191. }
  192. function checkProgressMeasured(minCheckInterval) {
  193. if (typeof minCheckInterval === 'undefined') {
  194. minCheckInterval = defaultMinCheckInterval_;
  195. }
  196. expect(progressMeterTestData_.getProgressPercentLatest()).toBe(100);
  197. expect(progressMeterTestData_.getProgressPercentSum())
  198. .toBe(progressMeterTestData_.calculateProgressPercentSum(
  199. numProgressChecks_, minCheckInterval));
  200. }
  201. function checkProgressNotMeasured() {
  202. expect(progressMeterTestData_.getProgressPercentLatest()).toBe(0);
  203. expect(progressMeterTestData_.getProgressPercentSum()).toBe(0);
  204. }
  205. });