schnorr-proof-progress.spec.js 7.3 KB

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