Accumulator.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ACCUMULATOR_H_
  3. #define ACCUMULATOR_H_
  4. namespace libzerocoin {
  5. /**
  6. * \brief Implementation of the RSA-based accumulator.
  7. **/
  8. class Accumulator {
  9. public:
  10. /**
  11. * @brief Construct an Accumulator from a stream.
  12. * @param p An AccumulatorAndProofParams object containing global parameters
  13. * @param d the denomination of coins we are accumulating
  14. * @throw Zerocoin exception in case of invalid parameters
  15. **/
  16. template<typename Stream>
  17. Accumulator(const AccumulatorAndProofParams* p, Stream& strm): params(p) {
  18. strm >> *this;
  19. }
  20. template<typename Stream>
  21. Accumulator(const Params* p, Stream& strm) {
  22. strm >> *this;
  23. this->params = &(p->accumulatorParams);
  24. }
  25. /**
  26. * @brief Construct an Accumulator from a Params object.
  27. * @param p A Params object containing global parameters
  28. * @param d the denomination of coins we are accumulating
  29. * @throw Zerocoin exception in case of invalid parameters
  30. **/
  31. Accumulator(const AccumulatorAndProofParams* p, const CoinDenomination d = ZQ_PEDERSEN);
  32. Accumulator(const Params* p, const CoinDenomination d = ZQ_PEDERSEN);
  33. /**
  34. * Accumulate a coin into the accumulator. Validates
  35. * the coin prior to accumulation.
  36. *
  37. * @param coin A PublicCoin to accumulate.
  38. *
  39. * @throw Zerocoin exception if the coin is not valid.
  40. *
  41. **/
  42. void accumulate(const PublicCoin &coin);
  43. const CoinDenomination getDenomination() const;
  44. /** Get the accumulator result
  45. *
  46. * @return a CBigNum containing the result.
  47. */
  48. const CBigNum& getValue() const;
  49. // /**
  50. // * Used to set the accumulator value
  51. // *
  52. // * Use this to handle accumulator checkpoints
  53. // * @param b the value to set the accumulator to.
  54. // * @throw A ZerocoinException if the accumulator value is invalid.
  55. // */
  56. // void setValue(CBigNum &b); // shouldn't this be a constructor?
  57. /** Used to accumulate a coin
  58. *
  59. * @param c the coin to accumulate
  60. * @return a refrence to the updated accumulator.
  61. */
  62. Accumulator& operator +=(const PublicCoin& c);
  63. bool operator==(const Accumulator rhs) const;
  64. IMPLEMENT_SERIALIZE
  65. (
  66. READWRITE(value);
  67. READWRITE(denomination);
  68. )
  69. private:
  70. const AccumulatorAndProofParams* params;
  71. CBigNum value;
  72. // Denomination is stored as an INT because storing
  73. // and enum raises amigiuities in the serialize code //FIXME if possible
  74. int denomination;
  75. };
  76. /**A witness that a PublicCoin is in the accumulation of a set of coins
  77. *
  78. */
  79. class AccumulatorWitness {
  80. public:
  81. template<typename Stream>
  82. AccumulatorWitness(const Params* p, Stream& strm): params(p) {
  83. strm >> *this;
  84. }
  85. /** Construct's a witness. You must add all elements after the witness
  86. * @param p pointer to params
  87. * @param checkpoint the last known accumulator value before the element was added
  88. * @param coin the coin we want a witness to
  89. */
  90. AccumulatorWitness(const Params* p, const Accumulator& checkpoint, const PublicCoin coin);
  91. /** Adds element to the set whose's accumulation we are proving coin is a member of
  92. *
  93. * @param c the coin to add
  94. */
  95. void AddElement(const PublicCoin& c);
  96. /**
  97. *
  98. * @return the value of the witness
  99. */
  100. const CBigNum& getValue() const;
  101. /** Checks that this is a witness to the accumulation of coin
  102. * @param a the accumulator we are checking against.
  103. * @param publicCoin the coin we're providing a witness for
  104. * @return True if the witness computation validates
  105. */
  106. bool VerifyWitness(const Accumulator& a, const PublicCoin &publicCoin) const;
  107. /**
  108. * Adds rhs to the set whose's accumulation ware proving coin is a member of
  109. * @param rhs the PublicCoin to add
  110. * @return
  111. */
  112. AccumulatorWitness& operator +=(const PublicCoin& rhs);
  113. private:
  114. const Params* params;
  115. Accumulator witness;
  116. const PublicCoin element;
  117. };
  118. } /* namespace libzerocoin */
  119. #endif /* ACCUMULATOR_H_ */