2
0

plaintext-proof-progress.spec.js 7.9 KB

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