Implement static method for creating a randomized JSDescription
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#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"
|
||||||
|
|
||||||
@@ -41,6 +42,29 @@ JSDescription::JSDescription(ZCJoinSplit& params,
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JSDescription JSDescription::Randomized(
|
||||||
|
ZCJoinSplit& params,
|
||||||
|
const uint256& pubKeyHash,
|
||||||
|
const uint256& anchor,
|
||||||
|
boost::array<libzcash::JSInput, ZC_NUM_JS_INPUTS>& inputs,
|
||||||
|
boost::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS>& outputs,
|
||||||
|
boost::array<size_t, ZC_NUM_JS_INPUTS>& inputMap,
|
||||||
|
boost::array<size_t, ZC_NUM_JS_OUTPUTS>& outputMap,
|
||||||
|
CAmount vpub_old,
|
||||||
|
CAmount vpub_new,
|
||||||
|
bool computeProof)
|
||||||
|
{
|
||||||
|
// Randomize the order of the inputs and outputs
|
||||||
|
inputMap = {0, 1};
|
||||||
|
outputMap = {0, 1};
|
||||||
|
MappedShuffle(inputs.begin(), inputMap.begin(), ZC_NUM_JS_INPUTS, GetRandInt);
|
||||||
|
MappedShuffle(outputs.begin(), outputMap.begin(), ZC_NUM_JS_OUTPUTS, GetRandInt);
|
||||||
|
|
||||||
|
return JSDescription(
|
||||||
|
params, pubKeyHash, anchor, inputs, outputs,
|
||||||
|
vpub_old, vpub_new, computeProof);
|
||||||
|
}
|
||||||
|
|
||||||
bool JSDescription::Verify(
|
bool JSDescription::Verify(
|
||||||
ZCJoinSplit& params,
|
ZCJoinSplit& params,
|
||||||
const uint256& pubKeyHash
|
const uint256& pubKeyHash
|
||||||
|
|||||||
@@ -78,6 +78,19 @@ public:
|
|||||||
bool computeProof = true // Set to false in some tests
|
bool computeProof = true // Set to false in some tests
|
||||||
);
|
);
|
||||||
|
|
||||||
|
static JSDescription Randomized(
|
||||||
|
ZCJoinSplit& params,
|
||||||
|
const uint256& pubKeyHash,
|
||||||
|
const uint256& rt,
|
||||||
|
boost::array<libzcash::JSInput, ZC_NUM_JS_INPUTS>& inputs,
|
||||||
|
boost::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS>& outputs,
|
||||||
|
boost::array<size_t, ZC_NUM_JS_INPUTS>& inputMap,
|
||||||
|
boost::array<size_t, ZC_NUM_JS_OUTPUTS>& outputMap,
|
||||||
|
CAmount vpub_old,
|
||||||
|
CAmount vpub_new,
|
||||||
|
bool computeProof = true // Set to false in some tests
|
||||||
|
);
|
||||||
|
|
||||||
// Verifies that the JoinSplit proof is correct.
|
// Verifies that the JoinSplit proof is correct.
|
||||||
bool Verify(ZCJoinSplit& params, const uint256& pubKeyHash) const;
|
bool Verify(ZCJoinSplit& params, const uint256& pubKeyHash) const;
|
||||||
|
|
||||||
|
|||||||
@@ -368,6 +368,32 @@ BOOST_AUTO_TEST_CASE(test_basic_joinsplit_verification)
|
|||||||
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;
|
delete p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user