17cd58ff3a8de16606c7ec6b158b49866b783ee9
[novacoin.git] / src / zerocoin / Accumulator.h
1 /**
2  * @file       Accumulator.h
3  *
4  * @brief      Accumulator and AccumulatorWitness classes for the Zerocoin library.
5  *
6  * @author     Ian Miers, Christina Garman and Matthew Green
7  * @date       June 2013
8  *
9  * @copyright  Copyright 2013 Ian Miers, Christina Garman and Matthew Green
10  * @license    This project is released under the MIT license.
11  **/
12 #ifndef ACCUMULATOR_H_
13 #define ACCUMULATOR_H_
14
15 namespace libzerocoin {
16 /**
17  * \brief Implementation of the RSA-based accumulator.
18  **/
19
20 class Accumulator {
21 public:
22
23         /**
24          * @brief      Construct an Accumulator from a stream.
25          * @param p    An AccumulatorAndProofParams object containing global parameters
26          * @param d    the denomination of coins we are accumulating
27          * @throw      Zerocoin exception in case of invalid parameters
28          **/
29         template<typename Stream>
30         Accumulator(const AccumulatorAndProofParams* p, Stream& strm): params(p) {
31                 strm >> *this;
32         }
33
34         template<typename Stream>
35         Accumulator(const Params* p, Stream& strm) {
36                 strm >> *this;
37                 this->params = &(p->accumulatorParams);
38         }
39
40         /**
41          * @brief      Construct an Accumulator from a Params object.
42          * @param p    A Params object containing global parameters
43          * @param d the denomination of coins we are accumulating
44          * @throw     Zerocoin exception in case of invalid parameters
45          **/
46         Accumulator(const AccumulatorAndProofParams* p, const CoinDenomination d = ZQ_LOVELACE);
47
48         Accumulator(const Params* p, const CoinDenomination d = ZQ_LOVELACE);
49
50         /**
51          * Accumulate a coin into the accumulator. Validates
52          * the coin prior to accumulation.
53          *
54          * @param coin  A PublicCoin to accumulate.
55          *
56          * @throw               Zerocoin exception if the coin is not valid.
57          *
58          **/
59         void accumulate(const PublicCoin &coin);
60
61         const CoinDenomination getDenomination() const;
62         /** Get the accumulator result
63          *
64          * @return a Bignum containing the result.
65          */
66         const Bignum& getValue() const;
67
68
69         // /**
70         //  * Used to set the accumulator value
71         //  *
72         //  * Use this to handle accumulator checkpoints
73         //  * @param b the value to set the accumulator to.
74         //  * @throw  A ZerocoinException if the accumulator value is invalid.
75         //  */
76         // void setValue(Bignum &b); // shouldn't this be a constructor?
77
78         /** Used to accumulate a coin
79          *
80          * @param c the coin to accumulate
81          * @return a refrence to the updated accumulator.
82          */
83         Accumulator& operator +=(const PublicCoin& c);
84         bool operator==(const Accumulator rhs) const;
85
86         IMPLEMENT_SERIALIZE
87         (
88             READWRITE(value);
89             READWRITE(denomination);
90         )
91 private:
92         const AccumulatorAndProofParams* params;
93         Bignum value;
94         // Denomination is stored as an INT because storing
95         // and enum raises amigiuities in the serialize code //FIXME if possible
96         int denomination;
97 };
98
99 /**A witness that a PublicCoin is in the accumulation of a set of coins
100  *
101  */
102 class AccumulatorWitness {
103 public:
104         template<typename Stream>
105         AccumulatorWitness(const Params* p, Stream& strm): params(p) {
106                 strm >> *this;
107         }
108
109         /**  Construct's a witness.  You must add all elements after the witness
110          * @param p pointer to params
111          * @param checkpoint the last known accumulator value before the element was added
112          * @param coin the coin we want a witness to
113          */
114         AccumulatorWitness(const Params* p, const Accumulator& checkpoint, const PublicCoin coin);
115
116         /** Adds element to the set whose's accumulation we are proving coin is a member of
117          *
118          * @param c the coin to add
119          */
120         void AddElement(const PublicCoin& c);
121
122         /**
123          *
124          * @return the value of the witness
125          */
126         const Bignum& getValue() const;
127
128         /** Checks that this is a witness to the accumulation of coin
129          * @param a             the accumulator we are checking against.
130          * @param publicCoin    the coin we're providing a witness for
131          * @return True if the witness computation validates
132          */
133         bool VerifyWitness(const Accumulator& a, const PublicCoin &publicCoin) const;
134
135         /**
136          * Adds rhs to the set whose's accumulation ware proving coin is a member of
137          * @param rhs the PublicCoin to add
138          * @return
139          */
140         AccumulatorWitness& operator +=(const PublicCoin& rhs);
141 private:
142         const Params* params;
143         Accumulator witness;
144         const PublicCoin element;
145 };
146
147 } /* namespace libzerocoin */
148 #endif /* ACCUMULATOR_H_ */