or-proof-validation.spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. /* jshint node:true */
  9. 'use strict';
  10. var OrProofTestData = require('../data/or-proof-data');
  11. var ProgressMeterTestData = require('../data/progress-meter-data');
  12. var ValidationTestData = require('../data/validation-data');
  13. var zkProof = require('../../lib/index');
  14. var elGamal = require('scytl-elgamal');
  15. describe('The zero-knowledge proof module should be able to ...', function() {
  16. var proofService_;
  17. var group_;
  18. var anotherGroup_;
  19. var secret_;
  20. var ciphertext_;
  21. var secretFromAnotherGroup_;
  22. var ciphertextFromAnotherGroup_;
  23. var ciphertextWithTooManyElements_;
  24. var publicKey_;
  25. var publicKeyFromAnotherGroup_;
  26. var index_;
  27. var tooSmallIndex_;
  28. var tooLargeIndex_;
  29. var numElements_;
  30. var elements_;
  31. var tooFewElements_;
  32. var elementsFromAnotherGroup_;
  33. var callback_;
  34. var nonObject_;
  35. var emptyObject_;
  36. var nonNumber_;
  37. var nonPositiveNumber_;
  38. var nonString_;
  39. var nonArray_;
  40. var emptyArray_;
  41. var nonObjectArray_;
  42. var nonFunction_;
  43. var nonAuxiliaryData_;
  44. var nonProofObject_;
  45. var proofHandler_;
  46. var proof_;
  47. beforeAll(function() {
  48. proofService_ = zkProof.newService();
  49. var testData = new OrProofTestData();
  50. var elGamalService = elGamal.newService();
  51. group_ = testData.getGroup();
  52. anotherGroup_ = testData.getAnotherGroup();
  53. var encryptedElements = testData.getEncryptedElements();
  54. secret_ = encryptedElements.secret;
  55. ciphertext_ = elGamalService.newEncryptedElements(
  56. encryptedElements.gamma, encryptedElements.phis);
  57. var encryptedElementsFromAnotherGroup =
  58. testData.getEncryptedElementsFromAnotherGroup();
  59. secretFromAnotherGroup_ = encryptedElementsFromAnotherGroup.secret;
  60. ciphertextFromAnotherGroup_ = elGamalService.newEncryptedElements(
  61. encryptedElementsFromAnotherGroup.gamma,
  62. encryptedElementsFromAnotherGroup.phis);
  63. var encryptedElementsWithTooManyElements =
  64. testData.getEncryptedElementsWithTooManyElements();
  65. ciphertextWithTooManyElements_ = elGamalService.newEncryptedElements(
  66. encryptedElementsWithTooManyElements.gamma,
  67. encryptedElementsWithTooManyElements.phis);
  68. publicKey_ = testData.getPublicKey();
  69. publicKeyFromAnotherGroup_ = testData.getPublicKeyFromAnotherGroup();
  70. index_ = testData.getIndex();
  71. tooSmallIndex_ = testData.getTooSmallIndex();
  72. tooLargeIndex_ = testData.getTooLargeIndex();
  73. numElements_ = testData.getNumElements();
  74. elements_ = testData.getElements();
  75. tooFewElements_ = testData.getTooFewElements();
  76. elementsFromAnotherGroup_ = testData.getElementsFromAnotherGroup();
  77. var progressMeterTestData = new ProgressMeterTestData();
  78. callback_ = progressMeterTestData.progressCallback;
  79. var validationTestData = new ValidationTestData();
  80. nonObject_ = validationTestData.getNonObject();
  81. emptyObject_ = validationTestData.getEmptyObject();
  82. nonNumber_ = validationTestData.getNonNumber();
  83. nonPositiveNumber_ = validationTestData.getNonPositiveNumber();
  84. nonString_ = validationTestData.getNonString();
  85. nonArray_ = validationTestData.getNonArray();
  86. emptyArray_ = validationTestData.getEmptyArray();
  87. nonObjectArray_ = validationTestData.getNonObjectArray();
  88. nonFunction_ = validationTestData.getNonFunction();
  89. nonAuxiliaryData_ = validationTestData.getNonAuxiliaryData();
  90. nonProofObject_ = validationTestData.getNonProofObject();
  91. proofHandler_ = proofService_.newOrProofHandler(group_).init(publicKey_);
  92. proof_ = proofHandler_.generate(secret_, elements_, index_, ciphertext_);
  93. });
  94. beforeEach(function() {
  95. proofHandler_ = proofService_.newOrProofHandler(group_).init(publicKey_);
  96. });
  97. describe('create a zero-knowledge proof service that should be able to ..', function() {
  98. describe('create an OR proof handler that should be able to', function() {
  99. it('throw an error when being created, using invalid input data',
  100. function() {
  101. expect(function() {
  102. proofService_.newOrProofHandler();
  103. }).toThrow();
  104. expect(function() {
  105. proofService_.newOrProofHandler(undefined);
  106. }).toThrow();
  107. expect(function() {
  108. proofService_.newOrProofHandler(null);
  109. }).toThrow();
  110. expect(function() {
  111. proofService_.newOrProofHandler(nonObject_);
  112. }).toThrow();
  113. expect(function() {
  114. proofService_.newOrProofHandler(emptyObject_);
  115. }).toThrow();
  116. });
  117. it('throw an error when being initialized, using invalid input data',
  118. function() {
  119. expect(function() {
  120. proofHandler_.init();
  121. }).toThrow();
  122. expect(function() {
  123. proofHandler_.init(undefined);
  124. }).toThrow();
  125. expect(function() {
  126. proofHandler_.init(null);
  127. }).toThrow();
  128. expect(function() {
  129. proofHandler_.init(nonObject_);
  130. }).toThrow();
  131. expect(function() {
  132. proofHandler_.init(emptyObject_);
  133. }).toThrow();
  134. expect(function() {
  135. proofHandler_.init(publicKeyFromAnotherGroup_);
  136. }).toThrow();
  137. });
  138. it('throw an error when generating an OR proof, using an invalid secret',
  139. function() {
  140. expect(function() {
  141. proofHandler_.generate(undefined, elements_, index_, ciphertext_);
  142. }).toThrow();
  143. expect(function() {
  144. proofHandler_.generate(null, elements_, index_, ciphertext_);
  145. }).toThrow();
  146. expect(function() {
  147. proofHandler_.generate(nonObject_, elements_, index_, ciphertext_);
  148. }).toThrow();
  149. expect(function() {
  150. proofHandler_.generate(
  151. emptyObject_, elements_, index_, ciphertext_);
  152. }).toThrow();
  153. expect(function() {
  154. proofHandler_.generate(
  155. secretFromAnotherGroup_, elements_, index_, ciphertext_);
  156. }).toThrow();
  157. });
  158. it('throw an error when generating an OR proof, using invalid elements',
  159. function() {
  160. expect(function() {
  161. proofHandler_.generate(secret_, undefined, index_, ciphertext_);
  162. }).toThrow();
  163. expect(function() {
  164. proofHandler_.generate(secret_, null, index_, ciphertext_);
  165. }).toThrow();
  166. expect(function() {
  167. proofHandler_.generate(secret_, nonArray_, index_, ciphertext_);
  168. }).toThrow();
  169. expect(function() {
  170. proofHandler_.generate(secret_, emptyArray_, index_, ciphertext_);
  171. }).toThrow();
  172. expect(function() {
  173. proofHandler_.generate(
  174. secret_, nonObjectArray_, index_, ciphertext_);
  175. }).toThrow();
  176. expect(function() {
  177. proofHandler_.generate(
  178. secret_, tooFewElements_, index_, ciphertext_);
  179. }).toThrow();
  180. expect(function() {
  181. proofHandler_.generate(
  182. secret_, elementsFromAnotherGroup_, index_, ciphertext_);
  183. }).toThrow();
  184. });
  185. it('throw an error when generating an OR proof, using invalid index',
  186. function() {
  187. expect(function() {
  188. proofHandler_.generate(secret_, elements_, undefined, ciphertext_);
  189. }).toThrow();
  190. expect(function() {
  191. proofHandler_.generate(secret_, elements_, null, ciphertext_);
  192. }).toThrow();
  193. expect(function() {
  194. proofHandler_.generate(
  195. secret_, elements_, nonNumber_, ciphertext_);
  196. }).toThrow();
  197. expect(function() {
  198. proofHandler_.generate(
  199. secret_, elements_, tooSmallIndex_, ciphertext_);
  200. }).toThrow();
  201. expect(function() {
  202. proofHandler_.generate(
  203. secret_, elements_, tooLargeIndex_, ciphertext_);
  204. }).toThrow();
  205. });
  206. it('throw an error when generating an OR proof, using an invalid ciphertext',
  207. function() {
  208. expect(function() {
  209. proofHandler_.generate(secret_, elements_, index_, undefined);
  210. }).toThrow();
  211. expect(function() {
  212. proofHandler_.generate(secret_, elements_, index_, null);
  213. }).toThrow();
  214. expect(function() {
  215. proofHandler_.generate(secret_, elements_, index_, nonObject_);
  216. }).toThrow();
  217. expect(function() {
  218. proofHandler_.generate(secret_, elements_, index_, emptyObject_);
  219. }).toThrow();
  220. expect(function() {
  221. proofHandler_.generate(
  222. secret_, elements_, index_, ciphertextWithTooManyElements_);
  223. }).toThrow();
  224. expect(function() {
  225. proofHandler_.generate(
  226. secret_, elements_, index_, ciphertextFromAnotherGroup_);
  227. }).toThrow();
  228. });
  229. it('throw an error when generating an OR proof, using invalid optional data',
  230. function() {
  231. expect(function() {
  232. proofHandler_.generate(
  233. secret_, elements_, index_, ciphertext_,
  234. {data: nonAuxiliaryData_});
  235. }).toThrow();
  236. expect(function() {
  237. proofHandler_.generate(
  238. secret_, elements_, index_, ciphertext_,
  239. {preComputation: nonObject_});
  240. }).toThrow();
  241. expect(function() {
  242. proofHandler_.generate(
  243. secret_, elements_, index_, ciphertext_,
  244. {preComputation: nonProofObject_});
  245. }).toThrow();
  246. });
  247. it('throw an error when pre-computing an OR proof, using invalid input data',
  248. function() {
  249. expect(function() {
  250. proofHandler_.preCompute();
  251. }).toThrow();
  252. expect(function() {
  253. proofHandler_.preCompute(undefined);
  254. }).toThrow();
  255. expect(function() {
  256. proofHandler_.preCompute(null);
  257. }).toThrow();
  258. expect(function() {
  259. proofHandler_.preCompute(nonNumber_);
  260. }).toThrow();
  261. expect(function() {
  262. proofHandler_.preCompute(nonPositiveNumber_);
  263. }).toThrow();
  264. expect(function() {
  265. proofHandler_.preCompute(1);
  266. }).toThrow();
  267. });
  268. it('throw an error when verifying an OR proof, using an invalid proof',
  269. function() {
  270. expect(function() {
  271. var verified =
  272. proofHandler_.verify(undefined, elements_, ciphertext_);
  273. expect(verified).toBeTruthy();
  274. }).toThrow();
  275. expect(function() {
  276. var verified = proofHandler_.verify(null, elements_, ciphertext_);
  277. expect(verified).toBeTruthy();
  278. }).toThrow();
  279. expect(function() {
  280. var verified =
  281. proofHandler_.verify(nonProofObject_, elements_, ciphertext_);
  282. expect(verified).toBeTruthy();
  283. }).toThrow();
  284. });
  285. it('throw an error when verifying an OR proof, using invalid elements',
  286. function() {
  287. expect(function() {
  288. var verified =
  289. proofHandler_.verify(proof_, undefined, ciphertext_);
  290. expect(verified).toBeTruthy();
  291. }).toThrow();
  292. expect(function() {
  293. var verified = proofHandler_.verify(proof_, null, ciphertext_);
  294. expect(verified).toBeTruthy();
  295. }).toThrow();
  296. expect(function() {
  297. var verified =
  298. proofHandler_.verify(proof_, nonArray_, ciphertext_);
  299. expect(verified).toBeTruthy();
  300. }).toThrow();
  301. expect(function() {
  302. var verified =
  303. proofHandler_.verify(proof_, emptyArray_, ciphertext_);
  304. expect(verified).toBeTruthy();
  305. }).toThrow();
  306. expect(function() {
  307. var verified =
  308. proofHandler_.verify(proof_, nonObjectArray_, ciphertext_);
  309. expect(verified).toBeTruthy();
  310. }).toThrow();
  311. expect(function() {
  312. var verified =
  313. proofHandler_.verify(proof_, tooFewElements_, ciphertext_);
  314. expect(verified).toBeTruthy();
  315. }).toThrow();
  316. expect(function() {
  317. var verified = proofHandler_.verify(
  318. proof_, elementsFromAnotherGroup_, ciphertext_);
  319. expect(verified).toBeTruthy();
  320. }).toThrow();
  321. });
  322. it('throw an error when verifying an OR proof, using an invalid ciphertext',
  323. function() {
  324. expect(function() {
  325. var verified = proofHandler_.verify(proof_, elements_, undefined);
  326. expect(verified).toBeTruthy();
  327. }).toThrow();
  328. expect(function() {
  329. var verified = proofHandler_.verify(proof_, elements_, null);
  330. expect(verified).toBeTruthy();
  331. }).toThrow();
  332. expect(function() {
  333. var verified = proofHandler_.verify(proof_, elements_, nonObject_);
  334. expect(verified).toBeTruthy();
  335. }).toThrow();
  336. expect(function() {
  337. var verified = proofHandler_.verify(emptyObject_);
  338. expect(verified).toBeTruthy();
  339. }).toThrow();
  340. expect(function() {
  341. var verified = proofHandler_.verify(
  342. proof_, elements_, ciphertextWithTooManyElements_);
  343. expect(verified).toBeTruthy();
  344. }).toThrow();
  345. expect(function() {
  346. var verified = proofHandler_.verify(
  347. proof_, elements_, ciphertextFromAnotherGroup_);
  348. expect(verified).toBeTruthy();
  349. }).toThrow();
  350. });
  351. it('throw an error when verifying an OR proof, using invalid optional data',
  352. function() {
  353. expect(function() {
  354. proofHandler_.verify(
  355. proof_, elements_, ciphertext_, {data: nonAuxiliaryData_});
  356. }).toThrow();
  357. });
  358. it('throw an error when measuring OR proof progress, using invalid input data',
  359. function() {
  360. expect(function() {
  361. proofHandler_.measureProgress();
  362. }).toThrow();
  363. expect(function() {
  364. proofHandler_.measureProgress(undefined);
  365. }).toThrow();
  366. expect(function() {
  367. proofHandler_.measureProgress(null);
  368. }).toThrow();
  369. expect(function() {
  370. proofHandler_.measureProgress(nonFunction_);
  371. }).toThrow();
  372. expect(function() {
  373. proofHandler_.measureProgress(callback_, null);
  374. }).toThrow();
  375. expect(function() {
  376. proofHandler_.measureProgress(callback_, nonNumber_);
  377. }).toThrow();
  378. expect(function() {
  379. proofHandler_.measureProgress(callback_, nonPositiveNumber_);
  380. }).toThrow();
  381. });
  382. });
  383. });
  384. });