mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
refactor on master, pass in offset as blinding_factor (#1064)
This commit is contained in:
parent
ce1dca1b68
commit
0d9242e5dc
4 changed files with 14 additions and 30 deletions
|
@ -22,7 +22,6 @@ use std::path::{Path, PathBuf};
|
|||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use util::static_secp_instance;
|
||||
use util::secp::pedersen::{Commitment, RangeProof};
|
||||
|
||||
use core::consensus::REWARD;
|
||||
|
@ -665,20 +664,14 @@ impl<'a> Extension<'a> {
|
|||
pub fn validate_sums(&self, header: &BlockHeader) -> Result<((Commitment, Commitment)), Error> {
|
||||
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.
|
||||
// If we have a supply of 6,000 grin then we should
|
||||
// have a corresponding 6,000 grin in unspent outputs.
|
||||
let supply = ((header.height * REWARD) as i64).checked_neg().unwrap_or(0);
|
||||
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 {
|
||||
return Err(Error::InvalidTxHashSet(
|
||||
|
|
|
@ -722,16 +722,9 @@ impl Block {
|
|||
let overage = (REWARD as i64).checked_neg().unwrap_or(0);
|
||||
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.
|
||||
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 {
|
||||
return Err(Error::KernelSumMismatch);
|
||||
|
|
|
@ -37,6 +37,8 @@ pub use self::id::ShortId;
|
|||
use core::hash::Hashed;
|
||||
use ser::{Error, Readable, Reader, Writeable, Writer};
|
||||
use global;
|
||||
use keychain;
|
||||
use keychain::BlindingFactor;
|
||||
|
||||
/// Implemented by types that hold inputs and outputs (and kernels)
|
||||
/// containing Pedersen commitments.
|
||||
|
@ -46,9 +48,9 @@ pub trait Committed {
|
|||
/// Gather the kernel excesses and sum them.
|
||||
fn sum_kernel_excesses(
|
||||
&self,
|
||||
offset: &Commitment,
|
||||
offset: &BlindingFactor,
|
||||
extra_excess: Option<&Commitment>,
|
||||
) -> Result<(Commitment, Commitment), secp::Error> {
|
||||
) -> Result<(Commitment, Commitment), keychain::Error> {
|
||||
let zero_commit = secp_static::commit_to_zero_value();
|
||||
|
||||
// then gather the kernel excess commitments
|
||||
|
@ -68,13 +70,16 @@ pub trait Committed {
|
|||
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 secp = static_secp_instance();
|
||||
let secp = secp.lock().unwrap();
|
||||
let mut commits = vec![kernel_sum];
|
||||
if *offset != zero_commit {
|
||||
commits.push(*offset);
|
||||
if *offset != BlindingFactor::zero() {
|
||||
let key = offset.secret_key(&secp)?;
|
||||
let offset_commit = secp.commit(0, key)?;
|
||||
commits.push(offset_commit);
|
||||
}
|
||||
secp.commit_sum(commits, vec![])?
|
||||
};
|
||||
|
|
|
@ -418,15 +418,8 @@ impl Transaction {
|
|||
let overage = self.fee() as i64;
|
||||
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.
|
||||
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
|
||||
// the sum of input/output commitments (minus fee)
|
||||
|
|
Loading…
Reference in a new issue