2
0

exponentiation-proof-validation.spec.js 9.5 KB

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