Commitment.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef COMMITMENT_H_
  3. #define COMMITMENT_H_
  4. #include "Params.h"
  5. #include "../serialize.h"
  6. // We use a SHA256 hash for our PoK challenges. Update the following
  7. // if we ever change hash functions.
  8. #define COMMITMENT_EQUALITY_CHALLENGE_SIZE 256
  9. // A 512-bit security parameter for the statistical ZK PoK.
  10. #define COMMITMENT_EQUALITY_SECMARGIN 512
  11. namespace libzerocoin {
  12. /**
  13. * A commitment, complete with contents and opening randomness.
  14. * These should remain secret. Publish only the commitment value.
  15. */
  16. class Commitment {
  17. public:
  18. /**Generates a Pedersen commitment to the given value.
  19. *
  20. * @param p the group parameters for the coin
  21. * @param value the value to commit to
  22. */
  23. Commitment(const IntegerGroupParams* p, const CBigNum& value);
  24. const CBigNum& getCommitmentValue() const;
  25. const CBigNum& getRandomness() const;
  26. const CBigNum& getContents() const;
  27. private:
  28. const IntegerGroupParams *params;
  29. CBigNum commitmentValue;
  30. CBigNum randomness;
  31. const CBigNum contents;
  32. IMPLEMENT_SERIALIZE
  33. (
  34. READWRITE(commitmentValue);
  35. READWRITE(randomness);
  36. READWRITE(contents);
  37. )
  38. };
  39. /**Proof that two commitments open to the same value.
  40. *
  41. */
  42. class CommitmentProofOfKnowledge {
  43. public:
  44. CommitmentProofOfKnowledge(const IntegerGroupParams* ap, const IntegerGroupParams* bp);
  45. /** Generates a proof that two commitments, a and b, open to the same value.
  46. *
  47. * @param ap the IntegerGroup for commitment a
  48. * @param bp the IntegerGroup for commitment b
  49. * @param a the first commitment
  50. * @param b the second commitment
  51. */
  52. CommitmentProofOfKnowledge(const IntegerGroupParams* aParams, const IntegerGroupParams* bParams, const Commitment& a, const Commitment& b);
  53. //FIXME: is it best practice that this is here?
  54. template<typename Stream>
  55. CommitmentProofOfKnowledge(const IntegerGroupParams* aParams,
  56. const IntegerGroupParams* bParams, Stream& strm): ap(aParams), bp(bParams)
  57. {
  58. strm >> *this;
  59. }
  60. const CBigNum calculateChallenge(const CBigNum& a, const CBigNum& b, const CBigNum &commitOne, const CBigNum &commitTwo) const;
  61. /**Verifies the proof
  62. *
  63. * @return true if the proof is valid.
  64. */
  65. /**Verifies the proof of equality of the two commitments
  66. *
  67. * @param A value of commitment one
  68. * @param B value of commitment two
  69. * @return
  70. */
  71. bool Verify(const CBigNum& A, const CBigNum& B) const;
  72. IMPLEMENT_SERIALIZE
  73. (
  74. READWRITE(S1);
  75. READWRITE(S2);
  76. READWRITE(S3);
  77. READWRITE(challenge);
  78. )
  79. private:
  80. const IntegerGroupParams *ap, *bp;
  81. CBigNum S1, S2, S3, challenge;
  82. };
  83. } /* namespace libzerocoin */
  84. #endif /* COMMITMENT_H_ */