plaintext-exponent-equality-proof.spec.js 13 KB

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