2
0

encrypted-elements.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 = ElGamalEncryptedElements;
  12. /**
  13. * @class ElGamalEncryptedElements
  14. * @classdesc Encapsulates the components of the ElGamal encryption of some Zp
  15. * group elements or the pre-computation of such an encryption. To
  16. * instantiate this object, use the method {@link
  17. * ElGamalCryptographyService.newEncryptedElements}.
  18. * @property {ZpGroupElement} gamma The gamma Zp group element that comprises
  19. * the ElGamal encryption or pre-computation.
  20. * @property {ZpGroupElement[]} phis The phi Zp group elements that comprise the
  21. * ElGamal encryption or pre-computation.
  22. * @property {Exponent} [secret=undefined] The secret exponent that comprises
  23. * the encryption or pre-computation. <b>CAUTION:</b> Provide only
  24. * when the secret is really needed later (e.g. for zero-knowledge
  25. * proof generation).
  26. */
  27. function ElGamalEncryptedElements(gamma, phis, secret) {
  28. this.gamma = gamma;
  29. Object.freeze(this.phis = phis);
  30. this.secret = secret;
  31. Object.freeze(this);
  32. }
  33. ElGamalEncryptedElements.prototype = {
  34. /**
  35. * Serializes this object into a JSON string representation.
  36. * <p>
  37. * <b>IMPORTANT:</b> This serialization must be exactly the same as the
  38. * corresponding serialization in the library <code>cryptoLib</code>,
  39. * implemented in Java, since the two libraries are expected to communicate
  40. * with each other via these serializations.
  41. * <p>
  42. * <b>NOTE:</b> For security reasons, the secret exponent of the ElGamal
  43. * encryption or pre-computation is not included in this serialization.
  44. *
  45. * @function toJson
  46. * @memberof ElGamalEncryptedElements
  47. * @returns {string} The JSON string representation of this object.
  48. */
  49. toJson: function() {
  50. var pB64 = codec.base64Encode(this.gamma.p);
  51. var qB64 = codec.base64Encode(this.gamma.q);
  52. var gammaB64 = codec.base64Encode(this.gamma.value);
  53. var phisB64 = [];
  54. for (var i = 0; i < this.phis.length; i++) {
  55. phisB64[i] = codec.base64Encode(this.phis[i].value);
  56. }
  57. return JSON.stringify({
  58. ciphertext: {
  59. p: pB64,
  60. q: qB64,
  61. gamma: gammaB64,
  62. phis: phisB64,
  63. }
  64. });
  65. }
  66. };