Duke Leto
4 years ago
19 changed files with 52 additions and 1183 deletions
@ -1,90 +0,0 @@ |
|||
#include <gtest/gtest.h> |
|||
|
|||
#include "primitives/transaction.h" |
|||
#include "zcash/Note.hpp" |
|||
#include "zcash/Address.hpp" |
|||
|
|||
#include <array> |
|||
|
|||
extern ZCJoinSplit* params; |
|||
extern int GenZero(int n); |
|||
extern int GenMax(int n); |
|||
|
|||
TEST(Transaction, JSDescriptionRandomized) { |
|||
// construct a merkle tree
|
|||
SproutMerkleTree merkleTree; |
|||
|
|||
libzcash::SproutSpendingKey k = libzcash::SproutSpendingKey::random(); |
|||
libzcash::SproutPaymentAddress addr = k.address(); |
|||
|
|||
libzcash::SproutNote note(addr.a_pk, 100, uint256(), uint256()); |
|||
|
|||
// commitment from coin
|
|||
uint256 commitment = note.cm(); |
|||
|
|||
// insert commitment into the merkle tree
|
|||
merkleTree.append(commitment); |
|||
|
|||
// compute the merkle root we will be working with
|
|||
uint256 rt = merkleTree.root(); |
|||
|
|||
auto witness = merkleTree.witness(); |
|||
|
|||
// create JSDescription
|
|||
uint256 joinSplitPubKey; |
|||
std::array<libzcash::JSInput, ZC_NUM_JS_INPUTS> inputs = { |
|||
libzcash::JSInput(witness, note, k), |
|||
libzcash::JSInput() // dummy input of zero value
|
|||
}; |
|||
std::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS> outputs = { |
|||
libzcash::JSOutput(addr, 50), |
|||
libzcash::JSOutput(addr, 50) |
|||
}; |
|||
std::array<size_t, ZC_NUM_JS_INPUTS> inputMap; |
|||
std::array<size_t, ZC_NUM_JS_OUTPUTS> outputMap; |
|||
|
|||
{ |
|||
auto jsdesc = JSDescription::Randomized( |
|||
false, |
|||
*params, joinSplitPubKey, rt, |
|||
inputs, outputs, |
|||
inputMap, outputMap, |
|||
0, 0, false); |
|||
|
|||
std::set<size_t> inputSet(inputMap.begin(), inputMap.end()); |
|||
std::set<size_t> expectedInputSet {0, 1}; |
|||
EXPECT_EQ(expectedInputSet, inputSet); |
|||
|
|||
std::set<size_t> outputSet(outputMap.begin(), outputMap.end()); |
|||
std::set<size_t> expectedOutputSet {0, 1}; |
|||
EXPECT_EQ(expectedOutputSet, outputSet); |
|||
} |
|||
|
|||
{ |
|||
auto jsdesc = JSDescription::Randomized( |
|||
false, |
|||
*params, joinSplitPubKey, rt, |
|||
inputs, outputs, |
|||
inputMap, outputMap, |
|||
0, 0, false, nullptr, GenZero); |
|||
|
|||
std::array<size_t, ZC_NUM_JS_INPUTS> expectedInputMap {1, 0}; |
|||
std::array<size_t, ZC_NUM_JS_OUTPUTS> expectedOutputMap {1, 0}; |
|||
EXPECT_EQ(expectedInputMap, inputMap); |
|||
EXPECT_EQ(expectedOutputMap, outputMap); |
|||
} |
|||
|
|||
{ |
|||
auto jsdesc = JSDescription::Randomized( |
|||
false, |
|||
*params, joinSplitPubKey, rt, |
|||
inputs, outputs, |
|||
inputMap, outputMap, |
|||
0, 0, false, nullptr, GenMax); |
|||
|
|||
std::array<size_t, ZC_NUM_JS_INPUTS> expectedInputMap {0, 1}; |
|||
std::array<size_t, ZC_NUM_JS_OUTPUTS> expectedOutputMap {0, 1}; |
|||
EXPECT_EQ(expectedInputMap, inputMap); |
|||
EXPECT_EQ(expectedOutputMap, outputMap); |
|||
} |
|||
} |
@ -1,154 +0,0 @@ |
|||
// Copyright (c) 2016 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.
|
|||
|
|||
#include "utiltest.h" |
|||
|
|||
#include "consensus/upgrades.h" |
|||
|
|||
#include <array> |
|||
|
|||
CWalletTx GetValidReceive(ZCJoinSplit& params, |
|||
const libzcash::SproutSpendingKey& sk, CAmount value, |
|||
bool randomInputs, |
|||
int32_t version /* = 2 */) { |
|||
CMutableTransaction mtx; |
|||
mtx.nVersion = version; |
|||
mtx.vin.resize(2); |
|||
if (randomInputs) { |
|||
mtx.vin[0].prevout.hash = GetRandHash(); |
|||
mtx.vin[1].prevout.hash = GetRandHash(); |
|||
} else { |
|||
mtx.vin[0].prevout.hash = uint256S("0000000000000000000000000000000000000000000000000000000000000001"); |
|||
mtx.vin[1].prevout.hash = uint256S("0000000000000000000000000000000000000000000000000000000000000002"); |
|||
} |
|||
mtx.vin[0].prevout.n = 0; |
|||
mtx.vin[1].prevout.n = 0; |
|||
|
|||
// Generate an ephemeral keypair.
|
|||
uint256 joinSplitPubKey; |
|||
unsigned char joinSplitPrivKey[crypto_sign_SECRETKEYBYTES]; |
|||
crypto_sign_keypair(joinSplitPubKey.begin(), joinSplitPrivKey); |
|||
mtx.joinSplitPubKey = joinSplitPubKey; |
|||
|
|||
std::array<libzcash::JSInput, 2> inputs = { |
|||
libzcash::JSInput(), // dummy input
|
|||
libzcash::JSInput() // dummy input
|
|||
}; |
|||
|
|||
std::array<libzcash::JSOutput, 2> outputs = { |
|||
libzcash::JSOutput(sk.address(), value), |
|||
libzcash::JSOutput(sk.address(), value) |
|||
}; |
|||
|
|||
// Prepare JoinSplits
|
|||
uint256 rt; |
|||
JSDescription jsdesc {params, mtx.joinSplitPubKey, rt, |
|||
inputs, outputs, 2*value, 0, false}; |
|||
mtx.vjoinsplit.push_back(jsdesc); |
|||
|
|||
if (version >= 4) { |
|||
// Shielded Output
|
|||
OutputDescription od; |
|||
mtx.vShieldedOutput.push_back(od); |
|||
} |
|||
|
|||
// Empty output script.
|
|||
uint32_t consensusBranchId = SPROUT_BRANCH_ID; |
|||
CScript scriptCode; |
|||
CTransaction signTx(mtx); |
|||
uint256 dataToBeSigned = SignatureHash(scriptCode, signTx, NOT_AN_INPUT, SIGHASH_ALL, 0, consensusBranchId); |
|||
|
|||
// Add the signature
|
|||
assert(crypto_sign_detached(&mtx.joinSplitSig[0], NULL, |
|||
dataToBeSigned.begin(), 32, |
|||
joinSplitPrivKey |
|||
) == 0); |
|||
|
|||
CTransaction tx {mtx}; |
|||
CWalletTx wtx {NULL, tx}; |
|||
return wtx; |
|||
} |
|||
|
|||
libzcash::SproutNote GetNote(ZCJoinSplit& params, |
|||
const libzcash::SproutSpendingKey& sk, |
|||
const CTransaction& tx, size_t js, size_t n) { |
|||
ZCNoteDecryption decryptor {sk.receiving_key()}; |
|||
auto hSig = tx.vjoinsplit[js].h_sig(params, tx.joinSplitPubKey); |
|||
auto note_pt = libzcash::SproutNotePlaintext::decrypt( |
|||
decryptor, |
|||
tx.vjoinsplit[js].ciphertexts[n], |
|||
tx.vjoinsplit[js].ephemeralKey, |
|||
hSig, |
|||
(unsigned char) n); |
|||
return note_pt.note(sk.address()); |
|||
} |
|||
|
|||
CWalletTx GetValidSpend(ZCJoinSplit& params, |
|||
const libzcash::SproutSpendingKey& sk, |
|||
const libzcash::SproutNote& note, CAmount value) { |
|||
CMutableTransaction mtx; |
|||
mtx.vout.resize(2); |
|||
mtx.vout[0].nValue = value; |
|||
mtx.vout[1].nValue = 0; |
|||
|
|||
// Generate an ephemeral keypair.
|
|||
uint256 joinSplitPubKey; |
|||
unsigned char joinSplitPrivKey[crypto_sign_SECRETKEYBYTES]; |
|||
crypto_sign_keypair(joinSplitPubKey.begin(), joinSplitPrivKey); |
|||
mtx.joinSplitPubKey = joinSplitPubKey; |
|||
|
|||
// Fake tree for the unused witness
|
|||
SproutMerkleTree tree; |
|||
|
|||
libzcash::JSOutput dummyout; |
|||
libzcash::JSInput dummyin; |
|||
|
|||
{ |
|||
if (note.value() > value) { |
|||
libzcash::SproutSpendingKey dummykey = libzcash::SproutSpendingKey::random(); |
|||
libzcash::SproutPaymentAddress dummyaddr = dummykey.address(); |
|||
dummyout = libzcash::JSOutput(dummyaddr, note.value() - value); |
|||
} else if (note.value() < value) { |
|||
libzcash::SproutSpendingKey dummykey = libzcash::SproutSpendingKey::random(); |
|||
libzcash::SproutPaymentAddress dummyaddr = dummykey.address(); |
|||
libzcash::SproutNote dummynote(dummyaddr.a_pk, (value - note.value()), uint256(), uint256()); |
|||
tree.append(dummynote.cm()); |
|||
dummyin = libzcash::JSInput(tree.witness(), dummynote, dummykey); |
|||
} |
|||
} |
|||
|
|||
tree.append(note.cm()); |
|||
|
|||
std::array<libzcash::JSInput, 2> inputs = { |
|||
libzcash::JSInput(tree.witness(), note, sk), |
|||
dummyin |
|||
}; |
|||
|
|||
std::array<libzcash::JSOutput, 2> outputs = { |
|||
dummyout, // dummy output
|
|||
libzcash::JSOutput() // dummy output
|
|||
}; |
|||
|
|||
// Prepare JoinSplits
|
|||
uint256 rt = tree.root(); |
|||
JSDescription jsdesc {params, mtx.joinSplitPubKey, rt, |
|||
inputs, outputs, 0, value, false}; |
|||
mtx.vjoinsplit.push_back(jsdesc); |
|||
|
|||
// Empty output script.
|
|||
uint32_t consensusBranchId = SPROUT_BRANCH_ID; |
|||
CScript scriptCode; |
|||
CTransaction signTx(mtx); |
|||
uint256 dataToBeSigned = SignatureHash(scriptCode, signTx, NOT_AN_INPUT, SIGHASH_ALL, 0, consensusBranchId); |
|||
|
|||
// Add the signature
|
|||
assert(crypto_sign_detached(&mtx.joinSplitSig[0], NULL, |
|||
dataToBeSigned.begin(), 32, |
|||
joinSplitPrivKey |
|||
) == 0); |
|||
CTransaction tx {mtx}; |
|||
CWalletTx wtx {NULL, tx}; |
|||
return wtx; |
|||
} |
@ -1,19 +0,0 @@ |
|||
// Copyright (c) 2016 The Zcash developers
|
|||
// Distributed under the MIT software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
|
|||
#include "wallet/wallet.h" |
|||
#include "zcash/JoinSplit.hpp" |
|||
#include "zcash/Note.hpp" |
|||
#include "zcash/NoteEncryption.hpp" |
|||
|
|||
CWalletTx GetValidReceive(ZCJoinSplit& params, |
|||
const libzcash::SproutSpendingKey& sk, CAmount value, |
|||
bool randomInputs, |
|||
int32_t version = 2); |
|||
libzcash::SproutNote GetNote(ZCJoinSplit& params, |
|||
const libzcash::SproutSpendingKey& sk, |
|||
const CTransaction& tx, size_t js, size_t n); |
|||
CWalletTx GetValidSpend(ZCJoinSplit& params, |
|||
const libzcash::SproutSpendingKey& sk, |
|||
const libzcash::SproutNote& note, CAmount value); |
Loading…
Reference in new issue