plaintext-exponent-equality-validation.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 PlaintextExponentEqualityProofTestData =
  11. require('../data/plaintext-exponent-equality-proof-data');
  12. var ProgressMeterTestData = require('../data/progress-meter-data');
  13. var ValidationTestData = require('../data/validation-data');
  14. var zkProof = require('../../lib/index');
  15. describe('The zero-knowledge proof module should be able to ...', function() {
  16. var proofService_;
  17. var group_;
  18. var anotherGroup_;
  19. var firstSecret_;
  20. var secondSecret_;
  21. var firstSecretFromAnotherGroup_;
  22. var secondSecretFromAnotherGroup_;
  23. var baseElements_;
  24. var lessBaseElements_;
  25. var baseElementsFromAnotherGroup_;
  26. var ciphertext_;
  27. var ciphertextWithLessElements_;
  28. var ciphertextFromAnotherGroup_;
  29. var callback_;
  30. var nonObject_;
  31. var emptyObject_;
  32. var nonNumber_;
  33. var nonPositiveNumber_;
  34. var nonString_;
  35. var nonArray_;
  36. var emptyArray_;
  37. var nonObjectArray_;
  38. var nonFunction_;
  39. var nonAuxiliaryData_;
  40. var nonProofObject_;
  41. var proofHandler_;
  42. var proof_;
  43. beforeAll(function() {
  44. proofService_ = zkProof.newService();
  45. var testData = new PlaintextExponentEqualityProofTestData();
  46. group_ = testData.getGroup();
  47. anotherGroup_ = testData.getAnotherGroup();
  48. firstSecret_ = testData.getFirstSecret();
  49. secondSecret_ = testData.getSecondSecret();
  50. firstSecretFromAnotherGroup_ = testData.getFirstSecretFromAnotherGroup();
  51. secondSecretFromAnotherGroup_ = testData.getSecondSecretFromAnotherGroup();
  52. baseElements_ = testData.getBaseElements();
  53. lessBaseElements_ = testData.getLessBaseElements();
  54. baseElementsFromAnotherGroup_ = testData.getBaseElementsFromAnotherGroup();
  55. ciphertext_ = testData.getCiphertext();
  56. ciphertextWithLessElements_ = testData.getCiphertextWithLessElements();
  57. ciphertextFromAnotherGroup_ = testData.getCiphertextFromAnotherGroup();
  58. var progressMeterTestData = new ProgressMeterTestData();
  59. callback_ = progressMeterTestData.progressCallback;
  60. var validationTestData = new ValidationTestData();
  61. nonObject_ = validationTestData.getNonObject();
  62. emptyObject_ = validationTestData.getEmptyObject();
  63. nonNumber_ = validationTestData.getNonNumber();
  64. nonPositiveNumber_ = validationTestData.getNonPositiveNumber();
  65. nonString_ = validationTestData.getNonString();
  66. nonArray_ = validationTestData.getNonArray();
  67. emptyArray_ = validationTestData.getEmptyArray();
  68. nonObjectArray_ = validationTestData.getNonObjectArray();
  69. nonFunction_ = validationTestData.getNonFunction();
  70. nonAuxiliaryData_ = validationTestData.getNonAuxiliaryData();
  71. nonProofObject_ = validationTestData.getNonProofObject();
  72. proofHandler_ =
  73. proofService_.newPlaintextExponentEqualityProofHandler(group_).init(
  74. baseElements_);
  75. proof_ = proofHandler_.generate(firstSecret_, secondSecret_, ciphertext_);
  76. });
  77. beforeEach(function() {
  78. proofHandler_ =
  79. proofService_.newPlaintextExponentEqualityProofHandler(group_).init(
  80. baseElements_);
  81. });
  82. describe('create a zero-knowledge proof service that should be able to ..', function() {
  83. describe(
  84. 'create a plaintext exponent equality proof handler that should be able to',
  85. function() {
  86. it('throw an error when being created, using invalid input data',
  87. function() {
  88. expect(function() {
  89. proofService_.newPlaintextExponentEqualityProofHandler();
  90. }).toThrow();
  91. expect(function() {
  92. proofService_.newPlaintextExponentEqualityProofHandler(
  93. undefined);
  94. }).toThrow();
  95. expect(function() {
  96. proofService_.newPlaintextExponentEqualityProofHandler(null);
  97. }).toThrow();
  98. expect(function() {
  99. proofService_.newPlaintextExponentEqualityProofHandler(
  100. nonObject_);
  101. }).toThrow();
  102. expect(function() {
  103. proofService_.newPlaintextExponentEqualityProofHandler(
  104. emptyObject_);
  105. }).toThrow();
  106. });
  107. it('throw an error when initializing a plaintext exponent equality proof, using invalid input data',
  108. function() {
  109. expect(function() {
  110. proofHandler_.init(undefined);
  111. }).toThrow();
  112. expect(function() {
  113. proofHandler_.init(null);
  114. }).toThrow();
  115. expect(function() {
  116. proofHandler_.init(nonArray_);
  117. }).toThrow();
  118. expect(function() {
  119. proofHandler_.init(emptyArray_);
  120. }).toThrow();
  121. expect(function() {
  122. proofHandler_.init(nonObjectArray_);
  123. }).toThrow();
  124. expect(function() {
  125. proofHandler_.init(baseElementsFromAnotherGroup_);
  126. }).toThrow();
  127. });
  128. it('throw an error when generating a plaintext exponent equality proof, using an invalid first secret',
  129. function() {
  130. expect(function() {
  131. proofHandler_.generate(undefined, secondSecret_, ciphertext_);
  132. }).toThrow();
  133. expect(function() {
  134. proofHandler_.generate(null, secondSecret_, ciphertext_);
  135. }).toThrow();
  136. expect(function() {
  137. proofHandler_.generate(nonObject_, secondSecret_, ciphertext_);
  138. }).toThrow();
  139. expect(function() {
  140. proofHandler_.generate(
  141. emptyObject_, secondSecret_, ciphertext_);
  142. }).toThrow();
  143. expect(function() {
  144. proofHandler_.generate(
  145. firstSecretFromAnotherGroup_, secondSecret_, ciphertext_);
  146. }).toThrow();
  147. });
  148. it('throw an error when generating a plaintext exponent equality proof, using an invalid second secret',
  149. function() {
  150. expect(function() {
  151. proofHandler_.generate(firstSecret_, undefined, ciphertext_);
  152. }).toThrow();
  153. expect(function() {
  154. proofHandler_.generate(firstSecret_, null, ciphertext_);
  155. }).toThrow();
  156. expect(function() {
  157. proofHandler_.generate(firstSecret_, nonObject_, ciphertext_);
  158. }).toThrow();
  159. expect(function() {
  160. proofHandler_.generate(
  161. firstSecret_, emptyObject_, ciphertext_);
  162. }).toThrow();
  163. expect(function() {
  164. proofHandler_.generate(
  165. firstSecret_, secondSecretFromAnotherGroup_, ciphertext_);
  166. }).toThrow();
  167. });
  168. it('throw an error when generating a plaintext exponent equality proof, using an invalid ciphertext',
  169. function() {
  170. expect(function() {
  171. proofHandler_.generate(firstSecret_, secondSecret_, undefined);
  172. }).toThrow();
  173. expect(function() {
  174. proofHandler_.generate(firstSecret_, secondSecret_, null);
  175. }).toThrow();
  176. expect(function() {
  177. proofHandler_.generate(
  178. firstSecret_, secondSecret_, nonObject_);
  179. }).toThrow();
  180. expect(function() {
  181. proofHandler_.generate(
  182. firstSecret_, secondSecret_, emptyObject_);
  183. }).toThrow();
  184. expect(function() {
  185. proofHandler_.generate(
  186. firstSecret_, secondSecret_, ciphertextWithLessElements_);
  187. }).toThrow();
  188. expect(function() {
  189. proofHandler_.generate(
  190. firstSecret_, secondSecret_, ciphertextFromAnotherGroup_);
  191. }).toThrow();
  192. });
  193. it('throw an error when generating a plaintext exponent equality proof, using invalid optional data',
  194. function() {
  195. expect(function() {
  196. proofHandler_.generate(
  197. firstSecret_, secondSecret_, ciphertext_,
  198. {data: nonAuxiliaryData_});
  199. }).toThrow();
  200. expect(function() {
  201. proofHandler_.generate(
  202. firstSecret_, secondSecret_, ciphertext_,
  203. {preComputation: nonObject_});
  204. }).toThrow();
  205. expect(function() {
  206. proofHandler_.generate(
  207. firstSecret_, secondSecret_, ciphertext_,
  208. {preComputation: nonProofObject_});
  209. }).toThrow();
  210. });
  211. it('throw an error when verifying a plaintext exponent equality proof, using an invalid proof',
  212. function() {
  213. expect(function() {
  214. proofHandler_.verify(undefined, ciphertext_);
  215. }).toThrow();
  216. expect(function() {
  217. proofHandler_.verify(null, ciphertext_);
  218. }).toThrow();
  219. expect(function() {
  220. proofHandler_.verify(nonProofObject_, ciphertext_);
  221. }).toThrow();
  222. });
  223. it('throw an error when verifying a plaintext exponent equality proof, using an invalid ciphertext',
  224. function() {
  225. expect(function() {
  226. proofHandler_.verify(proof_, undefined);
  227. }).toThrow();
  228. expect(function() {
  229. proofHandler_.verify(proof_, null);
  230. }).toThrow();
  231. expect(function() {
  232. proofHandler_.verify(proof_, nonObject_);
  233. }).toThrow();
  234. expect(function() {
  235. proofHandler_.verify(proof_, emptyObject_);
  236. }).toThrow();
  237. expect(function() {
  238. proofHandler_.verify(proof_, ciphertextWithLessElements_);
  239. }).toThrow();
  240. expect(function() {
  241. proofHandler_.verify(proof_, ciphertextFromAnotherGroup_);
  242. }).toThrow();
  243. });
  244. it('throw an error when verifying a plaintext exponent equality proof, using invalid optional data',
  245. function() {
  246. expect(function() {
  247. proofHandler_.verify(
  248. proof_, ciphertext_, {data: nonAuxiliaryData_});
  249. }).toThrow();
  250. });
  251. it('throw an error when measuring plaintext exponent equality proof progress, using invalid input data',
  252. function() {
  253. expect(function() {
  254. proofHandler_.measureProgress();
  255. }).toThrow();
  256. expect(function() {
  257. proofHandler_.measureProgress(undefined);
  258. }).toThrow();
  259. expect(function() {
  260. proofHandler_.measureProgress(null);
  261. }).toThrow();
  262. expect(function() {
  263. proofHandler_.measureProgress(nonFunction_);
  264. }).toThrow();
  265. expect(function() {
  266. proofHandler_.measureProgress(callback_, null);
  267. }).toThrow();
  268. expect(function() {
  269. proofHandler_.measureProgress(callback_, nonNumber_);
  270. }).toThrow();
  271. expect(function() {
  272. proofHandler_.measureProgress(callback_, nonPositiveNumber_);
  273. }).toThrow();
  274. });
  275. });
  276. });
  277. });