|
| 1 | +// Copyright (c) 2016-2020 The ZCash developers |
| 2 | +// Copyright (c) 2020 The PIVX developers |
| 3 | +// Distributed under the MIT software license, see the accompanying |
| 4 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +#ifndef PIVX_SAPLING_TRANSACTION_H |
| 7 | +#define PIVX_SAPLING_TRANSACTION_H |
| 8 | + |
| 9 | +#include "serialize.h" |
| 10 | +#include "streams.h" |
| 11 | +#include "uint256.h" |
| 12 | +#include "consensus/consensus.h" |
| 13 | + |
| 14 | +#include "sapling/noteencryption.hpp" |
| 15 | +#include "sapling/sapling.h" |
| 16 | +#include "sapling/proof.hpp" |
| 17 | + |
| 18 | +#include <boost/variant.hpp> |
| 19 | + |
| 20 | +// These constants are defined in the protocol § 7.1: |
| 21 | +// https://zips.z.cash/protocol/protocol.pdf#txnencoding |
| 22 | +#define OUTPUTDESCRIPTION_SIZE 948 |
| 23 | +#define SPENDDESCRIPTION_SIZE 384 |
| 24 | + |
| 25 | +namespace libzcash { |
| 26 | + static constexpr size_t GROTH_PROOF_SIZE = ( |
| 27 | + 48 + // π_A |
| 28 | + 96 + // π_B |
| 29 | + 48); // π_C |
| 30 | + |
| 31 | + typedef std::array<unsigned char, GROTH_PROOF_SIZE> GrothProof; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A shielded input to a transaction. It contains data that describes a Spend transfer. |
| 36 | + */ |
| 37 | +class SpendDescription |
| 38 | +{ |
| 39 | +public: |
| 40 | + typedef std::array<unsigned char, 64> spend_auth_sig_t; |
| 41 | + |
| 42 | + uint256 cv; //!< A value commitment to the value of the input note. |
| 43 | + uint256 anchor; //!< A Merkle root of the Sapling note commitment tree at some block height in the past. |
| 44 | + uint256 nullifier; //!< The nullifier of the input note. |
| 45 | + uint256 rk; //!< The randomized public key for spendAuthSig. |
| 46 | + libzcash::GrothProof zkproof; //!< A zero-knowledge proof using the spend circuit. |
| 47 | + spend_auth_sig_t spendAuthSig; //!< A signature authorizing this spend. |
| 48 | + |
| 49 | + SpendDescription() { } |
| 50 | + |
| 51 | + ADD_SERIALIZE_METHODS; |
| 52 | + |
| 53 | + template <typename Stream, typename Operation> |
| 54 | + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { |
| 55 | + READWRITE(cv); |
| 56 | + READWRITE(anchor); |
| 57 | + READWRITE(nullifier); |
| 58 | + READWRITE(rk); |
| 59 | + READWRITE(zkproof); |
| 60 | + READWRITE(spendAuthSig); |
| 61 | + } |
| 62 | + |
| 63 | + friend bool operator==(const SpendDescription& a, const SpendDescription& b) |
| 64 | + { |
| 65 | + return ( |
| 66 | + a.cv == b.cv && |
| 67 | + a.anchor == b.anchor && |
| 68 | + a.nullifier == b.nullifier && |
| 69 | + a.rk == b.rk && |
| 70 | + a.zkproof == b.zkproof && |
| 71 | + a.spendAuthSig == b.spendAuthSig |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + friend bool operator!=(const SpendDescription& a, const SpendDescription& b) |
| 76 | + { |
| 77 | + return !(a == b); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * A shielded output to a transaction. It contains data that describes an Output transfer. |
| 83 | + */ |
| 84 | +class OutputDescription |
| 85 | +{ |
| 86 | +public: |
| 87 | + uint256 cv; //!< A value commitment to the value of the output note. |
| 88 | + uint256 cmu; //!< The u-coordinate of the note commitment for the output note. |
| 89 | + uint256 ephemeralKey; //!< A Jubjub public key. |
| 90 | + libzcash::SaplingEncCiphertext encCiphertext; //!< A ciphertext component for the encrypted output note. |
| 91 | + libzcash::SaplingOutCiphertext outCiphertext; //!< A ciphertext component for the encrypted output note. |
| 92 | + libzcash::GrothProof zkproof; //!< A zero-knowledge proof using the output circuit. |
| 93 | + |
| 94 | + OutputDescription() { } |
| 95 | + |
| 96 | + ADD_SERIALIZE_METHODS; |
| 97 | + |
| 98 | + template <typename Stream, typename Operation> |
| 99 | + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { |
| 100 | + READWRITE(cv); |
| 101 | + READWRITE(cmu); |
| 102 | + READWRITE(ephemeralKey); |
| 103 | + READWRITE(encCiphertext); |
| 104 | + READWRITE(outCiphertext); |
| 105 | + READWRITE(zkproof); |
| 106 | + } |
| 107 | + |
| 108 | + friend bool operator==(const OutputDescription& a, const OutputDescription& b) |
| 109 | + { |
| 110 | + return ( |
| 111 | + a.cv == b.cv && |
| 112 | + a.cmu == b.cmu && |
| 113 | + a.ephemeralKey == b.ephemeralKey && |
| 114 | + a.encCiphertext == b.encCiphertext && |
| 115 | + a.outCiphertext == b.outCiphertext && |
| 116 | + a.zkproof == b.zkproof |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + friend bool operator!=(const OutputDescription& a, const OutputDescription& b) |
| 121 | + { |
| 122 | + return !(a == b); |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +class SaplingTxData |
| 127 | +{ |
| 128 | +public: |
| 129 | + typedef std::array<unsigned char, 64> binding_sig_t; |
| 130 | + |
| 131 | + CAmount valueBalance{0}; |
| 132 | + std::vector<SpendDescription> vShieldedSpend; |
| 133 | + std::vector<OutputDescription> vShieldedOutput; |
| 134 | + binding_sig_t bindingSig = {{0}}; |
| 135 | + |
| 136 | + ADD_SERIALIZE_METHODS; |
| 137 | + |
| 138 | + template <typename Stream, typename Operation> |
| 139 | + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) |
| 140 | + { |
| 141 | + READWRITE(*const_cast<CAmount*>(&valueBalance)); |
| 142 | + READWRITE(*const_cast<std::vector<SpendDescription>*>(&vShieldedSpend)); |
| 143 | + READWRITE(*const_cast<std::vector<OutputDescription>*>(&vShieldedOutput)); |
| 144 | + READWRITE(*const_cast<binding_sig_t*>(&bindingSig)); |
| 145 | + } |
| 146 | + |
| 147 | + explicit SaplingTxData() : valueBalance(0), vShieldedSpend(), vShieldedOutput() { } |
| 148 | + explicit SaplingTxData(const SaplingTxData& from) : valueBalance(from.valueBalance), vShieldedSpend(from.vShieldedSpend), vShieldedOutput(from.vShieldedOutput), bindingSig(from.bindingSig) {} |
| 149 | +}; |
| 150 | + |
| 151 | + |
| 152 | +#endif //PIVX_SAPLING_TRANSACTION_H |
0 commit comments