Skip to content

Commit

Permalink
Return optional for Sapling commitments and nullifiers.
Browse files Browse the repository at this point in the history
Unlike Sprout, they are not always computable in Sapling.
  • Loading branch information
bitcartel committed Jun 12, 2018
1 parent d17f8d1 commit 268e5df
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/gtest/test_sapling_note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST(SaplingNote, TestVectors)

// Test commitment
SaplingNote note = SaplingNote(diversifier, pk_d, v, r);
ASSERT_EQ(note.cm(), cm);
ASSERT_EQ(note.cm().get(), cm);

// Test nullifier
SaplingSpendingKey spendingKey(sk);
Expand Down
12 changes: 5 additions & 7 deletions src/zcash/Note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "zcash/util.h"
#include "librustzcash.h"

namespace libzcash {
using namespace libzcash;

SproutNote::SproutNote() {
a_pk = random_uint256();
Expand Down Expand Up @@ -48,7 +48,7 @@ SaplingNote::SaplingNote(const SaplingPaymentAddress& address, const uint64_t va
}

// Call librustzcash to compute the commitment
uint256 SaplingNote::cm() const {
boost::optional<uint256> SaplingNote::cm() const {
uint256 result;
if (!librustzcash_sapling_compute_cm(
d.data(),
Expand All @@ -58,14 +58,14 @@ uint256 SaplingNote::cm() const {
result.begin()
))
{
throw std::runtime_error("librustzcash_sapling_compute_cm returned false");
return boost::none;
}

return result;
}

// Call librustzcash to compute the nullifier
uint256 SaplingNote::nullifier(const SaplingSpendingKey& sk, const uint64_t position) const
boost::optional<uint256> SaplingNote::nullifier(const SaplingSpendingKey& sk, const uint64_t position) const
{
auto vk = sk.full_viewing_key();
auto ak = vk.ak;
Expand All @@ -83,7 +83,7 @@ uint256 SaplingNote::nullifier(const SaplingSpendingKey& sk, const uint64_t posi
result.begin()
))
{
throw std::runtime_error("librustzcash_sapling_compute_nf returned false");
return boost::none;
}

return result;
Expand Down Expand Up @@ -137,5 +137,3 @@ ZCNoteEncryption::Ciphertext SproutNotePlaintext::encrypt(ZCNoteEncryption& encr

return encryptor.encrypt(pk_enc, pt);
}

}
9 changes: 4 additions & 5 deletions src/zcash/Note.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "NoteEncryption.hpp"

#include <array>
#include <boost/optional.hpp>

namespace libzcash {

Expand All @@ -18,7 +19,6 @@ class BaseNote {
BaseNote(uint64_t value) : value_(value) {};
virtual ~BaseNote() {};

virtual uint256 cm() const = 0;
inline uint64_t value() const { return value_; };
};

Expand All @@ -35,7 +35,7 @@ class SproutNote : public BaseNote {

virtual ~SproutNote() {};

virtual uint256 cm() const override;
uint256 cm() const;

uint256 nullifier(const SproutSpendingKey& a_sk) const;
};
Expand All @@ -56,9 +56,8 @@ class SaplingNote : public BaseNote {

virtual ~SaplingNote() {};

virtual uint256 cm() const override;

uint256 nullifier(const SaplingSpendingKey &sk, const uint64_t position) const;
boost::optional<uint256> cm() const;
boost::optional<uint256> nullifier(const SaplingSpendingKey &sk, const uint64_t position) const;
};

class BaseNotePlaintext {
Expand Down

0 comments on commit 268e5df

Please sign in to comment.