Line data Source code
1 : // Copyright (c) 2016-2020 The ZCash developers
2 : // Copyright (c) 2021 The PIVX Core developers
3 : // Distributed under the MIT software license, see the accompanying
4 : // file COPYING or https://www.opensource.org/licenses/mit-license.php.
5 :
6 : #ifndef PIVX_SAPLING_NOTE_H
7 : #define PIVX_SAPLING_NOTE_H
8 :
9 : #include "optional.h"
10 : #include "sapling/address.h"
11 : #include "sapling/noteencryption.h"
12 : #include "sapling/sapling.h"
13 : #include "uint256.h"
14 :
15 : #include <array>
16 :
17 : namespace libzcash {
18 :
19 : /**
20 : * Notes are main primitive of Sapling, similar to a a tree of commitments.
21 : */
22 9049 : class BaseNote {
23 : protected:
24 : uint64_t value_{0};
25 : public:
26 : BaseNote() {}
27 2414 : explicit BaseNote(uint64_t value) : value_(value) {};
28 10966 : virtual ~BaseNote() {};
29 :
30 7366 : inline uint64_t value() const { return value_; };
31 : };
32 :
33 8379 : class SaplingNote : public BaseNote {
34 : public:
35 : diversifier_t d = {{0}};
36 : uint256 pk_d{UINT256_ZERO};
37 : uint256 r{UINT256_ZERO};
38 :
39 : SaplingNote() {};
40 2079 : SaplingNote(const diversifier_t& _d, const uint256& _pk_d, uint64_t value, const uint256& _r):
41 : BaseNote(value),
42 : d(_d),
43 : pk_d(_pk_d),
44 2079 : r(_r)
45 : {}
46 : SaplingNote(const SaplingPaymentAddress& address, uint64_t value);
47 12376 : virtual ~SaplingNote() {};
48 :
49 : Optional<uint256> cmu() const;
50 : Optional<uint256> nullifier(const SaplingFullViewingKey& vk, const uint64_t position) const;
51 : };
52 :
53 6784 : class BaseNotePlaintext {
54 : protected:
55 : uint64_t value_{0};
56 : std::array<unsigned char, ZC_MEMO_SIZE> memo_ = {{0}};
57 : public:
58 4241 : BaseNotePlaintext() {}
59 1324 : BaseNotePlaintext(const BaseNote& note, const std::array<unsigned char, ZC_MEMO_SIZE>& memo):
60 1324 : value_(note.value()),
61 1324 : memo_(memo)
62 : {}
63 6835 : virtual ~BaseNotePlaintext() {}
64 :
65 6394 : inline uint64_t value() const { return value_; }
66 3033 : inline const std::array<unsigned char, ZC_MEMO_SIZE> & memo() const { return memo_; }
67 : };
68 :
69 : typedef std::pair<SaplingEncCiphertext, SaplingNoteEncryption> SaplingNotePlaintextEncryptionResult;
70 :
71 6689 : class SaplingNotePlaintext : public BaseNotePlaintext {
72 : public:
73 : diversifier_t d = {{0}};
74 : uint256 rcm{UINT256_ZERO};
75 :
76 4241 : SaplingNotePlaintext() {}
77 : SaplingNotePlaintext(const SaplingNote& note, const std::array<unsigned char, ZC_MEMO_SIZE>& memo);
78 15840 : virtual ~SaplingNotePlaintext() {}
79 :
80 : static Optional<SaplingNotePlaintext> decrypt(
81 : const SaplingEncCiphertext& ciphertext,
82 : const uint256& ivk,
83 : const uint256& epk,
84 : const uint256& cmu
85 : );
86 :
87 : static Optional<SaplingNotePlaintext> decrypt(
88 : const SaplingEncCiphertext& ciphertext,
89 : const uint256& epk,
90 : const uint256& esk,
91 : const uint256& pk_d,
92 : const uint256& cmu
93 : );
94 :
95 : Optional<SaplingNote> note(const SaplingIncomingViewingKey& ivk) const;
96 :
97 11130 : SERIALIZE_METHODS(SaplingNotePlaintext, obj)
98 : {
99 5565 : unsigned char leadingByte = 0x01;
100 5565 : READWRITE(leadingByte);
101 :
102 5565 : if (leadingByte != 0x01) {
103 0 : throw std::ios_base::failure("lead byte of SaplingNotePlaintext is not recognized");
104 : }
105 :
106 5565 : READWRITE(obj.d); // 11 bytes
107 5565 : READWRITE(obj.value_); // 8 bytes
108 5565 : READWRITE(obj.rcm); // 32 bytes
109 5565 : READWRITE(obj.memo_); // 512 bytes
110 5565 : }
111 :
112 : Optional<SaplingNotePlaintextEncryptionResult> encrypt(const uint256& pk_d) const;
113 : };
114 :
115 : class SaplingOutgoingPlaintext
116 : {
117 : public:
118 : uint256 pk_d{UINT256_ZERO};
119 : uint256 esk{UINT256_ZERO};
120 :
121 97 : SaplingOutgoingPlaintext() {};
122 1323 : SaplingOutgoingPlaintext(const uint256& _pk_d, const uint256& _esk) :
123 : pk_d(_pk_d),
124 1323 : esk(_esk)
125 : {}
126 :
127 96 : SERIALIZE_METHODS(SaplingOutgoingPlaintext, obj)
128 : {
129 1420 : READWRITE(obj.pk_d); // 8 bytes
130 1420 : READWRITE(obj.esk); // 8 bytes
131 : }
132 :
133 : static Optional<SaplingOutgoingPlaintext> decrypt(
134 : const SaplingOutCiphertext& ciphertext,
135 : const uint256& ovk,
136 : const uint256& cv,
137 : const uint256& cm,
138 : const uint256& epk
139 : );
140 :
141 : SaplingOutCiphertext encrypt(
142 : const uint256& ovk,
143 : const uint256& cv,
144 : const uint256& cm,
145 : SaplingNoteEncryption& enc
146 : ) const;
147 : };
148 :
149 :
150 : }
151 :
152 : #endif // PIVX_SAPLING_NOTE_H
|