Accumulator.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include <sstream>
  3. #include "Zerocoin.h"
  4. namespace libzerocoin {
  5. //Accumulator class
  6. Accumulator::Accumulator(const AccumulatorAndProofParams* p, const CoinDenomination d): params(p), denomination(d) {
  7. if (!(params->initialized)) {
  8. throw ZerocoinException("Invalid parameters for accumulator");
  9. }
  10. this->value = this->params->accumulatorBase;
  11. }
  12. Accumulator::Accumulator(const Params* p, const CoinDenomination d) {
  13. this->params = &(p->accumulatorParams);
  14. this->denomination = d;
  15. if (!(params->initialized)) {
  16. throw ZerocoinException("Invalid parameters for accumulator");
  17. }
  18. this->value = this->params->accumulatorBase;
  19. }
  20. void Accumulator::accumulate(const PublicCoin& coin) {
  21. // Make sure we're initialized
  22. if(!(this->value)) {
  23. throw ZerocoinException("Accumulator is not initialized");
  24. }
  25. if(this->denomination != coin.getDenomination()) {
  26. //std::stringstream msg;
  27. std::string msg;
  28. msg = "Wrong denomination for coin. Expected coins of denomination: ";
  29. msg += this->denomination;
  30. msg += ". Instead, got a coin of denomination: ";
  31. msg += coin.getDenomination();
  32. throw std::invalid_argument(msg);
  33. }
  34. if(coin.validate()) {
  35. // Compute new accumulator = "old accumulator"^{element} mod N
  36. this->value = this->value.pow_mod(coin.getValue(), this->params->accumulatorModulus);
  37. } else {
  38. throw std::invalid_argument("Coin is not valid");
  39. }
  40. }
  41. const CoinDenomination Accumulator::getDenomination() const {
  42. return static_cast<CoinDenomination> (this->denomination);
  43. }
  44. const CBigNum& Accumulator::getValue() const {
  45. return this->value;
  46. }
  47. Accumulator& Accumulator::operator += (const PublicCoin& c) {
  48. this->accumulate(c);
  49. return *this;
  50. }
  51. bool Accumulator::operator == (const Accumulator rhs) const {
  52. return this->value == rhs.value;
  53. }
  54. //AccumulatorWitness class
  55. AccumulatorWitness::AccumulatorWitness(const Params* p,
  56. const Accumulator& checkpoint, const PublicCoin coin): params(p), witness(checkpoint), element(coin) {
  57. }
  58. void AccumulatorWitness::AddElement(const PublicCoin& c) {
  59. if(element != c) {
  60. witness += c;
  61. }
  62. }
  63. const CBigNum& AccumulatorWitness::getValue() const {
  64. return this->witness.getValue();
  65. }
  66. bool AccumulatorWitness::VerifyWitness(const Accumulator& a, const PublicCoin &publicCoin) const {
  67. Accumulator temp(witness);
  68. temp += element;
  69. return (temp == a && this->element == publicCoin);
  70. }
  71. AccumulatorWitness& AccumulatorWitness::operator +=(
  72. const PublicCoin& rhs) {
  73. this->AddElement(rhs);
  74. return *this;
  75. }
  76. } /* namespace libzerocoin */