plaintext-exponent-equality-proof-progress.spec.js 8.1 KB

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