schnorr-proof.spec.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 cryptoPolicy = require('scytl-cryptopolicy');
  12. var zkProof = require('../lib/index');
  13. var messageDigest = require('scytl-messagedigest');
  14. var mathematical = require('scytl-mathematical');
  15. var secureRandom = require('scytl-securerandom');
  16. var codec = require('scytl-codec');
  17. describe('The zero-knowledge proof module should be able to ...', function() {
  18. var proofService_;
  19. var group_;
  20. var secret_;
  21. var anotherSecret_;
  22. var gamma_;
  23. var gammaForAnotherSecret_;
  24. var data_;
  25. var otherData_;
  26. var voterId_;
  27. var electionId_;
  28. var otherVoterId_;
  29. var otherElectionId_;
  30. var proofHandler_;
  31. var proof_;
  32. var preComputation_;
  33. beforeAll(function() {
  34. proofService_ = zkProof.newService();
  35. var testData = new SchnorrProofTestData();
  36. group_ = testData.getGroup();
  37. secret_ = testData.getSecret();
  38. anotherSecret_ = testData.getAnotherSecret();
  39. gamma_ = testData.getGamma();
  40. gammaForAnotherSecret_ = testData.getGammaForAnotherSecret();
  41. data_ = testData.getStringData();
  42. otherData_ = testData.getOtherStringData();
  43. voterId_ = testData.getVoterId();
  44. electionId_ = testData.getElectionId();
  45. otherVoterId_ = voterId_ + 'a';
  46. otherElectionId_ = electionId_ + 'b';
  47. proofHandler_ = proofService_.newSchnorrProofHandler(group_);
  48. proof_ = proofHandler_.generate(secret_, gamma_);
  49. preComputation_ = proofHandler_.preCompute();
  50. });
  51. describe('create a zero-knowledge proof service that should be able to ..', function() {
  52. describe('create a Schnorr proof handler that should be able to', function() {
  53. it('generate and verify a Schnorr proof', function() {
  54. var verified = proofHandler_.verify(proof_, gamma_);
  55. expect(verified).toBeTruthy();
  56. });
  57. it('generate and verify a Schnorr proof, using a specified cryptographic policy',
  58. function() {
  59. var policy = cryptoPolicy.newInstance();
  60. policy.proofs.messageDigest.algorithm =
  61. cryptoPolicy.options.proofs.messageDigest.algorithm.SHA512_224;
  62. var proofService = zkProof.newService({policy: policy});
  63. var proofHandler = proofService.newSchnorrProofHandler(group_);
  64. proof_ = proofHandler.generate(secret_, gamma_);
  65. var verified = proofHandler.verify(proof_, gamma_);
  66. expect(verified).toBeTruthy();
  67. });
  68. it('generate and verify a Schnorr proof, using a specified message digest service',
  69. function() {
  70. var policy = cryptoPolicy.newInstance();
  71. policy.proofs.messageDigest.algorithm =
  72. cryptoPolicy.options.proofs.messageDigest.algorithm.SHA512_224;
  73. var messageDigestService =
  74. messageDigest.newService({policy: policy});
  75. var proofService =
  76. zkProof.newService({messageDigestService: messageDigestService});
  77. var proofHandler = proofService.newSchnorrProofHandler(group_);
  78. proof_ = proofHandler.generate(secret_, gamma_);
  79. var verified = proofHandler.verify(proof_, gamma_);
  80. expect(verified).toBeTruthy();
  81. });
  82. it('generate and verify a Schnorr proof, using a specified secure random service object',
  83. function() {
  84. var proofService = zkProof.newService(
  85. {secureRandomService: secureRandom.newService()});
  86. var proofHandler = proofService.newSchnorrProofHandler(group_);
  87. proof_ = proofHandler.generate(secret_, gamma_);
  88. var verified = proofHandler.verify(proof_, gamma_);
  89. expect(verified).toBeTruthy();
  90. });
  91. it('generate and verify a Schnorr proof, using a specified mathematical service object',
  92. function() {
  93. var proofService = zkProof.newService(
  94. {mathematicalService: mathematical.newService()});
  95. var proofHandler = proofService.newSchnorrProofHandler(group_);
  96. proof_ = proofHandler.generate(secret_, gamma_);
  97. var verified = proofHandler.verify(proof_, gamma_);
  98. expect(verified).toBeTruthy();
  99. });
  100. it('generate and verify a Schnorr proof, using provided auxiliary data',
  101. function() {
  102. // Data as string.
  103. var proof = proofHandler_.generate(secret_, gamma_, {data: data_});
  104. var verified = proofHandler_.verify(proof, gamma_, {data: data_});
  105. expect(verified).toBeTruthy();
  106. // Data as bytes.
  107. proof = proofHandler_.generate(
  108. secret_, gamma_, {data: codec.utf8Encode(data_)});
  109. verified = proofHandler_.verify(
  110. proof, gamma_, {data: codec.utf8Encode(data_)});
  111. expect(verified).toBeTruthy();
  112. });
  113. it('generate and verify a Schnorr proof, using a provided voter and election ID',
  114. function() {
  115. var proof = proofHandler_.generate(
  116. secret_, gamma_, {voterId: voterId_, electionId: electionId_});
  117. var verified = proofHandler_.verify(
  118. proof, gamma_, {voterId: voterId_, electionId: electionId_});
  119. expect(verified).toBeTruthy();
  120. });
  121. it('generate and verify a Schnorr proof, using a pre-computation',
  122. function() {
  123. expect(preComputation_).toBeDefined();
  124. var proof = proofHandler_.generate(
  125. secret_, gamma_, {preComputation: preComputation_});
  126. var verified = proofHandler_.verify(proof, gamma_);
  127. expect(verified).toBeTruthy();
  128. });
  129. it('generate and verify a Schnorr proof, using empty string auxiliary data',
  130. function() {
  131. var proof = proofHandler_.generate(secret_, gamma_, {data: ''});
  132. var verified = proofHandler_.verify(proof, gamma_, {data: ''});
  133. expect(verified).toBeTruthy();
  134. });
  135. it('pre-compute, generate and verify a Schnorr proof, using method chaining',
  136. function() {
  137. var preComputation =
  138. proofService_.newSchnorrProofHandler(group_).preCompute();
  139. var proof = proofService_.newSchnorrProofHandler(group_).generate(
  140. secret_, gamma_, {data: data_, preComputation: preComputation});
  141. var verified = proofService_.newSchnorrProofHandler(group_).verify(
  142. proof, gamma_, {data: data_});
  143. expect(verified).toBeTruthy();
  144. });
  145. it('create a new Schnorr ZeroKnowledgeProof object', function() {
  146. var hash = proof_.hash;
  147. var values = proof_.values;
  148. var proof = proofService_.newProof(hash, values);
  149. var verified = proofHandler_.verify(proof, gamma_);
  150. expect(verified).toBeTruthy();
  151. });
  152. it('create a new Schnorr ZeroKnowledgeProofPreComputation object',
  153. function() {
  154. var exponents = preComputation_.exponents;
  155. var phiOutputs = preComputation_.phiOutputs;
  156. var preComputation =
  157. proofService_.newPreComputation(exponents, phiOutputs);
  158. var proof = proofHandler_.generate(
  159. secret_, gamma_, {preComputation: preComputation});
  160. var verified = proofHandler_.verify(proof, gamma_);
  161. expect(verified).toBeTruthy();
  162. });
  163. it('serialize and deserialize a Schnorr proof', function() {
  164. var proofJson = proof_.toJson();
  165. var proof = proofService_.newProof(proofJson);
  166. var verified = proofHandler_.verify(proof, gamma_);
  167. expect(verified).toBeTruthy();
  168. });
  169. it('serialize and deserialize a Schnorr proof pre-computation',
  170. function() {
  171. var preComputationJson = preComputation_.toJson();
  172. var preComputation =
  173. proofService_.newPreComputation(preComputationJson);
  174. var proof = proofHandler_.generate(
  175. secret_, gamma_, {preComputation: preComputation});
  176. var verified = proofHandler_.verify(proof, gamma_);
  177. expect(verified).toBeTruthy();
  178. });
  179. it('fail to verify a Schnorr proof that was generated with a secret not used to generate the gamma element',
  180. function() {
  181. var proof = proofHandler_.generate(anotherSecret_, gamma_);
  182. var verified = proofHandler_.verify(proof, gamma_);
  183. expect(verified).toBeFalsy();
  184. });
  185. it('fail to verify a Schnorr proof when using a gamma element generated from a secret not used to generate the proof',
  186. function() {
  187. var verified = proofHandler_.verify(proof_, gammaForAnotherSecret_);
  188. expect(verified).toBeFalsy();
  189. });
  190. it('fail to verify a Schnorr proof when using auxiliary data not used to generate the proof',
  191. function() {
  192. var proof = proofHandler_.generate(secret_, gamma_, {data: data_});
  193. var verified =
  194. proofHandler_.verify(proof, gamma_, {data: otherData_});
  195. expect(verified).toBeFalsy();
  196. });
  197. it('fail to verify a Schnorr proof when using a voter ID not used to generate the proof',
  198. function() {
  199. var proof = proofHandler_.generate(
  200. secret_, gamma_, {voterId: voterId_, electionId: electionId_});
  201. var verified = proofHandler_.verify(
  202. proof, gamma_,
  203. {voterId: otherVoterId_, electionId: electionId_});
  204. expect(verified).toBeFalsy();
  205. });
  206. it('fail to verify a Schnorr proof when using an election ID not used to generate the proof',
  207. function() {
  208. var proof = proofHandler_.generate(
  209. secret_, gamma_, {voterId: voterId_, electionId: electionId_});
  210. var verified = proofHandler_.verify(
  211. proof, gamma_,
  212. {voterId: voterId_, electionId: otherElectionId_});
  213. expect(verified).toBeFalsy();
  214. });
  215. });
  216. });
  217. });