Auto merge of #3272 - bitcartel:3061_sapling_add_notes, r=bitcartel

Add SaplingNote class

Part of #3061, adding SaplingNote class.
This commit is contained in:
Homu
2018-06-12 16:58:38 -07:00
5 changed files with 149 additions and 7 deletions

View File

@@ -2,12 +2,14 @@
#include "prf.h"
#include "crypto/sha256.h"
#include "random.h"
#include "version.h"
#include "streams.h"
#include "zcash/util.h"
#include "librustzcash.h"
namespace libzcash {
using namespace libzcash;
SproutNote::SproutNote() {
a_pk = random_uint256();
@@ -38,6 +40,55 @@ uint256 SproutNote::nullifier(const SproutSpendingKey& a_sk) const {
return PRF_nf(a_sk, rho);
}
// Construct and populate Sapling note for a given payment address and value.
SaplingNote::SaplingNote(const SaplingPaymentAddress& address, const uint64_t value) : BaseNote(value) {
d = address.d;
pk_d = address.pk_d;
librustzcash_sapling_generate_r(r.begin());
}
// Call librustzcash to compute the commitment
boost::optional<uint256> SaplingNote::cm() const {
uint256 result;
if (!librustzcash_sapling_compute_cm(
d.data(),
pk_d.begin(),
value(),
r.begin(),
result.begin()
))
{
return boost::none;
}
return result;
}
// Call librustzcash to compute the nullifier
boost::optional<uint256> SaplingNote::nullifier(const SaplingSpendingKey& sk, const uint64_t position) const
{
auto vk = sk.full_viewing_key();
auto ak = vk.ak;
auto nk = vk.nk;
uint256 result;
if (!librustzcash_sapling_compute_nf(
d.data(),
pk_d.begin(),
value(),
r.begin(),
ak.begin(),
nk.begin(),
position,
result.begin()
))
{
return boost::none;
}
return result;
}
SproutNotePlaintext::SproutNotePlaintext(
const SproutNote& note,
std::array<unsigned char, ZC_MEMO_SIZE> memo) : BaseNotePlaintext(note, memo)
@@ -86,5 +137,3 @@ ZCNoteEncryption::Ciphertext SproutNotePlaintext::encrypt(ZCNoteEncryption& encr
return encryptor.encrypt(pk_enc, pt);
}
}

View File

@@ -7,6 +7,7 @@
#include "NoteEncryption.hpp"
#include <array>
#include <boost/optional.hpp>
namespace libzcash {
@@ -18,7 +19,6 @@ public:
BaseNote(uint64_t value) : value_(value) {};
virtual ~BaseNote() {};
virtual uint256 cm() const = 0;
inline uint64_t value() const { return value_; };
};
@@ -35,11 +35,31 @@ public:
virtual ~SproutNote() {};
virtual uint256 cm() const override;
uint256 cm() const;
uint256 nullifier(const SproutSpendingKey& a_sk) const;
};
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() {};
SaplingNote(const SaplingPaymentAddress &address, uint64_t value);
virtual ~SaplingNote() {};
boost::optional<uint256> cm() const;
boost::optional<uint256> nullifier(const SaplingSpendingKey &sk, const uint64_t position) const;
};
class BaseNotePlaintext {
protected:
uint64_t value_ = 0;