2
0

proofs.service.schnorr.spec.js 16 KB

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