public-key.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 codec = require('scytl-codec');
  11. module.exports = ElGamalPublicKey;
  12. /**
  13. * @class ElGamalPublicKey
  14. * @classdesc Encapsulates an ElGamal public key. To instantiate this object,
  15. * use the method {@link ElGamalCryptographyService.newPublicKey}.
  16. * @property {ZpSubgroup} group The Zp subgroup to which the Zp group elements
  17. * of this public key belong.
  18. * @property {ZpGroupElement[]} elements The Zp group elements that comprise
  19. * this public key.
  20. */
  21. function ElGamalPublicKey(group, elements) {
  22. this.group = group;
  23. Object.freeze(this.elements = elements);
  24. Object.freeze(this);
  25. }
  26. ElGamalPublicKey.prototype = {
  27. /**
  28. * Serializes this object into a JSON string representation.
  29. * <p>
  30. * <b>IMPORTANT:</b> This serialization must be exactly the same as the
  31. * corresponding serialization in the library <code>cryptoLib</code>,
  32. * implemented in Java, since the two libraries are expected to communicates
  33. * with each other via these serializations.
  34. *
  35. * @function toJson
  36. * @memberof ElGamalPublicKey
  37. * @returns {string} The JSON string representation of this object.
  38. */
  39. toJson: function() {
  40. var gB64 = codec.base64Encode(this.group.generator.value);
  41. var pB64 = codec.base64Encode(this.group.p);
  42. var qB64 = codec.base64Encode(this.group.q);
  43. var elementsB64 = [];
  44. for (var i = 0; i < this.elements.length; i++) {
  45. elementsB64.push(codec.base64Encode(this.elements[i].value));
  46. }
  47. return JSON.stringify({
  48. publicKey:
  49. {zpSubgroup: {g: gB64, p: pB64, q: qB64}, elements: elementsB64}
  50. });
  51. }
  52. };