2 * @file Accumulator.cpp
4 * @brief Accumulator and AccumulatorWitness classes for the Zerocoin library.
6 * @author Ian Miers, Christina Garman and Matthew Green
9 * @copyright Copyright 2013 Ian Miers, Christina Garman and Matthew Green
10 * @license This project is released under the MIT license.
16 namespace libzerocoin {
19 Accumulator::Accumulator(const AccumulatorAndProofParams* p, const CoinDenomination d): params(p), denomination(d) {
20 if (!(params->initialized)) {
21 throw ZerocoinException("Invalid parameters for accumulator");
24 this->value = this->params->accumulatorBase;
27 Accumulator::Accumulator(const Params* p, const CoinDenomination d) {
28 this->params = &(p->accumulatorParams);
29 this->denomination = d;
31 if (!(params->initialized)) {
32 throw ZerocoinException("Invalid parameters for accumulator");
35 this->value = this->params->accumulatorBase;
38 void Accumulator::accumulate(const PublicCoin& coin) {
39 // Make sure we're initialized
41 throw ZerocoinException("Accumulator is not initialized");
44 if(this->denomination != coin.getDenomination()) {
45 //std::stringstream msg;
47 msg = "Wrong denomination for coin. Expected coins of denomination: ";
48 msg += this->denomination;
49 msg += ". Instead, got a coin of denomination: ";
50 msg += coin.getDenomination();
51 throw std::invalid_argument(msg);
55 // Compute new accumulator = "old accumulator"^{element} mod N
56 this->value = this->value.pow_mod(coin.getValue(), this->params->accumulatorModulus);
58 throw std::invalid_argument("Coin is not valid");
62 const CoinDenomination Accumulator::getDenomination() const {
63 return static_cast<CoinDenomination> (this->denomination);
66 const Bignum& Accumulator::getValue() const {
70 Accumulator& Accumulator::operator += (const PublicCoin& c) {
75 bool Accumulator::operator == (const Accumulator rhs) const {
76 return this->value == rhs.value;
79 //AccumulatorWitness class
80 AccumulatorWitness::AccumulatorWitness(const Params* p,
81 const Accumulator& checkpoint, const PublicCoin coin): params(p), witness(checkpoint), element(coin) {
84 void AccumulatorWitness::AddElement(const PublicCoin& c) {
90 const Bignum& AccumulatorWitness::getValue() const {
91 return this->witness.getValue();
94 bool AccumulatorWitness::VerifyWitness(const Accumulator& a, const PublicCoin &publicCoin) const {
95 Accumulator temp(witness);
97 return (temp == a && this->element == publicCoin);
100 AccumulatorWitness& AccumulatorWitness::operator +=(
101 const PublicCoin& rhs) {
102 this->AddElement(rhs);
106 } /* namespace libzerocoin */