2
0

plaintext-equality-proof.spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 PlaintextEqualityProofTestData =
  11. require('./data/plaintext-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 elGamal = require('scytl-elgamal');
  18. var codec = require('scytl-codec');
  19. describe('The zero-knowledge proof module should be able to ...', function() {
  20. var proofService_;
  21. var group_;
  22. var primarySecret_;
  23. var primaryCiphertext_;
  24. var secondarySecret_;
  25. var secondaryCiphertext_;
  26. var anotherPrimarySecret_;
  27. var anotherPrimaryCiphertext_;
  28. var anotherSecondarySecret_;
  29. var anotherSecondaryCiphertext_;
  30. var primaryPublicKey_;
  31. var secondaryPublicKey_;
  32. var anotherPrimaryPublicKey_;
  33. var anotherSecondaryPublicKey_;
  34. var data_;
  35. var otherData_;
  36. var proofHandler_;
  37. var proof_;
  38. var preComputation_;
  39. beforeAll(function() {
  40. proofService_ = zkProof.newService();
  41. var testData = new PlaintextEqualityProofTestData();
  42. var elGamalService = elGamal.newService();
  43. group_ = testData.getGroup();
  44. var primaryEncryptedElements = testData.getPrimaryEncryptedElements();
  45. primarySecret_ = primaryEncryptedElements.secret;
  46. primaryCiphertext_ = elGamalService.newEncryptedElements(
  47. primaryEncryptedElements.gamma, primaryEncryptedElements.phis);
  48. var secondaryEncryptedElements = testData.getSecondaryEncryptedElements();
  49. secondarySecret_ = secondaryEncryptedElements.secret;
  50. secondaryCiphertext_ = elGamalService.newEncryptedElements(
  51. secondaryEncryptedElements.gamma, secondaryEncryptedElements.phis);
  52. var otherPrimaryEncryptedElements =
  53. testData.getOtherPrimaryEncryptedElements();
  54. anotherPrimarySecret_ = otherPrimaryEncryptedElements.secret;
  55. anotherPrimaryCiphertext_ = elGamalService.newEncryptedElements(
  56. otherPrimaryEncryptedElements.gamma,
  57. otherPrimaryEncryptedElements.phis);
  58. var otherSecondaryEncryptedElements =
  59. testData.getOtherSecondaryEncryptedElements();
  60. anotherSecondarySecret_ = otherSecondaryEncryptedElements.secret;
  61. anotherSecondaryCiphertext_ = elGamalService.newEncryptedElements(
  62. otherSecondaryEncryptedElements.gamma,
  63. otherSecondaryEncryptedElements.phis);
  64. primaryPublicKey_ = testData.getPrimaryPublicKey();
  65. secondaryPublicKey_ = testData.getSecondaryPublicKey();
  66. anotherPrimaryPublicKey_ = testData.getAnotherPrimaryPublicKey();
  67. anotherSecondaryPublicKey_ = testData.getAnotherSecondaryPublicKey();
  68. data_ = testData.getStringData();
  69. otherData_ = testData.getOtherStringData();
  70. proofHandler_ = proofService_.newPlaintextEqualityProofHandler(group_).init(
  71. primaryPublicKey_, secondaryPublicKey_);
  72. proof_ = proofHandler_.generate(
  73. primarySecret_, secondarySecret_, primaryCiphertext_,
  74. secondaryCiphertext_);
  75. preComputation_ = proofHandler_.preCompute();
  76. });
  77. beforeEach(function() {
  78. proofHandler_ = proofService_.newPlaintextEqualityProofHandler(group_).init(
  79. primaryPublicKey_, secondaryPublicKey_);
  80. });
  81. describe('create a zero-knowledge proof service that should be able to ..', function() {
  82. describe('create a plaintext equality proof handler that should be able to', function() {
  83. it('generate and verify a plaintext equality proof', function() {
  84. var verified = proofHandler_.verify(
  85. proof_, primaryCiphertext_, secondaryCiphertext_);
  86. expect(verified).toBeTruthy();
  87. });
  88. it('generate and verify a plaintext equality proof, using a specified cryptographic policy',
  89. function() {
  90. var policy = cryptoPolicy.newInstance();
  91. policy.proofs.messageDigest.algorithm =
  92. cryptoPolicy.options.proofs.messageDigest.algorithm.SHA512_224;
  93. var proofService = zkProof.newService({policy: policy});
  94. var proofHandler =
  95. proofService.newPlaintextEqualityProofHandler(group_).init(
  96. primaryPublicKey_, secondaryPublicKey_);
  97. proof_ = proofHandler.generate(
  98. primarySecret_, secondarySecret_, primaryCiphertext_,
  99. secondaryCiphertext_);
  100. var verified = proofHandler.verify(
  101. proof_, primaryCiphertext_, secondaryCiphertext_);
  102. expect(verified).toBeTruthy();
  103. });
  104. it('generate and verify a plaintext equality proof, using a specified message digest service',
  105. function() {
  106. var policy = cryptoPolicy.newInstance();
  107. policy.proofs.messageDigest.algorithm =
  108. cryptoPolicy.options.proofs.messageDigest.algorithm.SHA512_224;
  109. var messageDigestService =
  110. messageDigest.newService({policy: policy});
  111. var proofService =
  112. zkProof.newService({messageDigestService: messageDigestService});
  113. var proofHandler =
  114. proofService.newPlaintextEqualityProofHandler(group_).init(
  115. primaryPublicKey_, secondaryPublicKey_);
  116. proof_ = proofHandler.generate(
  117. primarySecret_, secondarySecret_, primaryCiphertext_,
  118. secondaryCiphertext_);
  119. var verified = proofHandler.verify(
  120. proof_, primaryCiphertext_, secondaryCiphertext_);
  121. expect(verified).toBeTruthy();
  122. });
  123. it('generate and verify a plaintext equality proof, using a specified secure random service object',
  124. function() {
  125. var proofService = zkProof.newService(
  126. {secureRandomService: secureRandom.newService()});
  127. var proofHandler =
  128. proofService.newPlaintextEqualityProofHandler(group_).init(
  129. primaryPublicKey_, secondaryPublicKey_);
  130. proof_ = proofHandler.generate(
  131. primarySecret_, secondarySecret_, primaryCiphertext_,
  132. secondaryCiphertext_);
  133. var verified = proofHandler.verify(
  134. proof_, primaryCiphertext_, secondaryCiphertext_);
  135. expect(verified).toBeTruthy();
  136. });
  137. it('generate and verify a plaintext equality proof, using a specified mathematical service object',
  138. function() {
  139. var proofService = zkProof.newService(
  140. {mathematicalService: mathematical.newService()});
  141. var proofHandler =
  142. proofService.newPlaintextEqualityProofHandler(group_).init(
  143. primaryPublicKey_, secondaryPublicKey_);
  144. proof_ = proofHandler.generate(
  145. primarySecret_, secondarySecret_, primaryCiphertext_,
  146. secondaryCiphertext_);
  147. var verified = proofHandler.verify(
  148. proof_, primaryCiphertext_, secondaryCiphertext_);
  149. expect(verified).toBeTruthy();
  150. });
  151. it('generate and verify a plaintext proof, using provided auxiliary data',
  152. function() {
  153. // Data as string.
  154. var proof = proofHandler_.generate(
  155. primarySecret_, secondarySecret_, primaryCiphertext_,
  156. secondaryCiphertext_, {data: data_});
  157. var verified = proofHandler_.verify(
  158. proof, primaryCiphertext_, secondaryCiphertext_, {data: data_});
  159. expect(verified).toBeTruthy();
  160. // Data as bytes.
  161. proof = proofHandler_.generate(
  162. primarySecret_, secondarySecret_, primaryCiphertext_,
  163. secondaryCiphertext_, {data: codec.utf8Encode(data_)});
  164. verified = proofHandler_.verify(
  165. proof, primaryCiphertext_, secondaryCiphertext_,
  166. {data: codec.utf8Encode(data_)});
  167. expect(verified).toBeTruthy();
  168. });
  169. it('generate and verify a plaintext equality proof, using a pre-computation',
  170. function() {
  171. var proof = proofHandler_.generate(
  172. primarySecret_, secondarySecret_, primaryCiphertext_,
  173. secondaryCiphertext_, {preComputation: preComputation_});
  174. var verified = proofHandler_.verify(
  175. proof, primaryCiphertext_, secondaryCiphertext_);
  176. expect(verified).toBeTruthy();
  177. });
  178. it('generate and verify a plaintext equality proof, using empty auxiliary data',
  179. function() {
  180. var proof = proofHandler_.generate(
  181. primarySecret_, secondarySecret_, primaryCiphertext_,
  182. secondaryCiphertext_, {data: ''});
  183. var verified = proofHandler_.verify(
  184. proof, primaryCiphertext_, secondaryCiphertext_, {data: ''});
  185. expect(verified).toBeTruthy();
  186. });
  187. it('pre-compute, generate and verify a plaintext proof, using method chaining',
  188. function() {
  189. var preComputation =
  190. proofService_.newPlaintextEqualityProofHandler(group_)
  191. .init(primaryPublicKey_, secondaryPublicKey_)
  192. .preCompute();
  193. var proof = proofService_.newPlaintextEqualityProofHandler(group_)
  194. .init(primaryPublicKey_, secondaryPublicKey_)
  195. .generate(
  196. primarySecret_, secondarySecret_,
  197. primaryCiphertext_, secondaryCiphertext_,
  198. {data: data_, preComputation: preComputation});
  199. var verified = proofService_.newPlaintextEqualityProofHandler(group_)
  200. .init(primaryPublicKey_, secondaryPublicKey_)
  201. .verify(
  202. proof, primaryCiphertext_,
  203. secondaryCiphertext_, {data: data_});
  204. expect(verified).toBeTruthy();
  205. });
  206. it('create a new plaintext equality ZeroKnowledgeProof object',
  207. function() {
  208. var hash = proof_.hash;
  209. var values = proof_.values;
  210. var proof = proofService_.newProof(hash, values);
  211. var verified = proofHandler_.verify(
  212. proof, primaryCiphertext_, secondaryCiphertext_);
  213. expect(verified).toBeTruthy();
  214. });
  215. it('create a new plaintext equality ZeroKnowledgeProofPreComputation object',
  216. function() {
  217. var exponents = preComputation_.exponents;
  218. var phiOutputs = preComputation_.phiOutputs;
  219. var preComputation =
  220. proofService_.newPreComputation(exponents, phiOutputs);
  221. var proof = proofHandler_.generate(
  222. primarySecret_, secondarySecret_, primaryCiphertext_,
  223. secondaryCiphertext_, {preComputation: preComputation});
  224. var verified = proofHandler_.verify(
  225. proof, primaryCiphertext_, secondaryCiphertext_);
  226. expect(verified).toBeTruthy();
  227. });
  228. it('serialize and deserialize a plaintext equality proof', function() {
  229. var proofJson = proof_.toJson();
  230. var proof = proofService_.newProof(proofJson);
  231. var verified = proofHandler_.verify(
  232. proof, primaryCiphertext_, secondaryCiphertext_);
  233. expect(verified).toBeTruthy();
  234. });
  235. it('serialize and deserialize a plaintext equality proof pre-computation',
  236. function() {
  237. var preComputationJson = preComputation_.toJson();
  238. var preComputation =
  239. proofService_.newPreComputation(preComputationJson);
  240. var proof = proofHandler_.generate(
  241. primarySecret_, secondarySecret_, primaryCiphertext_,
  242. secondaryCiphertext_, {preComputation: preComputation});
  243. var verified = proofHandler_.verify(
  244. proof, primaryCiphertext_, secondaryCiphertext_);
  245. expect(verified).toBeTruthy();
  246. });
  247. it('fail to verify a plaintext equality proof that was generated with a primary secret not used to generate the primary ciphertext',
  248. function() {
  249. var proof = proofHandler_.generate(
  250. anotherPrimarySecret_, secondarySecret_, primaryCiphertext_,
  251. secondaryCiphertext_);
  252. var verified = proofHandler_.verify(
  253. proof, primaryCiphertext_, secondaryCiphertext_);
  254. expect(verified).toBeFalsy();
  255. });
  256. it('fail to verify a plaintext equality proof that was generated with a secondary secret not used to generate the secondary ciphertext',
  257. function() {
  258. var proof = proofHandler_.generate(
  259. primarySecret_, anotherSecondarySecret_, primaryCiphertext_,
  260. secondaryCiphertext_);
  261. var verified = proofHandler_.verify(
  262. proof, primaryCiphertext_, secondaryCiphertext_);
  263. expect(verified).toBeFalsy();
  264. });
  265. it('fail to verify a plaintext equality proof that was generated with a primary publicKey not used to generate the primary ciphertext',
  266. function() {
  267. proofHandler_.init(anotherPrimaryPublicKey_, secondaryPublicKey_);
  268. var proof = proofHandler_.generate(
  269. primarySecret_, secondarySecret_, primaryCiphertext_,
  270. secondaryCiphertext_);
  271. var verified = proofHandler_.verify(
  272. proof, primaryCiphertext_, secondaryCiphertext_);
  273. expect(verified).toBeFalsy();
  274. });
  275. it('fail to verify a plaintext equality proof that was generated with a secondary publicKey not used to generate the secondary ciphertext',
  276. function() {
  277. proofHandler_.init(primaryPublicKey_, anotherSecondaryPublicKey_);
  278. var proof = proofHandler_.generate(
  279. primarySecret_, secondarySecret_, primaryCiphertext_,
  280. secondaryCiphertext_);
  281. var verified = proofHandler_.verify(
  282. proof, primaryCiphertext_, secondaryCiphertext_);
  283. expect(verified).toBeFalsy();
  284. });
  285. it('fail to verify a plaintext equality proof that was generated with a primary ciphertext not generated from the primary secret',
  286. function() {
  287. var proof = proofHandler_.generate(
  288. primarySecret_, secondarySecret_, anotherPrimaryCiphertext_,
  289. secondaryCiphertext_);
  290. var verified = proofHandler_.verify(
  291. proof, anotherPrimaryCiphertext_, secondaryCiphertext_);
  292. expect(verified).toBeFalsy();
  293. });
  294. it('fail to verify a plaintext equality proof that was generated with a secondary ciphertext not generated from the secondary secret',
  295. function() {
  296. var proof = proofHandler_.generate(
  297. primarySecret_, secondarySecret_, primaryCiphertext_,
  298. anotherSecondaryCiphertext_);
  299. var verified = proofHandler_.verify(
  300. proof, primaryCiphertext_, anotherSecondaryCiphertext_);
  301. expect(verified).toBeFalsy();
  302. });
  303. it('fail to verify a plaintext equality proof when using a primary public key not used to generate the proof',
  304. function() {
  305. var verified =
  306. proofHandler_.init(anotherPrimaryPublicKey_, secondaryPublicKey_)
  307. .verify(proof_, primaryCiphertext_, secondaryCiphertext_);
  308. expect(verified).toBeFalsy();
  309. });
  310. it('fail to verify a plaintext equality proof when using a secondary public key not used to generate the proof',
  311. function() {
  312. var verified =
  313. proofHandler_.init(primaryPublicKey_, anotherSecondaryPublicKey_)
  314. .verify(proof_, primaryCiphertext_, secondaryCiphertext_);
  315. expect(verified).toBeFalsy();
  316. });
  317. it('fail to verify a plaintext equality proof when using a primary ciphertext not used to generate the proof',
  318. function() {
  319. var verified = proofHandler_.verify(
  320. proof_, anotherPrimaryCiphertext_, secondaryCiphertext_);
  321. expect(verified).toBeFalsy();
  322. });
  323. it('fail to verify a plaintext equality proof when using a secondary ciphertext not used to generate the proof',
  324. function() {
  325. var verified = proofHandler_.verify(
  326. proof_, primaryCiphertext_, anotherSecondaryCiphertext_);
  327. expect(verified).toBeFalsy();
  328. });
  329. it('fail to verify a plaintext equality proof when using auxiliary data not used to generate the proof',
  330. function() {
  331. var proof = proofHandler_.generate(
  332. primarySecret_, secondarySecret_, primaryCiphertext_,
  333. secondaryCiphertext_, {data: data_});
  334. var verified = proofHandler_.verify(
  335. proof, primaryCiphertext_, secondaryCiphertext_,
  336. {data: otherData_});
  337. expect(verified).toBeFalsy();
  338. });
  339. it('throw an error when generating a proof before the handler has been initialized with any pubilc key',
  340. function() {
  341. var proofHandler =
  342. proofService_.newPlaintextEqualityProofHandler(group_);
  343. expect(function() {
  344. proofHandler.generate(
  345. primarySecret_, secondarySecret_, primaryCiphertext_,
  346. secondaryCiphertext_);
  347. }).toThrow();
  348. });
  349. it('throw an error when pre-computing a proof before the handler has been initialized with any pubilc key',
  350. function() {
  351. var proofHandler =
  352. proofService_.newPlaintextEqualityProofHandler(group_);
  353. expect(function() {
  354. proofHandler.preCompute();
  355. }).toThrow();
  356. });
  357. it('throw an error when verifying a proof before the handler has been initialized with any pubilc key',
  358. function() {
  359. var proofHandler =
  360. proofService_.newPlaintextEqualityProofHandler(group_);
  361. expect(function() {
  362. proofHandler.verify(
  363. proof_, primaryCiphertext_, secondaryCiphertext_);
  364. }).toThrow();
  365. });
  366. });
  367. });
  368. });