Skip to content

Commit

Permalink
Refactoring: SproutNote member variable value moved to BaseNote.
Browse files Browse the repository at this point in the history
All notes have a value, so the member variable has been moved to the
base class, and direct member access has been replaced with a getter.
  • Loading branch information
bitcartel committed Apr 26, 2018
1 parent b230fe6 commit 5d99e3e
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/gtest/test_joinsplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void test_full_api(ZCJoinSplit* js)

auto decrypted_note = note_pt.note(recipient_addr);

ASSERT_TRUE(decrypted_note.value == 10);
ASSERT_TRUE(decrypted_note.value() == 10);

// Insert the commitments from the last tx into the tree
tree.append(commitments[0]);
Expand Down Expand Up @@ -543,7 +543,7 @@ TEST(joinsplit, note_plaintexts)
ASSERT_TRUE(decrypted_note.a_pk == note.a_pk);
ASSERT_TRUE(decrypted_note.rho == note.rho);
ASSERT_TRUE(decrypted_note.r == note.r);
ASSERT_TRUE(decrypted_note.value == note.value);
ASSERT_TRUE(decrypted_note.value() == note.value());

ASSERT_TRUE(decrypted.memo == note_pt.memo);
}
8 changes: 4 additions & 4 deletions src/utiltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ CWalletTx GetValidSpend(ZCJoinSplit& params,
libzcash::JSInput dummyin;

{
if (note.value > value) {
if (note.value() > value) {
libzcash::SpendingKey dummykey = libzcash::SpendingKey::random();
libzcash::PaymentAddress dummyaddr = dummykey.address();
dummyout = libzcash::JSOutput(dummyaddr, note.value - value);
} else if (note.value < value) {
dummyout = libzcash::JSOutput(dummyaddr, note.value() - value);
} else if (note.value() < value) {
libzcash::SpendingKey dummykey = libzcash::SpendingKey::random();
libzcash::PaymentAddress dummyaddr = dummykey.address();
libzcash::SproutNote dummynote(dummyaddr.a_pk, (value - note.value), uint256(), uint256());
libzcash::SproutNote dummynote(dummyaddr.a_pk, (value - note.value()), uint256(), uint256());
tree.append(dummynote.cm());
dummyin = libzcash::JSInput(tree.witness(), dummynote, dummykey);
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/asyncrpcoperation_mergetoaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ UniValue AsyncRPCOperation_mergetoaddress::perform_joinsplit(
getId(),
tx_.vjoinsplit.size(),
FormatMoney(info.vpub_old), FormatMoney(info.vpub_new),
FormatMoney(info.vjsin[0].note.value), FormatMoney(info.vjsin[1].note.value),
FormatMoney(info.vjsin[0].note.value()), FormatMoney(info.vjsin[1].note.value()),
FormatMoney(info.vjsout[0].value), FormatMoney(info.vjsout[1].value));

// Generate the proof, this can take over a minute.
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/asyncrpcoperation_sendmany.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ UniValue AsyncRPCOperation_sendmany::perform_joinsplit(
getId(),
tx_.vjoinsplit.size(),
FormatMoney(info.vpub_old), FormatMoney(info.vpub_new),
FormatMoney(info.vjsin[0].note.value), FormatMoney(info.vjsin[1].note.value),
FormatMoney(info.vjsin[0].note.value()), FormatMoney(info.vjsin[1].note.value()),
FormatMoney(info.vjsout[0].value), FormatMoney(info.vjsout[1].value)
);

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/asyncrpcoperation_shieldcoinbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ UniValue AsyncRPCOperation_shieldcoinbase::perform_joinsplit(ShieldCoinbaseJSInf
getId(),
tx_.vjoinsplit.size(),
FormatMoney(info.vpub_old), FormatMoney(info.vpub_new),
FormatMoney(info.vjsin[0].note.value), FormatMoney(info.vjsin[1].note.value),
FormatMoney(info.vjsin[0].note.value()), FormatMoney(info.vjsin[1].note.value()),
FormatMoney(info.vjsout[0].value), FormatMoney(info.vjsout[1].value)
);

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2833,7 +2833,7 @@ UniValue zc_raw_receive(const UniValue& params, bool fHelp)
ss << npt;

UniValue result(UniValue::VOBJ);
result.push_back(Pair("amount", ValueFromAmount(decrypted_note.value)));
result.push_back(Pair("amount", ValueFromAmount(decrypted_note.value())));
result.push_back(Pair("note", HexStr(ss.begin(), ss.end())));
result.push_back(Pair("exists", (bool) witnesses[0]));
return result;
Expand Down
6 changes: 3 additions & 3 deletions src/zcash/JoinSplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class JoinSplitCircuit : public JoinSplit<NumInputs, NumOutputs> {
// Sanity checks of input
{
// If note has nonzero value
if (inputs[i].note.value != 0) {
if (inputs[i].note.value() != 0) {
// The witness root must equal the input root.
if (inputs[i].witness.root() != rt) {
throw std::invalid_argument("joinsplit not anchored to the correct root");
Expand All @@ -186,11 +186,11 @@ class JoinSplitCircuit : public JoinSplit<NumInputs, NumOutputs> {
}

// Balance must be sensical
if (inputs[i].note.value > MAX_MONEY) {
if (inputs[i].note.value() > MAX_MONEY) {
throw std::invalid_argument("nonsensical input note value");
}

lhs_value += inputs[i].note.value;
lhs_value += inputs[i].note.value();

if (lhs_value > MAX_MONEY) {
throw std::invalid_argument("nonsensical left hand size of joinsplit balance");
Expand Down
5 changes: 2 additions & 3 deletions src/zcash/Note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ SproutNote::SproutNote() {
a_pk = random_uint256();
rho = random_uint256();
r = random_uint256();
value = 0;
}

uint256 SproutNote::cm() const {
Expand All @@ -23,7 +22,7 @@ uint256 SproutNote::cm() const {
hasher.Write(&discriminant, 1);
hasher.Write(a_pk.begin(), 32);

auto value_vec = convertIntToVectorLE(value);
auto value_vec = convertIntToVectorLE(value_);

hasher.Write(&value_vec[0], value_vec.size());
hasher.Write(rho.begin(), 32);
Expand All @@ -43,7 +42,7 @@ NotePlaintext::NotePlaintext(
const SproutNote& note,
boost::array<unsigned char, ZC_MEMO_SIZE> memo) : memo(memo)
{
value = note.value;
value = note.value();
rho = note.rho;
r = note.r;
}
Expand Down
8 changes: 6 additions & 2 deletions src/zcash/Note.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@
namespace libzcash {

class BaseNote {
protected:
uint64_t value_ = 0;
public:
BaseNote() {}
BaseNote(uint64_t value) : value_(value) {};
virtual uint256 cm() const {};
inline uint64_t value() const { return value_; };
};

class SproutNote : public BaseNote {
public:
uint256 a_pk;
uint64_t value;
uint256 rho;
uint256 r;

SproutNote(uint256 a_pk, uint64_t value, uint256 rho, uint256 r)
: a_pk(a_pk), value(value), rho(rho), r(r) {}
: BaseNote(value), a_pk(a_pk), rho(rho), r(r) {}

SproutNote();

Expand Down
2 changes: 1 addition & 1 deletion src/zcash/circuit/gadget.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public:
// Witness total_uint64 bits
uint64_t left_side_acc = vpub_old;
for (size_t i = 0; i < NumInputs; i++) {
left_side_acc += inputs[i].note.value;
left_side_acc += inputs[i].note.value();
}

zk_total_uint64.fill_with_bits(
Expand Down
4 changes: 2 additions & 2 deletions src/zcash/circuit/note.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public:

void generate_r1cs_witness(const SproutNote& note) {
r->bits.fill_with_bits(this->pb, uint256_to_bool_vector(note.r));
value.fill_with_bits(this->pb, uint64_to_bool_vector(note.value));
value.fill_with_bits(this->pb, uint64_to_bool_vector(note.value()));
}
};

Expand Down Expand Up @@ -158,7 +158,7 @@ public:
);

// Set enforce flag for nonzero input value
this->pb.val(value_enforce) = (note.value != 0) ? FieldT::one() : FieldT::zero();
this->pb.val(value_enforce) = (note.value() != 0) ? FieldT::one() : FieldT::zero();

// Witness merkle tree authentication path
witness_input->generate_r1cs_witness(path);
Expand Down

0 comments on commit 5d99e3e

Please sign in to comment.