2
0

pre-computation.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 = ZeroKnowledgeProofPreComputation;
  12. /**
  13. * @class ZeroKnowledgeProofPreComputation
  14. * @classdesc Encapsulates a zero-knowledge proof of knowledge pre-computation.
  15. * This object is instantiated by the method {@link
  16. * ZeroKnowledgeProofService.newPreComputation} or internally by the
  17. * <code>preCompute</code> method of any zero-knowledge proof of
  18. * knowledge prover object.
  19. * @property {Exponent[]} exponents The array of randomly generated exponents
  20. * that comprise the pre-computation.
  21. * @property {ZpGroupElement[]} phiOutputs The array of PHI function output
  22. * elements that comprise the pre-computation.
  23. */
  24. function ZeroKnowledgeProofPreComputation(exponents, phiOutputs) {
  25. Object.freeze(this.exponents = exponents);
  26. Object.freeze(this.phiOutputs = phiOutputs);
  27. Object.freeze(this);
  28. }
  29. ZeroKnowledgeProofPreComputation.prototype = {
  30. /**
  31. * Serializes this object into a JSON string representation.
  32. * <p>
  33. * <b>IMPORTANT:</b> This serialization must be exactly the same as the
  34. * corresponding serialization in the library <code>cryptoLib</code>,
  35. * implemented in Java, since the two libraries are expected to communicates
  36. * with each other via these serializations.
  37. *
  38. * @function toJson
  39. * @memberof ZeroKnowledgeProofPreComputation
  40. * @returns {string} The JSON string representation of this object.
  41. */
  42. toJson: function() {
  43. var pB64 = codec.base64Encode(this.phiOutputs[0].p);
  44. var qB64 = codec.base64Encode(this.phiOutputs[0].q);
  45. var exponentValuesB64 = [];
  46. for (var i = 0; i < this.exponents.length; i++) {
  47. exponentValuesB64[i] = codec.base64Encode(this.exponents[i].value);
  48. }
  49. var phiOutputValuesB64 = [];
  50. for (var j = 0; j < this.phiOutputs.length; j++) {
  51. phiOutputValuesB64[j] = codec.base64Encode(this.phiOutputs[j].value);
  52. }
  53. return JSON.stringify({
  54. preComputed: {
  55. p: pB64,
  56. q: qB64,
  57. exponents: exponentValuesB64,
  58. phiOutputs: phiOutputValuesB64
  59. }
  60. });
  61. }
  62. };