exponentiation-proof.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 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 anotherGroup_;
  21. var secret_;
  22. var anotherSecret_;
  23. var baseElements_;
  24. var otherBaseElements_;
  25. var exponentiatedElements_;
  26. var otherExponentiatedElements_;
  27. var data_;
  28. var otherData_;
  29. var proofHandler_;
  30. var proof_;
  31. var preComputation_;
  32. beforeAll(function() {
  33. proofService_ = zkProof.newService();
  34. var testData = new ExponentiationProofTestData();
  35. group_ = testData.getGroup();
  36. anotherGroup_ = testData.getAnotherGroup();
  37. secret_ = testData.getSecret();
  38. anotherSecret_ = testData.getAnotherSecret();
  39. baseElements_ = testData.getBaseElements();
  40. otherBaseElements_ = testData.getOtherBaseElements();
  41. exponentiatedElements_ = testData.getExponentiatedElements();
  42. otherExponentiatedElements_ = testData.getOtherExponentiatedElements();
  43. data_ = testData.getStringData();
  44. otherData_ = testData.getOtherStringData();
  45. proofHandler_ =
  46. proofService_.newExponentiationProofHandler(group_).init(baseElements_);
  47. proof_ = proofHandler_.generate(secret_, exponentiatedElements_);
  48. preComputation_ = proofHandler_.preCompute();
  49. });
  50. describe('create a zero-knowledge proof service that should be able to ..', function() {
  51. describe('create an exponentiation proof handler that should be able to', function() {
  52. it('generate and verify an exponentiation proof', function() {
  53. var verified = proofHandler_.verify(proof_, exponentiatedElements_);
  54. expect(verified).toBeTruthy();
  55. });
  56. it('generate and verify an exponentiation proof, using a specified cryptographic policy',
  57. function() {
  58. var policy = cryptoPolicy.newInstance();
  59. policy.proofs.messageDigest.algorithm =
  60. cryptoPolicy.options.proofs.messageDigest.algorithm.SHA512_224;
  61. var proofService = zkProof.newService({policy: policy});
  62. var proofHandler =
  63. proofService.newExponentiationProofHandler(group_).init(
  64. baseElements_);
  65. var proof = proofHandler.generate(secret_, exponentiatedElements_);
  66. var verified = proofHandler.verify(proof, exponentiatedElements_);
  67. expect(verified).toBeTruthy();
  68. });
  69. it('generate and verify an exponentiation proof, using a specified message digest service object',
  70. function() {
  71. var policy = cryptoPolicy.newInstance();
  72. policy.proofs.messageDigest.algorithm =
  73. cryptoPolicy.options.proofs.messageDigest.algorithm.SHA512_224;
  74. var messageDigestService =
  75. messageDigest.newService({policy: policy});
  76. var proofService =
  77. zkProof.newService({messageDigestService: messageDigestService});
  78. var proofHandler =
  79. proofService.newExponentiationProofHandler(group_).init(
  80. baseElements_);
  81. var proof = proofHandler.generate(secret_, exponentiatedElements_);
  82. var verified = proofHandler.verify(proof, exponentiatedElements_);
  83. expect(verified).toBeTruthy();
  84. });
  85. it('generate and verify an exponentiation proof, using a specified secure random service object',
  86. function() {
  87. var proofService = zkProof.newService(
  88. {secureRandomService: secureRandom.newService()});
  89. var proofHandler =
  90. proofService.newExponentiationProofHandler(group_).init(
  91. baseElements_);
  92. var proof = proofHandler.generate(secret_, exponentiatedElements_);
  93. var verified = proofHandler.verify(proof, exponentiatedElements_);
  94. expect(verified).toBeTruthy();
  95. });
  96. it('generate and verify an exponentiation proof, using a specified mathematical service object',
  97. function() {
  98. var proofService = zkProof.newService(
  99. {mathematicalService: mathematical.newService()});
  100. var proofHandler =
  101. proofService.newExponentiationProofHandler(group_).init(
  102. baseElements_);
  103. var proof = proofHandler.generate(secret_, exponentiatedElements_);
  104. var verified = proofHandler.verify(proof, exponentiatedElements_);
  105. expect(verified).toBeTruthy();
  106. });
  107. it('generate and verify a plaintext proof, using provided auxiliary data',
  108. function() {
  109. // Data as string.
  110. var proof = proofHandler_.generate(
  111. secret_, exponentiatedElements_, {data: data_});
  112. var verified = proofHandler_.verify(
  113. proof, exponentiatedElements_, {data: data_});
  114. expect(verified).toBeTruthy();
  115. // Data as bytes.
  116. proof = proofHandler_.generate(
  117. secret_, exponentiatedElements_,
  118. {data: codec.utf8Encode(data_)});
  119. verified = proofHandler_.verify(
  120. proof, exponentiatedElements_, {data: codec.utf8Encode(data_)});
  121. expect(verified).toBeTruthy();
  122. });
  123. it('generate and verify an exponentiation proof, using a pre-computation',
  124. function() {
  125. var proof = proofHandler_.generate(
  126. secret_, exponentiatedElements_,
  127. {preComputation: preComputation_});
  128. var verified = proofHandler_.verify(proof, exponentiatedElements_);
  129. expect(verified).toBeTruthy();
  130. });
  131. it('generate and verify an exponentiation proof, using empty string auxiliary data',
  132. function() {
  133. var proof = proofHandler_.generate(
  134. secret_, exponentiatedElements_, {data: ''});
  135. var verified =
  136. proofHandler_.verify(proof, exponentiatedElements_, {data: ''});
  137. expect(verified).toBeTruthy();
  138. });
  139. it('pre-compute, generate and verify a plaintext proof, using method chaining',
  140. function() {
  141. var preComputation =
  142. proofService_.newExponentiationProofHandler(group_)
  143. .init(baseElements_)
  144. .preCompute();
  145. var proof = proofService_.newExponentiationProofHandler(group_)
  146. .init(baseElements_)
  147. .generate(
  148. secret_, exponentiatedElements_,
  149. {data: data_, preComputation: preComputation});
  150. var verified =
  151. proofService_.newExponentiationProofHandler(group_)
  152. .init(baseElements_)
  153. .verify(proof, exponentiatedElements_, {data: data_});
  154. expect(verified).toBeTruthy();
  155. });
  156. it('create a new exponentiation ZeroKnowledgeProof object', function() {
  157. var hash = proof_.hash;
  158. var values = proof_.values;
  159. var proof = proofService_.newProof(hash, values);
  160. var verified = proofHandler_.verify(proof, exponentiatedElements_);
  161. expect(verified).toBeTruthy();
  162. });
  163. it('create a new exponentiation ZeroKnowledgeProofPreComputation object',
  164. function() {
  165. var exponents = preComputation_.exponents;
  166. var phiOutputs = preComputation_.phiOutputs;
  167. var preComputation =
  168. proofService_.newPreComputation(exponents, phiOutputs);
  169. var proof = proofHandler_.generate(
  170. secret_, exponentiatedElements_,
  171. {preComputation: preComputation});
  172. var verified = proofHandler_.verify(proof, exponentiatedElements_);
  173. expect(verified).toBeTruthy();
  174. });
  175. it('serialize and deserialize an exponentiation proof', function() {
  176. var proofJson = proof_.toJson();
  177. var proof = proofService_.newProof(proofJson);
  178. var verified = proofHandler_.verify(proof, exponentiatedElements_);
  179. expect(verified).toBeTruthy();
  180. });
  181. it('serialize and deserialize an exponentiation proof pre-computation',
  182. function() {
  183. var preComputationJson = preComputation_.toJson();
  184. var preComputation =
  185. proofService_.newPreComputation(preComputationJson);
  186. var proof = proofHandler_.generate(
  187. secret_, exponentiatedElements_,
  188. {preComputation: preComputation});
  189. var verified = proofHandler_.verify(proof, exponentiatedElements_);
  190. expect(verified).toBeTruthy();
  191. });
  192. it('fail to verify an exponentiation proof that was generated with a secret not used to exponentiate',
  193. function() {
  194. var proof =
  195. proofHandler_.generate(anotherSecret_, exponentiatedElements_);
  196. var verified = proofHandler_.verify(proof, exponentiatedElements_);
  197. expect(verified).toBeFalsy();
  198. });
  199. it('fail to verify an exponentiation proof that was generated with base elements not used to exponentiate',
  200. function() {
  201. proofHandler_.init(otherBaseElements_);
  202. var proof = proofHandler_.generate(secret_, exponentiatedElements_);
  203. var verified = proofHandler_.verify(proof, exponentiatedElements_);
  204. expect(verified).toBeFalsy();
  205. });
  206. it('fail to verify an exponentiation proof that was generated with base elements exponentiated to another secret',
  207. function() {
  208. var proof =
  209. proofHandler_.generate(secret_, otherExponentiatedElements_);
  210. var verified =
  211. proofHandler_.verify(proof, otherExponentiatedElements_);
  212. expect(verified).toBeFalsy();
  213. });
  214. it('fail to verify an exponentiation proof when using base elements not used to exponentiate',
  215. function() {
  216. var verified = proofHandler_.init(otherBaseElements_)
  217. .verify(proof_, exponentiatedElements_);
  218. expect(verified).toBeFalsy();
  219. });
  220. it('fail to verify an exponentiation proof when using elements exponentiated to the wrong secret',
  221. function() {
  222. var verified =
  223. proofHandler_.verify(proof_, otherExponentiatedElements_);
  224. expect(verified).toBeFalsy();
  225. });
  226. it('fail to verify an exponentiation proof when using the wrong data',
  227. function() {
  228. var proof = proofHandler_.generate(
  229. secret_, exponentiatedElements_, {data: data_});
  230. var verified = proofHandler_.verify(
  231. proof, exponentiatedElements_, {data: otherData_});
  232. expect(verified).toBeFalsy();
  233. });
  234. it('throw an error when generating a proof before the handler has been initialized with base elements',
  235. function() {
  236. var proofHandler =
  237. proofService_.newExponentiationProofHandler(group_);
  238. expect(function() {
  239. proofHandler.generate(secret_, exponentiatedElements_);
  240. }).toThrow();
  241. });
  242. it('throw an error when pre-computing a proof before the handler has been initialized with base elements',
  243. function() {
  244. var proofHandler =
  245. proofService_.newExponentiationProofHandler(group_);
  246. expect(function() {
  247. proofHandler.preCompute();
  248. }).toThrow();
  249. });
  250. it('throw an error when verifying a proof before the handler has been initialized with base elements',
  251. function() {
  252. var proofHandler =
  253. proofService_.newExponentiationProofHandler(group_);
  254. expect(function() {
  255. proofHandler.verify(proof_, exponentiatedElements_);
  256. }).toThrow();
  257. });
  258. });
  259. });
  260. });