2
0

proofs.service.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. cryptolib.modules.proofs = cryptolib.modules.proofs || {};
  9. /**
  10. * Allows several cryptographic proofs to be generated and verified.
  11. * <P>
  12. */
  13. cryptolib.modules.proofs.service = function(box) {
  14. 'use strict';
  15. box.proofs = box.proofs || {};
  16. box.proofs.service = {};
  17. var proofsFactory;
  18. var f = function(box) {
  19. proofsFactory = new box.proofs.ProofsFactory();
  20. };
  21. f.policies = {
  22. proofs: {
  23. messagedigest: {
  24. algorithm: box.policies.proofs.messagedigest.algorithm,
  25. provider: box.policies.proofs.messagedigest.provider
  26. },
  27. secureRandom: {provider: box.policies.proofs.secureRandom.provider},
  28. charset: box.policies.proofs.charset
  29. },
  30. primitives: {
  31. secureRandom: {provider: box.policies.primitives.secureRandom.provider}
  32. }
  33. };
  34. cryptolib(
  35. 'proofs.implementation', 'primitives.securerandom', 'commons.exceptions',
  36. 'commons.mathematical', f);
  37. /**
  38. * Creates a Schnorr proof.
  39. *
  40. * @param voterID
  41. * The voter ID. This should be a string.
  42. * @param electionID
  43. * The election event ID. This should be a string.
  44. * @param exponentiatedElement
  45. * The exponentiated element. This should be an element of the
  46. * received group.
  47. * @param witness
  48. * The witness. This should be an Exponent of the received group.
  49. * @param group
  50. * The mathematical group. This should be a Zp subgroup.
  51. * @param progressCallbackOrPreComputedValues
  52. * Progress callback function or pre-computed values (optional).
  53. * @param progressPercentMinCheckInterval
  54. * Progress percentage minimum check interval, if the previous
  55. * parameter is a progressCallback function (optional).
  56. *
  57. * @returns The Schnorr proof.
  58. */
  59. box.proofs.service.createSchnorrProof = function(
  60. voterID, electionID, exponentiatedElement, witness, group,
  61. progressCallbackOrPreComputedValues, progressPercentMinCheckInterval) {
  62. var proofGenerator =
  63. proofsFactory.createSchnorrProofGenerator(voterID, electionID, group);
  64. if (!progressCallbackOrPreComputedValues) {
  65. return proofGenerator.generate(exponentiatedElement, witness);
  66. } else if (isPreComputedValues(progressCallbackOrPreComputedValues)) {
  67. var preComputedValues = progressCallbackOrPreComputedValues;
  68. return proofGenerator.generate(
  69. exponentiatedElement, witness, preComputedValues);
  70. } else {
  71. var progressCallback = progressCallbackOrPreComputedValues;
  72. return proofGenerator
  73. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  74. .generate(exponentiatedElement, witness);
  75. }
  76. };
  77. /**
  78. * Creates an exponentiation proof.
  79. *
  80. * @param exponentiatedElements
  81. * The exponentiated elements. These should consist of the
  82. * following base elements, exponentiated with the specified
  83. * exponent.
  84. * @param baseElements
  85. * The base elements used for the exponentiations. These should
  86. * correspond to elements of the specified group.
  87. * @param exponent
  88. * The exponent for which this proof is to be generated. It
  89. * should be an exponent of the specified group.
  90. * @param group
  91. * The mathematical group. This should be a Zp subgroup.
  92. * @param progressCallbackOrPreComputedValues
  93. * Progress callback function or pre-computed values (optional).
  94. * @param progressPercentMinCheckInterval
  95. * Progress percentage minimum check interval, if the previous
  96. * parameter is a progressCallback function (optional).
  97. *
  98. * @returns The exponentiation proof.
  99. */
  100. box.proofs.service.createExponentiationProof = function(
  101. exponentiatedElements, baseElements, exponent, group,
  102. progressCallbackOrPreComputedValues, progressPercentMinCheckInterval) {
  103. var proofGenerator =
  104. proofsFactory.createExponentiationProofGenerator(baseElements, group);
  105. if (!progressCallbackOrPreComputedValues) {
  106. return proofGenerator.generate(exponentiatedElements, exponent);
  107. } else if (isPreComputedValues(progressCallbackOrPreComputedValues)) {
  108. var preComputedValues = progressCallbackOrPreComputedValues;
  109. return proofGenerator.generate(
  110. exponentiatedElements, exponent, preComputedValues);
  111. } else {
  112. var progressCallback = progressCallbackOrPreComputedValues;
  113. return proofGenerator
  114. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  115. .generate(exponentiatedElements, exponent);
  116. }
  117. };
  118. /**
  119. * Creates a plaintext proof.
  120. *
  121. * @param publicKey
  122. * The primary public key. This should be an ElGamal public key.
  123. * @param ciphertext
  124. * The ciphertext. This should be the output from ElGamal
  125. * encryption.
  126. * @param plaintext
  127. * The plaintext. This should be an array of elements of the
  128. * received group.
  129. * @param witness
  130. * The witness. This should be an Exponent of the received group.
  131. * @param group
  132. * The mathematical group. This should be a Zp subgroup.
  133. * @param progressCallbackOrPreComputedValues
  134. * Progress callback function or pre-computed values (optional).
  135. * @param progressPercentMinCheckInterval
  136. * Progress percentage minimum check interval, if the previous
  137. * parameter is a progressCallback function (optional).
  138. *
  139. * @returns The plaintext proof.
  140. */
  141. box.proofs.service.createPlaintextProof = function(
  142. publicKey, ciphertext, plaintext, witness, group,
  143. progressCallbackOrPreComputedValues, progressPercentMinCheckInterval) {
  144. var proofGenerator =
  145. proofsFactory.createPlaintextProofGenerator(publicKey, group);
  146. if (!progressCallbackOrPreComputedValues) {
  147. return proofGenerator.generate(ciphertext, plaintext, witness);
  148. } else if (isPreComputedValues(progressCallbackOrPreComputedValues)) {
  149. var preComputedValues = progressCallbackOrPreComputedValues;
  150. return proofGenerator.generate(
  151. ciphertext, plaintext, witness, preComputedValues);
  152. } else {
  153. var progressCallback = progressCallbackOrPreComputedValues;
  154. return proofGenerator
  155. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  156. .generate(ciphertext, plaintext, witness);
  157. }
  158. };
  159. /**
  160. * Creates a simple plaintext equality Proof.
  161. *
  162. * @param primaryCiphertext
  163. * The primary ciphertext. This should be the output from ElGamal
  164. * encryption.
  165. * @param primaryPublicKey
  166. * The primary public key. This should be an ElGamal public key.
  167. * @param witness
  168. * The witness of the proof. This should be an Exponent of the
  169. * received group.
  170. * @param secondaryCiphertext
  171. * The secondary ciphertext. This should be the output from
  172. * ElGamal encryption.
  173. * @param secondaryPublicKey
  174. * The secondary public key. This should be an ElGamal public
  175. * key.
  176. * @param group
  177. * The mathematical group. This should be a Zp subgroup.
  178. * @param progressCallbackOrPreComputedValues
  179. * Progress callback function or pre-computed values (optional).
  180. * @param progressPercentMinCheckInterval
  181. * Progress percentage minimum check interval, if the previous
  182. * parameter is a progressCallback function (optional).
  183. *
  184. * @returns The simple plaintext equality proof.
  185. */
  186. box.proofs.service.createSimplePlaintextEqualityProof = function(
  187. primaryCiphertext, primaryPublicKey, witness, secondaryCiphertext,
  188. secondaryPublicKey, group, progressCallbackOrPreComputedValues,
  189. progressPercentMinCheckInterval) {
  190. var proofGenerator =
  191. proofsFactory.createSimplePlaintextEqualityProofGenerator(
  192. primaryPublicKey, secondaryPublicKey, group);
  193. if (!progressCallbackOrPreComputedValues) {
  194. return proofGenerator.generate(
  195. primaryCiphertext, secondaryCiphertext, witness);
  196. } else if (isPreComputedValues(progressCallbackOrPreComputedValues)) {
  197. var preComputedValues = progressCallbackOrPreComputedValues;
  198. return proofGenerator.generate(
  199. primaryCiphertext, secondaryCiphertext, witness, preComputedValues);
  200. } else {
  201. var progressCallback = progressCallbackOrPreComputedValues;
  202. return proofGenerator
  203. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  204. .generate(primaryCiphertext, secondaryCiphertext, witness);
  205. }
  206. };
  207. /**
  208. * Creates a plaintext equality proof.
  209. *
  210. * @param primaryCiphertext
  211. * The primary ciphertext. This should be the output from ElGamal
  212. * encryption.
  213. * @param primaryPublicKey
  214. * The primary public key. This should be an ElGamal public key.
  215. * @param primaryWitness
  216. * The primary witness of the proof. This should be an Exponent
  217. * of the received group.
  218. * @param secondaryCiphertext
  219. * The secondary ciphertext. This should be the output from
  220. * ElGamal encryption.
  221. * @param secondaryPublicKey
  222. * The secondary public key. This should be an ElGamal public
  223. * key.
  224. * @param secondaryWitness
  225. * The secondary witness of the proof. This should be an Exponent
  226. * of the received group.
  227. * @param group
  228. * The mathematical group. This should be a Zp subgroup.
  229. * @param progressCallbackOrPreComputedValues
  230. * Progress callback function or pre-computed values (optional).
  231. * @param progressPercentMinCheckInterval
  232. * Progress percentage minimum check interval, if the previous
  233. * parameter is a progressCallback function (optional).
  234. *
  235. * @returns The plaintext equality proof.
  236. */
  237. box.proofs.service.createPlaintextEqualityProof = function(
  238. primaryCiphertext, primaryPublicKey, primaryWitness, secondaryCiphertext,
  239. secondaryPublicKey, secondaryWitness, group,
  240. progressCallbackOrPreComputedValues, progressPercentMinCheckInterval) {
  241. var proofGenerator = proofsFactory.createPlaintextEqualityProofGenerator(
  242. primaryPublicKey, secondaryPublicKey, group);
  243. if (!progressCallbackOrPreComputedValues) {
  244. return proofGenerator.generate(
  245. primaryCiphertext, primaryWitness, secondaryCiphertext,
  246. secondaryWitness);
  247. } else if (isPreComputedValues(progressCallbackOrPreComputedValues)) {
  248. var preComputedValues = progressCallbackOrPreComputedValues;
  249. return proofGenerator.generate(
  250. primaryCiphertext, primaryWitness, secondaryCiphertext,
  251. secondaryWitness, preComputedValues);
  252. } else {
  253. var progressCallback = progressCallbackOrPreComputedValues;
  254. return proofGenerator
  255. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  256. .generate(
  257. primaryCiphertext, primaryWitness, secondaryCiphertext,
  258. secondaryWitness);
  259. }
  260. };
  261. /**
  262. * Creates a plaintext exponent equality proof.
  263. *
  264. * @param ciphertext
  265. * The ciphertext. This should be the output from ElGamal
  266. * encryption.
  267. * @param plaintext
  268. * The plaintext. It should be an array of members of the
  269. * received group.
  270. * @param primaryWitness
  271. * The primary witness. This should be an Exponent of the
  272. * received group.
  273. * @param secondaryWitness
  274. * The secondary witness. This should be an Exponent of the
  275. * received group.
  276. * @param group
  277. * The mathematical group. This should be a Zp subgroup.
  278. * @param progressCallbackOrPreComputedValues
  279. * Progress callback function or pre-computed values (optional).
  280. * @param progressPercentMinCheckInterval
  281. * Progress percentage minimum check interval, if the previous
  282. * parameter is a progressCallback function (optional).
  283. *
  284. * @returns The plaintext exponent equality proof.
  285. */
  286. box.proofs.service.createPlaintextExponentEqualityProof = function(
  287. ciphertext, plaintext, primaryWitness, secondaryWitness, group,
  288. progressCallbackOrPreComputedValues, progressPercentMinCheckInterval) {
  289. var proofGenerator =
  290. proofsFactory.createPlaintextExponentEqualityProofGenerator(
  291. plaintext, group);
  292. if (!progressCallbackOrPreComputedValues) {
  293. return proofGenerator.generate(
  294. ciphertext, primaryWitness, secondaryWitness);
  295. } else if (isPreComputedValues(progressCallbackOrPreComputedValues)) {
  296. var preComputedValues = progressCallbackOrPreComputedValues;
  297. return proofGenerator.generate(
  298. ciphertext, primaryWitness, secondaryWitness, preComputedValues);
  299. } else {
  300. var progressCallback = progressCallbackOrPreComputedValues;
  301. return proofGenerator
  302. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  303. .generate(ciphertext, primaryWitness, secondaryWitness);
  304. }
  305. };
  306. /**
  307. * Creates an OR-proof.
  308. *
  309. * @param publicKey
  310. * The public key. This should be an ElGamal public key.
  311. * @param ciphertext
  312. * The ciphertext of the mathematical group element that was
  313. * encrypted. This should be the output from ElGamal encryption.
  314. * @param witness
  315. * The witness used to generate the ciphertext. This should be an
  316. * Exponent of the received group.
  317. * @param elements
  318. * The mathematical group elements, one of which was encrypted.
  319. * This should be an array of elements of the received group.
  320. * @param index
  321. * The index of the mathematical group element that was
  322. * encrypted. This should be an integer.
  323. * @param data
  324. * Any extra data to use for the proof generation. This should be
  325. * a String. NOTE: If this parameter is null, then data will be set
  326. * to empty string.
  327. * @param group
  328. * The mathematical group. This should be a Zp subgroup.
  329. * @param progressCallbackOrPreComputedValues
  330. * Progress callback function or pre-computed values (optional).
  331. * @param progressPercentMinCheckInterval
  332. * Progress percentage minimum check interval, if the previous
  333. * parameter is a progressCallback function (optional).
  334. *
  335. * @returns The OR-proof.
  336. */
  337. box.proofs.service.createORProof = function(
  338. publicKey, ciphertext, witness, elements, index, data, group,
  339. progressCallbackOrPreComputedValues, progressPercentMinCheckInterval) {
  340. var proofGenerator =
  341. proofsFactory.createORProofGenerator(publicKey, elements.length, group);
  342. if (!progressCallbackOrPreComputedValues) {
  343. return proofGenerator.generate(
  344. ciphertext, witness, elements, index, data);
  345. } else if (isPreComputedValues(progressCallbackOrPreComputedValues)) {
  346. var preComputedValues = progressCallbackOrPreComputedValues;
  347. return proofGenerator.generate(
  348. ciphertext, witness, elements, index, data, preComputedValues);
  349. } else {
  350. var progressCallback = progressCallbackOrPreComputedValues;
  351. return proofGenerator
  352. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  353. .generate(ciphertext, witness, elements, index, data);
  354. }
  355. };
  356. /**
  357. * Verifies a Schnorr proof.
  358. *
  359. * @param exponentiatedElement
  360. * The exponentiated element. This should be an element of the
  361. * specified group.
  362. * @param voterID
  363. * The voter IS. This should be a string.
  364. * @param electionID
  365. * The election event ID. This should be a string.
  366. * @param proof
  367. * The proof that is to be verified.
  368. * @param group
  369. * The mathematical group. This should be a Zp subgroup.
  370. * @param progressCallback
  371. * Progress callback function (optional).
  372. * @param progressPercentMinCheckInterval
  373. * Progress percentage minimum check interval (optional).
  374. *
  375. * @returns true if the Schnorr Proof can be verified, false otherwise.
  376. */
  377. box.proofs.service.verifySchnorrProof = function(
  378. element, voterID, electionEventID, proof, group, progressCallback,
  379. progressPercentMinCheckInterval) {
  380. return proofsFactory
  381. .createSchnorrProofVerifier(
  382. element, voterID, electionEventID, proof, group)
  383. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  384. .verify();
  385. };
  386. /**
  387. * Verifies an exponentiation proof.
  388. *
  389. * @param exponentiatedElements
  390. * The exponentiated elements. These should consist of the
  391. * following base elements, exponentiated with the specified
  392. * exponent.
  393. * @param baseElements
  394. * The base elements used for the exponentiations. These should
  395. * correspond to elements of the specified group.
  396. * @param proof
  397. * The proof that is to be verified.
  398. * @param group
  399. * The mathematical group. This should be a Zp subgroup.
  400. * @param progressCallback
  401. * Progress callback function (optional).
  402. * @param progressPercentMinCheckInterval
  403. * Progress percentage minimum check interval (optional).
  404. *
  405. * @returns true if the exponentiation proof can be verified, false
  406. * otherwise.
  407. */
  408. box.proofs.service.verifyExponentiationProof = function(
  409. exponentiatedElements, baseElements, proof, group, progressCallback,
  410. progressPercentMinCheckInterval) {
  411. return proofsFactory
  412. .createExponentiationProofVerifier(
  413. exponentiatedElements, baseElements, proof, group)
  414. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  415. .verify();
  416. };
  417. /**
  418. * Verifies a plaintext proof.
  419. *
  420. * @param publicKey
  421. * The primary public key. This should be an ElGamal public key.
  422. * @param ciphertext
  423. * The ciphertext. This should be the output from ElGamal
  424. * encryption.
  425. * @param plaintext
  426. * The plaintext. This should be an array of elements of the
  427. * received group.
  428. * @param proof
  429. * The proof that is to be verified.
  430. * @param group
  431. * The mathematical group. This should be a Zp subgroup.
  432. * @param progressCallback
  433. * Progress callback function (optional).
  434. * @param progressPercentMinCheckInterval
  435. * Progress percentage minimum check interval (optional).
  436. * @returns true if the plaintext proof can be verified, false otherwise.
  437. */
  438. box.proofs.service.verifyPlaintextProof = function(
  439. publicKey, ciphertext, plaintext, proof, group, progressCallback,
  440. progressPercentMinCheckInterval) {
  441. return proofsFactory.createPlaintextProofVerifier(publicKey, group)
  442. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  443. .verify(ciphertext, plaintext, proof);
  444. };
  445. /**
  446. * Verifies a simple plaintext equality proof.
  447. *
  448. * @param primaryCiphertext
  449. * The primary ciphertext. This should be the output from ElGamal
  450. * encryption.
  451. * @param primaryPublicKey
  452. * The primary public key. This should be an ElGamal public key.
  453. * @param secondaryCiphertext
  454. * The secondary ciphertext. This should be the output from
  455. * ElGamal encryption.
  456. * @param secondaryPublicKey
  457. * The secondary public key. This should be an ElGamal public
  458. * key.
  459. * @param proof
  460. * The proof that is to be verified.
  461. * @param group
  462. * The mathematical group. This should be a Zp subgroup.
  463. * @param progressCallback
  464. * Progress callback function (optional).
  465. * @param progressPercentMinCheckInterval
  466. * Progress percentage minimum check interval (optional).
  467. *
  468. * @returns true if the simple plaintext equality proof can be verified,
  469. * false otherwise.
  470. */
  471. box.proofs.service.verifySimplePlaintextEqualityProof = function(
  472. primaryCiphertext, primaryPublicKey, secondaryCiphertext,
  473. secondaryPublicKey, proof, group, progressCallback,
  474. progressPercentMinCheckInterval) {
  475. return proofsFactory
  476. .createSimplePlaintextEqualityProofVerifier(
  477. primaryCiphertext, primaryPublicKey, secondaryCiphertext,
  478. secondaryPublicKey, proof, group)
  479. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  480. .verify();
  481. };
  482. /**
  483. * Verifies a plaintext equality proof.
  484. *
  485. * @param primaryCiphertext
  486. * The primary ciphertext. This should be the output from ElGamal
  487. * encryption.
  488. * @param primaryPublicKey
  489. * The primary public key. This should be an ElGamal public key.
  490. * @param secondaryCiphertext
  491. * The secondary ciphertext. This should be the output from
  492. * ElGamal encryption.
  493. * @param secondaryPublicKey
  494. * The secondary public key. This should be an ElGamal public
  495. * key.
  496. * @param proof
  497. * The proof that is to be verified.
  498. * @param group
  499. * The mathematical group. This should be a Zp subgroup.
  500. * @param progressCallback
  501. * Progress callback function (optional).
  502. * @param progressPercentMinCheckInterval
  503. * Progress percentage minimum check interval (optional).
  504. *
  505. * @returns true if the plaintext equality proof can be verified, false
  506. * otherwise.
  507. */
  508. box.proofs.service.verifyPlaintextEqualityProof = function(
  509. primaryCiphertext, primaryPublicKey, secondaryCiphertext,
  510. secondaryPublicKey, proof, group, progressCallback,
  511. progressPercentMinCheckInterval) {
  512. return proofsFactory
  513. .createPlaintextEqualityProofVerifier(
  514. primaryCiphertext, primaryPublicKey, secondaryCiphertext,
  515. secondaryPublicKey, proof, group)
  516. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  517. .verify();
  518. };
  519. /**
  520. * Verifies a plaintext exponent equality proof.
  521. *
  522. * @param ciphertext
  523. * The ciphertext. This should be the output from ElGamal
  524. * encryption.
  525. * @param baseElements
  526. * The array of base elements. All of these elements should be
  527. * members of the received group.
  528. * @param witness1
  529. * The primary witness. This should be an Exponent.
  530. * @param witness2
  531. * The secondary witness. This should be an Exponent.
  532. * @param proof
  533. * The proof that is to be verified.
  534. * @param group
  535. * The mathematical group. This should be a Zp subgroup.
  536. * @param progressCallback
  537. * Progress callback function (optional).
  538. * @param progressPercentMinCheckInterval
  539. * Progress percentage minimum check interval (optional).
  540. *
  541. * @returns true if the plaintext exponent equality proof can be verified,
  542. * false otherwise.
  543. */
  544. box.proofs.service.verifyPlaintextExponentEqualityProof = function(
  545. ciphertext, baseElements, proof, group, progressCallback,
  546. progressPercentMinCheckInterval) {
  547. return proofsFactory
  548. .createPlaintextExponentEqualityProofVerifier(
  549. ciphertext, baseElements, proof, group)
  550. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  551. .verify();
  552. };
  553. /**
  554. * Verifies an OR-proof.
  555. *
  556. * @param publicKey
  557. * The public key. This should be an ElGamal public key.
  558. * @param ciphertext
  559. * The ciphertext of the mathematical group element that was
  560. * encrypted. This should be the output from ElGamal encryption.
  561. * @param elements
  562. * The mathematical group elements, one of which was encrypted.
  563. * This should be an array of elements of the received group.
  564. * @param data
  565. * Any extra data used for the proof generation. This should be a
  566. * String. NOTE: If this parameter is null, then data will be set
  567. * to empty string.
  568. * @param proof
  569. * The proof that is to be verified.
  570. * @param group
  571. * The mathematical group. This should be a Zp subgroup.
  572. * @param progressCallback
  573. * Progress callback function (optional).
  574. * @param progressPercentMinCheckInterval
  575. * Progress percentage minimum check interval (optional).
  576. * @returns true if the OR-proof can be verified, false otherwise.
  577. */
  578. box.proofs.service.verifyORProof = function(
  579. publicKey, ciphertext, elements, data, proof, group, progressCallback,
  580. progressPercentMinCheckInterval) {
  581. return proofsFactory
  582. .createORProofVerifier(publicKey, elements.length, group)
  583. .initProgressMeter(progressCallback, progressPercentMinCheckInterval)
  584. .verify(ciphertext, elements, data, proof);
  585. };
  586. /**
  587. * Pre-computes a Schnorr proof. IMPORTANT: The same pre-computed values
  588. * must not be used twice.
  589. *
  590. * @param voterID
  591. * The voter ID. This should be a string.
  592. * @param electionID
  593. * The election event ID. This should be a string.
  594. * @param group
  595. * The mathematical group. This should be a Zp subgroup.
  596. * @param progressCallback
  597. * Progress callback function or pre-computed values (optional).
  598. * @param progressPercentMinCheckInterval
  599. * Progress percentage minimum check interval, if the previous
  600. * parameter is a progressCallback function (optional).
  601. *
  602. * @returns The Schnorr proof pre-computed values.
  603. */
  604. box.proofs.service.preComputeSchnorrProof = function(
  605. voterID, electionID, group, progressCallbackOrPreComputedValues,
  606. progressPercentMinCheckInterval) {
  607. return proofsFactory.createSchnorrProofGenerator(voterID, electionID, group)
  608. .initProgressMeter(
  609. progressCallbackOrPreComputedValues,
  610. progressPercentMinCheckInterval)
  611. .preCompute();
  612. };
  613. /**
  614. * Pre-computes an exponentiation proof. IMPORTANT: The same pre-computed
  615. * values must not be used twice.
  616. *
  617. * @param baseElements
  618. * The base elements used for the exponentiations. These should
  619. * correspond to elements of the specified group.
  620. * @param group
  621. * The mathematical group. This should be a Zp subgroup.
  622. * @param progressCallback
  623. * Progress callback function or pre-computed values (optional).
  624. * @param progressPercentMinCheckInterval
  625. * Progress percentage minimum check interval, if the previous
  626. * parameter is a progressCallback function (optional).
  627. *
  628. * @returns The exponentiation proof pre-computed values.
  629. */
  630. box.proofs.service.preComputeExponentiationProof = function(
  631. baseElements, group, progressCallbackOrPreComputedValues,
  632. progressPercentMinCheckInterval) {
  633. return proofsFactory.createExponentiationProofGenerator(baseElements, group)
  634. .initProgressMeter(
  635. progressCallbackOrPreComputedValues,
  636. progressPercentMinCheckInterval)
  637. .preCompute();
  638. };
  639. /**
  640. * Pre-computes a plaintext proof. IMPORTANT: The same pre-computed values
  641. * must not be used twice.
  642. *
  643. * @param publicKey
  644. * The primary public key. This should be an ElGamal public key.
  645. * @param group
  646. * The mathematical group. This should be a Zp subgroup.
  647. * @param progressCallback
  648. * Progress callback function or pre-computed values (optional).
  649. * @param progressPercentMinCheckInterval
  650. * Progress percentage minimum check interval, if the previous
  651. * parameter is a progressCallback function (optional).
  652. *
  653. * @returns The plaintext proof pre-computed values.
  654. */
  655. box.proofs.service.preComputePlaintextProof = function(
  656. publicKey, group, progressCallbackOrPreComputedValues,
  657. progressPercentMinCheckInterval) {
  658. return proofsFactory.createPlaintextProofGenerator(publicKey, group)
  659. .initProgressMeter(
  660. progressCallbackOrPreComputedValues,
  661. progressPercentMinCheckInterval)
  662. .preCompute();
  663. };
  664. /**
  665. * Pre-computes values for a simple plaintext equality proof. IMPORTANT: The
  666. * same pre-computed values must not be used twice.
  667. *
  668. * @param primaryPublicKey
  669. * The primary public key. This should be an ElGamal public key.
  670. * @param secondaryPublicKey
  671. * The secondary public key. This should be an ElGamal public
  672. * key.
  673. * @param group
  674. * The mathematical group. This should be a Zp subgroup.
  675. * @param progressCallback
  676. * Progress callback function or pre-computed values (optional).
  677. * @param progressPercentMinCheckInterval
  678. * Progress percentage minimum check interval, if the previous
  679. * parameter is a progressCallback function (optional).
  680. *
  681. * @returns The simple plaintext equality proof pre-computed values.
  682. */
  683. box.proofs.service.preComputeSimplePlaintextEqualityProof = function(
  684. primaryPublicKey, secondaryPublicKey, group,
  685. progressCallbackOrPreComputedValues, progressPercentMinCheckInterval) {
  686. return proofsFactory
  687. .createSimplePlaintextEqualityProofGenerator(
  688. primaryPublicKey, secondaryPublicKey, group)
  689. .initProgressMeter(
  690. progressCallbackOrPreComputedValues,
  691. progressPercentMinCheckInterval)
  692. .preCompute();
  693. };
  694. /**
  695. * Pre-computes values for a plaintext equality proof. IMPORTANT: The same
  696. * pre-computed values must not be used twice.
  697. *
  698. * @param primaryPublicKey
  699. * The primary public key. This should be an ElGamal public key.
  700. * @param secondaryPublicKey
  701. * The secondary public key. This should be an ElGamal public
  702. * key.
  703. * @param group
  704. * The mathematical group. This should be a Zp subgroup.
  705. * @param progressCallback
  706. * Progress callback function or pre-computed values (optional).
  707. * @param progressPercentMinCheckInterval
  708. * Progress percentage minimum check interval, if the previous
  709. * parameter is a progressCallback function (optional).
  710. *
  711. * @returns The plaintext equality proof pre-computed values.
  712. */
  713. box.proofs.service.preComputePlaintextEqualityProof = function(
  714. primaryPublicKey, secondaryPublicKey, group,
  715. progressCallbackOrPreComputedValues, progressPercentMinCheckInterval) {
  716. return proofsFactory
  717. .createPlaintextEqualityProofGenerator(
  718. primaryPublicKey, secondaryPublicKey, group)
  719. .initProgressMeter(
  720. progressCallbackOrPreComputedValues,
  721. progressPercentMinCheckInterval)
  722. .preCompute();
  723. };
  724. /**
  725. * Returns pre-computed values for a plaintext exponent equality proof.
  726. * IMPORTANT: The same pre-computed values must not be used twice.
  727. *
  728. * @param plaintext
  729. * The plaintext. It should be an array of members of the
  730. * received group.
  731. * @param group
  732. * The mathematical group. This should be a Zp subgroup.
  733. * @param progressCallback
  734. * Progress callback function or pre-computed values (optional).
  735. * @param progressPercentMinCheckInterval
  736. * Progress percentage minimum check interval, if the previous
  737. * parameter is a progressCallback function (optional).
  738. *
  739. * @returns The plaintext exponent equality proof pre-computed values.
  740. */
  741. box.proofs.service.preComputePlaintextExponentEqualityProof = function(
  742. plaintext, group, progressCallbackOrPreComputedValues,
  743. progressPercentMinCheckInterval) {
  744. return proofsFactory
  745. .createPlaintextExponentEqualityProofGenerator(plaintext, group)
  746. .initProgressMeter(
  747. progressCallbackOrPreComputedValues,
  748. progressPercentMinCheckInterval)
  749. .preCompute();
  750. };
  751. /**
  752. * Pre-computes an OR-proof. IMPORTANT: The same pre-computed values must
  753. * not be used twice.
  754. *
  755. * @param publicKey
  756. * The public key. This should be an ElGamal public key.
  757. * @param numElements
  758. * The number of elements belonging to the received mathematical
  759. * group that will be used for the proof, one of which will be
  760. * encrypted.
  761. * @param group
  762. * The mathematical group. This should be a Zp subgroup.
  763. * @param progressCallback
  764. * Progress callback function or pre-computed values (optional).
  765. * @param progressPercentMinCheckInterval
  766. * Progress percentage minimum check interval, if the previous
  767. * parameter is a progressCallback function (optional).
  768. *
  769. * @returns The OR-proof pre-computed values.
  770. */
  771. box.proofs.service.preComputeORProof = function(
  772. publicKey, numElements, group, progressCallbackOrPreComputedValues,
  773. progressPercentMinCheckInterval) {
  774. return proofsFactory.createORProofGenerator(publicKey, numElements, group)
  775. .initProgressMeter(
  776. progressCallbackOrPreComputedValues,
  777. progressPercentMinCheckInterval)
  778. .preCompute();
  779. };
  780. /**
  781. * Utility method that checks if the argument is instance of type
  782. * ProofPreComputedValues.
  783. *
  784. * @param values
  785. * argument to check.
  786. * @returns {boolean} true if values is instance of ProofPreComputedValues.
  787. */
  788. function isPreComputedValues(values) {
  789. return !!(values) && values.className === 'ProofPreComputedValues';
  790. }
  791. };