proofs.service.plaintext_exponent_equality.spec.js 18 KB

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