2
0

proofs.service.plaintext.spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. describe('Proofs service ...', function() {
  9. 'use strict';
  10. // Initialize variables common to all proof tests.
  11. beforeEach(initializeTestVars);
  12. // Plaintext proof tests.
  13. describe('should be able to ..', function() {
  14. var num_plaintext_elements = 3;
  15. var num_progress_checks = num_plaintext_elements + 1;
  16. var initialized = false;
  17. var plaintext;
  18. var publicKey;
  19. var ciphertext;
  20. var witness;
  21. var proof;
  22. var preComputedValues;
  23. var anotherPlaintext;
  24. var anotherPublicKey;
  25. var anotherWitness;
  26. var anotherCiphertext;
  27. var aShorterPlaintext;
  28. var aShorterCiphertext;
  29. // Initialize variables common to plaintext proof tests.
  30. beforeEach(function() {
  31. if (!initialized) {
  32. cryptolib('proofs.service', function(box) {
  33. plaintext = generatePlaintext(num_plaintext_elements);
  34. publicKey =
  35. generateElGamalKeyPair(num_plaintext_elements).getPublicKey();
  36. ciphertext = generateCiphertext(publicKey, plaintext);
  37. witness = ciphertext.getR();
  38. proof = box.proofs.service.createPlaintextProof(
  39. publicKey, ciphertext, plaintext, witness, _zpSubgroup);
  40. preComputedValues = box.proofs.service.preComputePlaintextProof(
  41. publicKey, _zpSubgroup);
  42. do {
  43. anotherPlaintext = generatePlaintext(num_plaintext_elements);
  44. } while (anotherPlaintext.equals(plaintext));
  45. do {
  46. anotherPublicKey =
  47. generateElGamalKeyPair(num_plaintext_elements).getPublicKey();
  48. } while (anotherPublicKey.getGroupElementsArray().equals(
  49. publicKey.getGroupElementsArray()));
  50. anotherCiphertext = generateCiphertext(publicKey, anotherPlaintext);
  51. do {
  52. anotherWitness = generateRandomExponent(_zpSubgroup);
  53. } while (anotherWitness.getValue().equals(witness.getValue()));
  54. aShorterPlaintext = generatePlaintext(num_plaintext_elements - 1);
  55. var subPublicKey =
  56. generateElGamalKeyPair(num_plaintext_elements - 1).getPublicKey();
  57. aShorterCiphertext =
  58. generateCiphertext(subPublicKey, aShorterPlaintext);
  59. });
  60. initialized = true;
  61. }
  62. });
  63. it('generate and verify a plaintext proof', function() {
  64. cryptolib('proofs.service', function(box) {
  65. var verification = box.proofs.service.verifyPlaintextProof(
  66. publicKey, ciphertext, plaintext, proof, _zpSubgroup);
  67. expect(verification).toBeTruthy();
  68. });
  69. });
  70. it('and generate and verify a plaintext proof, using pre-computed values',
  71. function() {
  72. cryptolib('proofs.service', function(box) {
  73. var anotherProof = box.proofs.service.createPlaintextProof(
  74. publicKey, ciphertext, plaintext, witness, _zpSubgroup,
  75. preComputedValues);
  76. var verification = box.proofs.service.verifyPlaintextProof(
  77. publicKey, ciphertext, plaintext, anotherProof, _zpSubgroup);
  78. expect(verification).toBeTruthy();
  79. });
  80. });
  81. it('and generate and verify a plaintext proof, using null for the pre-computed values option',
  82. function() {
  83. cryptolib('proofs.service', function(box) {
  84. var anotherProof = box.proofs.service.createPlaintextProof(
  85. publicKey, ciphertext, plaintext, witness, _zpSubgroup, null);
  86. var verification = box.proofs.service.verifyPlaintextProof(
  87. publicKey, ciphertext, plaintext, anotherProof, _zpSubgroup);
  88. expect(verification).toBeTruthy();
  89. });
  90. });
  91. it('generate and verify a plaintext proof, using serialization',
  92. function() {
  93. cryptolib('commons.mathematical', 'proofs', function(box) {
  94. var proofAsJson = proof.stringify();
  95. var proofFromJson = box.proofs.utils.deserializeProof(proofAsJson);
  96. var verification = box.proofs.service.verifyPlaintextProof(
  97. publicKey, ciphertext, plaintext, proofFromJson, _zpSubgroup);
  98. expect(verification).toBeTruthy();
  99. });
  100. });
  101. it('generate and verify a plaintext proof, using pre-computed values and serialization',
  102. function() {
  103. cryptolib('commons.mathematical', 'proofs', function(box) {
  104. var preComputedValuesAsJson = preComputedValues.stringify();
  105. var preComputedValuesFromJson =
  106. box.proofs.utils.deserializeProofPreComputedValues(
  107. preComputedValuesAsJson);
  108. var anotherProof = box.proofs.service.createPlaintextProof(
  109. publicKey, ciphertext, plaintext, witness, _zpSubgroup,
  110. preComputedValuesFromJson);
  111. var verification = box.proofs.service.verifyPlaintextProof(
  112. publicKey, ciphertext, plaintext, anotherProof, _zpSubgroup);
  113. expect(verification).toBeTruthy();
  114. });
  115. });
  116. it('and measure progress when generating a plaintext proof', function() {
  117. cryptolib('proofs.service', function(box) {
  118. initializeProgressMeter();
  119. var anotherProof = box.proofs.service.createPlaintextProof(
  120. publicKey, ciphertext, plaintext, witness, _zpSubgroup,
  121. progressCallback, PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
  122. expect(_progressPercentSum)
  123. .toBe(getProgressPercentSum(
  124. num_progress_checks, PROGRESS_PERCENT_MIN_CHECK_INTERVAL));
  125. expect(_progressPercentLatest).toBe(100);
  126. var verification = box.proofs.service.verifyPlaintextProof(
  127. publicKey, ciphertext, plaintext, anotherProof, _zpSubgroup);
  128. expect(verification).toBeTruthy();
  129. });
  130. });
  131. it('and measure progress when pre-computing values for a plaintext proof',
  132. function() {
  133. cryptolib('proofs.service', function(box) {
  134. initializeProgressMeter();
  135. box.proofs.service.preComputePlaintextProof(
  136. publicKey, _zpSubgroup, progressCallback,
  137. PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
  138. expect(_progressPercentSum)
  139. .toBe(getProgressPercentSum(
  140. num_progress_checks, PROGRESS_PERCENT_MIN_CHECK_INTERVAL));
  141. expect(_progressPercentLatest).toBe(100);
  142. var anotherProof = box.proofs.service.createPlaintextProof(
  143. publicKey, ciphertext, plaintext, witness, _zpSubgroup,
  144. preComputedValues);
  145. var verification = box.proofs.service.verifyPlaintextProof(
  146. publicKey, ciphertext, plaintext, anotherProof, _zpSubgroup);
  147. expect(verification).toBeTruthy();
  148. });
  149. });
  150. it('and measure progress when verifying a plaintext proof', function() {
  151. cryptolib('proofs.service', function(box) {
  152. initializeProgressMeter();
  153. var verification = box.proofs.service.verifyPlaintextProof(
  154. publicKey, ciphertext, plaintext, proof, _zpSubgroup,
  155. progressCallback, PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
  156. expect(verification).toBeTruthy();
  157. expect(_progressPercentSum)
  158. .toBe(getProgressPercentSum(
  159. num_progress_checks, PROGRESS_PERCENT_MIN_CHECK_INTERVAL));
  160. expect(_progressPercentLatest).toBe(100);
  161. });
  162. });
  163. it('and fail to verify a plaintext proof that was generated with the wrong plaintext',
  164. function() {
  165. cryptolib('proofs.service', function(box) {
  166. var anotherProof = box.proofs.service.createPlaintextProof(
  167. publicKey, ciphertext, anotherPlaintext, witness, _zpSubgroup);
  168. var verification = box.proofs.service.verifyPlaintextProof(
  169. publicKey, ciphertext, anotherPlaintext, anotherProof,
  170. _zpSubgroup);
  171. expect(verification).toBeFalsy();
  172. });
  173. });
  174. it('and fail to verify a plaintext proof that was generated with the wrong public key',
  175. function() {
  176. cryptolib('proofs.service', function(box) {
  177. var anotherProof = box.proofs.service.createPlaintextProof(
  178. anotherPublicKey, ciphertext, plaintext, witness, _zpSubgroup);
  179. var verification = box.proofs.service.verifyPlaintextProof(
  180. anotherPublicKey, ciphertext, plaintext, anotherProof,
  181. _zpSubgroup);
  182. expect(verification).toBeFalsy();
  183. });
  184. });
  185. it('and fail to verify a plaintext proof that was generated with the wrong witness',
  186. function() {
  187. cryptolib('proofs.service', function(box) {
  188. var anotherProof = box.proofs.service.createPlaintextProof(
  189. publicKey, ciphertext, plaintext, anotherWitness, _zpSubgroup);
  190. var verification = box.proofs.service.verifyPlaintextProof(
  191. publicKey, ciphertext, plaintext, anotherProof, _zpSubgroup);
  192. expect(verification).toBeFalsy();
  193. });
  194. });
  195. it('and fail to verify a plaintext proof when using the wrong plaintext',
  196. function() {
  197. cryptolib('proofs.service', function(box) {
  198. var verification = box.proofs.service.verifyPlaintextProof(
  199. publicKey, ciphertext, anotherPlaintext, proof, _zpSubgroup);
  200. expect(verification).toBeFalsy();
  201. });
  202. });
  203. it('and fail to verify a plaintext proof when using the wrong ciphertext',
  204. function() {
  205. cryptolib('proofs.service', function(box) {
  206. var verification = box.proofs.service.verifyPlaintextProof(
  207. publicKey, anotherCiphertext, plaintext, proof, _zpSubgroup);
  208. expect(verification).toBeFalsy();
  209. });
  210. });
  211. it('and fail to verify a plaintext proof when using the wrong public key',
  212. function() {
  213. cryptolib('proofs.service', function(box) {
  214. var verification = box.proofs.service.verifyPlaintextProof(
  215. anotherPublicKey, ciphertext, plaintext, proof, _zpSubgroup);
  216. expect(verification).toBeFalsy();
  217. });
  218. });
  219. it('and throw an exception when generating a plaintext proof, using null input data',
  220. function() {
  221. cryptolib('proofs.service', function(box) {
  222. expect(function() {
  223. box.proofs.service.createPlaintextProof(
  224. null, ciphertext, plaintext, witness, _zpSubgroup);
  225. }).toThrow();
  226. expect(function() {
  227. box.proofs.service.createPlaintextProof(
  228. publicKey, null, plaintext, witness, _zpSubgroup);
  229. }).toThrow();
  230. expect(function() {
  231. box.proofs.service.createPlaintextProof(
  232. publicKey, ciphertext, null, witness, _zpSubgroup);
  233. }).toThrow();
  234. expect(function() {
  235. box.proofs.service.createPlaintextProof(
  236. publicKey, ciphertext, plaintext, null, _zpSubgroup);
  237. }).toThrow();
  238. expect(function() {
  239. box.proofs.service.createPlaintextProof(
  240. publicKey, ciphertext, plaintext, witness, null);
  241. }).toThrow();
  242. });
  243. });
  244. it('and throw an exception when generating a plaintext proof, using the wrong mathematical group',
  245. function() {
  246. cryptolib('proofs.service', function(box) {
  247. expect(function() {
  248. box.proofs.service.createPlaintextProof(
  249. publicKey, ciphertext, plaintext, witness, _anotherZpSubgroup);
  250. }).toThrow();
  251. });
  252. });
  253. it('and throw an exception when generating a plaintext proof, using a public key and a plaintext with different lengths',
  254. function() {
  255. cryptolib('proofs.service', function(box) {
  256. expect(function() {
  257. box.proofs.service.createPlaintextProof(
  258. publicKey, ciphertext, aShorterPlaintext, witness,
  259. _zpSubgroup);
  260. }).toThrow();
  261. });
  262. });
  263. it('and throw an exception when generating a plaintext proof, using a public key and a ciphertext with the incorrect length relation',
  264. function() {
  265. cryptolib('proofs.service', function(box) {
  266. expect(function() {
  267. box.proofs.service.createPlaintextProof(
  268. publicKey, aShorterCiphertext, plaintext, witness,
  269. _zpSubgroup);
  270. }).toThrow();
  271. });
  272. });
  273. it('and throw an exception when generating a plaintext proof, using a progress callback function that is not a function',
  274. function() {
  275. cryptolib('proofs.service', function(box) {
  276. expect(function() {
  277. box.proofs.service.createPlaintextProof(
  278. publicKey, ciphertext, plaintext, witness, _zpSubgroup,
  279. PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
  280. }).toThrow();
  281. });
  282. });
  283. it('and throw an exception when pre-computing values for a plaintext proof, using null input data',
  284. function() {
  285. cryptolib('proofs.service', function(box) {
  286. expect(function() {
  287. box.proofs.service.preComputePlaintextProof(null, _zpSubgroup);
  288. }).toThrow();
  289. expect(function() {
  290. box.proofs.service.preComputePlaintextProof(publicKey, null);
  291. }).toThrow();
  292. });
  293. });
  294. it('and throw an exception when generating a plaintext proof, using a progress percent check interval that is not a number',
  295. function() {
  296. cryptolib('proofs.service', function(box) {
  297. var percentMeasuredLatest = 0;
  298. var progressCallback = function(progressPercent) {
  299. percentMeasuredLatest = progressPercent;
  300. };
  301. expect(function() {
  302. box.proofs.service.createPlaintextProof(
  303. publicKey, ciphertext, plaintext, witness, _zpSubgroup,
  304. progressCallback, progressCallback);
  305. }).toThrow();
  306. });
  307. });
  308. it('and throw an exception when pre-computing values for a plaintext proof, using null input data',
  309. function() {
  310. cryptolib('proofs.service', function(box) {
  311. expect(function() {
  312. box.proofs.service.preComputePlaintextProof(null, _zpSubgroup);
  313. }).toThrow();
  314. expect(function() {
  315. box.proofs.service.preComputePlaintextProof(publicKey, null);
  316. }).toThrow();
  317. });
  318. });
  319. it('and throw an exception when verifying a plaintext proof, using null input data',
  320. function() {
  321. cryptolib('proofs.service', function(box) {
  322. expect(function() {
  323. box.proofs.service.verifyPlaintextProof(
  324. null, ciphertext, plaintext, proof, _zpSubgroup);
  325. }).toThrow();
  326. expect(function() {
  327. box.proofs.service.verifyPlaintextProof(
  328. publicKey, null, plaintext, proof, _zpSubgroup);
  329. }).toThrow();
  330. expect(function() {
  331. box.proofs.service.verifyPlaintextProof(
  332. publicKey, ciphertext, null, proof, _zpSubgroup);
  333. }).toThrow();
  334. expect(function() {
  335. box.proofs.service.verifyPlaintextProof(
  336. publicKey, ciphertext, plaintext, null, _zpSubgroup);
  337. }).toThrow();
  338. expect(function() {
  339. box.proofs.service.verifyPlaintextProof(
  340. publicKey, ciphertext, plaintext, proof, null);
  341. }).toThrow();
  342. });
  343. });
  344. it('and throw an exception when verifying a plaintext proof, using the wrong mathematical group',
  345. function() {
  346. cryptolib('proofs.service', function(box) {
  347. expect(function() {
  348. box.proofs.service.verifyPlaintextProof(
  349. publicKey, ciphertext, plaintext, proof, _anotherZpSubgroup);
  350. }).toThrow();
  351. });
  352. });
  353. it('and throw an exception when verifying a plaintext proof, using a public key and a plaintext with different lengths',
  354. function() {
  355. cryptolib('proofs.service', function(box) {
  356. expect(function() {
  357. box.proofs.service.verifyPlaintextProof(
  358. publicKey, ciphertext, aShorterPlaintext, proof, _zpSubgroup);
  359. }).toThrow();
  360. });
  361. });
  362. it('and throw an exception when verifying a plaintext proof, using a public key and a ciphertext with the incorrect length relation',
  363. function() {
  364. cryptolib('proofs.service', function(box) {
  365. expect(function() {
  366. box.proofs.service.verifyPlaintextProof(
  367. publicKey, aShorterCiphertext, plaintext, proof, _zpSubgroup);
  368. }).toThrow();
  369. });
  370. });
  371. it('and throw an exception when verifying a plaintext proof, using a progress callback function that is not a function',
  372. function() {
  373. cryptolib('proofs.service', function(box) {
  374. expect(function() {
  375. box.proofs.service.verifyPlaintextProof(
  376. publicKey, ciphertext, plaintext, proof, _zpSubgroup,
  377. PROGRESS_PERCENT_MIN_CHECK_INTERVAL);
  378. }).toThrow();
  379. });
  380. });
  381. it('and throw an exception when verifying a plaintext proof, using a progress percent check interval that is not a number',
  382. function() {
  383. cryptolib('proofs.service', function(box) {
  384. var percentMeasuredLatest = 0;
  385. var progressCallback = function(progressPercent) {
  386. percentMeasuredLatest = progressPercent;
  387. };
  388. expect(function() {
  389. box.proofs.service.verifyPlaintextProof(
  390. publicKey, ciphertext, plaintext, proof, _zpSubgroup,
  391. progressCallback, progressCallback);
  392. }).toThrow();
  393. });
  394. });
  395. });
  396. });