Line data Source code
1 : /**
2 : * @file CoinSpend.h
3 : *
4 : * @brief CoinSpend class 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 : // Copyright (c) 2017-2021 The PIVX Core developers
13 :
14 : #ifndef PIVX_LIBZEROCOIN_COINSPEND_H
15 : #define PIVX_LIBZEROCOIN_COINSPEND_H
16 :
17 : #include <streams.h>
18 : #include <utilstrencodings.h>
19 : #include "Coin.h"
20 : #include "Commitment.h"
21 : #include "Params.h"
22 : #include "SpendType.h"
23 :
24 : #include "bignum.h"
25 : #include "pubkey.h"
26 : #include "serialize.h"
27 :
28 : namespace libzerocoin
29 : {
30 : // Legacy zPIV - Only for serialization
31 : // Proof that a value inside a commitment C is accumulated in accumulator A
32 : class AccumulatorProofOfKnowledge {
33 : public:
34 0 : AccumulatorProofOfKnowledge() {};
35 0 : SERIALIZE_METHODS(AccumulatorProofOfKnowledge, obj) {
36 0 : READWRITE(obj.C_e, obj.C_u, obj.C_r, obj.st_1, obj.st_2, obj.st_3);
37 0 : READWRITE(obj.t_1, obj.t_2, obj.t_3, obj.t_4, obj.s_alpha, obj.s_beta);
38 0 : READWRITE(obj.s_zeta, obj.s_sigma, obj.s_eta, obj.s_epsilon);
39 0 : READWRITE(obj.s_delta, obj.s_xi, obj.s_phi, obj.s_gamma, obj.s_psi);
40 0 : }
41 : private:
42 : CBigNum C_e, C_u, C_r;
43 : CBigNum st_1, st_2, st_3;
44 : CBigNum t_1, t_2, t_3, t_4;
45 : CBigNum s_alpha, s_beta, s_zeta, s_sigma, s_eta, s_epsilon, s_delta;
46 : CBigNum s_xi, s_phi, s_gamma, s_psi;
47 : };
48 :
49 : // Legacy zPIV - Only for serialization
50 : // Signature of knowledge attesting that the signer knows the values to
51 : // open a commitment to a coin with given serial number
52 0 : class SerialNumberSignatureOfKnowledge {
53 : public:
54 0 : SerialNumberSignatureOfKnowledge(){};
55 0 : SERIALIZE_METHODS(SerialNumberSignatureOfKnowledge, obj) { READWRITE(obj.s_notprime, obj.sprime, obj.hash); }
56 : private:
57 : uint256 hash;
58 : std::vector<CBigNum> s_notprime;
59 : std::vector<CBigNum> sprime;
60 : };
61 :
62 : // Legacy zPIV - Only for serialization
63 : // Proof that two commitments open to the same value (BROKEN)
64 : class CommitmentProofOfKnowledge {
65 : public:
66 0 : CommitmentProofOfKnowledge() {};
67 0 : SERIALIZE_METHODS(CommitmentProofOfKnowledge, obj) { READWRITE(obj.S1,obj.S2, obj.S3, obj.challenge); }
68 : private:
69 : CBigNum S1, S2, S3, challenge;
70 : };
71 :
72 :
73 : // Legacy zPIV - Only for serialization
74 : /** The complete proof needed to spend a zerocoin.
75 : * Composes together a proof that a coin is accumulated
76 : * and that it has a given serial number.
77 : */
78 : class CoinSpend
79 : {
80 : public:
81 :
82 0 : CoinSpend() {};
83 0 : explicit CoinSpend(CDataStream& strm) { strm >> *this; }
84 0 : virtual ~CoinSpend(){};
85 :
86 0 : const CBigNum& getCoinSerialNumber() const { return this->coinSerialNumber; }
87 0 : CoinDenomination getDenomination() const { return this->denomination; }
88 0 : uint32_t getAccumulatorChecksum() const { return this->accChecksum; }
89 0 : uint256 getTxOutHash() const { return ptxHash; }
90 : CBigNum getAccCommitment() const { return accCommitmentToCoinValue; }
91 : CBigNum getSerialComm() const { return serialCommitmentToCoinValue; }
92 : uint8_t getVersion() const { return version; }
93 0 : int getCoinVersion() const { return libzerocoin::ExtractVersionFromSerial(coinSerialNumber); }
94 0 : CPubKey getPubKey() const { return pubkey; }
95 0 : SpendType getSpendType() const { return spendType; }
96 : std::vector<unsigned char> getSignature() const { return vchSig; }
97 :
98 : static std::vector<unsigned char> ParseSerial(CDataStream& s);
99 :
100 : virtual const uint256 signatureHash() const;
101 : bool HasValidSerial(ZerocoinParams* params) const;
102 : bool HasValidSignature() const;
103 0 : void setTxOutHash(uint256 txOutHash) { this->ptxHash = txOutHash; };
104 0 : void setDenom(libzerocoin::CoinDenomination denom) { this->denomination = denom; }
105 : void setPubKey(CPubKey pkey, bool fUpdateSerial = false);
106 :
107 : CBigNum CalculateValidSerial(ZerocoinParams* params);
108 : std::string ToString() const;
109 :
110 0 : SERIALIZE_METHODS(CoinSpend, obj)
111 : {
112 0 : READWRITE(obj.denomination, obj.ptxHash, obj.accChecksum, obj.accCommitmentToCoinValue);
113 0 : READWRITE(obj.serialCommitmentToCoinValue, obj.coinSerialNumber, obj.accumulatorPoK);
114 0 : READWRITE(obj.serialNumberSoK, obj.commitmentPoK);
115 : try {
116 0 : READWRITE(obj.version, obj.pubkey, obj.vchSig, obj.spendType);
117 0 : } catch (...) {
118 0 : SER_READ(obj, obj.version = 1);
119 : }
120 0 : }
121 :
122 : protected:
123 : CoinDenomination denomination{ZQ_ERROR};
124 : CBigNum coinSerialNumber{};
125 : uint8_t version{0};
126 : //As of version 2
127 : CPubKey pubkey;
128 : std::vector<unsigned char> vchSig;
129 : SpendType spendType{SPEND};
130 : uint256 ptxHash{UINT256_ZERO};
131 :
132 : private:
133 : uint32_t accChecksum{0};
134 : CBigNum accCommitmentToCoinValue;
135 : CBigNum serialCommitmentToCoinValue;
136 : AccumulatorProofOfKnowledge accumulatorPoK;
137 : SerialNumberSignatureOfKnowledge serialNumberSoK;
138 : CommitmentProofOfKnowledge commitmentPoK;
139 :
140 : };
141 :
142 : } /* namespace libzerocoin */
143 : #endif // PIVX_LIBZEROCOIN_COINSPEND_H
|