2
0

proofs.spec.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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 ...', function() {
  9. 'use strict';
  10. describe('Should be loaded if cryptolib is called with proofs: ', function() {
  11. it('should be defined a proofs', function() {
  12. cryptolib('*', function(box) {
  13. expect(typeof box.proofs.SimplePlaintextEqualityProofGenerator)
  14. .toBeDefined();
  15. });
  16. });
  17. });
  18. describe('PhiFunction ...', function() {
  19. describe('validate inputs ...', function() {
  20. it('valid inputs', function() {
  21. cryptolib('proofs', 'commons.mathematical', function(box) {
  22. // create PhiFunction
  23. var q = new box.forge.jsbn.BigInteger('11');
  24. var generator = new box.forge.jsbn.BigInteger('2');
  25. var p = new box.forge.jsbn.BigInteger('23');
  26. var zpSubgroup =
  27. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  28. var numInputs = new box.forge.jsbn.BigInteger('1');
  29. var numOutputs = new box.forge.jsbn.BigInteger('1');
  30. var baseElements = [
  31. new box.forge.jsbn.BigInteger('1'),
  32. new box.forge.jsbn.BigInteger('2')
  33. ];
  34. var computationsRules = [[0, 1], [1, 1]];
  35. var createdPhiFunction = new box.proofs.PhiFunction(
  36. zpSubgroup, numInputs, numOutputs, baseElements,
  37. computationsRules);
  38. expect(createdPhiFunction).toBeDefined();
  39. });
  40. });
  41. it('invalid group', function() {
  42. cryptolib('proofs', 'commons.mathematical', function(box) {
  43. // create PhiFunction
  44. var zpSubgroup = new box.forge.jsbn.BigInteger('11');
  45. var numInputs = new box.forge.jsbn.BigInteger('1');
  46. var numOutputs = new box.forge.jsbn.BigInteger('1');
  47. var baseElements = [
  48. new box.forge.jsbn.BigInteger('1'),
  49. new box.forge.jsbn.BigInteger('2')
  50. ];
  51. var computationsRules = [[0, 1], [1, 1]];
  52. // Note: Expect exception not to be thrown here but proof verification
  53. // will fail; See service tests.
  54. box.proofs.PhiFunction(
  55. zpSubgroup, numInputs, numOutputs, baseElements,
  56. computationsRules);
  57. });
  58. });
  59. it('invalid number of inputs', function() {
  60. cryptolib('proofs', 'commons.mathematical', function(box) {
  61. // create PhiFunction
  62. var q = new box.forge.jsbn.BigInteger('11');
  63. var generator = new box.forge.jsbn.BigInteger('2');
  64. var p = new box.forge.jsbn.BigInteger('23');
  65. var zpSubgroup =
  66. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  67. var numInputs;
  68. var numOutputs = new box.forge.jsbn.BigInteger('1');
  69. var baseElements = [
  70. new box.forge.jsbn.BigInteger('1'),
  71. new box.forge.jsbn.BigInteger('2')
  72. ];
  73. var computationsRules = [[0, 1], [1, 1]];
  74. expect(function() {
  75. box.proofs.PhiFunction(
  76. zpSubgroup, numInputs, numOutputs, baseElements,
  77. computationsRules);
  78. }).toThrow();
  79. });
  80. });
  81. it('invalid number of outputs (null)', function() {
  82. cryptolib('proofs', 'commons.mathematical', function(box) {
  83. // create PhiFunction
  84. var q = new box.forge.jsbn.BigInteger('11');
  85. var generator = new box.forge.jsbn.BigInteger('2');
  86. var p = new box.forge.jsbn.BigInteger('23');
  87. var zpSubgroup =
  88. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  89. var numInputs = new box.forge.jsbn.BigInteger('1');
  90. var numOutputs;
  91. var baseElements = [
  92. new box.forge.jsbn.BigInteger('1'),
  93. new box.forge.jsbn.BigInteger('2')
  94. ];
  95. var computationsRules = [[0, 1], [1, 1]];
  96. expect(function() {
  97. box.proofs.PhiFunction(
  98. zpSubgroup, numInputs, numOutputs, baseElements,
  99. computationsRules);
  100. }).toThrow();
  101. });
  102. });
  103. it('invalid number of outputs (negative)', function() {
  104. cryptolib('proofs', 'commons.mathematical', function(box) {
  105. // create PhiFunction
  106. var q = new box.forge.jsbn.BigInteger('11');
  107. var generator = new box.forge.jsbn.BigInteger('2');
  108. var p = new box.forge.jsbn.BigInteger('23');
  109. var zpSubgroup =
  110. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  111. var numInputs = new box.forge.jsbn.BigInteger('1');
  112. var numOutputs = -2;
  113. var baseElements = [
  114. new box.forge.jsbn.BigInteger('1'),
  115. new box.forge.jsbn.BigInteger('2')
  116. ];
  117. var computationsRules = [[0, 1], [1, 1]];
  118. expect(function() {
  119. box.proofs.PhiFunction(
  120. zpSubgroup, numInputs, numOutputs, baseElements,
  121. computationsRules);
  122. }).toThrow();
  123. });
  124. });
  125. it('invalid number of outputs (array)', function() {
  126. cryptolib('proofs', 'commons.mathematical', function(box) {
  127. // create PhiFunction
  128. var q = new box.forge.jsbn.BigInteger('11');
  129. var generator = new box.forge.jsbn.BigInteger('2');
  130. var p = new box.forge.jsbn.BigInteger('23');
  131. var zpSubgroup =
  132. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  133. var numInputs = new box.forge.jsbn.BigInteger('1');
  134. var numOutputs = [[0, 1], [1, 1]];
  135. var baseElements = [
  136. new box.forge.jsbn.BigInteger('1'),
  137. new box.forge.jsbn.BigInteger('2')
  138. ];
  139. var computationsRules = [[0, 1], [1, 1]];
  140. expect(function() {
  141. box.proofs.PhiFunction(
  142. zpSubgroup, numInputs, numOutputs, baseElements,
  143. computationsRules);
  144. }).toThrow();
  145. });
  146. });
  147. it('invalid base elements (null)', function() {
  148. cryptolib('proofs', 'commons.mathematical', function(box) {
  149. // create PhiFunction
  150. var q = new box.forge.jsbn.BigInteger('11');
  151. var generator = new box.forge.jsbn.BigInteger('2');
  152. var p = new box.forge.jsbn.BigInteger('23');
  153. var zpSubgroup =
  154. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  155. var numInputs = new box.forge.jsbn.BigInteger('1');
  156. var numOutputs = new box.forge.jsbn.BigInteger('1');
  157. var baseElements;
  158. var computationsRules = [[0, 1], [1, 1]];
  159. expect(function() {
  160. box.proofs.PhiFunction(
  161. zpSubgroup, numInputs, numOutputs, baseElements,
  162. computationsRules);
  163. }).toThrow();
  164. });
  165. });
  166. it('invalid base elements (not array)', function() {
  167. cryptolib('proofs', 'commons.mathematical', function(box) {
  168. // create PhiFunction
  169. var q = new box.forge.jsbn.BigInteger('11');
  170. var generator = new box.forge.jsbn.BigInteger('2');
  171. var p = new box.forge.jsbn.BigInteger('23');
  172. var zpSubgroup =
  173. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  174. var numInputs = new box.forge.jsbn.BigInteger('1');
  175. var numOutputs = new box.forge.jsbn.BigInteger('1');
  176. var baseElements = new box.forge.jsbn.BigInteger('1');
  177. var computationsRules = [[0, 1], [1, 1]];
  178. expect(function() {
  179. box.proofs.PhiFunction(
  180. zpSubgroup, numInputs, numOutputs, baseElements,
  181. computationsRules);
  182. }).toThrow();
  183. });
  184. });
  185. it('invalid computation rules (null)', function() {
  186. cryptolib('proofs', 'commons.mathematical', function(box) {
  187. // create PhiFunction
  188. var q = new box.forge.jsbn.BigInteger('11');
  189. var generator = new box.forge.jsbn.BigInteger('2');
  190. var p = new box.forge.jsbn.BigInteger('23');
  191. var zpSubgroup =
  192. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  193. var numInputs = new box.forge.jsbn.BigInteger('1');
  194. var numOutputs = new box.forge.jsbn.BigInteger('1');
  195. var baseElements = new box.forge.jsbn.BigInteger('1');
  196. var computationsRules;
  197. expect(function() {
  198. box.proofs.PhiFunction(
  199. zpSubgroup, numInputs, numOutputs, baseElements,
  200. computationsRules);
  201. }).toThrow();
  202. });
  203. });
  204. it('invalid computation rules (not array)', function() {
  205. cryptolib('proofs', 'commons.mathematical', function(box) {
  206. // create PhiFunction
  207. var q = new box.forge.jsbn.BigInteger('11');
  208. var generator = new box.forge.jsbn.BigInteger('2');
  209. var p = new box.forge.jsbn.BigInteger('23');
  210. var zpSubgroup =
  211. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  212. var numInputs = new box.forge.jsbn.BigInteger('1');
  213. var numOutputs = new box.forge.jsbn.BigInteger('1');
  214. var baseElements = new box.forge.jsbn.BigInteger('1');
  215. var computationsRules = new box.forge.jsbn.BigInteger('1');
  216. expect(function() {
  217. box.proofs.PhiFunction(
  218. zpSubgroup, numInputs, numOutputs, baseElements,
  219. computationsRules);
  220. }).toThrow();
  221. });
  222. });
  223. it('invalid computation rules: first index value is smaller than 1',
  224. function() {
  225. cryptolib('proofs', 'commons.mathematical', function(box) {
  226. // create PhiFunction
  227. var q = new box.forge.jsbn.BigInteger('11');
  228. var generator = new box.forge.jsbn.BigInteger('2');
  229. var p = new box.forge.jsbn.BigInteger('23');
  230. var zpSubgroup =
  231. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  232. var numInputs = new box.forge.jsbn.BigInteger('1');
  233. var numOutputs = new box.forge.jsbn.BigInteger('1');
  234. var zpGroupElement_p23_v2 =
  235. new box.commons.mathematical.ZpGroupElement(
  236. new box.forge.jsbn.BigInteger('2'), p, q);
  237. var baseElements = [zpGroupElement_p23_v2];
  238. var computationsRules = [[[0, 1]]];
  239. expect(function() {
  240. box.proofs.PhiFunction(
  241. zpSubgroup, numInputs, numOutputs, baseElements,
  242. computationsRules);
  243. }).toThrow();
  244. });
  245. });
  246. it('invalid computation rules: first index value larger then number of base elements',
  247. function() {
  248. cryptolib('proofs', 'commons.mathematical', function(box) {
  249. // create PhiFunction
  250. var q = new box.forge.jsbn.BigInteger('11');
  251. var generator = new box.forge.jsbn.BigInteger('2');
  252. var p = new box.forge.jsbn.BigInteger('23');
  253. var zpSubgroup =
  254. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  255. var numInputs = new box.forge.jsbn.BigInteger('1');
  256. var numOutputs = new box.forge.jsbn.BigInteger('1');
  257. var zpGroupElement_p23_v2 =
  258. new box.commons.mathematical.ZpGroupElement(
  259. new box.forge.jsbn.BigInteger('2'), p, q);
  260. var baseElements = [zpGroupElement_p23_v2, zpGroupElement_p23_v2];
  261. var computationsRules = [[[3, 1]]];
  262. expect(function() {
  263. box.proofs.PhiFunction(
  264. zpSubgroup, numInputs, numOutputs, baseElements,
  265. computationsRules);
  266. }).toThrow();
  267. });
  268. });
  269. it('invalid computation rules: second index value smaller than 1',
  270. function() {
  271. cryptolib('proofs', 'commons.mathematical', function(box) {
  272. // create PhiFunction
  273. var q = new box.forge.jsbn.BigInteger('11');
  274. var generator = new box.forge.jsbn.BigInteger('2');
  275. var p = new box.forge.jsbn.BigInteger('23');
  276. var zpSubgroup =
  277. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  278. var numInputs = new box.forge.jsbn.BigInteger('1');
  279. var numOutputs = new box.forge.jsbn.BigInteger('1');
  280. var zpGroupElement_p23_v2 =
  281. new box.commons.mathematical.ZpGroupElement(
  282. new box.forge.jsbn.BigInteger('2'), p, q);
  283. var baseElements = [zpGroupElement_p23_v2];
  284. var computationsRules = [[[1, 0]]];
  285. expect(function() {
  286. box.proofs.PhiFunction(
  287. zpSubgroup, numInputs, numOutputs, baseElements,
  288. computationsRules);
  289. }).toThrow();
  290. });
  291. });
  292. it('invalid computation rules: second index value bigger than number of inputs',
  293. function() {
  294. cryptolib('proofs', 'commons.mathematical', function(box) {
  295. // create PhiFunction
  296. var q = new box.forge.jsbn.BigInteger('11');
  297. var generator = new box.forge.jsbn.BigInteger('2');
  298. var p = new box.forge.jsbn.BigInteger('23');
  299. var zpSubgroup =
  300. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  301. var numInputs = new box.forge.jsbn.BigInteger('2');
  302. var numOutputs = new box.forge.jsbn.BigInteger('1');
  303. var zpGroupElement_p23_v2 =
  304. new box.commons.mathematical.ZpGroupElement(
  305. new box.forge.jsbn.BigInteger('2'), p, q);
  306. var zpGroupElement_p23_v3 =
  307. new box.commons.mathematical.ZpGroupElement(
  308. new box.forge.jsbn.BigInteger('3'), p, q);
  309. var baseElements = [zpGroupElement_p23_v2, zpGroupElement_p23_v3];
  310. var computationsRules = [[[1, 2], [1, 3]]];
  311. expect(function() {
  312. box.proofs.PhiFunction(
  313. zpSubgroup, numInputs, numOutputs, baseElements,
  314. computationsRules);
  315. }).toThrow();
  316. });
  317. });
  318. });
  319. describe('calculatePhi ...', function() {
  320. it('invalid inputs', function() {
  321. cryptolib('proofs', 'commons.mathematical', function(box) {
  322. // create group
  323. var q = new box.forge.jsbn.BigInteger('11');
  324. var generator = new box.forge.jsbn.BigInteger('2');
  325. var p = new box.forge.jsbn.BigInteger('23');
  326. var zpSubgroup =
  327. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  328. // create additional needed parameters
  329. var numInputs = new box.forge.jsbn.BigInteger('1');
  330. var numOutputs = new box.forge.jsbn.BigInteger('1');
  331. var baseElements = [zpSubgroup.getGenerator()];
  332. var computationsRules = [[[1, 1]]];
  333. // create PhiFunction
  334. var createdPhiFunction = new box.proofs.PhiFunction(
  335. zpSubgroup, numInputs, numOutputs, baseElements,
  336. computationsRules);
  337. // create array of exponents
  338. var value1 = new box.forge.jsbn.BigInteger('2');
  339. var exponent1 =
  340. new box.commons.mathematical.Exponent(zpSubgroup.getQ(), value1);
  341. var exponentArray = [exponent1];
  342. var result = createdPhiFunction.calculatePhi(exponentArray);
  343. var expectedElement = new box.commons.mathematical.ZpGroupElement(
  344. new box.forge.jsbn.BigInteger('4'), zpSubgroup.getP(),
  345. zpSubgroup.getQ());
  346. var expectedResultArray = [expectedElement];
  347. expect(expectedResultArray.equals(result)).toBeTruthy();
  348. });
  349. });
  350. });
  351. });
  352. describe('Prover ...', function() {
  353. describe('validate inputs ...', function() {
  354. it('validate inputs', function() {
  355. cryptolib('proofs', function() {});
  356. });
  357. it('invalid inputs', function() {
  358. cryptolib('proofs', function() {});
  359. });
  360. });
  361. describe('prove ...', function() {
  362. describe('validate inputs ...', function() {
  363. });
  364. });
  365. });
  366. describe('Proof ...', function() {
  367. it('validate inputs', function() {
  368. cryptolib('proofs', 'commons.mathematical', function(box) {
  369. // create proof
  370. var q = new box.forge.jsbn.BigInteger('11');
  371. var generator = new box.forge.jsbn.BigInteger('2');
  372. var p = new box.forge.jsbn.BigInteger('23');
  373. var zpSubgroup =
  374. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  375. var value = new box.forge.jsbn.BigInteger('2');
  376. var hash =
  377. new box.commons.mathematical.Exponent(zpSubgroup.getQ(), value);
  378. var value1 = new box.forge.jsbn.BigInteger('2');
  379. var exp1 =
  380. new box.commons.mathematical.Exponent(zpSubgroup.getQ(), value1);
  381. var value2 = new box.forge.jsbn.BigInteger('3');
  382. var exp2 =
  383. new box.commons.mathematical.Exponent(zpSubgroup.getQ(), value2);
  384. var values = [exp1, exp2];
  385. var createdProof = new box.proofs.Proof(hash, values);
  386. expect(createdProof).toBeDefined();
  387. });
  388. });
  389. it('validate equals true', function() {
  390. cryptolib('proofs', 'commons.mathematical', function(box) {
  391. // create proof1
  392. var p1_q = new box.forge.jsbn.BigInteger('11');
  393. var p1_generator = new box.forge.jsbn.BigInteger('2');
  394. var p1_p = new box.forge.jsbn.BigInteger('23');
  395. var p1_zpSubgroup =
  396. new box.commons.mathematical.ZpSubgroup(p1_generator, p1_p, p1_q);
  397. var p1_value = new box.forge.jsbn.BigInteger('5');
  398. var p1_hash = new box.commons.mathematical.Exponent(
  399. p1_zpSubgroup.getQ(), p1_value);
  400. var p1_value1 = new box.forge.jsbn.BigInteger('3');
  401. var p1_exp1 = new box.commons.mathematical.Exponent(
  402. p1_zpSubgroup.getQ(), p1_value1);
  403. var p1_value2 = new box.forge.jsbn.BigInteger('4');
  404. var p1_exp2 = new box.commons.mathematical.Exponent(
  405. p1_zpSubgroup.getQ(), p1_value2);
  406. var p1_values = [p1_exp1, p1_exp2];
  407. var p1_createdProof = new box.proofs.Proof(p1_hash, p1_values);
  408. expect(p1_createdProof).toBeDefined();
  409. // create proof2
  410. var p2_q = new box.forge.jsbn.BigInteger('11');
  411. var p2_generator = new box.forge.jsbn.BigInteger('2');
  412. var p2_p = new box.forge.jsbn.BigInteger('23');
  413. var p2_zpSubgroup =
  414. new box.commons.mathematical.ZpSubgroup(p2_generator, p2_p, p2_q);
  415. var p2_value = new box.forge.jsbn.BigInteger('5');
  416. var p2_hash = new box.commons.mathematical.Exponent(
  417. p2_zpSubgroup.getQ(), p2_value);
  418. var p2_value1 = new box.forge.jsbn.BigInteger('3');
  419. var p2_exp1 = new box.commons.mathematical.Exponent(
  420. p2_zpSubgroup.getQ(), p2_value1);
  421. var p2_value2 = new box.forge.jsbn.BigInteger('4');
  422. var p2_exp2 = new box.commons.mathematical.Exponent(
  423. p2_zpSubgroup.getQ(), p2_value2);
  424. var p2_values = [p2_exp1, p2_exp2];
  425. var p2_createdProof = new box.proofs.Proof(p2_hash, p2_values);
  426. expect(p2_createdProof).toBeDefined();
  427. expect(p1_createdProof.equals(p2_createdProof)).toBeTruthy();
  428. });
  429. });
  430. it('validate equals false', function() {
  431. cryptolib('proofs', 'commons.mathematical', function(box) {
  432. // create proof1
  433. var p1_q = new box.forge.jsbn.BigInteger('11');
  434. var p1_generator = new box.forge.jsbn.BigInteger('2');
  435. var p1_p = new box.forge.jsbn.BigInteger('23');
  436. var p1_zpSubgroup =
  437. new box.commons.mathematical.ZpSubgroup(p1_generator, p1_p, p1_q);
  438. var p1_value = new box.forge.jsbn.BigInteger('5');
  439. var p1_hash = new box.commons.mathematical.Exponent(
  440. p1_zpSubgroup.getQ(), p1_value);
  441. var p1_value1 = new box.forge.jsbn.BigInteger('3');
  442. var p1_exp1 = new box.commons.mathematical.Exponent(
  443. p1_zpSubgroup.getQ(), p1_value1);
  444. var p1_value2 = new box.forge.jsbn.BigInteger('3');
  445. var p1_exp2 = new box.commons.mathematical.Exponent(
  446. p1_zpSubgroup.getQ(), p1_value2);
  447. var p1_values = [p1_exp1, p1_exp2];
  448. var p1_createdProof = new box.proofs.Proof(p1_hash, p1_values);
  449. expect(p1_createdProof).toBeDefined();
  450. // create proof2
  451. var p2_q = new box.forge.jsbn.BigInteger('11');
  452. var p2_generator = new box.forge.jsbn.BigInteger('2');
  453. var p2_p = new box.forge.jsbn.BigInteger('23');
  454. var p2_zpSubgroup =
  455. new box.commons.mathematical.ZpSubgroup(p2_generator, p2_p, p2_q);
  456. var p2_value = new box.forge.jsbn.BigInteger('5');
  457. var p2_hash = new box.commons.mathematical.Exponent(
  458. p2_zpSubgroup.getQ(), p2_value);
  459. var p2_value1 = new box.forge.jsbn.BigInteger('3');
  460. var p2_exp1 = new box.commons.mathematical.Exponent(
  461. p2_zpSubgroup.getQ(), p2_value1);
  462. var p2_value2 = new box.forge.jsbn.BigInteger('4');
  463. var p2_exp2 = new box.commons.mathematical.Exponent(
  464. p2_zpSubgroup.getQ(), p2_value2);
  465. var p2_values = [p2_exp1, p2_exp2];
  466. var p2_createdProof = new box.proofs.Proof(p2_hash, p2_values);
  467. expect(p2_createdProof).toBeDefined();
  468. expect(p1_createdProof.equals(p2_createdProof)).not.toBeTruthy();
  469. });
  470. });
  471. it('stringify correctly to the JSON string: {"zkProof":{"q":"Cw==","hash":"Ag==","values":["Ag==","Aw=="]}}',
  472. function() {
  473. cryptolib('proofs', 'commons.mathematical', function(box) {
  474. // create proof
  475. var q = new box.forge.jsbn.BigInteger('11');
  476. var generator = new box.forge.jsbn.BigInteger('2');
  477. var p = new box.forge.jsbn.BigInteger('23');
  478. var zpSubgroup =
  479. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  480. var value = new box.forge.jsbn.BigInteger('2');
  481. var hash =
  482. new box.commons.mathematical.Exponent(zpSubgroup.getQ(), value);
  483. var value1 = new box.forge.jsbn.BigInteger('2');
  484. var exp1 =
  485. new box.commons.mathematical.Exponent(zpSubgroup.getQ(), value1);
  486. var value2 = new box.forge.jsbn.BigInteger('3');
  487. var exp2 =
  488. new box.commons.mathematical.Exponent(zpSubgroup.getQ(), value2);
  489. var values = [exp1, exp2];
  490. var proof = new box.proofs.Proof(hash, values);
  491. var jsonAsString = proof.stringify();
  492. expect(jsonAsString).toContain('"zkProof"');
  493. expect(jsonAsString).toContain('"q":"Cw=="');
  494. expect(jsonAsString).toContain('"hash":"Ag=="');
  495. expect(jsonAsString).toContain('"values":');
  496. expect(jsonAsString.length)
  497. .toBe(
  498. '{"zkProof":{"q":"Cw==","hash":"Ag==","values":["Ag==","Aw=="]}}'
  499. .length);
  500. });
  501. });
  502. it('deserialize correctly from the JSON string: {"zkProof":{"q":"Cw==","hash":"Ag==","values":["Ag==","Aw=="]}}',
  503. function() {
  504. cryptolib('proofs', 'commons.mathematical', function(box) {
  505. var proofAsJson =
  506. '{"zkProof":{"q":"Cw==","hash":"Ag==","values":["Ag==","Aw=="]}}';
  507. var deserializedProof =
  508. box.proofs.utils.deserializeProof(proofAsJson);
  509. var serializedProof = deserializedProof.stringify();
  510. expect(serializedProof).toEqual(proofAsJson);
  511. });
  512. });
  513. });
  514. describe('HashBuilder ...', function() {
  515. describe('When created with ...', function() {
  516. it('valid inputs', function() {
  517. cryptolib(
  518. 'proofs', 'commons.mathematical', 'primitives.messagedigest',
  519. function(box) {
  520. var hashbuilder = new box.proofs.HashBuilder();
  521. expect(hashbuilder).toBeDefined();
  522. });
  523. });
  524. });
  525. describe('When buildHashForProof is called ...', function() {
  526. describe('passing a one character String ...', function() {
  527. it('then expected an exception', function() {
  528. cryptolib(
  529. 'proofs', 'commons.mathematical', 'primitives.messagedigest',
  530. 'commons.utils', function(box) {
  531. var hashbuilder = new box.proofs.HashBuilder();
  532. var oneCharacterString = 'a';
  533. var inputObjectArray = [];
  534. inputObjectArray.push(oneCharacterString);
  535. expect(function() {
  536. hashbuilder.buildHashForProof(inputObjectArray);
  537. }).toThrow();
  538. });
  539. });
  540. });
  541. describe('get hash twice from same objects ...', function() {
  542. it('then same result both times', function() {
  543. cryptolib(
  544. 'proofs', 'commons.mathematical', 'primitives.messagedigest',
  545. 'commons.utils', function(box) {
  546. var converters = new box.commons.utils.Converters();
  547. var hashbuilder = new box.proofs.HashBuilder();
  548. // create group and elements
  549. var q = new box.forge.jsbn.BigInteger('11');
  550. var generator = new box.forge.jsbn.BigInteger('2');
  551. var p = new box.forge.jsbn.BigInteger('23');
  552. var group =
  553. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  554. var ZpGroupElement_p23_v2 =
  555. new box.commons.mathematical.ZpGroupElement(
  556. new box.forge.jsbn.BigInteger('2'), group.getP(),
  557. group.getQ());
  558. var ZpGroupElement_p23_v3 =
  559. new box.commons.mathematical.ZpGroupElement(
  560. new box.forge.jsbn.BigInteger('3'), group.getP(),
  561. group.getQ());
  562. var ZpGroupElement_p23_v4 =
  563. new box.commons.mathematical.ZpGroupElement(
  564. new box.forge.jsbn.BigInteger('4'), group.getP(),
  565. group.getQ());
  566. // Create objects that will be hashed.
  567. var _simpleList1 = [];
  568. _simpleList1.push(ZpGroupElement_p23_v2);
  569. _simpleList1.push(ZpGroupElement_p23_v3);
  570. var _simpleList2 = [];
  571. _simpleList2.push(ZpGroupElement_p23_v4);
  572. var _short_string1 = 'I am a short String';
  573. var _simpleList3 = [];
  574. _simpleList3.push(ZpGroupElement_p23_v2);
  575. _simpleList3.push(ZpGroupElement_p23_v3);
  576. var _simpleList4 = [];
  577. _simpleList4.push(ZpGroupElement_p23_v4);
  578. var _short_string2 = 'I am a short String';
  579. var bytesB64_1 = hashbuilder.buildHashForProof(
  580. _simpleList1, _simpleList2, _short_string1);
  581. var bytes1 = converters.base64Decode(bytesB64_1);
  582. var result1 = new box.forge.jsbn.BigInteger(
  583. converters.bytesFromString(bytes1));
  584. var bytesB64_2 = hashbuilder.buildHashForProof(
  585. _simpleList3, _simpleList4, _short_string2);
  586. var bytes2 = converters.base64Decode(bytesB64_2);
  587. var result2 = new box.forge.jsbn.BigInteger(
  588. converters.bytesFromString(bytes2));
  589. expect(result1.equals(result2)).toBeTruthy();
  590. });
  591. });
  592. });
  593. describe(
  594. 'passing lists containing Zp subgroup elements and strings ...',
  595. function() {
  596. it('then expected output', function() {
  597. cryptolib(
  598. 'proofs', 'commons.mathematical', 'primitives.messagedigest',
  599. 'commons.utils', function(box) {
  600. var converters = new box.commons.utils.Converters();
  601. var hashbuilder = new box.proofs.HashBuilder();
  602. // create group and elements
  603. var q = new box.forge.jsbn.BigInteger('11');
  604. var generator = new box.forge.jsbn.BigInteger('2');
  605. var p = new box.forge.jsbn.BigInteger('23');
  606. var group = new box.commons.mathematical.ZpSubgroup(
  607. generator, p, q);
  608. var ZpGroupElement_p23_v2 =
  609. new box.commons.mathematical.ZpGroupElement(
  610. new box.forge.jsbn.BigInteger('2'), group.getP(),
  611. group.getQ());
  612. var ZpGroupElement_p23_v3 =
  613. new box.commons.mathematical.ZpGroupElement(
  614. new box.forge.jsbn.BigInteger('3'), group.getP(),
  615. group.getQ());
  616. var ZpGroupElement_p23_v4 =
  617. new box.commons.mathematical.ZpGroupElement(
  618. new box.forge.jsbn.BigInteger('4'), group.getP(),
  619. group.getQ());
  620. // Create objects that will be hashed.
  621. var _simpleList1 = [];
  622. _simpleList1.push(ZpGroupElement_p23_v2);
  623. _simpleList1.push(ZpGroupElement_p23_v3);
  624. var _simpleList2 = [];
  625. _simpleList2.push(ZpGroupElement_p23_v4);
  626. var _short_string = 'I am a short String';
  627. var bytesB64_1 = hashbuilder.buildHashForProof(
  628. _simpleList1, _simpleList2, _short_string);
  629. var bytes1 = converters.base64Decode(bytesB64_1);
  630. var result = new box.forge.jsbn.BigInteger(
  631. converters.bytesFromString(bytes1));
  632. var javaResult = new box.forge.jsbn.BigInteger(
  633. '-52725384769643723964651772032205801420047176905115581490667997821371105732649');
  634. expect(javaResult.equals(result)).toBeTruthy();
  635. });
  636. });
  637. });
  638. describe('passing an Exponent ...', function() {
  639. it('then expected output', function() {
  640. cryptolib(
  641. 'proofs', 'commons.mathematical', 'primitives.messagedigest',
  642. function() {});
  643. });
  644. });
  645. });
  646. });
  647. describe('Maurer proof ...', function() {
  648. it('calculatePhi must be defined', function() {
  649. cryptolib('proofs', 'commons.mathematical', function(box) {
  650. // create group
  651. var q = new box.forge.jsbn.BigInteger('11');
  652. var generator = new box.forge.jsbn.BigInteger('2');
  653. var p = new box.forge.jsbn.BigInteger('23');
  654. var zpSubgroup =
  655. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  656. // create additional needed parameters
  657. var numInputs = new box.forge.jsbn.BigInteger('1');
  658. var numOutputs = new box.forge.jsbn.BigInteger('1');
  659. var baseElements = [zpSubgroup.getGenerator()];
  660. var computationsRules = [[[1, 1]]];
  661. // create PhiFunction
  662. var createdPhiFunction = new box.proofs.PhiFunction(
  663. zpSubgroup, numInputs, numOutputs, baseElements, computationsRules);
  664. expect(createdPhiFunction).toBeDefined();
  665. // create array of exponents
  666. var value1 = new box.forge.jsbn.BigInteger('2');
  667. var exponent1 =
  668. new box.commons.mathematical.Exponent(zpSubgroup.getQ(), value1);
  669. var exponentArray = [exponent1];
  670. var data = 'testdata';
  671. var prover = new box.proofs.Prover(zpSubgroup, createdPhiFunction);
  672. var exponentiatedElement =
  673. zpSubgroup.getGenerator().exponentiate(exponent1);
  674. var precomputationValues = prover.preCompute(1);
  675. var proofResult = prover.prove(
  676. [exponentiatedElement], exponentArray, data, precomputationValues);
  677. expect(proofResult).toBeTruthy();
  678. });
  679. });
  680. });
  681. describe('Schnorr proof ...', function() {
  682. it('should be able to be generate and verify a Schnorr proof', function() {
  683. cryptolib('proofs', 'commons', 'primitives.messagedigest', function(box) {
  684. // create group
  685. var q = new box.forge.jsbn.BigInteger('11');
  686. var generator = new box.forge.jsbn.BigInteger('2');
  687. var p = new box.forge.jsbn.BigInteger('23');
  688. var group = new box.commons.mathematical.ZpSubgroup(generator, p, q);
  689. var exponent = new box.commons.mathematical.Exponent(
  690. q, new box.forge.jsbn.BigInteger('8'));
  691. var exponentiatedElement = group.getGenerator().exponentiate(exponent);
  692. var proofsFactory = new box.proofs.ProofsFactory();
  693. var schnorrProof =
  694. proofsFactory
  695. .createSchnorrProofGenerator('123456789', '99999999', group)
  696. .generate(exponentiatedElement, exponent);
  697. var proofTruth = proofsFactory
  698. .createSchnorrProofVerifier(
  699. exponentiatedElement, '123456789', '99999999',
  700. schnorrProof, group)
  701. .verify();
  702. expect(proofTruth).toBeTruthy();
  703. });
  704. });
  705. it('should throw an exception in Schnorr proof generator if voterID is null',
  706. function() {
  707. cryptolib(
  708. 'proofs', 'commons', 'primitives.messagedigest', function(box) {
  709. // create group
  710. var q = new box.forge.jsbn.BigInteger('11');
  711. var generator = new box.forge.jsbn.BigInteger('2');
  712. var p = new box.forge.jsbn.BigInteger('23');
  713. var group =
  714. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  715. var exponent = new box.commons.mathematical.Exponent(
  716. q, new box.forge.jsbn.BigInteger('8'));
  717. var exponentiatedElement =
  718. group.getGenerator().exponentiate(exponent);
  719. var voterIdGenerator;
  720. var electionEventIdGenerator = '99999999';
  721. var proofsFactory = new box.proofs.ProofsFactory();
  722. expect(function() {
  723. createSchnorrProof(
  724. proofsFactory, group, exponentiatedElement, exponent,
  725. voterIdGenerator, electionEventIdGenerator);
  726. }).toThrow();
  727. });
  728. });
  729. it('should throw an exception in schnorr proof generator if voterID is empty',
  730. function() {
  731. cryptolib(
  732. 'proofs', 'commons', 'primitives.messagedigest', function(box) {
  733. // create group
  734. var q = new box.forge.jsbn.BigInteger('11');
  735. var generator = new box.forge.jsbn.BigInteger('2');
  736. var p = new box.forge.jsbn.BigInteger('23');
  737. var group =
  738. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  739. var exponent = new box.commons.mathematical.Exponent(
  740. q, new box.forge.jsbn.BigInteger('8'));
  741. var exponentiatedElement =
  742. group.getGenerator().exponentiate(exponent);
  743. var voterIdGenerator = '';
  744. var electionEventIdGenerator = '99999999';
  745. var proofsFactory = new box.proofs.ProofsFactory();
  746. expect(function() {
  747. createSchnorrProof(
  748. proofsFactory, group, exponentiatedElement, exponent,
  749. voterIdGenerator, electionEventIdGenerator);
  750. }).toThrow();
  751. });
  752. });
  753. it('should throw an exception in Schnorr proof generator if electionEventId is null',
  754. function() {
  755. cryptolib(
  756. 'proofs', 'commons', 'primitives.messagedigest', function(box) {
  757. // create group
  758. var q = new box.forge.jsbn.BigInteger('11');
  759. var generator = new box.forge.jsbn.BigInteger('2');
  760. var p = new box.forge.jsbn.BigInteger('23');
  761. var group =
  762. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  763. var exponent = new box.commons.mathematical.Exponent(
  764. q, new box.forge.jsbn.BigInteger('8'));
  765. var exponentiatedElement =
  766. group.getGenerator().exponentiate(exponent);
  767. var voterIdGenerator = '123456789';
  768. var electionEventIdGenerator;
  769. var proofsFactory = new box.proofs.ProofsFactory();
  770. expect(function() {
  771. createSchnorrProof(
  772. proofsFactory, group, exponentiatedElement, exponent,
  773. voterIdGenerator, electionEventIdGenerator);
  774. }).toThrow();
  775. });
  776. });
  777. it('should throw an exception in schnorr proof generator if electionEventId is empty',
  778. function() {
  779. cryptolib(
  780. 'proofs', 'commons', 'primitives.messagedigest', function(box) {
  781. // create group
  782. var q = new box.forge.jsbn.BigInteger('11');
  783. var generator = new box.forge.jsbn.BigInteger('2');
  784. var p = new box.forge.jsbn.BigInteger('23');
  785. var group =
  786. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  787. var exponent = new box.commons.mathematical.Exponent(
  788. q, new box.forge.jsbn.BigInteger('8'));
  789. var exponentiatedElement =
  790. group.getGenerator().exponentiate(exponent);
  791. var voterIdGenerator = '123456789';
  792. var electionEventIdGenerator = '';
  793. var proofsFactory = new box.proofs.ProofsFactory();
  794. expect(function() {
  795. createSchnorrProof(
  796. proofsFactory, group, exponentiatedElement, exponent,
  797. voterIdGenerator, electionEventIdGenerator);
  798. }).toThrow();
  799. });
  800. });
  801. it('should throw an exception in Schnorr proof verifier if voterID is null',
  802. function() {
  803. cryptolib(
  804. 'proofs', 'commons', 'primitives.messagedigest', function(box) {
  805. // create group
  806. var q = new box.forge.jsbn.BigInteger('11');
  807. var generator = new box.forge.jsbn.BigInteger('2');
  808. var p = new box.forge.jsbn.BigInteger('23');
  809. var group =
  810. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  811. var exponent = new box.commons.mathematical.Exponent(
  812. q, new box.forge.jsbn.BigInteger('8'));
  813. var exponentiatedElement =
  814. group.getGenerator().exponentiate(exponent);
  815. var voterIdGenerator = '123456789';
  816. var electionEventIdGenerator = '99999999';
  817. var voterIdVerifier;
  818. var electionEventIdVerifier = '99999999';
  819. var proofsFactory = new box.proofs.ProofsFactory();
  820. var schnorrProof =
  821. proofsFactory
  822. .createSchnorrProofGenerator(
  823. voterIdGenerator, electionEventIdGenerator, group)
  824. .generate(exponentiatedElement, exponent);
  825. expect(function() {
  826. proofsFactory
  827. .createSchnorrProofVerifier(
  828. exponentiatedElement, voterIdVerifier,
  829. electionEventIdVerifier, schnorrProof, group)
  830. .verify();
  831. }).toThrow();
  832. });
  833. });
  834. it('should throw an exception in Schnorr proof verifier if voterID is empty',
  835. function() {
  836. cryptolib(
  837. 'proofs', 'commons', 'primitives.messagedigest', function(box) {
  838. // create group
  839. var q = new box.forge.jsbn.BigInteger('11');
  840. var generator = new box.forge.jsbn.BigInteger('2');
  841. var p = new box.forge.jsbn.BigInteger('23');
  842. var group =
  843. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  844. var exponent = new box.commons.mathematical.Exponent(
  845. q, new box.forge.jsbn.BigInteger('8'));
  846. var exponentiatedElement =
  847. group.getGenerator().exponentiate(exponent);
  848. var voterIdGenerator = '123456789';
  849. var electionEventIdGenerator = '99999999';
  850. var voterIdVerifier = '';
  851. var electionEventIdVerifier = '99999999';
  852. var proofsFactory = new box.proofs.ProofsFactory();
  853. var schnorrProof =
  854. proofsFactory
  855. .createSchnorrProofGenerator(
  856. voterIdGenerator, electionEventIdGenerator, group)
  857. .generate(exponentiatedElement, exponent);
  858. expect(function() {
  859. proofsFactory
  860. .createSchnorrProofVerifier(
  861. exponentiatedElement, voterIdVerifier,
  862. electionEventIdVerifier, schnorrProof, group)
  863. .verify();
  864. }).toThrow();
  865. });
  866. });
  867. it('should throw an exception in Schnorr proof verifier if electionEventId is null',
  868. function() {
  869. cryptolib(
  870. 'proofs', 'commons', 'primitives.messagedigest', function(box) {
  871. // create group
  872. var q = new box.forge.jsbn.BigInteger('11');
  873. var generator = new box.forge.jsbn.BigInteger('2');
  874. var p = new box.forge.jsbn.BigInteger('23');
  875. var group =
  876. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  877. var exponent = new box.commons.mathematical.Exponent(
  878. q, new box.forge.jsbn.BigInteger('8'));
  879. var exponentiatedElement =
  880. group.getGenerator().exponentiate(exponent);
  881. var voterIdGenerator = '123456789';
  882. var electionEventIdGenerator = '99999999';
  883. var voterIdVerifier = '123456789';
  884. var electionEventIdVerifier;
  885. var proofsFactory = new box.proofs.ProofsFactory();
  886. var schnorrProof =
  887. proofsFactory
  888. .createSchnorrProofGenerator(
  889. voterIdGenerator, electionEventIdGenerator, group)
  890. .generate(exponentiatedElement, exponent);
  891. expect(function() {
  892. proofsFactory
  893. .createSchnorrProofVerifier(
  894. exponentiatedElement, voterIdVerifier,
  895. electionEventIdVerifier, schnorrProof, group)
  896. .verify();
  897. }).toThrow();
  898. });
  899. });
  900. it('should throw an exception in Schnorr proof verifier if electionEventId is empty',
  901. function() {
  902. cryptolib(
  903. 'proofs', 'commons', 'primitives.messagedigest', function(box) {
  904. // create group
  905. var q = new box.forge.jsbn.BigInteger('11');
  906. var generator = new box.forge.jsbn.BigInteger('2');
  907. var p = new box.forge.jsbn.BigInteger('23');
  908. var group =
  909. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  910. var exponent = new box.commons.mathematical.Exponent(
  911. q, new box.forge.jsbn.BigInteger('8'));
  912. var exponentiatedElement =
  913. group.getGenerator().exponentiate(exponent);
  914. var voterIdGenerator = '123456789';
  915. var electionEventIdGenerator = '99999999';
  916. var voterIdVerifier = '123456789';
  917. var electionEventIdVerifier = '';
  918. var proofsFactory = new box.proofs.ProofsFactory();
  919. var schnorrProof =
  920. proofsFactory
  921. .createSchnorrProofGenerator(
  922. voterIdGenerator, electionEventIdGenerator, group)
  923. .generate(exponentiatedElement, exponent);
  924. expect(function() {
  925. proofsFactory
  926. .createSchnorrProofVerifier(
  927. exponentiatedElement, voterIdVerifier,
  928. electionEventIdVerifier, schnorrProof, group)
  929. .verify();
  930. }).toThrow();
  931. });
  932. });
  933. describe('plaintext proof', function() {
  934. it('must fail if plaintext and the public key have a different number of arguments',
  935. function() {
  936. cryptolib('*', function(box) {
  937. var elGamalCipherFactory =
  938. new box.homomorphic.cipher.factory.ElGamalCipherFactory();
  939. var keyFactory = new box.homomorphic.keypair.factory.KeyFactory();
  940. var p = new box.forge.jsbn.BigInteger(
  941. '16495768820687182934009927347170440458984298969141051719651115246768228783533993705048522613555003722650163857541125634359204551517133551948222225229585505473854815904204008387572449239572558997742610313328207134588877674958965112510857989409372589489554367815761232020129433262382857561981217475899722020926354221968452402385754809505679432634661210095701454463437258972380072638986797327376359801509825066468445666991457815992978097875265987862649581950696045865510189368336440306143914792406918514769060068547350680676698071521344166677089027364466838527577675388855690260527795812471209293838170074431021658585769');
  942. var q = new box.forge.jsbn.BigInteger(
  943. '96020784172851661018443454307652214686331639842501459760074411037927395553939');
  944. var generator = new box.forge.jsbn.BigInteger(
  945. '15130260694873909459986577784204643986832006548758872568127622990613286376947389953860539257554587993925082794507831758880350292337036239661614300430033670574377146869864951070388980213523097422627922584301566881102629991359716406049006152033115595975218156523898904324261190660093347613804122603060933508651584944555007181780395855126908781477484110624848678463182770318846991956864510428932259181036820923323061517278008748271670358287455340697499004281391354210072366308177032171585041936224750556137265259765883084633653387023221827373593952176436229429968550363326770646572341416840037353790767464212018954641189');
  946. var plaintextValues =
  947. '13001958155330604183283440349326249836910073645746606152086317081542370925085323829577576343220220970830242334422088928335776849693699317596697308070491655988846936544490170854760622263229593132359288598939754030438763377074672036413960553517345827407874240954931856460473275871494952759213809834622191632962928152282758068810785997095964792534886071457598558200037994837193502017861972726981702601645743757363301481452380559572428601079773415878406878889283104921714313573036550932216793644548235628221219857860491590917419188368179146906649074848254341715041556794836535625862564803932425535053708105664756328176096,13231973095563029671832096284884900786942379344314765999736867442501058904676516417925101650742389109597711424534876455182391728375468332757263296899395351639193353410112338073499025899736418030529915324365551044922832874533047175715575665120707774002324384088317703371342441254493502521221058572909498259802514124188958407279318160157200557318801491755304390175475424809222021529440615330205038504228764024791004717836281566455683657139407029417762297815388016430137008344688954594677000059036704922079131336478242363366789868164308084428340745898909076987038168924165852440487400698910077949024596427386762477753127,14962213442437221630882804374502807481065630653554446432724314309377638233420429625929060616149342043360565572219423869257414407292350550499520720685455656488232644853879526758539416856088474668103469469539535531657659096824387210346084505215030036305938457977750754924998601715702575511194945674310421459334341817535064482476480331751171219419177177690275196093512303662432640330876384877571377755052302164787402266089676321850441055346812973279399849013829646051767527082830058880156180804337513629780923908712327566442241827419862207132399832645578550470975229103528239481602587884774035574980636031799832387127824';
  948. plaintextValues = plaintextValues.split(',');
  949. var publickey =
  950. '2310052637281374826163980155072703478550584783282353794746356503851736624950501110627753937319986533000522391643440057809085228551963268616650076198499939080674555452672248616965754928293235729345758867884335314010776031118255924016690941973710021793693357184670835265674549998079458749041612609723185323556418090275187898731901111571900886838242789201627029788753318822261542649942667275986354422715719555421167864656006766965757148569592487004683308842191788591435697129834559463075935646313837018575516319421503514590374988521988732632646137082482614398122769481744648900076625388202771688539721397175709860657215,10992040365912330212603556159890860989323436780424734140469776597559966083776288793792880271187249577475862540041272265165115141691436552098724587148672885376255101720449710739879257167893375710652999579682329540866493597320896396042034863583144965792072687133983833723367505914551649613644957007736294052065759536432864892568886279889430332858208212502323472429844596803009794879201124168960173108172884523529447057933188670605500761004550561618533233760048349811136008540506656716161194168608193768775892759430117403245548580741988574926317015003026743034307396424854331578214723160905311258827711514036727758661443,2966454546948674594238484453818820495774263494738027649885606698735626946977004754473084281799793802225340057166391055854897568455044693120227990997044954520274944912364134982880734979624278026290300479874061515681112538596438177835791333982353892136013223338695390663410151102503147241467125785231395240349671275198727614663753021483991961941587552003516822754894985284436598456509038426029451562110689110854200741545361500678654576568967692431517292344770939502725349301506972598902856573261152531261931708724855752770349361196880952259389543735606329934472812841863925491905824596299316439858950888740168931063033,16685125032225465591271616129024153088399594055921872535905780296582091131024932497146054111843749963435248897831820496896855297302629640382562118371399923587479991981933850508478973979849045483506501618718824692791050899387832529345913319557326115349466503641556936384534756871248087479236266210661587928787040614097509670326965654744522366069845631192304570814294808544467147342154545110452347943098767091892623807533767816665018437155453169438229244628337513271777636288916880160385420829994354060040651547330045332962210808881666427991175014815542151556166234899209736414331653334634166108740897978989610512241';
  951. publickey = publickey.split(',');
  952. var group =
  953. new box.commons.mathematical.ZpSubgroup(generator, p, q);
  954. // create public key
  955. var value, groupElement;
  956. var elements = [];
  957. for (var i = 0; i < publickey.length; i++) {
  958. value = new box.forge.jsbn.BigInteger('' + publickey[i]);
  959. groupElement = new box.commons.mathematical.ZpGroupElement(
  960. value, group.getP(), group.getQ());
  961. elements.push(groupElement);
  962. }
  963. var elGamalPublicKey = keyFactory.createPublicKey(elements, group);
  964. // plaintext
  965. var plaintext = [];
  966. for (i = 0; i < plaintextValues.length; i++) {
  967. value = new box.forge.jsbn.BigInteger(plaintextValues[i]);
  968. groupElement = new box.commons.mathematical.ZpGroupElement(
  969. value, group.getP(), group.getQ());
  970. plaintext.push(groupElement);
  971. }
  972. // get encrypters
  973. var encrypter =
  974. elGamalCipherFactory.createEncrypter(elGamalPublicKey);
  975. var ciphertext = encrypter.encryptGroupElements(plaintext);
  976. expect(function() {
  977. box.proofs.service.createPlaintextProof(
  978. elGamalPublicKey, ciphertext, plaintext, ciphertext.getR(),
  979. group);
  980. }).toThrow();
  981. });
  982. });
  983. });
  984. });
  985. describe('ProofPreComputedValues ... ', function() {
  986. it('should be serialized correctly to JSON format', function() {
  987. cryptolib('*', function(box) {
  988. var p = new box.forge.jsbn.BigInteger('23');
  989. var q = new box.forge.jsbn.BigInteger('11');
  990. var exponent0 = new box.commons.mathematical.Exponent(
  991. q, new box.forge.jsbn.BigInteger('2'));
  992. var exponent1 = new box.commons.mathematical.Exponent(
  993. q, new box.forge.jsbn.BigInteger('8'));
  994. var exponent2 = new box.commons.mathematical.Exponent(
  995. q, new box.forge.jsbn.BigInteger('9'));
  996. var exponents = [exponent0, exponent1, exponent2];
  997. var zpGroupElement_p23_v2 = new box.commons.mathematical.ZpGroupElement(
  998. new box.forge.jsbn.BigInteger('2'), p, q);
  999. var zpGroupElement_p23_v3 = new box.commons.mathematical.ZpGroupElement(
  1000. new box.forge.jsbn.BigInteger('3'), p, q);
  1001. var zpGroupElement_p23_v4 = new box.commons.mathematical.ZpGroupElement(
  1002. new box.forge.jsbn.BigInteger('4'), p, q);
  1003. var phiOutputs = [
  1004. zpGroupElement_p23_v2, zpGroupElement_p23_v3, zpGroupElement_p23_v4
  1005. ];
  1006. var precomputationValues =
  1007. new box.proofs.ProofPreComputedValues(exponents, phiOutputs);
  1008. var jsonAsString = precomputationValues.stringify();
  1009. expect(jsonAsString).toContain('"preComputed":');
  1010. expect(jsonAsString).toContain('"p":"Fw=="');
  1011. expect(jsonAsString).toContain('"q":"Cw=="');
  1012. expect(jsonAsString).toContain('"exponents":["Ag==","CA==","CQ=="]');
  1013. expect(jsonAsString).toContain('"phiOutputs":["Ag==","Aw==","BA=="]');
  1014. expect(jsonAsString.length)
  1015. .toBe(
  1016. '{"preComputed":{"p":"Fw==","q":"Cw==","exponents":["Ag==","CA==","CQ=="],"phiOutputs":["Ag==","Aw==","BA=="]}}'
  1017. .length);
  1018. });
  1019. });
  1020. it('should be deserialized correctly from JSON format', function() {
  1021. cryptolib('*', function(box) {
  1022. var preComputedValues = box.proofs.utils.deserializeProofPreComputedValues(
  1023. '{"preComputed":{"p":"Fw==","q":"Cw==","exponents":["Ag==","CA==","CQ=="],"phiOutputs":["Ag==","Aw==","BA=="]}}');
  1024. expect(preComputedValues).toBeDefined();
  1025. expect(preComputedValues.getExponents().length).toEqual(3);
  1026. expect(preComputedValues.getExponents()[0].getQ().toString())
  1027. .toEqual('11');
  1028. expect(preComputedValues.getExponents()[0].getValue().toString())
  1029. .toEqual('2');
  1030. expect(preComputedValues.getExponents()[1].getValue().toString())
  1031. .toEqual('8');
  1032. expect(preComputedValues.getExponents()[2].getValue().toString())
  1033. .toEqual('9');
  1034. expect(preComputedValues.getPhiOutputs().length).toEqual(3);
  1035. expect(preComputedValues.getPhiOutputs()[0].getP().toString())
  1036. .toEqual('23');
  1037. expect(preComputedValues.getPhiOutputs()[0].getQ().toString())
  1038. .toEqual('11');
  1039. expect(
  1040. preComputedValues.getPhiOutputs()[0].getElementValue().toString())
  1041. .toEqual('2');
  1042. expect(
  1043. preComputedValues.getPhiOutputs()[1].getElementValue().toString())
  1044. .toEqual('3');
  1045. expect(
  1046. preComputedValues.getPhiOutputs()[2].getElementValue().toString())
  1047. .toEqual('4');
  1048. });
  1049. });
  1050. });
  1051. function createSchnorrProof(
  1052. proofsFactory, group, exponentiatedElement, exponent, voterIdGenerator,
  1053. electionEventIdGenerator) {
  1054. return proofsFactory.createSchnorrProofGenerator(group).generate(
  1055. exponentiatedElement, exponent, voterIdGenerator,
  1056. electionEventIdGenerator);
  1057. }
  1058. });