PIVX Core  5.6.99
P2P Digital Currency
note.h
Go to the documentation of this file.
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 
22 class BaseNote {
23 protected:
24  uint64_t value_{0};
25 public:
26  BaseNote() {}
27  explicit BaseNote(uint64_t value) : value_(value) {};
28  virtual ~BaseNote() {};
29 
30  inline uint64_t value() const { return value_; };
31 };
32 
33 class SaplingNote : public BaseNote {
34 public:
35  diversifier_t d = {{0}};
38 
40  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  r(_r)
45  {}
46  SaplingNote(const SaplingPaymentAddress& address, uint64_t value);
47  virtual ~SaplingNote() {};
48 
49  Optional<uint256> cmu() const;
50  Optional<uint256> nullifier(const SaplingFullViewingKey& vk, const uint64_t position) const;
51 };
52 
54 protected:
55  uint64_t value_{0};
56  std::array<unsigned char, ZC_MEMO_SIZE> memo_ = {{0}};
57 public:
59  BaseNotePlaintext(const BaseNote& note, const std::array<unsigned char, ZC_MEMO_SIZE>& memo):
60  value_(note.value()),
61  memo_(memo)
62  {}
63  virtual ~BaseNotePlaintext() {}
64 
65  inline uint64_t value() const { return value_; }
66  inline const std::array<unsigned char, ZC_MEMO_SIZE> & memo() const { return memo_; }
67 };
68 
69 typedef std::pair<SaplingEncCiphertext, SaplingNoteEncryption> SaplingNotePlaintextEncryptionResult;
70 
72 public:
73  diversifier_t d = {{0}};
75 
77  SaplingNotePlaintext(const SaplingNote& note, const std::array<unsigned char, ZC_MEMO_SIZE>& memo);
78  virtual ~SaplingNotePlaintext() {}
79 
81  const SaplingEncCiphertext& ciphertext,
82  const uint256& ivk,
83  const uint256& epk,
84  const uint256& cmu
85  );
86 
88  const SaplingEncCiphertext& ciphertext,
89  const uint256& epk,
90  const uint256& esk,
91  const uint256& pk_d,
92  const uint256& cmu
93  );
94 
96 
98  {
99  unsigned char leadingByte = 0x01;
100  READWRITE(leadingByte);
101 
102  if (leadingByte != 0x01) {
103  throw std::ios_base::failure("lead byte of SaplingNotePlaintext is not recognized");
104  }
105 
106  READWRITE(obj.d); // 11 bytes
107  READWRITE(obj.value_); // 8 bytes
108  READWRITE(obj.rcm); // 32 bytes
109  READWRITE(obj.memo_); // 512 bytes
110  }
111 
113 };
114 
116 {
117 public:
120 
122  SaplingOutgoingPlaintext(const uint256& _pk_d, const uint256& _esk) :
123  pk_d(_pk_d),
124  esk(_esk)
125  {}
126 
128  {
129  READWRITE(obj.pk_d); // 8 bytes
130  READWRITE(obj.esk); // 8 bytes
131  }
132 
134  const SaplingOutCiphertext& ciphertext,
135  const uint256& ovk,
136  const uint256& cv,
137  const uint256& cm,
138  const uint256& epk
139  );
140 
142  const uint256& ovk,
143  const uint256& cv,
144  const uint256& cm,
146  ) const;
147 };
148 
149 
150 }
151 
152 #endif // PIVX_SAPLING_NOTE_H
Notes are main primitive of Sapling, similar to a a tree of commitments.
Definition: note.h:22
uint64_t value_
Definition: note.h:24
uint64_t value() const
Definition: note.h:30
virtual ~BaseNote()
Definition: note.h:28
BaseNote(uint64_t value)
Definition: note.h:27
const std::array< unsigned char, ZC_MEMO_SIZE > & memo() const
Definition: note.h:66
BaseNotePlaintext(const BaseNote &note, const std::array< unsigned char, ZC_MEMO_SIZE > &memo)
Definition: note.h:59
virtual ~BaseNotePlaintext()
Definition: note.h:63
std::array< unsigned char, ZC_MEMO_SIZE > memo_
Definition: note.h:56
uint64_t value() const
Definition: note.h:65
This is not a thread-safe API.
Optional< uint256 > cmu() const
Definition: note.cpp:29
diversifier_t d
Definition: note.h:35
SaplingNote(const diversifier_t &_d, const uint256 &_pk_d, uint64_t value, const uint256 &_r)
Definition: note.h:40
virtual ~SaplingNote()
Definition: note.h:47
uint256 pk_d
Definition: note.h:36
Optional< uint256 > nullifier(const SaplingFullViewingKey &vk, const uint64_t position) const
Definition: note.cpp:47
Optional< SaplingNote > note(const SaplingIncomingViewingKey &ivk) const
Definition: note.cpp:80
Optional< SaplingNotePlaintextEncryptionResult > encrypt(const uint256 &pk_d) const
Definition: note.cpp:201
SERIALIZE_METHODS(SaplingNotePlaintext, obj)
Definition: note.h:97
virtual ~SaplingNotePlaintext()
Definition: note.h:78
static Optional< SaplingNotePlaintext > decrypt(const SaplingEncCiphertext &ciphertext, const uint256 &ivk, const uint256 &epk, const uint256 &cmu)
Definition: note.cpp:115
SaplingOutgoingPlaintext(const uint256 &_pk_d, const uint256 &_esk)
Definition: note.h:122
SaplingOutCiphertext encrypt(const uint256 &ovk, const uint256 &cv, const uint256 &cm, SaplingNoteEncryption &enc) const
Definition: note.cpp:226
SERIALIZE_METHODS(SaplingOutgoingPlaintext, obj)
Definition: note.h:127
static Optional< SaplingOutgoingPlaintext > decrypt(const SaplingOutCiphertext &ciphertext, const uint256 &ovk, const uint256 &cv, const uint256 &cm, const uint256 &epk)
Definition: note.cpp:90
Sapling functions.
Definition: address.h:30
256-bit opaque blob.
Definition: uint256.h:138
std::array< unsigned char, ZC_SAPLING_ENCCIPHERTEXT_SIZE > SaplingEncCiphertext
std::pair< SaplingEncCiphertext, SaplingNoteEncryption > SaplingNotePlaintextEncryptionResult
Definition: note.h:69
std::array< unsigned char, ZC_SAPLING_OUTCIPHERTEXT_SIZE > SaplingOutCiphertext
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:12
std::array< unsigned char, ZC_DIVERSIFIER_SIZE > diversifier_t
Definition: sapling.h:38
#define READWRITE(...)
Definition: serialize.h:183
const uint256 UINT256_ZERO
constant uint256 instances
Definition: uint256.h:175