2017-11-23 11:20:20 -08:00
|
|
|
#ifndef ZC_NOTE_H_
|
|
|
|
#define ZC_NOTE_H_
|
2016-05-04 17:25:42 -07:00
|
|
|
|
|
|
|
#include "uint256.h"
|
|
|
|
#include "Zcash.h"
|
|
|
|
#include "Address.hpp"
|
|
|
|
#include "NoteEncryption.hpp"
|
|
|
|
|
2018-05-03 03:53:51 -07:00
|
|
|
#include <array>
|
2018-06-12 10:01:20 -07:00
|
|
|
#include <boost/optional.hpp>
|
2018-05-03 03:53:51 -07:00
|
|
|
|
2016-05-04 17:25:42 -07:00
|
|
|
namespace libzcash {
|
|
|
|
|
2018-04-24 17:13:13 -07:00
|
|
|
class BaseNote {
|
2018-04-25 13:32:13 -07:00
|
|
|
protected:
|
|
|
|
uint64_t value_ = 0;
|
2018-04-24 17:13:13 -07:00
|
|
|
public:
|
2018-04-25 13:32:13 -07:00
|
|
|
BaseNote() {}
|
|
|
|
BaseNote(uint64_t value) : value_(value) {};
|
2018-04-25 19:03:43 -07:00
|
|
|
virtual ~BaseNote() {};
|
|
|
|
|
2018-04-25 13:32:13 -07:00
|
|
|
inline uint64_t value() const { return value_; };
|
2018-04-24 17:13:13 -07:00
|
|
|
};
|
|
|
|
|
2018-04-24 21:20:01 -07:00
|
|
|
class SproutNote : public BaseNote {
|
2016-05-04 17:25:42 -07:00
|
|
|
public:
|
|
|
|
uint256 a_pk;
|
|
|
|
uint256 rho;
|
|
|
|
uint256 r;
|
|
|
|
|
2018-04-24 21:20:01 -07:00
|
|
|
SproutNote(uint256 a_pk, uint64_t value, uint256 rho, uint256 r)
|
2018-04-25 13:32:13 -07:00
|
|
|
: BaseNote(value), a_pk(a_pk), rho(rho), r(r) {}
|
2016-05-04 17:25:42 -07:00
|
|
|
|
2018-04-24 21:20:01 -07:00
|
|
|
SproutNote();
|
2016-05-04 17:25:42 -07:00
|
|
|
|
2018-04-25 19:03:43 -07:00
|
|
|
virtual ~SproutNote() {};
|
|
|
|
|
2018-06-12 10:01:20 -07:00
|
|
|
uint256 cm() const;
|
2018-04-24 17:13:13 -07:00
|
|
|
|
2018-04-25 18:51:17 -07:00
|
|
|
uint256 nullifier(const SproutSpendingKey& a_sk) const;
|
2016-05-04 17:25:42 -07:00
|
|
|
};
|
|
|
|
|
2018-06-08 15:25:15 -07:00
|
|
|
|
|
|
|
class SaplingNote : public BaseNote {
|
|
|
|
public:
|
|
|
|
diversifier_t d;
|
|
|
|
uint256 pk_d;
|
|
|
|
uint256 r;
|
|
|
|
|
|
|
|
SaplingNote(diversifier_t d, uint256 pk_d, uint64_t value, uint256 r)
|
|
|
|
: BaseNote(value), d(d), pk_d(pk_d), r(r) {}
|
|
|
|
|
|
|
|
SaplingNote() {};
|
|
|
|
|
2018-06-12 09:36:38 -07:00
|
|
|
SaplingNote(const SaplingPaymentAddress &address, uint64_t value);
|
|
|
|
|
2018-06-08 15:25:15 -07:00
|
|
|
virtual ~SaplingNote() {};
|
|
|
|
|
2018-06-12 10:01:20 -07:00
|
|
|
boost::optional<uint256> cm() const;
|
2018-06-16 04:06:15 -07:00
|
|
|
boost::optional<uint256> nullifier(const SaplingFullViewingKey &vk, const uint64_t position) const;
|
2018-06-08 15:25:15 -07:00
|
|
|
};
|
|
|
|
|
2018-04-26 14:05:36 -07:00
|
|
|
class BaseNotePlaintext {
|
|
|
|
protected:
|
|
|
|
uint64_t value_ = 0;
|
2018-05-03 03:53:51 -07:00
|
|
|
std::array<unsigned char, ZC_MEMO_SIZE> memo_;
|
2018-04-26 14:05:36 -07:00
|
|
|
public:
|
|
|
|
BaseNotePlaintext() {}
|
2018-05-03 03:53:51 -07:00
|
|
|
BaseNotePlaintext(const BaseNote& note, std::array<unsigned char, ZC_MEMO_SIZE> memo)
|
2018-04-26 14:53:54 -07:00
|
|
|
: value_(note.value()), memo_(memo) {}
|
2018-04-26 14:05:36 -07:00
|
|
|
virtual ~BaseNotePlaintext() {}
|
|
|
|
|
|
|
|
inline uint64_t value() const { return value_; }
|
2018-05-03 03:53:51 -07:00
|
|
|
inline const std::array<unsigned char, ZC_MEMO_SIZE> & memo() const { return memo_; }
|
2018-04-26 14:05:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class SproutNotePlaintext : public BaseNotePlaintext {
|
2016-05-04 17:25:42 -07:00
|
|
|
public:
|
|
|
|
uint256 rho;
|
|
|
|
uint256 r;
|
|
|
|
|
2018-04-25 23:08:36 -07:00
|
|
|
SproutNotePlaintext() {}
|
2016-05-04 17:25:42 -07:00
|
|
|
|
2018-05-03 03:53:51 -07:00
|
|
|
SproutNotePlaintext(const SproutNote& note, std::array<unsigned char, ZC_MEMO_SIZE> memo);
|
2016-05-04 17:25:42 -07:00
|
|
|
|
2018-04-25 18:51:17 -07:00
|
|
|
SproutNote note(const SproutPaymentAddress& addr) const;
|
2016-05-04 17:25:42 -07:00
|
|
|
|
2018-04-26 14:05:36 -07:00
|
|
|
virtual ~SproutNotePlaintext() {}
|
|
|
|
|
2016-05-04 17:25:42 -07:00
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
2018-04-15 20:09:19 -07:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
2016-05-04 17:25:42 -07:00
|
|
|
unsigned char leadingByte = 0x00;
|
|
|
|
READWRITE(leadingByte);
|
|
|
|
|
|
|
|
if (leadingByte != 0x00) {
|
2018-04-25 23:08:36 -07:00
|
|
|
throw std::ios_base::failure("lead byte of SproutNotePlaintext is not recognized");
|
2016-05-04 17:25:42 -07:00
|
|
|
}
|
|
|
|
|
2018-04-26 14:05:36 -07:00
|
|
|
READWRITE(value_);
|
2016-05-04 17:25:42 -07:00
|
|
|
READWRITE(rho);
|
|
|
|
READWRITE(r);
|
2018-04-26 14:53:54 -07:00
|
|
|
READWRITE(memo_);
|
2016-05-04 17:25:42 -07:00
|
|
|
}
|
|
|
|
|
2018-04-25 23:08:36 -07:00
|
|
|
static SproutNotePlaintext decrypt(const ZCNoteDecryption& decryptor,
|
2016-05-04 17:25:42 -07:00
|
|
|
const ZCNoteDecryption::Ciphertext& ciphertext,
|
|
|
|
const uint256& ephemeralKey,
|
|
|
|
const uint256& h_sig,
|
|
|
|
unsigned char nonce
|
|
|
|
);
|
|
|
|
|
|
|
|
ZCNoteEncryption::Ciphertext encrypt(ZCNoteEncryption& encryptor,
|
|
|
|
const uint256& pk_enc
|
|
|
|
) const;
|
|
|
|
};
|
|
|
|
|
2018-07-16 17:34:14 -07:00
|
|
|
typedef std::pair<SaplingEncCiphertext, SaplingNoteEncryption> SaplingNotePlaintextEncryptionResult;
|
2018-07-11 22:03:04 -07:00
|
|
|
|
|
|
|
class SaplingNotePlaintext : public BaseNotePlaintext {
|
|
|
|
public:
|
|
|
|
diversifier_t d;
|
|
|
|
uint256 rcm;
|
|
|
|
|
|
|
|
SaplingNotePlaintext() {}
|
|
|
|
|
|
|
|
SaplingNotePlaintext(const SaplingNote& note, std::array<unsigned char, ZC_MEMO_SIZE> memo);
|
|
|
|
|
2018-07-16 17:34:14 -07:00
|
|
|
static boost::optional<SaplingNotePlaintext> decrypt(
|
|
|
|
const SaplingEncCiphertext &ciphertext,
|
|
|
|
const uint256 &ivk,
|
2018-07-30 13:37:12 -07:00
|
|
|
const uint256 &epk,
|
|
|
|
const uint256 &cmu
|
2018-07-16 17:34:14 -07:00
|
|
|
);
|
|
|
|
|
2018-07-23 10:48:26 -07:00
|
|
|
static boost::optional<SaplingNotePlaintext> decrypt(
|
2018-07-20 16:31:48 -07:00
|
|
|
const SaplingEncCiphertext &ciphertext,
|
|
|
|
const uint256 &epk,
|
|
|
|
const uint256 &esk,
|
2018-07-30 13:37:12 -07:00
|
|
|
const uint256 &pk_d,
|
|
|
|
const uint256 &cmu
|
2018-07-20 16:31:48 -07:00
|
|
|
);
|
|
|
|
|
2018-07-11 22:03:04 -07:00
|
|
|
boost::optional<SaplingNote> note(const SaplingIncomingViewingKey& ivk) const;
|
|
|
|
|
|
|
|
virtual ~SaplingNotePlaintext() {}
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
|
|
unsigned char leadingByte = 0x01;
|
|
|
|
READWRITE(leadingByte);
|
|
|
|
|
|
|
|
if (leadingByte != 0x01) {
|
|
|
|
throw std::ios_base::failure("lead byte of SaplingNotePlaintext is not recognized");
|
|
|
|
}
|
|
|
|
|
|
|
|
READWRITE(d); // 11 bytes
|
|
|
|
READWRITE(value_); // 8 bytes
|
|
|
|
READWRITE(rcm); // 32 bytes
|
|
|
|
READWRITE(memo_); // 512 bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<SaplingNotePlaintextEncryptionResult> encrypt(const uint256& pk_d) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SaplingOutgoingPlaintext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint256 pk_d;
|
|
|
|
uint256 esk;
|
|
|
|
|
|
|
|
SaplingOutgoingPlaintext() {};
|
|
|
|
|
|
|
|
SaplingOutgoingPlaintext(uint256 pk_d, uint256 esk) : pk_d(pk_d), esk(esk) {}
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
|
|
READWRITE(pk_d); // 8 bytes
|
|
|
|
READWRITE(esk); // 8 bytes
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:34:14 -07:00
|
|
|
static boost::optional<SaplingOutgoingPlaintext> decrypt(
|
|
|
|
const SaplingOutCiphertext &ciphertext,
|
|
|
|
const uint256& ovk,
|
|
|
|
const uint256& cv,
|
|
|
|
const uint256& cm,
|
|
|
|
const uint256& epk
|
|
|
|
);
|
|
|
|
|
2018-07-11 22:03:04 -07:00
|
|
|
SaplingOutCiphertext encrypt(
|
|
|
|
const uint256& ovk,
|
|
|
|
const uint256& cv,
|
|
|
|
const uint256& cm,
|
|
|
|
SaplingNoteEncryption& enc
|
|
|
|
) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-05-04 17:25:42 -07:00
|
|
|
}
|
|
|
|
|
2017-11-23 11:20:20 -08:00
|
|
|
#endif // ZC_NOTE_H_
|