plaintext-proof-validation.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 PlaintextProofTestData = require('../data/plaintext-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 secretForCiphertextWithLessElements_;
  22. var ciphertextWithLessElements_;
  23. var secretFromAnotherGroup_;
  24. var ciphertextFromAnotherGroup_;
  25. var publicKey_;
  26. var publicKeyWithLessElements_;
  27. var publicKeyFromAnotherGroup_;
  28. var plaintext_;
  29. var plaintextWithLessElements_;
  30. var plaintextFromAnotherGroup_;
  31. var callback_;
  32. var nonObject_;
  33. var emptyObject_;
  34. var nonNumber_;
  35. var nonPositiveNumber_;
  36. var nonString_;
  37. var nonArray_;
  38. var emptyArray_;
  39. var nonObjectArray_;
  40. var nonFunction_;
  41. var nonAuxiliaryData_;
  42. var nonProofObject_;
  43. var proofHandler_;
  44. var proof_;
  45. beforeAll(function() {
  46. proofService_ = zkProof.newService();
  47. var testData = new PlaintextProofTestData();
  48. var elGamalService = elGamal.newService();
  49. group_ = testData.getGroup();
  50. anotherGroup_ = testData.getAnotherGroup();
  51. var encryptedElements = testData.getEncryptedElements();
  52. secret_ = encryptedElements.secret;
  53. ciphertext_ = elGamalService.newEncryptedElements(
  54. encryptedElements.gamma, encryptedElements.phis);
  55. var encryptedElementsWithLessElements =
  56. testData.getEncryptedElementsWithLessElements();
  57. secretForCiphertextWithLessElements_ =
  58. encryptedElementsWithLessElements.secret;
  59. ciphertextWithLessElements_ = elGamalService.newEncryptedElements(
  60. encryptedElementsWithLessElements.gamma,
  61. encryptedElementsWithLessElements.phis);
  62. var encryptedElementsFromAnotherGroup =
  63. testData.getEncryptedElementsFromAnotherGroup();
  64. secretFromAnotherGroup_ = encryptedElementsFromAnotherGroup.secret;
  65. ciphertextFromAnotherGroup_ = elGamalService.newEncryptedElements(
  66. encryptedElementsFromAnotherGroup.gamma,
  67. encryptedElementsFromAnotherGroup.phis);
  68. publicKey_ = testData.getPublicKey();
  69. publicKeyWithLessElements_ = testData.getPublicKeyWithLessElements();
  70. publicKeyFromAnotherGroup_ = testData.getPublicKeyFromAnotherGroup();
  71. plaintext_ = testData.getPlaintext();
  72. plaintextWithLessElements_ = testData.getPlaintextWithLessElements();
  73. plaintextFromAnotherGroup_ = testData.getPlaintextFromAnotherGroup();
  74. var progressMeterTestData = new ProgressMeterTestData();
  75. callback_ = progressMeterTestData.progressCallback;
  76. var validationTestData = new ValidationTestData();
  77. nonObject_ = validationTestData.getNonObject();
  78. emptyObject_ = validationTestData.getEmptyObject();
  79. nonNumber_ = validationTestData.getNonNumber();
  80. nonPositiveNumber_ = validationTestData.getNonPositiveNumber();
  81. nonString_ = validationTestData.getNonString();
  82. nonArray_ = validationTestData.getNonArray();
  83. emptyArray_ = validationTestData.getEmptyArray();
  84. nonObjectArray_ = validationTestData.getNonObjectArray();
  85. nonFunction_ = validationTestData.getNonFunction();
  86. nonAuxiliaryData_ = validationTestData.getNonAuxiliaryData();
  87. nonProofObject_ = validationTestData.getNonProofObject();
  88. proofHandler_ =
  89. proofService_.newPlaintextProofHandler(group_).init(publicKey_);
  90. proof_ = proofHandler_.generate(secret_, plaintext_, ciphertext_);
  91. });
  92. beforeEach(function() {
  93. proofHandler_ =
  94. proofService_.newPlaintextProofHandler(group_).init(publicKey_);
  95. });
  96. describe('create a zero-knowledge proof service that should be able to ..', function() {
  97. describe('create a plaintext proof handler that should be able to', function() {
  98. it('throw an error when being created, using invalid input data',
  99. function() {
  100. expect(function() {
  101. proofService_.newPlaintextProofHandler();
  102. }).toThrow();
  103. expect(function() {
  104. proofService_.newPlaintextProofHandler(undefined);
  105. }).toThrow();
  106. expect(function() {
  107. proofService_.newPlaintextProofHandler(null);
  108. }).toThrow();
  109. expect(function() {
  110. proofService_.newPlaintextProofHandler(nonObject_);
  111. }).toThrow();
  112. expect(function() {
  113. proofService_.newPlaintextProofHandler(emptyObject_);
  114. }).toThrow();
  115. });
  116. it('throw an error when being initialized, using invalid input data',
  117. function() {
  118. expect(function() {
  119. proofHandler_.init(undefined);
  120. }).toThrow();
  121. expect(function() {
  122. proofHandler_.init(null);
  123. }).toThrow();
  124. expect(function() {
  125. proofHandler_.init(nonObject_);
  126. }).toThrow();
  127. expect(function() {
  128. proofHandler_.init(emptyObject_);
  129. }).toThrow();
  130. expect(function() {
  131. proofHandler_.init(publicKeyFromAnotherGroup_);
  132. }).toThrow();
  133. });
  134. it('throw an error when generating a plaintext proof, using an invalid secret',
  135. function() {
  136. expect(function() {
  137. proofHandler_.generate(undefined, plaintext_, ciphertext_);
  138. }).toThrow();
  139. expect(function() {
  140. proofHandler_.generate(null, plaintext_, ciphertext_);
  141. }).toThrow();
  142. expect(function() {
  143. proofHandler_.generate(nonObject_, plaintext_, ciphertext_);
  144. }).toThrow();
  145. expect(function() {
  146. proofHandler_.generate(emptyObject_, plaintext_, ciphertext_);
  147. }).toThrow();
  148. expect(function() {
  149. proofHandler_.generate(
  150. secretFromAnotherGroup_, plaintext_, ciphertext_);
  151. }).toThrow();
  152. });
  153. it('throw an error when generating a plaintext proof, using an invalid plaintext',
  154. function() {
  155. expect(function() {
  156. proofHandler_.generate(secret_, undefined, ciphertext_);
  157. }).toThrow();
  158. expect(function() {
  159. proofHandler_.generate(secret_, null, ciphertext_);
  160. }).toThrow();
  161. expect(function() {
  162. proofHandler_.generate(secret_, nonArray_, ciphertext_);
  163. }).toThrow();
  164. expect(function() {
  165. proofHandler_.generate(secret_, emptyArray_, ciphertext_);
  166. }).toThrow();
  167. expect(function() {
  168. proofHandler_.generate(secret_, nonObjectArray_, ciphertext_);
  169. }).toThrow();
  170. expect(function() {
  171. proofHandler_.generate(
  172. secret_, plaintextWithLessElements_, ciphertext_);
  173. }).toThrow();
  174. expect(function() {
  175. proofHandler_.generate(
  176. secret_, plaintextFromAnotherGroup_, ciphertext_);
  177. }).toThrow();
  178. });
  179. it('throw an error when generating a plaintext proof, using an invalid ciphertext',
  180. function() {
  181. expect(function() {
  182. proofHandler_.generate(secret_, plaintext_, undefined);
  183. }).toThrow();
  184. expect(function() {
  185. proofHandler_.generate(secret_, plaintext_, null);
  186. }).toThrow();
  187. expect(function() {
  188. proofHandler_.generate(secret_, plaintext_, nonObject_);
  189. }).toThrow();
  190. expect(function() {
  191. proofHandler_.generate(secret_, plaintext_, emptyObject_);
  192. }).toThrow();
  193. expect(function() {
  194. proofHandler_.generate(
  195. secret_, plaintext_, ciphertextWithLessElements_);
  196. }).toThrow();
  197. expect(function() {
  198. proofHandler_.generate(
  199. secret_, plaintext_, ciphertextFromAnotherGroup_);
  200. }).toThrow();
  201. });
  202. it('throw an error when generating a plaintext proof, using invalid optional data',
  203. function() {
  204. expect(function() {
  205. proofHandler_.generate(
  206. secret_, plaintext_, ciphertext_, {data: nonAuxiliaryData_});
  207. }).toThrow();
  208. expect(function() {
  209. proofHandler_.generate(
  210. secret_, plaintext_, ciphertext_,
  211. {preComputation: nonObject_});
  212. }).toThrow();
  213. expect(function() {
  214. proofHandler_.generate(
  215. secret_, plaintext_, ciphertext_,
  216. {preComputation: nonProofObject_});
  217. }).toThrow();
  218. });
  219. it('throw an error when verifying a plaintext proof, using an invalid proof',
  220. function() {
  221. expect(function() {
  222. proofHandler_.verify(undefined, plaintext_, ciphertext_);
  223. }).toThrow();
  224. expect(function() {
  225. proofHandler_.verify(null, plaintext_, ciphertext_);
  226. }).toThrow();
  227. expect(function() {
  228. proofHandler_.verify(nonProofObject_, plaintext_, ciphertext_);
  229. }).toThrow();
  230. });
  231. it('throw an error when verifying a plaintext proof, using an invalid plaintext',
  232. function() {
  233. expect(function() {
  234. proofHandler_.verify(proof_, undefined, ciphertext_);
  235. }).toThrow();
  236. expect(function() {
  237. proofHandler_.verify(proof_, null, ciphertext_);
  238. }).toThrow();
  239. expect(function() {
  240. proofHandler_.verify(proof_, nonArray_, ciphertext_);
  241. }).toThrow();
  242. expect(function() {
  243. proofHandler_.verify(proof_, emptyArray_, ciphertext_);
  244. }).toThrow();
  245. expect(function() {
  246. proofHandler_.verify(proof_, nonObjectArray_, ciphertext_);
  247. }).toThrow();
  248. expect(function() {
  249. proofHandler_.verify(
  250. proof_, plaintextWithLessElements_, ciphertext_);
  251. }).toThrow();
  252. expect(function() {
  253. proofHandler_.verify(
  254. proof_, plaintextFromAnotherGroup_, ciphertext_);
  255. }).toThrow();
  256. });
  257. it('throw an error when verifying a plaintext proof, using an invalid ciphertext',
  258. function() {
  259. expect(function() {
  260. proofHandler_.verify(proof_, plaintext_, undefined);
  261. }).toThrow();
  262. expect(function() {
  263. proofHandler_.verify(proof_, plaintext_, null);
  264. }).toThrow();
  265. expect(function() {
  266. proofHandler_.verify(proof_, plaintext_, nonObject_);
  267. }).toThrow();
  268. expect(function() {
  269. proofHandler_.verify(proof_, plaintext_, emptyObject_);
  270. }).toThrow();
  271. expect(function() {
  272. proofHandler_.verify(
  273. proof_, ciphertextWithLessElements_, ciphertext_);
  274. }).toThrow();
  275. expect(function() {
  276. proofHandler_.verify(
  277. proof_, plaintext_, ciphertextFromAnotherGroup_);
  278. }).toThrow();
  279. });
  280. it('throw an error when verifying a plaintext proof, using invalid optional data',
  281. function() {
  282. expect(function() {
  283. proofHandler_.verify(
  284. proof_, plaintext_, ciphertext_, {data: nonAuxiliaryData_});
  285. }).toThrow();
  286. });
  287. it('throw an error when measuring plaintext proof progress, using invalid input data',
  288. function() {
  289. expect(function() {
  290. proofHandler_.measureProgress();
  291. }).toThrow();
  292. expect(function() {
  293. proofHandler_.measureProgress(undefined);
  294. }).toThrow();
  295. expect(function() {
  296. proofHandler_.measureProgress(null);
  297. }).toThrow();
  298. expect(function() {
  299. proofHandler_.measureProgress(nonFunction_);
  300. }).toThrow();
  301. expect(function() {
  302. proofHandler_.measureProgress(callback_, null);
  303. }).toThrow();
  304. expect(function() {
  305. proofHandler_.measureProgress(callback_, nonNumber_);
  306. }).toThrow();
  307. expect(function() {
  308. proofHandler_.measureProgress(callback_, nonPositiveNumber_);
  309. }).toThrow();
  310. });
  311. });
  312. });
  313. });