refactor on master, pass in offset as blinding_factor (#1064)

This commit is contained in:
Antioch Peverell 2018-05-13 13:59:27 -04:00 committed by GitHub
parent ce1dca1b68
commit 0d9242e5dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 30 deletions

View file

@ -22,7 +22,6 @@ use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
use std::time::Instant; use std::time::Instant;
use util::static_secp_instance;
use util::secp::pedersen::{Commitment, RangeProof}; use util::secp::pedersen::{Commitment, RangeProof};
use core::consensus::REWARD; use core::consensus::REWARD;
@ -665,20 +664,14 @@ impl<'a> Extension<'a> {
pub fn validate_sums(&self, header: &BlockHeader) -> Result<((Commitment, Commitment)), Error> { pub fn validate_sums(&self, header: &BlockHeader) -> Result<((Commitment, Commitment)), Error> {
let now = Instant::now(); let now = Instant::now();
let offset = {
let secp = static_secp_instance();
let secp = secp.lock().unwrap();
let key = header.total_kernel_offset.secret_key(&secp)?;
secp.commit(0, key)?
};
// Treat the total "supply" as one huge overage that needs to be accounted for. // Treat the total "supply" as one huge overage that needs to be accounted for.
// If we have a supply of 6,000 grin then we should // If we have a supply of 6,000 grin then we should
// have a corresponding 6,000 grin in unspent outputs. // have a corresponding 6,000 grin in unspent outputs.
let supply = ((header.height * REWARD) as i64).checked_neg().unwrap_or(0); let supply = ((header.height * REWARD) as i64).checked_neg().unwrap_or(0);
let output_sum = self.sum_commitments(supply, None)?; let output_sum = self.sum_commitments(supply, None)?;
let (kernel_sum, kernel_sum_plus_offset) = self.sum_kernel_excesses(&offset, None)?; let (kernel_sum, kernel_sum_plus_offset) =
self.sum_kernel_excesses(&header.total_kernel_offset, None)?;
if output_sum != kernel_sum_plus_offset { if output_sum != kernel_sum_plus_offset {
return Err(Error::InvalidTxHashSet( return Err(Error::InvalidTxHashSet(

View file

@ -722,16 +722,9 @@ impl Block {
let overage = (REWARD as i64).checked_neg().unwrap_or(0); let overage = (REWARD as i64).checked_neg().unwrap_or(0);
let io_sum = self.sum_commitments(overage, Some(prev_output_sum))?; let io_sum = self.sum_commitments(overage, Some(prev_output_sum))?;
let offset = {
let secp = static_secp_instance();
let secp = secp.lock().unwrap();
let key = self.header.total_kernel_offset.secret_key(&secp)?;
secp.commit(0, key)?
};
// Sum the kernel excesses accounting for the kernel offset. // Sum the kernel excesses accounting for the kernel offset.
let (kernel_sum, kernel_sum_plus_offset) = let (kernel_sum, kernel_sum_plus_offset) =
self.sum_kernel_excesses(&offset, Some(prev_kernel_sum))?; self.sum_kernel_excesses(&self.header.total_kernel_offset, Some(prev_kernel_sum))?;
if io_sum != kernel_sum_plus_offset { if io_sum != kernel_sum_plus_offset {
return Err(Error::KernelSumMismatch); return Err(Error::KernelSumMismatch);

View file

@ -37,6 +37,8 @@ pub use self::id::ShortId;
use core::hash::Hashed; use core::hash::Hashed;
use ser::{Error, Readable, Reader, Writeable, Writer}; use ser::{Error, Readable, Reader, Writeable, Writer};
use global; use global;
use keychain;
use keychain::BlindingFactor;
/// Implemented by types that hold inputs and outputs (and kernels) /// Implemented by types that hold inputs and outputs (and kernels)
/// containing Pedersen commitments. /// containing Pedersen commitments.
@ -46,9 +48,9 @@ pub trait Committed {
/// Gather the kernel excesses and sum them. /// Gather the kernel excesses and sum them.
fn sum_kernel_excesses( fn sum_kernel_excesses(
&self, &self,
offset: &Commitment, offset: &BlindingFactor,
extra_excess: Option<&Commitment>, extra_excess: Option<&Commitment>,
) -> Result<(Commitment, Commitment), secp::Error> { ) -> Result<(Commitment, Commitment), keychain::Error> {
let zero_commit = secp_static::commit_to_zero_value(); let zero_commit = secp_static::commit_to_zero_value();
// then gather the kernel excess commitments // then gather the kernel excess commitments
@ -68,13 +70,16 @@ pub trait Committed {
secp.commit_sum(kernel_commits, vec![])? secp.commit_sum(kernel_commits, vec![])?
}; };
// sum the commitments along with the specified offset // sum the commitments along with the
// commit to zero built from the offset
let kernel_sum_plus_offset = { let kernel_sum_plus_offset = {
let secp = static_secp_instance(); let secp = static_secp_instance();
let secp = secp.lock().unwrap(); let secp = secp.lock().unwrap();
let mut commits = vec![kernel_sum]; let mut commits = vec![kernel_sum];
if *offset != zero_commit { if *offset != BlindingFactor::zero() {
commits.push(*offset); let key = offset.secret_key(&secp)?;
let offset_commit = secp.commit(0, key)?;
commits.push(offset_commit);
} }
secp.commit_sum(commits, vec![])? secp.commit_sum(commits, vec![])?
}; };

View file

@ -418,15 +418,8 @@ impl Transaction {
let overage = self.fee() as i64; let overage = self.fee() as i64;
let io_sum = self.sum_commitments(overage, None)?; let io_sum = self.sum_commitments(overage, None)?;
let offset = {
let secp = static_secp_instance();
let secp = secp.lock().unwrap();
let key = self.offset.secret_key(&secp)?;
secp.commit(0, key)?
};
// Sum the kernel excesses accounting for the kernel offset. // Sum the kernel excesses accounting for the kernel offset.
let (_, kernel_sum) = self.sum_kernel_excesses(&offset, None)?; let (_, kernel_sum) = self.sum_kernel_excesses(&self.offset, None)?;
// sum of kernel commitments (including the offset) must match // sum of kernel commitments (including the offset) must match
// the sum of input/output commitments (minus fee) // the sum of input/output commitments (minus fee)