From 12e9202e8cac0e7003a186759d13eff78ede7de6 Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Fri, 3 Sep 2021 13:26:03 +0900 Subject: [PATCH] accumulator/pollard: Add a VerifyBatchProof method Pollard now has a method just for verifying a given proof. This new method does not alter the Pollard in any way, making it useful during verifying mempool Bitcoin transactions. --- accumulator/pollardproof.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/accumulator/pollardproof.go b/accumulator/pollardproof.go index 49295e4d..2b6f60d0 100644 --- a/accumulator/pollardproof.go +++ b/accumulator/pollardproof.go @@ -4,8 +4,39 @@ import ( "fmt" ) +// VerifyBatchProof verifies the hash and the proof passed in. It does not +// make any modifications to the pollard. +// +// NOTE: The order in which the hashes are given matter (aka permutation matters). +// The hashes being verified should be in the same order as they were +// proven. +func (p *Pollard) VerifyBatchProof(toProve []Hash, bp BatchProof) error { + // verify the batch proof. + rootHashes := p.rootHashesForward() + _, _, err := verifyBatchProof(toProve, bp, rootHashes, p.numLeaves, + // pass a closure that checks the pollard for cached nodes. + // returns true and the hash value of the node if it exists. + // returns false if the node does not exist or the hash value is empty. + func(pos uint64) (bool, Hash) { + n, _, _, err := p.readPos(pos) + if err != nil { + return false, empty + } + if n != nil && n.data != empty { + return true, n.data + } + + return false, empty + }) + return err +} + // IngestBatchProof populates the Pollard with all needed data to delete the -// targets in the block proof +// targets in the block proof. +// +// NOTE: The order in which the hashes are given matter (aka permutation matters). +// The hashes being verified should be in the same order as they were +// proven. func (p *Pollard) IngestBatchProof(toProve []Hash, bp BatchProof) error { // verify the batch proof. rootHashes := p.rootHashesForward()