schnorr-proof-validation.spec.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 SchnorrProofTestData = require('../data/schnorr-proof-data');
  11. var ProgressMeterTestData = require('../data/progress-meter-data');
  12. var ValidationTestData = require('../data/validation-data');
  13. var zkProof = require('../../lib/index');
  14. describe('The zero-knowledge proof module should be able to ...', function() {
  15. var proofService_;
  16. var group_;
  17. var anotherGroup_;
  18. var secret_;
  19. var secretFromAnotherGroup_;
  20. var gamma_;
  21. var gammaFromAnotherGroup_;
  22. var voterId_;
  23. var electionId_;
  24. var callback_;
  25. var nonObject_;
  26. var emptyObject_;
  27. var nonNumber_;
  28. var nonPositiveNumber_;
  29. var nonString_;
  30. var emptyString_;
  31. var nonFunction_;
  32. var nonAuxiliaryData_;
  33. var nonProofObject_;
  34. var proofHandler_;
  35. var proof_;
  36. beforeAll(function() {
  37. proofService_ = zkProof.newService();
  38. var testData = new SchnorrProofTestData();
  39. group_ = testData.getGroup();
  40. anotherGroup_ = testData.getAnotherGroup();
  41. secret_ = testData.getSecret();
  42. secretFromAnotherGroup_ = testData.getSecretFromAnotherGroup();
  43. gamma_ = testData.getGamma();
  44. gammaFromAnotherGroup_ = testData.getGammaFromAnotherGroup();
  45. voterId_ = testData.getVoterId();
  46. electionId_ = testData.getElectionId();
  47. var progressMeterTestData = new ProgressMeterTestData();
  48. callback_ = progressMeterTestData.progressCallback;
  49. var validationTestData = new ValidationTestData();
  50. nonObject_ = validationTestData.getNonObject();
  51. emptyObject_ = validationTestData.getEmptyObject();
  52. nonNumber_ = validationTestData.getNonNumber();
  53. nonPositiveNumber_ = validationTestData.getNonPositiveNumber();
  54. nonString_ = validationTestData.getNonString();
  55. emptyString_ = validationTestData.getEmptyString();
  56. nonFunction_ = validationTestData.getNonFunction();
  57. nonAuxiliaryData_ = validationTestData.getNonAuxiliaryData();
  58. nonProofObject_ = validationTestData.getNonProofObject();
  59. proofHandler_ = proofService_.newSchnorrProofHandler(group_);
  60. proof_ = proofHandler_.generate(secret_, gamma_);
  61. });
  62. describe('create a zero-knowledge proof service that should be able to ..', function() {
  63. describe('create a Schnorr proof handler that should be able to', function() {
  64. it('throw an error when being created, using invalid input data',
  65. function() {
  66. expect(function() {
  67. proofService_.newSchnorrProofHandler();
  68. }).toThrow();
  69. expect(function() {
  70. proofService_.newSchnorrProofHandler(undefined);
  71. }).toThrow();
  72. expect(function() {
  73. proofService_.newSchnorrProofHandler(null);
  74. }).toThrow();
  75. expect(function() {
  76. proofService_.newSchnorrProofHandler(nonObject_);
  77. }).toThrow();
  78. expect(function() {
  79. proofService_.newSchnorrProofHandler(emptyObject_);
  80. }).toThrow();
  81. });
  82. it('throw an error when generating a Schnorr proof, using an invalid secret',
  83. function() {
  84. expect(function() {
  85. proofHandler_.generate(undefined, gamma_);
  86. }).toThrow();
  87. expect(function() {
  88. proofHandler_.generate(null, gamma_);
  89. }).toThrow();
  90. expect(function() {
  91. proofHandler_.generate(nonObject_, gamma_);
  92. }).toThrow();
  93. expect(function() {
  94. proofHandler_.generate(emptyObject_, gamma_);
  95. }).toThrow();
  96. expect(function() {
  97. proofHandler_.generate(secretFromAnotherGroup_, gamma_);
  98. }).toThrow();
  99. });
  100. it('throw an error when generating a Schnorr proof, using an invalid gamma element',
  101. function() {
  102. expect(function() {
  103. proofHandler_.generate(secret_, undefined);
  104. }).toThrow();
  105. expect(function() {
  106. proofHandler_.generate(secret_, null);
  107. }).toThrow();
  108. expect(function() {
  109. proofHandler_.generate(secret_, nonObject_);
  110. }).toThrow();
  111. expect(function() {
  112. proofHandler_.generate(secret_, emptyObject_);
  113. }).toThrow();
  114. expect(function() {
  115. proofHandler_.generate(secret_, gammaFromAnotherGroup_);
  116. }).toThrow();
  117. });
  118. it('throw an error when generating a Schnorr proof, using invalid optional data',
  119. function() {
  120. expect(function() {
  121. proofHandler_.generate(secret_, gamma_, {data: nonAuxiliaryData_});
  122. }).toThrow();
  123. expect(function() {
  124. proofHandler_.generate(
  125. secret_, gamma_,
  126. {voterId: nonString_, electionId: electionId_});
  127. }).toThrow();
  128. expect(function() {
  129. proofHandler_.generate(
  130. secret_, gamma_, {voterId: voterId_, electionId: nonString_});
  131. }).toThrow();
  132. expect(function() {
  133. proofHandler_.generate(
  134. secret_, gamma_,
  135. {voterId: emptyString_, electionId: electionId_});
  136. }).toThrow();
  137. expect(function() {
  138. proofHandler_.generate(
  139. secret_, gamma_,
  140. {voterId: voterId_, electionId: emptyString_});
  141. }).toThrow();
  142. expect(function() {
  143. proofHandler_.generate(secret_, gamma_, {voterId: voterId_});
  144. }).toThrow();
  145. expect(function() {
  146. proofHandler_.generate(secret_, gamma_, {electionId: electionId_});
  147. }).toThrow();
  148. expect(function() {
  149. proofHandler_.generate(
  150. secret_, gamma_, {preComputation: nonObject_});
  151. }).toThrow();
  152. expect(function() {
  153. proofHandler_.generate(
  154. secret_, gamma_, {preComputation: nonProofObject_});
  155. }).toThrow();
  156. });
  157. it('throw an error when verifying a Schnorr proof, using an invalid proof',
  158. function() {
  159. expect(function() {
  160. proofHandler_.verify(undefined, gamma_);
  161. }).toThrow();
  162. expect(function() {
  163. proofHandler_.verify(null, gamma_);
  164. }).toThrow();
  165. expect(function() {
  166. proofHandler_.verify(nonProofObject_, gamma_);
  167. }).toThrow();
  168. });
  169. it('throw an error when verifying a Schnorr proof, using an invalid gamma element',
  170. function() {
  171. expect(function() {
  172. proofHandler_.verify(proof_, undefined);
  173. }).toThrow();
  174. expect(function() {
  175. proofHandler_.verify(proof_, null);
  176. }).toThrow();
  177. expect(function() {
  178. proofHandler_.verify(proof_, nonObject_);
  179. }).toThrow();
  180. expect(function() {
  181. proofHandler_.verify(proof_, emptyObject_);
  182. }).toThrow();
  183. expect(function() {
  184. proofHandler_.verify(proof_, gammaFromAnotherGroup_);
  185. }).toThrow();
  186. });
  187. it('throw an error when verifying a Schnorr proof, using invalid optional data',
  188. function() {
  189. expect(function() {
  190. proofHandler_.verify(proof_, gamma_, {data: nonAuxiliaryData_});
  191. }).toThrow();
  192. expect(function() {
  193. proofHandler_.verify(
  194. proof_, gamma_,
  195. {voterId: nonString_, electionId: electionId_});
  196. }).toThrow();
  197. expect(function() {
  198. proofHandler_.verify(
  199. proof_, gamma_, {voterId: voterId_, electionId: nonString_});
  200. }).toThrow();
  201. expect(function() {
  202. proofHandler_.verify(
  203. proof_, gamma_,
  204. {voterId: emptyString_, electionId: electionId_});
  205. }).toThrow();
  206. expect(function() {
  207. proofHandler_.verify(
  208. proof_, gamma_, {voterId: voterId_, electionId: emptyString_});
  209. }).toThrow();
  210. expect(function() {
  211. proofHandler_.verify(proof_, gamma_, {voterId: voterId_});
  212. }).toThrow();
  213. expect(function() {
  214. proofHandler_.verify(proof_, gamma_, {electionId: electionId_});
  215. }).toThrow();
  216. });
  217. it('throw an error when measuring Schnorr proof progress, using invalid input data',
  218. function() {
  219. expect(function() {
  220. proofHandler_.measureProgress();
  221. }).toThrow();
  222. expect(function() {
  223. proofHandler_.measureProgress(undefined);
  224. }).toThrow();
  225. expect(function() {
  226. proofHandler_.measureProgress(null);
  227. }).toThrow();
  228. expect(function() {
  229. proofHandler_.measureProgress(nonFunction_);
  230. }).toThrow();
  231. expect(function() {
  232. proofHandler_.measureProgress(callback_, null);
  233. }).toThrow();
  234. expect(function() {
  235. proofHandler_.measureProgress(callback_, nonNumber_);
  236. }).toThrow();
  237. expect(function() {
  238. proofHandler_.measureProgress(callback_, nonPositiveNumber_);
  239. }).toThrow();
  240. });
  241. });
  242. });
  243. });