simplify and speedup surjection proof caching

This commit is contained in:
Gregory Sanders 2018-04-25 10:45:37 -04:00
parent 9572165238
commit 4815bc62cd
3 changed files with 20 additions and 22 deletions

View file

@ -68,6 +68,13 @@ public:
CSHA256().Write(nonce.begin(), nonce.size()).Write(proof.data(), proof.size()).Write(commitment.data(), commitment.size()).Finalize(entry.begin()); CSHA256().Write(nonce.begin(), nonce.size()).Write(proof.data(), proof.size()).Write(commitment.data(), commitment.size()).Finalize(entry.begin());
} }
void ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& proof, const std::vector<unsigned char>& commitment)
{
CSHA256().Write(nonce.begin(), nonce.size()).Write(hash.begin(), 32).Write(proof.data(), proof.size()).Write(commitment.data(), commitment.size()).Finalize(entry.begin());
}
bool bool
Get(const uint256& entry, const bool erase) Get(const uint256& entry, const bool erase)
{ {
@ -190,31 +197,19 @@ bool CachingRangeProofChecker::VerifyRangeProof(const std::vector<unsigned char>
return true; return true;
} }
bool CachingSurjectionProofChecker::VerifySurjectionProof(secp256k1_surjectionproof& proof, std::vector<secp256k1_generator>& vTags, secp256k1_generator& gen, const secp256k1_context* secp256k1_ctx_verify_amounts) const bool CachingSurjectionProofChecker::VerifySurjectionProof(secp256k1_surjectionproof& proof, std::vector<secp256k1_generator>& vTags, secp256k1_generator& gen, const secp256k1_context* secp256k1_ctx_verify_amounts, const uint256& wtxid) const
{ {
// Serialize objects
// Serialize proof
std::vector<unsigned char> vchproof; std::vector<unsigned char> vchproof;
size_t proof_len = 0; size_t proof_len = 0;
vchproof.resize(secp256k1_surjectionproof_serialized_size(secp256k1_ctx_verify_amounts, &proof)); vchproof.resize(secp256k1_surjectionproof_serialized_size(secp256k1_ctx_verify_amounts, &proof));
secp256k1_surjectionproof_serialize(secp256k1_ctx_verify_amounts, &vchproof[0], &proof_len, &proof); secp256k1_surjectionproof_serialize(secp256k1_ctx_verify_amounts, &vchproof[0], &proof_len, &proof);
std::vector<unsigned char> tagCommit; // wtxid commits to all data including surj targets
tagCommit.resize(33); // we need to specify the proof and output asset point to be unique
CSHA256 sha2;
for (unsigned int i = 0; i <vTags.size(); i++) {
secp256k1_generator_serialize(secp256k1_ctx_verify_amounts, tagCommit.data(), &vTags[i]);
sha2.Write(tagCommit.data(), tagCommit.size());
}
tagCommit.resize(32);
sha2.Finalize(tagCommit.data());
std::vector<unsigned char> vchGen;
vchGen.resize(CConfidentialValue::nCommittedSize);
secp256k1_generator_serialize(secp256k1_ctx_verify_amounts, &vchGen[0], &gen);
CPubKey pubkey(vchGen);
uint256 entry; uint256 entry;
surjectionProofCache.ComputeEntry(entry, uint256(tagCommit), vchproof, pubkey, vchGen, CScript()); surjectionProofCache.ComputeEntry(entry, wtxid, vchproof, std::vector<unsigned char>(std::begin(gen.data), std::end(gen.data)));
if (surjectionProofCache.Get(entry, !store)) { if (surjectionProofCache.Get(entry, !store)) {
return true; return true;

View file

@ -55,7 +55,7 @@ public:
store = storeIn; store = storeIn;
}; };
bool VerifySurjectionProof(secp256k1_surjectionproof& proof, std::vector<secp256k1_generator>& vTags, secp256k1_generator& gen, const secp256k1_context* ctx) const; bool VerifySurjectionProof(secp256k1_surjectionproof& proof, std::vector<secp256k1_generator>& vTags, secp256k1_generator& gen, const secp256k1_context* ctx, const uint256& wtxid) const;
}; };

View file

@ -628,9 +628,10 @@ private:
secp256k1_surjectionproof proof; secp256k1_surjectionproof proof;
std::vector<secp256k1_generator> vTags; std::vector<secp256k1_generator> vTags;
secp256k1_generator gen; secp256k1_generator gen;
uint256 wtxid;
const bool store; const bool store;
public: public:
CSurjectionCheck(secp256k1_surjectionproof& proofIn, std::vector<secp256k1_generator>& tags_in, secp256k1_generator& genIn, const bool storeIn) : proof(proofIn), vTags(tags_in), gen(genIn), store(storeIn) {} CSurjectionCheck(secp256k1_surjectionproof& proof_in, std::vector<secp256k1_generator>& tags_in, secp256k1_generator& gen_in, uint256& wtxid_in, const bool store_in) : proof(proof_in), vTags(tags_in), gen(gen_in), wtxid(wtxid_in), store(store_in) {}
bool operator()(); bool operator()();
}; };
@ -676,7 +677,7 @@ bool CBalanceCheck::operator()()
bool CSurjectionCheck::operator()() bool CSurjectionCheck::operator()()
{ {
return CachingSurjectionProofChecker(store).VerifySurjectionProof(proof, vTags, gen, secp256k1_ctx_verify_amounts); return CachingSurjectionProofChecker(store).VerifySurjectionProof(proof, vTags, gen, secp256k1_ctx_verify_amounts, wtxid);
} }
} // namespace } // namespace
@ -713,6 +714,8 @@ bool VerifyAmounts(const CCoinsViewCache& cache, const CTransaction& tx, std::ve
memset(explBlinds, 0, sizeof(explBlinds)); memset(explBlinds, 0, sizeof(explBlinds));
int ret; int ret;
uint256 wtxid(tx.GetHashWithWitness());
// This list is used to verify surjection proofs. // This list is used to verify surjection proofs.
// Proofs must be constructed with the list being in // Proofs must be constructed with the list being in
// order of input and non-null issuance pseudo-inputs, with // order of input and non-null issuance pseudo-inputs, with
@ -993,7 +996,7 @@ bool VerifyAmounts(const CCoinsViewCache& cache, const CTransaction& tx, std::ve
if (secp256k1_surjectionproof_parse(secp256k1_ctx_verify_amounts, &proof, &ptxoutwit->vchSurjectionproof[0], ptxoutwit->vchSurjectionproof.size()) != 1) if (secp256k1_surjectionproof_parse(secp256k1_ctx_verify_amounts, &proof, &ptxoutwit->vchSurjectionproof[0], ptxoutwit->vchSurjectionproof.size()) != 1)
return false; return false;
if (QueueCheck(pvChecks, new CSurjectionCheck(proof, targetGenerators, gen, cacheStore)) != SCRIPT_ERR_OK) { if (QueueCheck(pvChecks, new CSurjectionCheck(proof, targetGenerators, gen, wtxid, cacheStore)) != SCRIPT_ERR_OK) {
return false; return false;
} }
} }