Skip to content

Commit

Permalink
scripted-diff: replace boost::optional with Optional<> wrapper
Browse files Browse the repository at this point in the history
-BEGIN VERIFY SCRIPT-
sed -i -e 's/boost::optional/Optional/g' -e 's/boost::none/nullopt/g' -e 's?#include <boost/optional.hpp>?#include "optional.h"?g' src/sapling/*.h src/sapling/*.cpp src/util/system.h src/wallet/test/wallet_shielded_balances_tests.cpp;
-END VERIFY SCRIPT-

Github-Pull: #2642
Rebased-From: d74cd79
  • Loading branch information
random-zebra authored and furszy committed Dec 11, 2021
1 parent 08c6da7 commit fdd4045
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 68 deletions.
6 changes: 3 additions & 3 deletions src/sapling/address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ SaplingSpendingKey SaplingSpendingKey::random() {
}
}

boost::optional<SaplingPaymentAddress> SaplingIncomingViewingKey::address(diversifier_t d) const {
Optional<SaplingPaymentAddress> SaplingIncomingViewingKey::address(diversifier_t d) const {
uint256 pk_d;
if (librustzcash_check_diversifier(d.data())) {
librustzcash_ivk_to_pkd(this->begin(), d.data(), pk_d.begin());
return SaplingPaymentAddress(d, pk_d);
} else {
return boost::none;
return nullopt;
}
}

SaplingPaymentAddress SaplingSpendingKey::default_address() const {
// Iterates within default_diversifier to ensure a valid address is returned
auto addrOpt = full_viewing_key().in_viewing_key().address(default_diversifier(*this));
assert(addrOpt != boost::none);
assert(addrOpt != nullopt);
return addrOpt.value();
}

Expand Down
4 changes: 2 additions & 2 deletions src/sapling/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "sapling/sapling.h"

#include <array>
#include <boost/optional.hpp>
#include "optional.h"
#include <boost/variant.hpp>

namespace libzcash {
Expand Down Expand Up @@ -50,7 +50,7 @@ class SaplingIncomingViewingKey : public uint256 {
SaplingIncomingViewingKey(uint256 ivk) : uint256(ivk) { }

// Can pass in diversifier for Sapling addr
boost::optional<SaplingPaymentAddress> address(diversifier_t d) const;
Optional<SaplingPaymentAddress> address(diversifier_t d) const;
};

class SaplingFullViewingKey {
Expand Down
8 changes: 4 additions & 4 deletions src/sapling/incrementalmerkletree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,17 +932,17 @@ void IncrementalMerkleTree<Depth, Hash>::append(Hash obj) {
right = obj;
} else {
// Combine the leaves and propagate it up the tree
boost::optional<Hash> combined = Hash::combine(*left, *right, 0);
Optional<Hash> combined = Hash::combine(*left, *right, 0);

// Set the "left" leaf to the object and make the "right" leaf none
left = obj;
right = boost::none;
right = nullopt;

for (size_t i = 0; i < Depth; i++) {
if (i < parents.size()) {
if (parents[i]) {
combined = Hash::combine(*parents[i], *combined, i+1);
parents[i] = boost::none;
parents[i] = nullopt;
} else {
parents[i] = *combined;
break;
Expand Down Expand Up @@ -1119,7 +1119,7 @@ void IncrementalWitness<Depth, Hash>::append(Hash obj) {

if (cursor->is_complete(cursor_depth)) {
filled.push_back(cursor->root(cursor_depth));
cursor = boost::none;
cursor = nullopt;
}
} else {
cursor_depth = tree.next_depth(filled.size());
Expand Down
40 changes: 20 additions & 20 deletions src/sapling/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SaplingNote::SaplingNote(const SaplingPaymentAddress& address, const uint64_t va
}

// Call librustzcash to compute the commitment
boost::optional<uint256> SaplingNote::cmu() const
Optional<uint256> SaplingNote::cmu() const
{
uint256 result;
if (!librustzcash_sapling_compute_cm(
Expand All @@ -33,14 +33,14 @@ boost::optional<uint256> SaplingNote::cmu() const
result.begin()
))
{
return boost::none;
return nullopt;
}

return result;
}

// Call librustzcash to compute the nullifier
boost::optional<uint256> SaplingNote::nullifier(const SaplingFullViewingKey& vk, const uint64_t position) const
Optional<uint256> SaplingNote::nullifier(const SaplingFullViewingKey& vk, const uint64_t position) const
{
auto ak = vk.ak;
auto nk = vk.nk;
Expand All @@ -57,7 +57,7 @@ boost::optional<uint256> SaplingNote::nullifier(const SaplingFullViewingKey& vk,
result.begin()
))
{
return boost::none;
return nullopt;
}

return result;
Expand All @@ -73,17 +73,17 @@ SaplingNotePlaintext::SaplingNotePlaintext(
}


boost::optional<SaplingNote> SaplingNotePlaintext::note(const SaplingIncomingViewingKey& ivk) const
Optional<SaplingNote> SaplingNotePlaintext::note(const SaplingIncomingViewingKey& ivk) const
{
auto addr = ivk.address(d);
if (addr) {
return SaplingNote(d, addr.get().pk_d, value_, rcm);
} else {
return boost::none;
return nullopt;
}
}

boost::optional<SaplingOutgoingPlaintext> SaplingOutgoingPlaintext::decrypt(
Optional<SaplingOutgoingPlaintext> SaplingOutgoingPlaintext::decrypt(
const SaplingOutCiphertext& ciphertext,
const uint256& ovk,
const uint256& cv,
Expand All @@ -93,7 +93,7 @@ boost::optional<SaplingOutgoingPlaintext> SaplingOutgoingPlaintext::decrypt(
{
auto pt = AttemptSaplingOutDecryption(ciphertext, ovk, cv, cm, epk);
if (!pt) {
return boost::none;
return nullopt;
}

// Deserialize from the plaintext
Expand All @@ -108,7 +108,7 @@ boost::optional<SaplingOutgoingPlaintext> SaplingOutgoingPlaintext::decrypt(
return ret;
}

boost::optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(
Optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(
const SaplingEncCiphertext& ciphertext,
const uint256& ivk,
const uint256& epk,
Expand All @@ -117,7 +117,7 @@ boost::optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(
{
auto pt = AttemptSaplingEncDecryption(ciphertext, ivk, epk);
if (!pt) {
return boost::none;
return nullopt;
}

// Deserialize from the plaintext
Expand All @@ -131,7 +131,7 @@ boost::optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(

uint256 pk_d;
if (!librustzcash_ivk_to_pkd(ivk.begin(), ret.d.data(), pk_d.begin())) {
return boost::none;
return nullopt;
}

uint256 cmu_expected;
Expand All @@ -143,17 +143,17 @@ boost::optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(
cmu_expected.begin()
))
{
return boost::none;
return nullopt;
}

if (cmu_expected != cmu) {
return boost::none;
return nullopt;
}

return ret;
}

boost::optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(
Optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(
const SaplingEncCiphertext& ciphertext,
const uint256& epk,
const uint256& esk,
Expand All @@ -163,7 +163,7 @@ boost::optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(
{
auto pt = AttemptSaplingEncDecryption(ciphertext, epk, esk, pk_d);
if (!pt) {
return boost::none;
return nullopt;
}

// Deserialize from the plaintext
Expand All @@ -182,24 +182,24 @@ boost::optional<SaplingNotePlaintext> SaplingNotePlaintext::decrypt(
cmu_expected.begin()
))
{
return boost::none;
return nullopt;
}

if (cmu_expected != cmu) {
return boost::none;
return nullopt;
}

assert(ss.size() == 0);

return ret;
}

boost::optional<SaplingNotePlaintextEncryptionResult> SaplingNotePlaintext::encrypt(const uint256& pk_d) const
Optional<SaplingNotePlaintextEncryptionResult> SaplingNotePlaintext::encrypt(const uint256& pk_d) const
{
// Get the encryptor
auto sne = SaplingNoteEncryption::FromDiversifier(d);
if (!sne) {
return boost::none;
return nullopt;
}
auto enc = sne.get();

Expand All @@ -213,7 +213,7 @@ boost::optional<SaplingNotePlaintextEncryptionResult> SaplingNotePlaintext::encr
// Encrypt the plaintext
auto encciphertext = enc.encrypt_to_recipient(pk_d, pt);
if (!encciphertext) {
return boost::none;
return nullopt;
}
return SaplingNotePlaintextEncryptionResult(encciphertext.get(), enc);
}
Expand Down
16 changes: 8 additions & 8 deletions src/sapling/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "uint256.h"

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

namespace libzcash {

Expand Down Expand Up @@ -41,8 +41,8 @@ class SaplingNote : public BaseNote {
SaplingNote(const SaplingPaymentAddress& address, uint64_t value);
virtual ~SaplingNote() {};

boost::optional<uint256> cmu() const;
boost::optional<uint256> nullifier(const SaplingFullViewingKey& vk, const uint64_t position) const;
Optional<uint256> cmu() const;
Optional<uint256> nullifier(const SaplingFullViewingKey& vk, const uint64_t position) const;
};

class BaseNotePlaintext {
Expand Down Expand Up @@ -72,22 +72,22 @@ class SaplingNotePlaintext : public BaseNotePlaintext {
SaplingNotePlaintext(const SaplingNote& note, const std::array<unsigned char, ZC_MEMO_SIZE>& memo);
virtual ~SaplingNotePlaintext() {}

static boost::optional<SaplingNotePlaintext> decrypt(
static Optional<SaplingNotePlaintext> decrypt(
const SaplingEncCiphertext& ciphertext,
const uint256& ivk,
const uint256& epk,
const uint256& cmu
);

static boost::optional<SaplingNotePlaintext> decrypt(
static Optional<SaplingNotePlaintext> decrypt(
const SaplingEncCiphertext& ciphertext,
const uint256& epk,
const uint256& esk,
const uint256& pk_d,
const uint256& cmu
);

boost::optional<SaplingNote> note(const SaplingIncomingViewingKey& ivk) const;
Optional<SaplingNote> note(const SaplingIncomingViewingKey& ivk) const;

SERIALIZE_METHODS(SaplingNotePlaintext, obj)
{
Expand All @@ -104,7 +104,7 @@ class SaplingNotePlaintext : public BaseNotePlaintext {
READWRITE(obj.memo_); // 512 bytes
}

boost::optional<SaplingNotePlaintextEncryptionResult> encrypt(const uint256& pk_d) const;
Optional<SaplingNotePlaintextEncryptionResult> encrypt(const uint256& pk_d) const;
};

class SaplingOutgoingPlaintext
Expand All @@ -125,7 +125,7 @@ class SaplingOutgoingPlaintext
READWRITE(obj.esk); // 8 bytes
}

static boost::optional<SaplingOutgoingPlaintext> decrypt(
static Optional<SaplingOutgoingPlaintext> decrypt(
const SaplingOutCiphertext& ciphertext,
const uint256& ovk,
const uint256& cv,
Expand Down
24 changes: 12 additions & 12 deletions src/sapling/noteencryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void KDF(unsigned char K[NOTEENCRYPTION_CIPHER_KEYSIZE],

namespace libzcash {

boost::optional<SaplingNoteEncryption> SaplingNoteEncryption::FromDiversifier(diversifier_t d) {
Optional<SaplingNoteEncryption> SaplingNoteEncryption::FromDiversifier(diversifier_t d) {
uint256 epk;
uint256 esk;

Expand All @@ -118,13 +118,13 @@ boost::optional<SaplingNoteEncryption> SaplingNoteEncryption::FromDiversifier(di

// Compute epk given the diversifier
if (!librustzcash_sapling_ka_derivepublic(d.begin(), esk.begin(), epk.begin())) {
return boost::none;
return nullopt;
}

return SaplingNoteEncryption(epk, esk);
}

boost::optional<SaplingEncCiphertext> SaplingNoteEncryption::encrypt_to_recipient(
Optional<SaplingEncCiphertext> SaplingNoteEncryption::encrypt_to_recipient(
const uint256 &pk_d,
const SaplingEncPlaintext &message
)
Expand All @@ -136,7 +136,7 @@ boost::optional<SaplingEncCiphertext> SaplingNoteEncryption::encrypt_to_recipien
uint256 dhsecret;

if (!librustzcash_sapling_ka_agree(pk_d.begin(), esk.begin(), dhsecret.begin())) {
return boost::none;
return nullopt;
}

// Construct the symmetric key
Expand All @@ -160,7 +160,7 @@ boost::optional<SaplingEncCiphertext> SaplingNoteEncryption::encrypt_to_recipien
return ciphertext;
}

boost::optional<SaplingEncPlaintext> AttemptSaplingEncDecryption(
Optional<SaplingEncPlaintext> AttemptSaplingEncDecryption(
const SaplingEncCiphertext &ciphertext,
const uint256 &ivk,
const uint256 &epk
Expand All @@ -169,7 +169,7 @@ boost::optional<SaplingEncPlaintext> AttemptSaplingEncDecryption(
uint256 dhsecret;

if (!librustzcash_sapling_ka_agree(epk.begin(), ivk.begin(), dhsecret.begin())) {
return boost::none;
return nullopt;
}

// Construct the symmetric key
Expand All @@ -189,13 +189,13 @@ boost::optional<SaplingEncPlaintext> AttemptSaplingEncDecryption(
0,
cipher_nonce, K) != 0)
{
return boost::none;
return nullopt;
}

return plaintext;
}

boost::optional<SaplingEncPlaintext> AttemptSaplingEncDecryption (
Optional<SaplingEncPlaintext> AttemptSaplingEncDecryption (
const SaplingEncCiphertext &ciphertext,
const uint256 &epk,
const uint256 &esk,
Expand All @@ -205,7 +205,7 @@ boost::optional<SaplingEncPlaintext> AttemptSaplingEncDecryption (
uint256 dhsecret;

if (!librustzcash_sapling_ka_agree(pk_d.begin(), esk.begin(), dhsecret.begin())) {
return boost::none;
return nullopt;
}

// Construct the symmetric key
Expand All @@ -225,7 +225,7 @@ boost::optional<SaplingEncPlaintext> AttemptSaplingEncDecryption (
0,
cipher_nonce, K) != 0)
{
return boost::none;
return nullopt;
}

return plaintext;
Expand Down Expand Up @@ -264,7 +264,7 @@ SaplingOutCiphertext SaplingNoteEncryption::encrypt_to_ourselves(
return ciphertext;
}

boost::optional<SaplingOutPlaintext> AttemptSaplingOutDecryption(
Optional<SaplingOutPlaintext> AttemptSaplingOutDecryption(
const SaplingOutCiphertext &ciphertext,
const uint256 &ovk,
const uint256 &cv,
Expand All @@ -289,7 +289,7 @@ boost::optional<SaplingOutPlaintext> AttemptSaplingOutDecryption(
0,
cipher_nonce, K) != 0)
{
return boost::none;
return nullopt;
}

return plaintext;
Expand Down
Loading

0 comments on commit fdd4045

Please sign in to comment.