2
0

index-validation.spec.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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 mathematical = require('../lib/index');
  11. var CommonTestData = require('./data/common-data');
  12. var ValidationTestData = require('./data/validation-data');
  13. describe('The mathematical module should be able to ...', function() {
  14. var mathService_;
  15. var mathArrayCompressor_;
  16. var mathRandomGenerator_;
  17. var mathGroupHandler_;
  18. var g_;
  19. var p_;
  20. var q_;
  21. var group_;
  22. var qrGroup_;
  23. var elementValue_;
  24. var element_;
  25. var exponentValue_;
  26. var exponent_;
  27. var elements_;
  28. var exponents_;
  29. var elementFromAnotherGroup_;
  30. var exponentFromAnotherGroup_;
  31. var multiGroupElementValues_;
  32. var minCertaintyLevel_;
  33. var nonObject_;
  34. var emptyObject_;
  35. var nonSecureRandomService_;
  36. var nonBoolean_;
  37. var nonNumber_;
  38. var nonPositiveNumber_;
  39. var nonString_;
  40. var nonJsonString_;
  41. var nonArray_;
  42. var nonMathObject_;
  43. var nonMathObjectArray_;
  44. var tooSmallModulus_;
  45. var tooSmallOrder_;
  46. var tooLargeOrder_;
  47. var tooSmallGenerator_;
  48. var tooLargeGenerator_;
  49. var tooSmallElementValue_;
  50. var tooLargeElementValue_;
  51. var tooSmallModulusBitLength_;
  52. var tooLowCertaintyLevel_;
  53. var tooLargeNumMembersRequired_;
  54. beforeAll(function() {
  55. mathService_ = mathematical.newService();
  56. mathArrayCompressor_ = mathService_.newArrayCompressor();
  57. mathRandomGenerator_ = mathService_.newRandomGenerator();
  58. mathGroupHandler_ = mathService_.newGroupHandler();
  59. var commonTestData = new CommonTestData();
  60. p_ = commonTestData.getP();
  61. q_ = commonTestData.getQ();
  62. g_ = commonTestData.getG();
  63. group_ = commonTestData.getGroup();
  64. qrGroup_ = commonTestData.getQuadraticResidueGroup();
  65. elementValue_ = commonTestData.getElementValue();
  66. element_ = commonTestData.getElement();
  67. exponentValue_ = commonTestData.getExponentValue();
  68. exponent_ = commonTestData.getExponent();
  69. elements_ = commonTestData.getElements();
  70. exponents_ = commonTestData.getExponents();
  71. elementFromAnotherGroup_ = commonTestData.getElementFromAnotherGroup();
  72. exponentFromAnotherGroup_ = commonTestData.getExponentFromAnotherGroup();
  73. multiGroupElementValues_ = commonTestData.multiGroupElementValues();
  74. minCertaintyLevel_ = commonTestData.getMinimumCertaintyLevel();
  75. var validationTestData = new ValidationTestData();
  76. nonObject_ = validationTestData.getNonObject();
  77. emptyObject_ = validationTestData.getEmptyObject();
  78. nonSecureRandomService_ = validationTestData.getNonSecureRandomService();
  79. nonBoolean_ = validationTestData.getNonBoolean();
  80. nonNumber_ = validationTestData.getNonNumber();
  81. nonPositiveNumber_ = validationTestData.getNonPositiveNumber();
  82. nonString_ = validationTestData.getNonString();
  83. nonJsonString_ = validationTestData.getNonJsonString();
  84. nonArray_ = validationTestData.getNonArray();
  85. nonMathObject_ = validationTestData.getNonMathObject();
  86. nonMathObjectArray_ = validationTestData.getNonMathObjectArray();
  87. tooSmallModulus_ = validationTestData.getTooSmallModulus();
  88. tooSmallOrder_ = validationTestData.getTooSmallOrder();
  89. tooLargeOrder_ = validationTestData.getTooLargeOrder();
  90. tooSmallGenerator_ = validationTestData.getTooSmallGenerator();
  91. tooLargeGenerator_ = validationTestData.getTooLargeGenerator();
  92. tooSmallElementValue_ = validationTestData.getTooSmallElementValue();
  93. tooLargeElementValue_ = validationTestData.getTooLargeElementValue();
  94. tooSmallModulusBitLength_ =
  95. validationTestData.getTooSmallModulusBitLength();
  96. tooLowCertaintyLevel_ = validationTestData.getTooLowCertaintyLevel();
  97. tooLargeNumMembersRequired_ =
  98. validationTestData.getTooLargeNumMembersRequired();
  99. });
  100. describe('create a mathematical service that should be able to ..', function() {
  101. it('throw an error when being created, using an invalid secure random service object',
  102. function() {
  103. expect(function() {
  104. Object.create(mathematical.newService({secureRandomService: null}));
  105. }).toThrow();
  106. expect(function() {
  107. Object.create(
  108. mathematical.newService({secureRandomService: nonObject_}));
  109. }).toThrow();
  110. expect(function() {
  111. Object.create(
  112. mathematical.newService({secureRandomService: emptyObject_}));
  113. }).toThrow();
  114. });
  115. it('throw an error when creating a new ZpSubgroup object, using an invalid modulus p or JSON representation',
  116. function() {
  117. expect(function() {
  118. mathService_.newZpSubgroup(undefined, q_, g_);
  119. }).toThrow();
  120. expect(function() {
  121. mathService_.newZpSubgroup(null, q_, g_);
  122. }).toThrow();
  123. expect(function() {
  124. mathService_.newZpSubgroup(nonMathObject_, q_, g_);
  125. }).toThrow();
  126. expect(function() {
  127. mathService_.newZpSubgroup(tooSmallModulus_, q_, g_);
  128. }).toThrow();
  129. expect(function() {
  130. mathService_.newZpSubgroup(nonJsonString_);
  131. }).toThrow();
  132. });
  133. it('throw an error when creating a new ZpSubgroup object, using an invalid order q',
  134. function() {
  135. expect(function() {
  136. mathService_.newZpSubgroup(p_, undefined, g_);
  137. }).toThrow();
  138. expect(function() {
  139. mathService_.newZpSubgroup(p_, null, g_);
  140. }).toThrow();
  141. expect(function() {
  142. mathService_.newZpSubgroup(p_, nonMathObject_, g_);
  143. }).toThrow();
  144. expect(function() {
  145. mathService_.newZpSubgroup(p_, tooSmallOrder_, g_);
  146. }).toThrow();
  147. expect(function() {
  148. mathService_.newZpSubgroup(p_, tooLargeOrder_, g_);
  149. }).toThrow();
  150. });
  151. it('throw an error when creating a new ZpSubgroup object, using an invalid generator',
  152. function() {
  153. expect(function() {
  154. mathService_.newZpSubgroup(p_, q_);
  155. }).toThrow();
  156. expect(function() {
  157. mathService_.newZpSubgroup(p_, q_, undefined);
  158. }).toThrow();
  159. expect(function() {
  160. mathService_.newZpSubgroup(p_, q_, null);
  161. }).toThrow();
  162. expect(function() {
  163. mathService_.newZpSubgroup(p_, q_, nonMathObject_);
  164. }).toThrow();
  165. expect(function() {
  166. mathService_.newZpSubgroup(p_, q_, tooSmallGenerator_);
  167. }).toThrow();
  168. expect(function() {
  169. mathService_.newZpSubgroup(p_, q_, tooLargeGenerator_);
  170. }).toThrow();
  171. });
  172. it('throw an error when creating a new ZpGroupElement object, using an invalid modulus p or JSON representation',
  173. function() {
  174. expect(function() {
  175. mathService_.newZpGroupElement(undefined, q_, elementValue_);
  176. }).toThrow();
  177. expect(function() {
  178. mathService_.newZpGroupElement(null, q_, elementValue_);
  179. }).toThrow();
  180. expect(function() {
  181. mathService_.newZpGroupElement(nonMathObject_, q_, elementValue_);
  182. }).toThrow();
  183. expect(function() {
  184. mathService_.newZpGroupElement(tooSmallModulus_, q_, elementValue_);
  185. }).toThrow();
  186. expect(function() {
  187. mathService_.newZpGroupElement(nonJsonString_);
  188. }).toThrow();
  189. });
  190. it('throw an error when creating a new ZpGroupElement object, using an invalid order q',
  191. function() {
  192. expect(function() {
  193. mathService_.newZpGroupElement(p_, undefined, elementValue_);
  194. }).toThrow();
  195. expect(function() {
  196. mathService_.newZpGroupElement(p_, null, elementValue_);
  197. }).toThrow();
  198. expect(function() {
  199. mathService_.newZpGroupElement(p_, nonMathObject_, elementValue_);
  200. }).toThrow();
  201. expect(function() {
  202. mathService_.newZpGroupElement(p_, tooSmallOrder_, elementValue_);
  203. }).toThrow();
  204. expect(function() {
  205. mathService_.newZpGroupElement(p_, tooLargeOrder_, elementValue_);
  206. }).toThrow();
  207. });
  208. it('throw an error when creating a new ZpGroupElement object, using an invalid element value',
  209. function() {
  210. expect(function() {
  211. mathService_.newZpGroupElement(p_, q_);
  212. }).toThrow();
  213. expect(function() {
  214. mathService_.newZpGroupElement(p_, q_, undefined);
  215. }).toThrow();
  216. expect(function() {
  217. mathService_.newZpGroupElement(p_, q_, null);
  218. }).toThrow();
  219. expect(function() {
  220. mathService_.newZpGroupElement(p_, q_, nonMathObject_);
  221. }).toThrow();
  222. expect(function() {
  223. mathService_.newZpGroupElement(p_, q_, tooSmallElementValue_);
  224. }).toThrow();
  225. expect(function() {
  226. mathService_.newZpGroupElement(p_, q_, tooLargeElementValue_);
  227. }).toThrow();
  228. });
  229. it('throw an error when creating a new Exponent object, using an invalid order q or JSON representation',
  230. function() {
  231. expect(function() {
  232. mathService_.newExponent(undefined, exponentValue_);
  233. }).toThrow();
  234. expect(function() {
  235. mathService_.newExponent(null, exponentValue_);
  236. }).toThrow();
  237. expect(function() {
  238. mathService_.newExponent(nonMathObject_, exponentValue_);
  239. }).toThrow();
  240. expect(function() {
  241. mathService_.newExponent(tooSmallOrder_, exponentValue_);
  242. }).toThrow();
  243. expect(function() {
  244. mathService_.newExponent(nonJsonString_);
  245. }).toThrow();
  246. });
  247. it('throw an error when creating a new Exponent object, using an invalid exponent value',
  248. function() {
  249. expect(function() {
  250. mathService_.newExponent(q_);
  251. }).toThrow();
  252. expect(function() {
  253. mathService_.newExponent(q_, undefined);
  254. }).toThrow();
  255. expect(function() {
  256. mathService_.newExponent(q_, null);
  257. }).toThrow();
  258. expect(function() {
  259. mathService_.newExponent(q_, nonMathObject_);
  260. }).toThrow();
  261. });
  262. it('throw an error when creating a new quadratic residue Zp subgroup, using invalid input data',
  263. function() {
  264. expect(function() {
  265. mathService_.newQuadraticResidueGroup(p_);
  266. }).toThrow();
  267. expect(function() {
  268. mathService_.newQuadraticResidueGroup(undefined, q_);
  269. }).toThrow();
  270. expect(function() {
  271. mathService_.newQuadraticResidueGroup(null, q_);
  272. }).toThrow();
  273. expect(function() {
  274. mathService_.newQuadraticResidueGroup(nonMathObject_, q_);
  275. }).toThrow();
  276. expect(function() {
  277. mathService_.newQuadraticResidueGroup(tooSmallModulus_, q_);
  278. }).toThrow();
  279. expect(function() {
  280. mathService_.newQuadraticResidueGroup(p_, undefined);
  281. }).toThrow();
  282. expect(function() {
  283. mathService_.newQuadraticResidueGroup(p_, null);
  284. }).toThrow();
  285. expect(function() {
  286. mathService_.newQuadraticResidueGroup(p_, nonMathObject_);
  287. }).toThrow();
  288. expect(function() {
  289. mathService_.newQuadraticResidueGroup(p_, tooSmallOrder_);
  290. }).toThrow();
  291. expect(function() {
  292. mathService_.newQuadraticResidueGroup(p_, tooLargeOrder_);
  293. }).toThrow();
  294. });
  295. describe('create a new ZpSubgroup object that should ..', function() {
  296. it('throw an error when checking for group membership, using invalid input data',
  297. function() {
  298. expect(function() {
  299. group_.isGroupMember();
  300. }).toThrow();
  301. expect(function() {
  302. group_.isGroupMember(undefined);
  303. }).toThrow();
  304. expect(function() {
  305. group_.isGroupMember(null);
  306. }).toThrow();
  307. expect(function() {
  308. group_.isGroupMember(nonMathObject_);
  309. }).toThrow();
  310. });
  311. it('throw an error when checking for equality with another ZpSubgroup object, using invalid input data',
  312. function() {
  313. expect(function() {
  314. group_.equals();
  315. }).toThrow();
  316. expect(function() {
  317. group_.equals(undefined);
  318. }).toThrow();
  319. expect(function() {
  320. group_.equals(null);
  321. }).toThrow();
  322. expect(function() {
  323. group_.equals(nonMathObject_);
  324. }).toThrow();
  325. });
  326. });
  327. describe('create a new ZpGroupElement object that should ..', function() {
  328. it('throw an error when multiplying itself with another ZpGroupElement object, using invalid input data',
  329. function() {
  330. expect(function() {
  331. element_.multiply();
  332. }).toThrow();
  333. expect(function() {
  334. element_.multiply(undefined);
  335. }).toThrow();
  336. expect(function() {
  337. element_.multiply(null);
  338. }).toThrow();
  339. expect(function() {
  340. element_.multiply(nonMathObject_);
  341. }).toThrow();
  342. expect(function() {
  343. element_.multiply(elementFromAnotherGroup_);
  344. }).toThrow();
  345. });
  346. it('throw an error when exponentiating itself with an Exponent object, using invalid input data',
  347. function() {
  348. expect(function() {
  349. element_.exponentiate();
  350. }).toThrow();
  351. expect(function() {
  352. element_.exponentiate(undefined);
  353. }).toThrow();
  354. expect(function() {
  355. element_.exponentiate(null);
  356. }).toThrow();
  357. expect(function() {
  358. element_.exponentiate(nonMathObject_);
  359. }).toThrow();
  360. expect(function() {
  361. element_.exponentiate(exponentFromAnotherGroup_);
  362. }).toThrow();
  363. });
  364. it('throw an error when comparing itself to another ZpGroupElement object, using invalid input data',
  365. function() {
  366. expect(function() {
  367. element_.equals();
  368. }).toThrow();
  369. expect(function() {
  370. element_.equals(undefined);
  371. }).toThrow();
  372. expect(function() {
  373. element_.equals(null);
  374. }).toThrow();
  375. expect(function() {
  376. element_.equals(nonMathObject_);
  377. }).toThrow();
  378. });
  379. });
  380. describe('create a new Exponent object that should ..', function() {
  381. it('throw an error when adding itself to another Exponent object, using invalid input data',
  382. function() {
  383. expect(function() {
  384. exponent_.add();
  385. }).toThrow();
  386. expect(function() {
  387. exponent_.add(undefined);
  388. }).toThrow();
  389. expect(function() {
  390. exponent_.add(null);
  391. }).toThrow();
  392. expect(function() {
  393. exponent_.add(nonMathObject_);
  394. }).toThrow();
  395. expect(function() {
  396. exponent_.add(exponentFromAnotherGroup_);
  397. }).toThrow();
  398. });
  399. it('throw an error when subtracting another Exponent object from itself, using invalid input data',
  400. function() {
  401. expect(function() {
  402. exponent_.subtract();
  403. }).toThrow();
  404. expect(function() {
  405. exponent_.subtract(undefined);
  406. }).toThrow();
  407. expect(function() {
  408. exponent_.subtract(null);
  409. }).toThrow();
  410. expect(function() {
  411. exponent_.subtract(nonMathObject_);
  412. }).toThrow();
  413. expect(function() {
  414. exponent_.add(exponentFromAnotherGroup_);
  415. }).toThrow();
  416. });
  417. it('throw an error when multiplying itself with another Exponent object, using invalid input data',
  418. function() {
  419. expect(function() {
  420. exponent_.multiply();
  421. }).toThrow();
  422. expect(function() {
  423. exponent_.multiply(undefined);
  424. }).toThrow();
  425. expect(function() {
  426. exponent_.multiply(null);
  427. }).toThrow();
  428. expect(function() {
  429. exponent_.multiply(nonMathObject_);
  430. }).toThrow();
  431. expect(function() {
  432. exponent_.add(exponentFromAnotherGroup_);
  433. }).toThrow();
  434. });
  435. it('throw an error when comparing itself to another Exponent object, using invalid input data',
  436. function() {
  437. expect(function() {
  438. exponent_.equals();
  439. }).toThrow();
  440. expect(function() {
  441. exponent_.equals(undefined);
  442. }).toThrow();
  443. expect(function() {
  444. exponent_.equals(null);
  445. }).toThrow();
  446. expect(function() {
  447. exponent_.equals(nonMathObject_);
  448. }).toThrow();
  449. });
  450. });
  451. describe(
  452. 'create a new MathematicalArrayCompressor object that should ..',
  453. function() {
  454. it('throw an error when compressing an array of Zp group elements, using invalid input data',
  455. function() {
  456. expect(function() {
  457. mathArrayCompressor_.compressZpGroupElements();
  458. }).toThrow();
  459. expect(function() {
  460. mathArrayCompressor_.compressZpGroupElements(undefined);
  461. }).toThrow();
  462. expect(function() {
  463. mathArrayCompressor_.compressZpGroupElements(null);
  464. }).toThrow();
  465. expect(function() {
  466. mathArrayCompressor_.compressZpGroupElements(nonArray_);
  467. }).toThrow();
  468. expect(function() {
  469. mathArrayCompressor_.compressZpGroupElements(
  470. nonMathObjectArray_);
  471. }).toThrow();
  472. });
  473. it('throw an error when compressing an array of exponents, using invalid input data',
  474. function() {
  475. expect(function() {
  476. mathArrayCompressor_.compressExponents();
  477. }).toThrow();
  478. expect(function() {
  479. mathArrayCompressor_.compressExponents(undefined);
  480. }).toThrow();
  481. expect(function() {
  482. mathArrayCompressor_.compressExponents(null);
  483. }).toThrow();
  484. expect(function() {
  485. mathArrayCompressor_.compressExponents(nonArray_);
  486. }).toThrow();
  487. expect(function() {
  488. mathArrayCompressor_.compressExponents(nonMathObjectArray_);
  489. }).toThrow();
  490. });
  491. it('throw an error when compressing the trailing elements of an array of Zp group elements, using invalid input data',
  492. function() {
  493. expect(function() {
  494. mathArrayCompressor_.compressTrailingZpGroupElements(
  495. elements_);
  496. }).toThrow();
  497. expect(function() {
  498. mathArrayCompressor_.compressTrailingZpGroupElements(
  499. undefined, 2);
  500. }).toThrow();
  501. expect(function() {
  502. mathArrayCompressor_.compressTrailingZpGroupElements(null, 2);
  503. }).toThrow();
  504. expect(function() {
  505. mathArrayCompressor_.compressTrailingZpGroupElements(
  506. nonArray_, 2);
  507. }).toThrow();
  508. expect(function() {
  509. mathArrayCompressor_.compressTrailingZpGroupElements(
  510. nonMathObjectArray_, 2);
  511. }).toThrow();
  512. expect(function() {
  513. mathArrayCompressor_.compressTrailingZpGroupElements(
  514. elements_, undefined);
  515. }).toThrow();
  516. expect(function() {
  517. mathArrayCompressor_.compressTrailingZpGroupElements(
  518. elements_, null);
  519. }).toThrow();
  520. expect(function() {
  521. mathArrayCompressor_.compressTrailingZpGroupElements(
  522. elements_, nonNumber_);
  523. }).toThrow();
  524. expect(function() {
  525. mathArrayCompressor_.compressTrailingZpGroupElements(
  526. elements_, nonPositiveNumber_);
  527. }).toThrow();
  528. });
  529. it('throw an error when compressing the trailing exponents of an array of exponents, using invalid input data',
  530. function() {
  531. expect(function() {
  532. mathArrayCompressor_.compressTrailingExponents(exponents_);
  533. }).toThrow();
  534. expect(function() {
  535. mathArrayCompressor_.compressTrailingExponents(undefined, 2);
  536. }).toThrow();
  537. expect(function() {
  538. mathArrayCompressor_.compressTrailingExponents(null, 2);
  539. }).toThrow();
  540. expect(function() {
  541. mathArrayCompressor_.compressTrailingExponents(nonArray_, 2);
  542. }).toThrow();
  543. expect(function() {
  544. mathArrayCompressor_.compressTrailingExponents(
  545. nonMathObjectArray_, 2);
  546. }).toThrow();
  547. expect(function() {
  548. mathArrayCompressor_.compressTrailingExponents(
  549. exponents_, undefined);
  550. }).toThrow();
  551. expect(function() {
  552. mathArrayCompressor_.compressTrailingExponents(
  553. exponents_, null);
  554. }).toThrow();
  555. expect(function() {
  556. mathArrayCompressor_.compressTrailingExponents(
  557. exponents_, nonNumber_);
  558. }).toThrow();
  559. expect(function() {
  560. mathArrayCompressor_.compressTrailingExponents(
  561. exponents_, nonPositiveNumber_);
  562. }).toThrow();
  563. });
  564. });
  565. describe(
  566. 'create a new MathematicalRandomGenerator object that should ..',
  567. function() {
  568. it('throw an error when generating a random exponent, using invalid input data',
  569. function() {
  570. expect(function() {
  571. mathRandomGenerator_.nextExponent();
  572. }).toThrow();
  573. expect(function() {
  574. mathRandomGenerator_.nextExponent(undefined);
  575. }).toThrow();
  576. expect(function() {
  577. mathRandomGenerator_.nextExponent(null);
  578. }).toThrow();
  579. expect(function() {
  580. mathRandomGenerator_.nextExponent(nonMathObject_);
  581. }).toThrow();
  582. expect(function() {
  583. mathRandomGenerator_.nextExponent(
  584. group_, {secureRandomGenerator: nonObject_});
  585. }).toThrow();
  586. expect(function() {
  587. mathRandomGenerator_.nextExponent(
  588. group_, {secureRandomGenerator: emptyObject_});
  589. }).toThrow();
  590. expect(function() {
  591. mathRandomGenerator_.nextExponent(
  592. group_, {secureRandomGenerator: nonMathObject_});
  593. }).toThrow();
  594. });
  595. it('throw an error when generating a random Zp group element, using invalid input data',
  596. function() {
  597. expect(function() {
  598. mathRandomGenerator_.nextZpGroupElement();
  599. }).toThrow();
  600. expect(function() {
  601. mathRandomGenerator_.nextZpGroupElement(undefined);
  602. }).toThrow();
  603. expect(function() {
  604. mathRandomGenerator_.nextZpGroupElement(null);
  605. }).toThrow();
  606. expect(function() {
  607. mathRandomGenerator_.nextZpGroupElement(nonMathObject_);
  608. }).toThrow();
  609. expect(function() {
  610. mathRandomGenerator_.nextZpGroupElement(
  611. group_, {secureRandomGenerator: nonObject_});
  612. }).toThrow();
  613. expect(function() {
  614. mathRandomGenerator_.nextZpGroupElement(
  615. group_, {secureRandomGenerator: emptyObject_});
  616. }).toThrow();
  617. expect(function() {
  618. mathRandomGenerator_.nextZpGroupElement(
  619. group_, {secureRandomGenerator: nonMathObject_});
  620. }).toThrow();
  621. });
  622. it('throw an error when generating a random quadratic residue group, using invalid input data',
  623. function() {
  624. expect(function() {
  625. mathRandomGenerator_.nextQuadraticResidueGroup(16);
  626. }).toThrow();
  627. expect(function() {
  628. mathRandomGenerator_.nextQuadraticResidueGroup(
  629. undefined, minCertaintyLevel_);
  630. }).toThrow();
  631. expect(function() {
  632. mathRandomGenerator_.nextQuadraticResidueGroup(
  633. null, minCertaintyLevel_);
  634. }).toThrow();
  635. expect(function() {
  636. mathRandomGenerator_.nextQuadraticResidueGroup(
  637. nonNumber_, minCertaintyLevel_);
  638. }).toThrow();
  639. expect(function() {
  640. mathRandomGenerator_.nextQuadraticResidueGroup(
  641. nonPositiveNumber_, minCertaintyLevel_);
  642. }).toThrow();
  643. expect(function() {
  644. mathRandomGenerator_.nextQuadraticResidueGroup(
  645. tooSmallModulusBitLength_, minCertaintyLevel_);
  646. }).toThrow();
  647. expect(function() {
  648. mathRandomGenerator_.nextQuadraticResidueGroup(16, undefined);
  649. }).toThrow();
  650. expect(function() {
  651. mathRandomGenerator_.nextQuadraticResidueGroup(16, null);
  652. }).toThrow();
  653. expect(function() {
  654. mathRandomGenerator_.nextQuadraticResidueGroup(16, nonNumber_);
  655. }).toThrow();
  656. expect(function() {
  657. mathRandomGenerator_.nextQuadraticResidueGroup(
  658. 16, nonPositiveNumber_);
  659. }).toThrow();
  660. expect(function() {
  661. mathRandomGenerator_.nextQuadraticResidueGroup(
  662. 16, tooLowCertaintyLevel_);
  663. }).toThrow();
  664. expect(function() {
  665. mathRandomGenerator_.nextQuadraticResidueGroup(
  666. 16, minCertaintyLevel_,
  667. {secureRandomGenerator: nonObject_});
  668. }).toThrow();
  669. expect(function() {
  670. mathRandomGenerator_.nextQuadraticResidueGroup(
  671. 16, minCertaintyLevel_,
  672. {secureRandomGenerator: emptyObject_});
  673. }).toThrow();
  674. expect(function() {
  675. mathRandomGenerator_.nextQuadraticResidueGroup(
  676. 16, minCertaintyLevel_,
  677. {secureRandomGenerator: nonMathObject_});
  678. }).toThrow();
  679. });
  680. });
  681. describe('create a new MathematicalGroupHandler object that should ..', function() {
  682. it('throw an error when exponentiating an array of ZpGroupElement objects, using invalid input data',
  683. function() {
  684. expect(function() {
  685. mathGroupHandler_.exponentiateElements(group_);
  686. }).toThrow();
  687. expect(function() {
  688. mathGroupHandler_.exponentiateElements(group_, elements_);
  689. }).toThrow();
  690. expect(function() {
  691. mathGroupHandler_.exponentiateElements(
  692. undefined, elements_, exponent_);
  693. }).toThrow();
  694. expect(function() {
  695. mathGroupHandler_.exponentiateElements(null, elements_, exponent_);
  696. }).toThrow();
  697. expect(function() {
  698. mathGroupHandler_.exponentiateElements(
  699. nonMathObject_, elements_, exponent_);
  700. }).toThrow();
  701. expect(function() {
  702. mathGroupHandler_.exponentiateElements(
  703. group_, undefined, exponent_);
  704. }).toThrow();
  705. expect(function() {
  706. mathGroupHandler_.exponentiateElements(group_, null, exponent_);
  707. }).toThrow();
  708. expect(function() {
  709. mathGroupHandler_.exponentiateElements(
  710. group_, nonArray_, exponent_);
  711. }).toThrow();
  712. expect(function() {
  713. mathGroupHandler_.exponentiateElements(
  714. group_, nonMathObjectArray_, exponent_);
  715. }).toThrow();
  716. expect(function() {
  717. mathGroupHandler_.exponentiateElements(
  718. group_, elements_, undefined);
  719. }).toThrow();
  720. expect(function() {
  721. mathGroupHandler_.exponentiateElements(
  722. group_, elements_, nonMathObject_);
  723. }).toThrow();
  724. expect(function() {
  725. mathGroupHandler_.exponentiateElements(
  726. group_, elements_, exponentFromAnotherGroup_);
  727. }).toThrow();
  728. expect(function() {
  729. mathGroupHandler_.exponentiateElements(
  730. group_, elements_, exponent_, null);
  731. }).toThrow();
  732. expect(function() {
  733. mathGroupHandler_.exponentiateElements(
  734. group_, elements_, exponent_, nonBoolean_);
  735. }).toThrow();
  736. });
  737. it('throw an error when dividing one array of ZpGroupElement objects with another, using invalid input data',
  738. function() {
  739. expect(function() {
  740. mathGroupHandler_.divideElements(elements_);
  741. }).toThrow();
  742. expect(function() {
  743. mathGroupHandler_.divideElements(undefined, elements_);
  744. }).toThrow();
  745. expect(function() {
  746. mathGroupHandler_.divideElements(null, elements_);
  747. }).toThrow();
  748. expect(function() {
  749. mathGroupHandler_.divideElements(nonArray_, elements_);
  750. }).toThrow();
  751. expect(function() {
  752. mathGroupHandler_.divideElements(nonMathObjectArray_, elements_);
  753. }).toThrow();
  754. expect(function() {
  755. mathGroupHandler_.divideElements(elements_, undefined);
  756. }).toThrow();
  757. expect(function() {
  758. mathGroupHandler_.divideElements(elements_, null);
  759. }).toThrow();
  760. expect(function() {
  761. mathGroupHandler_.divideElements(elements_, nonArray_);
  762. }).toThrow();
  763. expect(function() {
  764. mathGroupHandler_.divideElements(elements_, nonMathObjectArray_);
  765. }).toThrow();
  766. expect(function() {
  767. mathGroupHandler_.divideElements(elements_, [element_]);
  768. }).toThrow();
  769. });
  770. it('throw an error when checking for group membership, using invalid input data',
  771. function() {
  772. expect(function() {
  773. mathGroupHandler_.checkGroupMembership(undefined, elements_);
  774. }).toThrow();
  775. expect(function() {
  776. mathGroupHandler_.checkGroupMembership(null, elements_);
  777. }).toThrow();
  778. expect(function() {
  779. mathGroupHandler_.checkGroupMembership(nonMathObject_, elements_);
  780. }).toThrow();
  781. expect(function() {
  782. mathGroupHandler_.checkGroupMembership(group_, undefined);
  783. }).toThrow();
  784. expect(function() {
  785. mathGroupHandler_.checkGroupMembership(group_, null);
  786. }).toThrow();
  787. expect(function() {
  788. mathGroupHandler_.checkGroupMembership(group_, nonArray_);
  789. }).toThrow();
  790. expect(function() {
  791. mathGroupHandler_.checkGroupMembership(
  792. group_, nonMathObjectArray_);
  793. }).toThrow();
  794. });
  795. it('throw an error when extracting Zp group elements from an array of BigIntegers, using invalid input data',
  796. function() {
  797. expect(function() {
  798. mathGroupHandler_.extractGroupMembers(
  799. group_, multiGroupElementValues_);
  800. }).toThrow();
  801. expect(function() {
  802. mathGroupHandler_.extractGroupMembers(
  803. undefined, multiGroupElementValues_, 2);
  804. }).toThrow();
  805. expect(function() {
  806. mathGroupHandler_.extractGroupMembers(
  807. null, multiGroupElementValues_, 2);
  808. }).toThrow();
  809. expect(function() {
  810. mathGroupHandler_.extractGroupMembers(
  811. nonMathObject_, multiGroupElementValues_, 2);
  812. }).toThrow();
  813. expect(function() {
  814. mathGroupHandler_.extractGroupMembers(group_, undefined, 2);
  815. }).toThrow();
  816. expect(function() {
  817. mathGroupHandler_.extractGroupMembers(group_, null, 2);
  818. }).toThrow();
  819. expect(function() {
  820. mathGroupHandler_.extractGroupMembers(group_, nonArray_, 2);
  821. }).toThrow();
  822. expect(function() {
  823. mathGroupHandler_.extractGroupMembers(
  824. group_, nonMathObjectArray_, 2);
  825. }).toThrow();
  826. expect(function() {
  827. mathGroupHandler_.extractGroupMembers(
  828. group_, multiGroupElementValues_, undefined);
  829. }).toThrow();
  830. expect(function() {
  831. mathGroupHandler_.extractGroupMembers(
  832. group_, multiGroupElementValues_, null);
  833. }).toThrow();
  834. expect(function() {
  835. mathGroupHandler_.extractGroupMembers(
  836. group_, multiGroupElementValues_, nonNumber_);
  837. }).toThrow();
  838. expect(function() {
  839. mathGroupHandler_.extractGroupMembers(
  840. group_, multiGroupElementValues_, nonPositiveNumber_);
  841. }).toThrow();
  842. expect(function() {
  843. mathGroupHandler_.extractGroupMembers(
  844. group_, multiGroupElementValues_, tooLargeNumMembersRequired_);
  845. }).toThrow();
  846. });
  847. it('throw an error when finding the smallest generator for a given Zp subgroup, using invalid input data',
  848. function() {
  849. expect(function() {
  850. mathGroupHandler_.findMinGenerator(p_);
  851. }).toThrow();
  852. expect(function() {
  853. mathGroupHandler_.findMinGenerator(undefined, q_);
  854. }).toThrow();
  855. expect(function() {
  856. mathGroupHandler_.findMinGenerator(null, q_);
  857. }).toThrow();
  858. expect(function() {
  859. mathGroupHandler_.findMinGenerator(nonMathObject_, q_);
  860. }).toThrow();
  861. expect(function() {
  862. mathGroupHandler_.findMinGenerator(tooSmallModulus_, q_);
  863. }).toThrow();
  864. expect(function() {
  865. mathGroupHandler_.findMinGenerator(p_, undefined);
  866. }).toThrow();
  867. expect(function() {
  868. mathGroupHandler_.findMinGenerator(p_, null);
  869. }).toThrow();
  870. expect(function() {
  871. mathGroupHandler_.findMinGenerator(p_, nonMathObject_);
  872. }).toThrow();
  873. expect(function() {
  874. mathGroupHandler_.findMinGenerator(p_, tooSmallOrder_);
  875. }).toThrow();
  876. expect(function() {
  877. mathGroupHandler_.findMinGenerator(p_, tooLargeOrder_);
  878. }).toThrow();
  879. });
  880. });
  881. });
  882. });