desprout
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
|
||||
#ifndef ZC_ADDRESS_H_
|
||||
#define ZC_ADDRESS_H_
|
||||
|
||||
@@ -26,34 +28,6 @@ const size_t SerializedSaplingSpendingKeySize = 32;
|
||||
|
||||
typedef std::array<unsigned char, ZC_DIVERSIFIER_SIZE> diversifier_t;
|
||||
|
||||
class SproutPaymentAddress {
|
||||
public:
|
||||
uint256 a_pk;
|
||||
uint256 pk_enc;
|
||||
|
||||
SproutPaymentAddress() : a_pk(), pk_enc() { }
|
||||
SproutPaymentAddress(uint256 a_pk, uint256 pk_enc) : a_pk(a_pk), pk_enc(pk_enc) { }
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||
READWRITE(a_pk);
|
||||
READWRITE(pk_enc);
|
||||
}
|
||||
|
||||
//! Get the 256-bit SHA256d hash of this payment address.
|
||||
uint256 GetHash() const;
|
||||
|
||||
friend inline bool operator==(const SproutPaymentAddress& a, const SproutPaymentAddress& b) {
|
||||
return a.a_pk == b.a_pk && a.pk_enc == b.pk_enc;
|
||||
}
|
||||
friend inline bool operator<(const SproutPaymentAddress& a, const SproutPaymentAddress& b) {
|
||||
return (a.a_pk < b.a_pk ||
|
||||
(a.a_pk == b.a_pk && a.pk_enc < b.pk_enc));
|
||||
}
|
||||
};
|
||||
|
||||
class ReceivingKey : public uint256 {
|
||||
public:
|
||||
ReceivingKey() { }
|
||||
@@ -62,45 +36,6 @@ public:
|
||||
uint256 pk_enc() const;
|
||||
};
|
||||
|
||||
class SproutViewingKey {
|
||||
public:
|
||||
uint256 a_pk;
|
||||
ReceivingKey sk_enc;
|
||||
|
||||
SproutViewingKey() : a_pk(), sk_enc() { }
|
||||
SproutViewingKey(uint256 a_pk, ReceivingKey sk_enc) : a_pk(a_pk), sk_enc(sk_enc) { }
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||
READWRITE(a_pk);
|
||||
READWRITE(sk_enc);
|
||||
}
|
||||
|
||||
SproutPaymentAddress address() const;
|
||||
|
||||
friend inline bool operator==(const SproutViewingKey& a, const SproutViewingKey& b) {
|
||||
return a.a_pk == b.a_pk && a.sk_enc == b.sk_enc;
|
||||
}
|
||||
friend inline bool operator<(const SproutViewingKey& a, const SproutViewingKey& b) {
|
||||
return (a.a_pk < b.a_pk ||
|
||||
(a.a_pk == b.a_pk && a.sk_enc < b.sk_enc));
|
||||
}
|
||||
};
|
||||
|
||||
class SproutSpendingKey : public uint252 {
|
||||
public:
|
||||
SproutSpendingKey() : uint252() { }
|
||||
SproutSpendingKey(uint252 a_sk) : uint252(a_sk) { }
|
||||
|
||||
static SproutSpendingKey random();
|
||||
|
||||
ReceivingKey receiving_key() const;
|
||||
SproutViewingKey viewing_key() const;
|
||||
SproutPaymentAddress address() const;
|
||||
};
|
||||
|
||||
//! Sapling functions.
|
||||
class SaplingPaymentAddress {
|
||||
public:
|
||||
@@ -218,8 +153,8 @@ public:
|
||||
SaplingPaymentAddress default_address() const;
|
||||
};
|
||||
|
||||
typedef boost::variant<InvalidEncoding, SproutPaymentAddress, SaplingPaymentAddress> PaymentAddress;
|
||||
typedef boost::variant<InvalidEncoding, SproutViewingKey, SaplingIncomingViewingKey> ViewingKey;
|
||||
typedef boost::variant<InvalidEncoding, SaplingPaymentAddress> PaymentAddress;
|
||||
typedef boost::variant<InvalidEncoding, SaplingIncomingViewingKey> ViewingKey;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -246,24 +246,6 @@ uint256 JoinSplit<NumInputs, NumOutputs>::h_sig(
|
||||
return output;
|
||||
}
|
||||
|
||||
SproutNote JSOutput::note(const uint252& phi, const uint256& r, size_t i, const uint256& h_sig) const {
|
||||
uint256 rho = PRF_rho(phi, i, h_sig);
|
||||
|
||||
return SproutNote(addr.a_pk, value, rho, r);
|
||||
}
|
||||
|
||||
JSOutput::JSOutput() : addr(uint256(), uint256()), value(0) {
|
||||
SproutSpendingKey a_sk = SproutSpendingKey::random();
|
||||
addr = a_sk.address();
|
||||
}
|
||||
|
||||
JSInput::JSInput() : witness(SproutMerkleTree().witness()),
|
||||
key(SproutSpendingKey::random()) {
|
||||
note = SproutNote(key.address().a_pk, 0, random_uint256(), random_uint256());
|
||||
SproutMerkleTree dummy_tree;
|
||||
dummy_tree.append(note.cm());
|
||||
witness = dummy_tree.witness();
|
||||
}
|
||||
|
||||
template class JoinSplit<ZC_NUM_JS_INPUTS,
|
||||
ZC_NUM_JS_OUTPUTS>;
|
||||
|
||||
@@ -24,69 +24,13 @@ typedef std::array<unsigned char, GROTH_PROOF_SIZE> GrothProof;
|
||||
typedef boost::variant<PHGRProof, GrothProof> SproutProof;
|
||||
|
||||
class JSInput {
|
||||
public:
|
||||
SproutWitness witness;
|
||||
SproutNote note;
|
||||
SproutSpendingKey key;
|
||||
|
||||
JSInput();
|
||||
JSInput(SproutWitness witness,
|
||||
SproutNote note,
|
||||
SproutSpendingKey key) : witness(witness), note(note), key(key) { }
|
||||
|
||||
uint256 nullifier() const {
|
||||
return note.nullifier(key);
|
||||
}
|
||||
};
|
||||
|
||||
class JSOutput {
|
||||
public:
|
||||
SproutPaymentAddress addr;
|
||||
uint64_t value;
|
||||
std::array<unsigned char, ZC_MEMO_SIZE> memo = {{0xF6}}; // 0xF6 is invalid UTF8 as per spec, rest of array is 0x00
|
||||
|
||||
JSOutput();
|
||||
JSOutput(SproutPaymentAddress addr, uint64_t value) : addr(addr), value(value) { }
|
||||
|
||||
SproutNote note(const uint252& phi, const uint256& r, size_t i, const uint256& h_sig) const;
|
||||
};
|
||||
|
||||
template<size_t NumInputs, size_t NumOutputs>
|
||||
class JoinSplit {
|
||||
public:
|
||||
virtual ~JoinSplit() {}
|
||||
|
||||
static JoinSplit<NumInputs, NumOutputs>* Prepared();
|
||||
|
||||
static uint256 h_sig(const uint256& randomSeed,
|
||||
const std::array<uint256, NumInputs>& nullifiers,
|
||||
const uint256& joinSplitPubKey
|
||||
);
|
||||
|
||||
// Compute nullifiers, macs, note commitments & encryptions, and SNARK proof
|
||||
virtual SproutProof prove(
|
||||
const std::array<JSInput, NumInputs>& inputs,
|
||||
const std::array<JSOutput, NumOutputs>& outputs,
|
||||
std::array<SproutNote, NumOutputs>& out_notes,
|
||||
std::array<ZCNoteEncryption::Ciphertext, NumOutputs>& out_ciphertexts,
|
||||
uint256& out_ephemeralKey,
|
||||
const uint256& joinSplitPubKey,
|
||||
uint256& out_randomSeed,
|
||||
std::array<uint256, NumInputs>& out_hmacs,
|
||||
std::array<uint256, NumInputs>& out_nullifiers,
|
||||
std::array<uint256, NumOutputs>& out_commitments,
|
||||
uint64_t vpub_old,
|
||||
uint64_t vpub_new,
|
||||
const uint256& rt,
|
||||
bool computeProof = true,
|
||||
// For paymentdisclosure, we need to retrieve the esk.
|
||||
// Reference as non-const parameter with default value leads to compile error.
|
||||
// So use pointer for simplicity.
|
||||
uint256 *out_esk = nullptr
|
||||
) = 0;
|
||||
|
||||
protected:
|
||||
JoinSplit() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
|
||||
#ifndef ZC_NOTE_H_
|
||||
#define ZC_NOTE_H_
|
||||
|
||||
@@ -22,25 +24,6 @@ public:
|
||||
inline uint64_t value() const { return value_; };
|
||||
};
|
||||
|
||||
class SproutNote : public BaseNote {
|
||||
public:
|
||||
uint256 a_pk;
|
||||
uint256 rho;
|
||||
uint256 r;
|
||||
|
||||
SproutNote(uint256 a_pk, uint64_t value, uint256 rho, uint256 r)
|
||||
: BaseNote(value), a_pk(a_pk), rho(rho), r(r) {}
|
||||
|
||||
SproutNote();
|
||||
|
||||
virtual ~SproutNote() {};
|
||||
|
||||
uint256 cm() const;
|
||||
|
||||
uint256 nullifier(const SproutSpendingKey& a_sk) const;
|
||||
};
|
||||
|
||||
|
||||
class SaplingNote : public BaseNote {
|
||||
public:
|
||||
diversifier_t d;
|
||||
@@ -74,48 +57,6 @@ public:
|
||||
inline const std::array<unsigned char, ZC_MEMO_SIZE> & memo() const { return memo_; }
|
||||
};
|
||||
|
||||
class SproutNotePlaintext : public BaseNotePlaintext {
|
||||
public:
|
||||
uint256 rho;
|
||||
uint256 r;
|
||||
|
||||
SproutNotePlaintext() {}
|
||||
|
||||
SproutNotePlaintext(const SproutNote& note, std::array<unsigned char, ZC_MEMO_SIZE> memo);
|
||||
|
||||
SproutNote note(const SproutPaymentAddress& addr) const;
|
||||
|
||||
virtual ~SproutNotePlaintext() {}
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||
unsigned char leadingByte = 0x00;
|
||||
READWRITE(leadingByte);
|
||||
|
||||
if (leadingByte != 0x00) {
|
||||
throw std::ios_base::failure("lead byte of SproutNotePlaintext is not recognized");
|
||||
}
|
||||
|
||||
READWRITE(value_);
|
||||
READWRITE(rho);
|
||||
READWRITE(r);
|
||||
READWRITE(memo_);
|
||||
}
|
||||
|
||||
static SproutNotePlaintext decrypt(const ZCNoteDecryption& decryptor,
|
||||
const ZCNoteDecryption::Ciphertext& ciphertext,
|
||||
const uint256& ephemeralKey,
|
||||
const uint256& h_sig,
|
||||
unsigned char nonce
|
||||
);
|
||||
|
||||
ZCNoteEncryption::Ciphertext encrypt(ZCNoteEncryption& encryptor,
|
||||
const uint256& pk_enc
|
||||
) const;
|
||||
};
|
||||
|
||||
typedef std::pair<SaplingEncCiphertext, SaplingNoteEncryption> SaplingNotePlaintextEncryptionResult;
|
||||
|
||||
class SaplingNotePlaintext : public BaseNotePlaintext {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
/*
|
||||
Zcash uses SHA256Compress as a PRF for various components
|
||||
Hush uses SHA256Compress as a PRF for various components
|
||||
within the zkSNARK circuit.
|
||||
*/
|
||||
|
||||
@@ -11,13 +12,6 @@ within the zkSNARK circuit.
|
||||
|
||||
#include <array>
|
||||
|
||||
//! Sprout functions
|
||||
uint256 PRF_addr_a_pk(const uint252& a_sk);
|
||||
uint256 PRF_addr_sk_enc(const uint252& a_sk);
|
||||
uint256 PRF_nf(const uint252& a_sk, const uint256& rho);
|
||||
uint256 PRF_pk(const uint252& a_sk, size_t i0, const uint256& h_sig);
|
||||
uint256 PRF_rho(const uint252& phi, size_t i0, const uint256& h_sig);
|
||||
|
||||
//! Sapling functions
|
||||
uint256 PRF_ask(const uint256& sk);
|
||||
uint256 PRF_nsk(const uint256& sk);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// Copyright (c) 2018 The Zcash developers
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
@@ -134,7 +135,7 @@ struct SaplingExtendedSpendingKey {
|
||||
}
|
||||
};
|
||||
|
||||
typedef boost::variant<InvalidEncoding, SproutSpendingKey, SaplingExtendedSpendingKey> SpendingKey;
|
||||
typedef boost::variant<InvalidEncoding, SaplingExtendedSpendingKey> SpendingKey;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user