proof.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 = ZeroKnowledgeProof;
  12. /**
  13. * @class ZeroKnowledgeProof
  14. * @classdesc Encapsulates a zero-knowledge proof of knowledge. This object is
  15. * instantiated by the method
  16. * {@link ZeroKnowledgeProofService.newProof} or internally by the
  17. * <code>prove</code> method of any zero-knowledge proof of
  18. * knowledge prover object.
  19. * @property {Exponent} hash The hash of the zero-knowledge proof of knowledge.
  20. * @property {Exponent[]} values The values of the zero-knowledge proof of
  21. * knowledge.
  22. */
  23. function ZeroKnowledgeProof(hash, values) {
  24. this.hash = hash;
  25. Object.freeze(this.values = values);
  26. Object.freeze(this);
  27. }
  28. ZeroKnowledgeProof.prototype = {
  29. /**
  30. * Checks if this zero-knowledge proof of knowledge is equal to the
  31. * zero-knowledge proof of knowledge provided as input.
  32. *
  33. * @function equals
  34. * @memberof ZeroKnowledgeProof
  35. * @param {ZeroKnowledgeProof}
  36. * proof The zero-knowledge proof of knowledge that should be
  37. * checked against this zero-knowledge proof of knowledge for
  38. * equality.
  39. * @returns {boolean} <code>true</code> if the equality holds,
  40. * <code>false</code> otherwise.
  41. */
  42. equals: function(proof) {
  43. return proof.hash.equals(this.hash && proof.values.equals(this.values));
  44. },
  45. /**
  46. * Serializes this object into a JSON string representation.
  47. * <p>
  48. * <b>IMPORTANT:</b> This serialization must be exactly the same as the
  49. * corresponding serialization in the library <code>cryptoLib</code>,
  50. * implemented in Java, since the two libraries are expected to communicates
  51. * with each other via these serializations.
  52. *
  53. * @function toJson
  54. * @memberof ZeroKnowledgeProof
  55. * @returns {string} The JSON string representation of this object.
  56. */
  57. toJson: function() {
  58. var qB64 = codec.base64Encode(this.hash.q);
  59. var hashB64 = codec.base64Encode(this.hash.value);
  60. var valuesB64 = [];
  61. for (var i = 0; i < this.values.length; i++) {
  62. valuesB64[i] = codec.base64Encode(this.values[i].value);
  63. }
  64. return JSON.stringify(
  65. {zkProof: {q: qB64, hash: hashB64, values: valuesB64}});
  66. }
  67. };