simple-plaintext-equality-proof-validation.spec.js 15 KB

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