plaintext-equality-proof-validation.spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 ProgressMeterTestData = require('../data/progress-meter-data');
  13. var ValidationTestData = require('../data/validation-data');
  14. var zkProof = require('../../lib/index');
  15. var elGamal = require('scytl-elgamal');
  16. describe('The zero-knowledge proof module should be able to ...', function() {
  17. var proofService_;
  18. var group_;
  19. var anotherGroup_;
  20. var primarySecret_;
  21. var primaryCiphertext_;
  22. var secondarySecret_;
  23. var secondaryCiphertext_;
  24. var primarySecretFromAnotherGroup_;
  25. var primaryCiphertextFromAnotherGroup_;
  26. var secondarySecretFromAnotherGroup_;
  27. var secondaryCiphertextFromAnotherGroup_;
  28. var primaryCiphertextWithLessElements_;
  29. var secondaryCiphertextWithLessElements_;
  30. var primaryPublicKey_;
  31. var secondaryPublicKey_;
  32. var primaryPublicKeyWithLessElements_;
  33. var secondaryPublicKeyWithLessElements_;
  34. var primaryPublicKeyFromAnotherGroup_;
  35. var secondaryPublicKeyFromAnotherGroup_;
  36. var callback_;
  37. var nonObject_;
  38. var emptyObject_;
  39. var nonNumber_;
  40. var nonPositiveNumber_;
  41. var nonString_;
  42. var nonFunction_;
  43. var nonAuxiliaryData_;
  44. var nonProofObject_;
  45. var proofHandler_;
  46. var proof_;
  47. beforeAll(function() {
  48. proofService_ = zkProof.newService();
  49. var testData = new PlaintextEqualityProofTestData();
  50. var elGamalService = elGamal.newService();
  51. group_ = testData.getGroup();
  52. anotherGroup_ = testData.getAnotherGroup();
  53. var primaryEncryptedElements = testData.getPrimaryEncryptedElements();
  54. primarySecret_ = primaryEncryptedElements.secret;
  55. primaryCiphertext_ = elGamalService.newEncryptedElements(
  56. primaryEncryptedElements.gamma, primaryEncryptedElements.phis);
  57. var secondaryEncryptedElements = testData.getSecondaryEncryptedElements();
  58. secondarySecret_ = secondaryEncryptedElements.secret;
  59. secondaryCiphertext_ = elGamalService.newEncryptedElements(
  60. secondaryEncryptedElements.gamma, secondaryEncryptedElements.phis);
  61. var encryptedElementsFromAnotherGroup =
  62. testData.getEncryptedElementsFromAnotherGroup();
  63. primarySecretFromAnotherGroup_ = encryptedElementsFromAnotherGroup.secret;
  64. primaryCiphertextFromAnotherGroup_ = elGamalService.newEncryptedElements(
  65. encryptedElementsFromAnotherGroup.gamma,
  66. encryptedElementsFromAnotherGroup.phis);
  67. secondarySecretFromAnotherGroup_ = primarySecretFromAnotherGroup_;
  68. secondaryCiphertextFromAnotherGroup_ = primaryCiphertextFromAnotherGroup_;
  69. var encryptedElementsWithLessElements =
  70. testData.getEncryptedElementsWithLessElements();
  71. primaryCiphertextWithLessElements_ = elGamalService.newEncryptedElements(
  72. encryptedElementsWithLessElements.gamma,
  73. encryptedElementsWithLessElements.phis);
  74. secondaryCiphertextWithLessElements_ = primaryCiphertextWithLessElements_;
  75. primaryPublicKey_ = testData.getPrimaryPublicKey();
  76. secondaryPublicKey_ = testData.getSecondaryPublicKey();
  77. primaryPublicKeyWithLessElements_ = testData.getPublicKeyWithLessElements();
  78. secondaryPublicKeyWithLessElements_ =
  79. testData.getPublicKeyWithLessElements();
  80. primaryPublicKeyFromAnotherGroup_ = testData.getPublicKeyFromAnotherGroup();
  81. secondaryPublicKeyFromAnotherGroup_ =
  82. testData.getPublicKeyFromAnotherGroup();
  83. var progressMeterTestData = new ProgressMeterTestData();
  84. callback_ = progressMeterTestData.progressCallback;
  85. var validationTestData = new ValidationTestData();
  86. nonObject_ = validationTestData.getNonObject();
  87. emptyObject_ = validationTestData.getEmptyObject();
  88. nonNumber_ = validationTestData.getNonNumber();
  89. nonPositiveNumber_ = validationTestData.getNonPositiveNumber();
  90. nonString_ = validationTestData.getNonString();
  91. nonFunction_ = validationTestData.getNonFunction();
  92. nonAuxiliaryData_ = validationTestData.getNonAuxiliaryData();
  93. nonProofObject_ = validationTestData.getNonProofObject();
  94. proofHandler_ = proofService_.newPlaintextEqualityProofHandler(group_).init(
  95. primaryPublicKey_, secondaryPublicKey_);
  96. proof_ = proofHandler_.generate(
  97. primarySecret_, secondarySecret_, primaryCiphertext_,
  98. secondaryCiphertext_);
  99. });
  100. beforeEach(function() {
  101. proofHandler_ = proofService_.newPlaintextEqualityProofHandler(group_).init(
  102. primaryPublicKey_, secondaryPublicKey_);
  103. });
  104. describe('create a zero-knowledge proof service that should be able to ..', function() {
  105. describe('create a plaintext equality proof handler that should be able to', function() {
  106. it('throw an error when being created, using invalid input data',
  107. function() {
  108. expect(function() {
  109. proofService_.newPlaintextEqualityProofHandler();
  110. }).toThrow();
  111. expect(function() {
  112. proofService_.newPlaintextEqualityProofHandler(undefined);
  113. }).toThrow();
  114. expect(function() {
  115. proofService_.newPlaintextEqualityProofHandler(null);
  116. }).toThrow();
  117. expect(function() {
  118. proofService_.newPlaintextEqualityProofHandler(nonObject_);
  119. }).toThrow();
  120. expect(function() {
  121. proofService_.newPlaintextEqualityProofHandler(emptyObject_);
  122. }).toThrow();
  123. });
  124. it('throw an error when being initialized, using an invalid primary public key',
  125. function() {
  126. expect(function() {
  127. proofHandler_.init(undefined, secondaryPublicKey_);
  128. }).toThrow();
  129. expect(function() {
  130. proofHandler_.init(null, secondaryPublicKey_);
  131. }).toThrow();
  132. expect(function() {
  133. proofHandler_.init(nonObject_, secondaryPublicKey_);
  134. }).toThrow();
  135. expect(function() {
  136. proofHandler_.init(emptyObject_, secondaryPublicKey_);
  137. }).toThrow();
  138. expect(function() {
  139. proofHandler_.init(
  140. primaryPublicKeyWithLessElements_, secondaryPublicKey_);
  141. }).toThrow();
  142. expect(function() {
  143. proofHandler_.init(
  144. primaryPublicKeyFromAnotherGroup_, secondaryPublicKey_);
  145. }).toThrow();
  146. });
  147. it('throw an error when being initialized, using an invalid secondary public key',
  148. function() {
  149. expect(function() {
  150. proofHandler_.init(primaryPublicKey_);
  151. }).toThrow();
  152. expect(function() {
  153. proofHandler_.init(primaryPublicKey_, undefined);
  154. }).toThrow();
  155. expect(function() {
  156. proofHandler_.init(primaryPublicKey_, null);
  157. }).toThrow();
  158. expect(function() {
  159. proofHandler_.init(primaryPublicKey_, nonObject_);
  160. }).toThrow();
  161. expect(function() {
  162. proofHandler_.init(primaryPublicKey_, emptyObject_);
  163. }).toThrow();
  164. expect(function() {
  165. proofHandler_.init(
  166. primaryPublicKey_, secondaryPublicKeyFromAnotherGroup_);
  167. }).toThrow();
  168. });
  169. it('throw an error when generating a plaintext equality proof, using an invalid primary secret',
  170. function() {
  171. expect(function() {
  172. proofHandler_.generate(
  173. undefined, secondarySecret_, primaryCiphertext_,
  174. secondaryCiphertext_);
  175. }).toThrow();
  176. expect(function() {
  177. proofHandler_.generate(
  178. null, secondarySecret_, primaryCiphertext_,
  179. secondaryCiphertext_);
  180. }).toThrow();
  181. expect(function() {
  182. proofHandler_.generate(
  183. nonObject_, secondarySecret_, primaryCiphertext_,
  184. secondaryCiphertext_);
  185. }).toThrow();
  186. expect(function() {
  187. proofHandler_.generate(
  188. emptyObject_, secondarySecret_, primaryCiphertext_,
  189. secondaryCiphertext_);
  190. }).toThrow();
  191. expect(function() {
  192. proofHandler_.generate(
  193. primarySecretFromAnotherGroup_, secondarySecret_,
  194. primaryCiphertext_, secondaryCiphertext_);
  195. }).toThrow();
  196. });
  197. it('throw an error when generating a plaintext equality proof, using an invalid secondary secret',
  198. function() {
  199. expect(function() {
  200. proofHandler_.generate(
  201. primarySecret_, undefined, primaryCiphertext_,
  202. secondaryCiphertext_);
  203. }).toThrow();
  204. expect(function() {
  205. proofHandler_.generate(
  206. primarySecret_, null, primaryCiphertext_,
  207. secondaryCiphertext_);
  208. }).toThrow();
  209. expect(function() {
  210. proofHandler_.generate(
  211. primarySecret_, nonObject_, primaryCiphertext_,
  212. secondaryCiphertext_);
  213. }).toThrow();
  214. expect(function() {
  215. proofHandler_.generate(
  216. primarySecret_, emptyObject_, primaryCiphertext_,
  217. secondaryCiphertext_);
  218. }).toThrow();
  219. expect(function() {
  220. proofHandler_.generate(
  221. primarySecret_, secondarySecretFromAnotherGroup_,
  222. primaryCiphertext_, secondaryCiphertext_);
  223. }).toThrow();
  224. });
  225. it('throw an error when generating a plaintext equality proof, using an invalid primary ciphertext',
  226. function() {
  227. expect(function() {
  228. proofHandler_.generate(
  229. primarySecret_, secondarySecret_, undefined,
  230. secondaryCiphertext_);
  231. }).toThrow();
  232. expect(function() {
  233. proofHandler_.generate(
  234. primarySecret_, secondarySecret_, null, secondaryCiphertext_);
  235. }).toThrow();
  236. expect(function() {
  237. proofHandler_.generate(
  238. primarySecret_, secondarySecret_, nonObject_,
  239. secondaryCiphertext_);
  240. }).toThrow();
  241. expect(function() {
  242. proofHandler_.generate(
  243. primarySecret_, secondarySecret_, emptyObject_,
  244. secondaryCiphertext_);
  245. }).toThrow();
  246. expect(function() {
  247. proofHandler_.generate(
  248. primarySecret_, secondarySecret_,
  249. primaryCiphertextWithLessElements_, secondaryCiphertext_);
  250. }).toThrow();
  251. expect(function() {
  252. proofHandler_.generate(
  253. primarySecret_, secondarySecret_,
  254. primaryCiphertextFromAnotherGroup_, secondaryCiphertext_);
  255. }).toThrow();
  256. });
  257. it('throw an error when generating a plaintext equality proof, using an invalid secondary ciphertext',
  258. function() {
  259. expect(function() {
  260. proofHandler_.generate(
  261. primarySecret_, secondarySecret_, primaryCiphertext_,
  262. undefined);
  263. }).toThrow();
  264. expect(function() {
  265. proofHandler_.generate(
  266. primarySecret_, secondarySecret_, primaryCiphertext_, null);
  267. }).toThrow();
  268. expect(function() {
  269. proofHandler_.generate(
  270. primarySecret_, secondarySecret_, primaryCiphertext_,
  271. nonObject_);
  272. }).toThrow();
  273. expect(function() {
  274. proofHandler_.generate(
  275. primarySecret_, secondarySecret_, primaryCiphertext_,
  276. emptyObject_);
  277. }).toThrow();
  278. expect(function() {
  279. proofHandler_.generate(
  280. primarySecret_, secondarySecret_, primaryCiphertext_,
  281. secondaryCiphertextWithLessElements_);
  282. }).toThrow();
  283. expect(function() {
  284. proofHandler_.generate(
  285. primarySecret_, secondarySecret_, primaryCiphertext_,
  286. secondaryCiphertextFromAnotherGroup_);
  287. }).toThrow();
  288. });
  289. it('throw an error when generating a simple plaintext equality proof, using invalid optional data',
  290. function() {
  291. expect(function() {
  292. proofHandler_.generate(
  293. primarySecret_, secondarySecret_, primaryCiphertext_,
  294. secondaryCiphertext_, {data: nonAuxiliaryData_});
  295. }).toThrow();
  296. expect(function() {
  297. proofHandler_.generate(
  298. primarySecret_, secondarySecret_, primaryCiphertext_,
  299. secondaryCiphertext_, {preComputation: nonObject_});
  300. }).toThrow();
  301. expect(function() {
  302. proofHandler_.generate(
  303. primarySecret_, secondarySecret_, primaryCiphertext_,
  304. secondaryCiphertext_, {preComputation: nonProofObject_});
  305. }).toThrow();
  306. });
  307. it('throw an error when verifying a plaintext equality proof, using an invalid proof',
  308. function() {
  309. expect(function() {
  310. proofHandler_.verify(
  311. undefined, primaryCiphertext_, secondaryCiphertext_);
  312. }).toThrow();
  313. expect(function() {
  314. proofHandler_.verify(
  315. null, primaryCiphertext_, secondaryCiphertext_);
  316. }).toThrow();
  317. expect(function() {
  318. proofHandler_.verify(
  319. nonProofObject_, primaryCiphertext_, secondaryCiphertext_);
  320. }).toThrow();
  321. });
  322. it('throw an error when verifying a plaintext equality proof, using an invalid primary ciphertext',
  323. function() {
  324. expect(function() {
  325. proofHandler_.verify(proof_, undefined, secondaryCiphertext_);
  326. }).toThrow();
  327. expect(function() {
  328. proofHandler_.verify(proof_, null, secondaryCiphertext_);
  329. }).toThrow();
  330. expect(function() {
  331. proofHandler_.verify(proof_, nonObject_, secondaryCiphertext_);
  332. }).toThrow();
  333. expect(function() {
  334. proofHandler_.verify(proof_, emptyObject_, secondaryCiphertext_);
  335. }).toThrow();
  336. expect(function() {
  337. proofHandler_.verify(
  338. proof_, primaryCiphertextWithLessElements_,
  339. secondaryCiphertext_);
  340. }).toThrow();
  341. expect(function() {
  342. proofHandler_.verify(
  343. proof_, primaryCiphertextFromAnotherGroup_,
  344. secondaryCiphertext_);
  345. }).toThrow();
  346. });
  347. it('throw an error when verifying a plaintext equality proof, using an invalid secondary ciphertext',
  348. function() {
  349. expect(function() {
  350. proofHandler_.verify(proof_, primaryCiphertext_, undefined);
  351. }).toThrow();
  352. expect(function() {
  353. proofHandler_.verify(proof_, primaryCiphertext_, null);
  354. }).toThrow();
  355. expect(function() {
  356. proofHandler_.verify(proof_, primaryCiphertext_, nonObject_);
  357. }).toThrow();
  358. expect(function() {
  359. proofHandler_.verify(proof_, primaryCiphertext_, emptyObject_);
  360. }).toThrow();
  361. expect(function() {
  362. proofHandler_.verify(
  363. proof_, primaryCiphertext_,
  364. secondaryCiphertextWithLessElements_);
  365. }).toThrow();
  366. expect(function() {
  367. proofHandler_.verify(
  368. proof_, primaryCiphertext_,
  369. secondaryCiphertextFromAnotherGroup_);
  370. }).toThrow();
  371. });
  372. it('throw an error when verifying a simple plaintext equality proof, using invalid optional data',
  373. function() {
  374. expect(function() {
  375. proofHandler_.verify(
  376. proof_, primaryCiphertext_, secondaryCiphertext_,
  377. {data: nonAuxiliaryData_});
  378. }).toThrow();
  379. });
  380. it('throw an error when measuring plaintext equality proof progress, using invalid input data',
  381. function() {
  382. expect(function() {
  383. proofHandler_.measureProgress();
  384. }).toThrow();
  385. expect(function() {
  386. proofHandler_.measureProgress(undefined);
  387. }).toThrow();
  388. expect(function() {
  389. proofHandler_.measureProgress(null);
  390. }).toThrow();
  391. expect(function() {
  392. proofHandler_.measureProgress(nonFunction_);
  393. }).toThrow();
  394. expect(function() {
  395. proofHandler_.measureProgress(callback_, null);
  396. }).toThrow();
  397. expect(function() {
  398. proofHandler_.measureProgress(callback_, nonNumber_);
  399. }).toThrow();
  400. expect(function() {
  401. proofHandler_.measureProgress(callback_, nonPositiveNumber_);
  402. }).toThrow();
  403. });
  404. });
  405. });
  406. });