2
0

proofs.service.exponentiation.spec.js 15 KB

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