Refactor test code to better test JSDescription::Randomized()
This commit is contained in:
@@ -4,6 +4,7 @@ bin_PROGRAMS += zcash-gtest
|
|||||||
# tool for generating our public parameters
|
# tool for generating our public parameters
|
||||||
zcash_gtest_SOURCES = \
|
zcash_gtest_SOURCES = \
|
||||||
gtest/main.cpp \
|
gtest/main.cpp \
|
||||||
|
gtest/utils.cpp \
|
||||||
gtest/test_checktransaction.cpp \
|
gtest/test_checktransaction.cpp \
|
||||||
gtest/json_test_vectors.cpp \
|
gtest/json_test_vectors.cpp \
|
||||||
gtest/json_test_vectors.h \
|
gtest/json_test_vectors.h \
|
||||||
@@ -19,6 +20,7 @@ zcash_gtest_SOURCES = \
|
|||||||
gtest/test_pow.cpp \
|
gtest/test_pow.cpp \
|
||||||
gtest/test_random.cpp \
|
gtest/test_random.cpp \
|
||||||
gtest/test_rpc.cpp \
|
gtest/test_rpc.cpp \
|
||||||
|
gtest/test_transaction.cpp \
|
||||||
gtest/test_circuit.cpp \
|
gtest/test_circuit.cpp \
|
||||||
gtest/test_txid.cpp \
|
gtest/test_txid.cpp \
|
||||||
gtest/test_libzcash_utils.cpp \
|
gtest/test_libzcash_utils.cpp \
|
||||||
|
|||||||
@@ -2,15 +2,8 @@
|
|||||||
|
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
|
|
||||||
int GenZero(int n)
|
extern int GenZero(int n);
|
||||||
{
|
extern int GenMax(int n);
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GenMax(int n)
|
|
||||||
{
|
|
||||||
return n-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(Random, MappedShuffle) {
|
TEST(Random, MappedShuffle) {
|
||||||
std::vector<int> a {8, 4, 6, 3, 5};
|
std::vector<int> a {8, 4, 6, 3, 5};
|
||||||
|
|||||||
85
src/gtest/test_transaction.cpp
Normal file
85
src/gtest/test_transaction.cpp
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "primitives/transaction.h"
|
||||||
|
#include "zcash/Note.hpp"
|
||||||
|
#include "zcash/Address.hpp"
|
||||||
|
|
||||||
|
extern ZCJoinSplit* params;
|
||||||
|
extern int GenZero(int n);
|
||||||
|
extern int GenMax(int n);
|
||||||
|
|
||||||
|
TEST(Transaction, JSDescriptionRandomized) {
|
||||||
|
// construct a merkle tree
|
||||||
|
ZCIncrementalMerkleTree merkleTree;
|
||||||
|
|
||||||
|
libzcash::SpendingKey k = libzcash::SpendingKey::random();
|
||||||
|
libzcash::PaymentAddress addr = k.address();
|
||||||
|
|
||||||
|
libzcash::Note 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 pubKeyHash;
|
||||||
|
boost::array<libzcash::JSInput, ZC_NUM_JS_INPUTS> inputs = {
|
||||||
|
libzcash::JSInput(witness, note, k),
|
||||||
|
libzcash::JSInput() // dummy input of zero value
|
||||||
|
};
|
||||||
|
boost::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS> outputs = {
|
||||||
|
libzcash::JSOutput(addr, 50),
|
||||||
|
libzcash::JSOutput(addr, 50)
|
||||||
|
};
|
||||||
|
boost::array<size_t, ZC_NUM_JS_INPUTS> inputMap;
|
||||||
|
boost::array<size_t, ZC_NUM_JS_OUTPUTS> outputMap;
|
||||||
|
|
||||||
|
{
|
||||||
|
auto jsdesc = JSDescription::Randomized(
|
||||||
|
*params, pubKeyHash, 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(
|
||||||
|
*params, pubKeyHash, rt,
|
||||||
|
inputs, outputs,
|
||||||
|
inputMap, outputMap,
|
||||||
|
0, 0, false, GenZero);
|
||||||
|
|
||||||
|
boost::array<size_t, ZC_NUM_JS_INPUTS> expectedInputMap {1, 0};
|
||||||
|
boost::array<size_t, ZC_NUM_JS_OUTPUTS> expectedOutputMap {1, 0};
|
||||||
|
EXPECT_EQ(expectedInputMap, inputMap);
|
||||||
|
EXPECT_EQ(expectedOutputMap, outputMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto jsdesc = JSDescription::Randomized(
|
||||||
|
*params, pubKeyHash, rt,
|
||||||
|
inputs, outputs,
|
||||||
|
inputMap, outputMap,
|
||||||
|
0, 0, false, GenMax);
|
||||||
|
|
||||||
|
boost::array<size_t, ZC_NUM_JS_INPUTS> expectedInputMap {0, 1};
|
||||||
|
boost::array<size_t, ZC_NUM_JS_OUTPUTS> expectedOutputMap {0, 1};
|
||||||
|
EXPECT_EQ(expectedInputMap, inputMap);
|
||||||
|
EXPECT_EQ(expectedOutputMap, outputMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/gtest/utils.cpp
Normal file
13
src/gtest/utils.cpp
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include "zcash/JoinSplit.hpp"
|
||||||
|
|
||||||
|
ZCJoinSplit* params = ZCJoinSplit::Unopened();
|
||||||
|
|
||||||
|
int GenZero(int n)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GenMax(int n)
|
||||||
|
{
|
||||||
|
return n-1;
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
#include "primitives/transaction.h"
|
#include "primitives/transaction.h"
|
||||||
|
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "random.h"
|
|
||||||
#include "tinyformat.h"
|
#include "tinyformat.h"
|
||||||
#include "utilstrencodings.h"
|
#include "utilstrencodings.h"
|
||||||
|
|
||||||
@@ -52,13 +51,14 @@ JSDescription JSDescription::Randomized(
|
|||||||
boost::array<size_t, ZC_NUM_JS_OUTPUTS>& outputMap,
|
boost::array<size_t, ZC_NUM_JS_OUTPUTS>& outputMap,
|
||||||
CAmount vpub_old,
|
CAmount vpub_old,
|
||||||
CAmount vpub_new,
|
CAmount vpub_new,
|
||||||
bool computeProof)
|
bool computeProof,
|
||||||
|
std::function<int(int)> gen)
|
||||||
{
|
{
|
||||||
// Randomize the order of the inputs and outputs
|
// Randomize the order of the inputs and outputs
|
||||||
inputMap = {0, 1};
|
inputMap = {0, 1};
|
||||||
outputMap = {0, 1};
|
outputMap = {0, 1};
|
||||||
MappedShuffle(inputs.begin(), inputMap.begin(), ZC_NUM_JS_INPUTS, GetRandInt);
|
MappedShuffle(inputs.begin(), inputMap.begin(), ZC_NUM_JS_INPUTS, gen);
|
||||||
MappedShuffle(outputs.begin(), outputMap.begin(), ZC_NUM_JS_OUTPUTS, GetRandInt);
|
MappedShuffle(outputs.begin(), outputMap.begin(), ZC_NUM_JS_OUTPUTS, gen);
|
||||||
|
|
||||||
return JSDescription(
|
return JSDescription(
|
||||||
params, pubKeyHash, anchor, inputs, outputs,
|
params, pubKeyHash, anchor, inputs, outputs,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#define BITCOIN_PRIMITIVES_TRANSACTION_H
|
#define BITCOIN_PRIMITIVES_TRANSACTION_H
|
||||||
|
|
||||||
#include "amount.h"
|
#include "amount.h"
|
||||||
|
#include "random.h"
|
||||||
#include "script/script.h"
|
#include "script/script.h"
|
||||||
#include "serialize.h"
|
#include "serialize.h"
|
||||||
#include "uint256.h"
|
#include "uint256.h"
|
||||||
@@ -88,7 +89,8 @@ public:
|
|||||||
boost::array<size_t, ZC_NUM_JS_OUTPUTS>& outputMap,
|
boost::array<size_t, ZC_NUM_JS_OUTPUTS>& outputMap,
|
||||||
CAmount vpub_old,
|
CAmount vpub_old,
|
||||||
CAmount vpub_new,
|
CAmount vpub_new,
|
||||||
bool computeProof = true // Set to false in some tests
|
bool computeProof = true, // Set to false in some tests
|
||||||
|
std::function<int(int)> gen = GetRandInt
|
||||||
);
|
);
|
||||||
|
|
||||||
// Verifies that the JoinSplit proof is correct.
|
// Verifies that the JoinSplit proof is correct.
|
||||||
|
|||||||
@@ -367,34 +367,6 @@ BOOST_AUTO_TEST_CASE(test_basic_joinsplit_verification)
|
|||||||
test.anchor = GetRandHash();
|
test.anchor = GetRandHash();
|
||||||
BOOST_CHECK(!test.Verify(*p, pubKeyHash));
|
BOOST_CHECK(!test.Verify(*p, pubKeyHash));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
boost::array<size_t, ZC_NUM_JS_INPUTS> inputMap;
|
|
||||||
boost::array<size_t, ZC_NUM_JS_OUTPUTS> outputMap;
|
|
||||||
auto jsdesc = JSDescription::Randomized(
|
|
||||||
*p, pubKeyHash, rt,
|
|
||||||
inputs, outputs,
|
|
||||||
inputMap, outputMap,
|
|
||||||
0, 0);
|
|
||||||
BOOST_CHECK(jsdesc.Verify(*p, pubKeyHash));
|
|
||||||
|
|
||||||
std::set<size_t> inputSet;
|
|
||||||
for (size_t i = 0; i < ZC_NUM_JS_INPUTS; i++) {
|
|
||||||
inputSet.insert(inputMap[i]);
|
|
||||||
}
|
|
||||||
std::set<size_t> expectedInputSet {0, 1};
|
|
||||||
BOOST_CHECK(expectedInputSet == inputSet);
|
|
||||||
|
|
||||||
std::set<size_t> outputSet;
|
|
||||||
for (size_t i = 0; i < ZC_NUM_JS_OUTPUTS; i++) {
|
|
||||||
outputSet.insert(outputMap[i]);
|
|
||||||
}
|
|
||||||
std::set<size_t> expectedOutputSet {0, 1};
|
|
||||||
BOOST_CHECK(expectedOutputSet == outputSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
delete p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(test_simple_joinsplit_invalidity)
|
BOOST_AUTO_TEST_CASE(test_simple_joinsplit_invalidity)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
using ::testing::Return;
|
using ::testing::Return;
|
||||||
|
|
||||||
ZCJoinSplit* params = ZCJoinSplit::Unopened();
|
extern ZCJoinSplit* params;
|
||||||
|
|
||||||
ACTION(ThrowLogicError) {
|
ACTION(ThrowLogicError) {
|
||||||
throw std::logic_error("Boom");
|
throw std::logic_error("Boom");
|
||||||
|
|||||||
Reference in New Issue
Block a user