2
0

exponentiation-proof-progress.spec.js 7.8 KB

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