2
0

simple-plaintext-equality-proof-progress.spec.js 8.9 KB

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