From 54d3fc0af2c6e012f234358b5dd2722172c61238 Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Fri, 17 Aug 2018 23:18:48 +0800 Subject: [PATCH] improve: use batch verification for txhashset rangeproof validation (#1321) (#1363) * improve: use bullet rangeproof batch verification for txhashset validation (#1321) * update rust-secp256k1-zkp to tag 'grin_integration_22' --- chain/src/txhashset.rs | 23 +++++++++++++++++++++-- core/src/core/transaction.rs | 13 +++++++++++++ util/Cargo.toml | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/chain/src/txhashset.rs b/chain/src/txhashset.rs index 2425bf1ef..7bc3571b7 100644 --- a/chain/src/txhashset.rs +++ b/chain/src/txhashset.rs @@ -989,20 +989,27 @@ impl<'a> Extension<'a> { { let now = Instant::now(); + let mut commits:Vec = vec![]; + let mut proofs:Vec = vec![]; + let mut proof_count = 0; let total_rproofs = pmmr::n_leaves(self.output_pmmr.unpruned_size()); for n in 1..self.output_pmmr.unpruned_size() + 1 { if pmmr::is_leaf(n) { if let Some(out) = self.output_pmmr.get_data(n) { if let Some(rp) = self.rproof_pmmr.get_data(n) { - out.into_output(rp).verify_proof()?; + commits.push(out.commit); + proofs.push(rp); } else { // TODO - rangeproof not found return Err(ErrorKind::OutputNotFound.into()); } proof_count += 1; - if proof_count % 500 == 0 { + if proofs.len() >= 1000 { + Output::batch_verify_proofs(&commits, &proofs)?; + commits.clear(); + proofs.clear(); debug!( LOGGER, "txhashset: verify_rangeproofs: verified {} rangeproofs", proof_count, @@ -1014,6 +1021,18 @@ impl<'a> Extension<'a> { status.on_validation(0, 0, proof_count, total_rproofs); } } + + // remaining part which not full of 1000 range proofs + if proofs.len() > 0 { + Output::batch_verify_proofs(&commits, &proofs)?; + commits.clear(); + proofs.clear(); + debug!( + LOGGER, + "txhashset: verify_rangeproofs: verified {} rangeproofs", proof_count, + ); + } + debug!( LOGGER, "txhashset: verified {} rangeproofs, pmmr size {}, took {}s", diff --git a/core/src/core/transaction.rs b/core/src/core/transaction.rs index 28f4bb393..ca9f491ac 100644 --- a/core/src/core/transaction.rs +++ b/core/src/core/transaction.rs @@ -993,6 +993,19 @@ impl Output { Err(e) => Err(e), } } + + /// Batch validates the range proofs using the commitments + pub fn batch_verify_proofs( + commits: &Vec, + proofs: &Vec, + ) -> Result<(), secp::Error> { + let secp = static_secp_instance(); + let secp = secp.lock().unwrap(); + match secp.verify_bullet_proof_multi(commits.clone(), proofs.clone(), None) { + Ok(_) => Ok(()), + Err(e) => Err(e), + } + } } /// An output_identifier can be build from either an input _or_ an output and diff --git a/util/Cargo.toml b/util/Cargo.toml index 26955c5fd..5573f45f1 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -20,6 +20,6 @@ zip = "0.4" [dependencies.secp256k1zkp] git = "https://github.com/mimblewimble/rust-secp256k1-zkp" -tag = "grin_integration_21" +tag = "grin_integration_22" #path = "../../rust-secp256k1-zkp" features = ["bullet-proof-sizing"]