2
0

homomorphic.keypair.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. describe('ElGamalKeyFactory', function() {
  9. 'use strict';
  10. it('should be able to create Key Pairs', function() {
  11. cryptolib('homomorphic.keypair', 'commons.utils', function(box) {
  12. var converters = new box.commons.utils.Converters();
  13. var keyFactory = new box.homomorphic.keypair.factory.KeyFactory();
  14. var serializedElGamalPublicKey =
  15. '{"publicKey":{"zpSubgroup":{"p":"Fw==","q":"Cw==","g":"Ag=="},"elements":["Ag==","Ag=="]}}';
  16. var serializedElGamalPublicKeyB64 =
  17. converters.base64Encode(serializedElGamalPublicKey);
  18. var elGamalPublicKey =
  19. keyFactory.createPublicKey(serializedElGamalPublicKeyB64);
  20. var serializedElGamalPrivateKey =
  21. '{"privateKey":{"zpSubgroup":{"p":"Fw==","q":"Cw==","g":"Ag=="},"exponents":["BA==","BQ=="]}}';
  22. var serializedElGamalPrivateKeyB64 =
  23. converters.base64Encode(serializedElGamalPrivateKey);
  24. var elGamalPrivateKey =
  25. keyFactory.createPrivateKey(serializedElGamalPrivateKeyB64);
  26. var elGamalKeyPair =
  27. keyFactory.createKeyPair(elGamalPublicKey, elGamalPrivateKey);
  28. expect(elGamalKeyPair).toBeDefined();
  29. });
  30. });
  31. it('should be able to deserialize an ElGamalPublicKey', function() {
  32. cryptolib('homomorphic.keypair', 'commons', function(box) {
  33. var converters = new box.commons.utils.Converters();
  34. var keyFactory = new box.homomorphic.keypair.factory.KeyFactory();
  35. var p = new box.forge.jsbn.BigInteger('23');
  36. var generator = new box.forge.jsbn.BigInteger('2');
  37. var zpSubgroup =
  38. box.commons.mathematical.groupUtils.buildZpSubgroupFromPAndG(
  39. p, generator);
  40. var serializedElGamalPublicKey =
  41. '{"publicKey":{"zpSubgroup":{"p":"Fw==","q":"Cw==","g":"Ag=="},"elements":["Ag==","Ag=="]}}';
  42. var value = new box.forge.jsbn.BigInteger('' + 2);
  43. var groupElement = new box.commons.mathematical.ZpGroupElement(
  44. value, zpSubgroup.getP(), zpSubgroup.getQ());
  45. var serializedElGamalPublicKeyB64 =
  46. converters.base64Encode(serializedElGamalPublicKey);
  47. var elGamalPublicKey =
  48. keyFactory.createPublicKey(serializedElGamalPublicKeyB64);
  49. expect(elGamalPublicKey).toBeDefined();
  50. expect(elGamalPublicKey.getGroup().equals(zpSubgroup)).toBeTruthy();
  51. expect(elGamalPublicKey.getGroupElementsArray().length).toBe(2);
  52. expect(elGamalPublicKey.getGroupElementsArray()[0].equals(groupElement))
  53. .toBeTruthy();
  54. });
  55. });
  56. it('should be able to deserialize an ElGamalPrivateKey', function() {
  57. cryptolib('homomorphic.keypair', 'commons', function(box) {
  58. var converters = new box.commons.utils.Converters();
  59. var keyFactory = new box.homomorphic.keypair.factory.KeyFactory();
  60. var p = new box.forge.jsbn.BigInteger('23');
  61. var generator = new box.forge.jsbn.BigInteger('2');
  62. var zpSubgroup =
  63. box.commons.mathematical.groupUtils.buildZpSubgroupFromPAndG(
  64. p, generator);
  65. var serializedElGamalPrivateKey =
  66. '{"privateKey":{"zpSubgroup":{"p":"Fw==","q":"Cw==","g":"Ag=="},"exponents":["Ag==","Ag=="]}}';
  67. var serializedElGamalPrivateKeyB64 =
  68. converters.base64Encode(serializedElGamalPrivateKey);
  69. var elGamalPrivateKey =
  70. keyFactory.createPrivateKey(serializedElGamalPrivateKeyB64);
  71. expect(elGamalPrivateKey).toBeDefined();
  72. expect(elGamalPrivateKey.getGroup().equals(zpSubgroup)).toBeTruthy();
  73. expect(elGamalPrivateKey.getExponentsArray().length).toBe(2);
  74. expect(elGamalPrivateKey.getExponentsArray()[0].getValue().toString())
  75. .toBe('2');
  76. });
  77. });
  78. });