From b204ff74a8940af4c5fee8010d161c0abb556edd Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Mon, 15 Feb 2021 12:46:14 +0800 Subject: [PATCH] Do not return hash results from component hash() methods --- src/plonk.rs | 8 +++----- src/plonk/circuit.rs | 15 +++++---------- src/plonk/permutation.rs | 9 ++++++++- src/poly.rs | 4 +--- src/poly/domain.rs | 6 ++---- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/plonk.rs b/src/plonk.rs index bb00b2c..0655d18 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -86,12 +86,10 @@ impl VerifyingKey { .to_state(); // Hash in constants in the domain which influence the proof - let domain_hash = &self.domain.hash(&mut hasher); - transcript.common_scalar(C::Scalar::from_bytes_wide(domain_hash))?; + self.domain.hash(&mut hasher); // Hash in `ConstraintSystem` - let cs_hash = &self.cs.hash(&mut hasher); - transcript.common_scalar(C::Scalar::from_bytes_wide(cs_hash))?; + self.cs.hash(&mut hasher); // Hash in vector of fixed commitments hasher.update(b"num_fixed_commitments"); @@ -104,7 +102,7 @@ impl VerifyingKey { hasher.update(b"num_permutations"); hasher.update(&self.permutations.len().to_le_bytes()); for permutation in &self.permutations { - permutation.hash(transcript)?; + permutation.hash(&mut hasher, transcript)?; } // Hash in final Blake2bState diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index 020ee35..f7feea0 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -3,7 +3,7 @@ use core::cmp::max; use core::ops::{Add, Mul}; use ff::Field; use std::{ - convert::{TryFrom, TryInto}, + convert::TryFrom, ops::{Neg, Sub}, }; @@ -29,9 +29,8 @@ impl Column { &self.column_type } - pub(crate) fn hash(&self, hasher: &mut Blake2bState) -> [u8; 64] { + pub(crate) fn hash(&self, hasher: &mut Blake2bState) { hasher.update(&format!("{:?}", self).as_bytes()); - hasher.finalize().as_bytes().try_into().unwrap() } } @@ -325,9 +324,8 @@ impl Expression { } /// Hash an Expression into a Blake2bState - pub fn hash(&self, hasher: &mut Blake2bState) -> [u8; 64] { + pub fn hash(&self, hasher: &mut Blake2bState) { hasher.update(&format!("{:?}", self).as_bytes()); - hasher.finalize().as_bytes().try_into().unwrap() } } @@ -612,7 +610,7 @@ impl ConstraintSystem { } /// Hashes the `ConstraintSystem` into a `u64`. - pub fn hash(&self, mut hasher: &mut Blake2bState) -> [u8; 64] { + pub fn hash(&self, mut hasher: &mut Blake2bState) { hasher.update(b"num_fixed_columns"); hasher.update(&self.num_fixed_columns.to_le_bytes()); @@ -625,7 +623,6 @@ impl ConstraintSystem { hasher.update(b"num_gates"); hasher.update(&self.gates.len().to_le_bytes()); for gate in self.gates.iter() { - hasher.update(gate.0.to_owned().as_bytes()); gate.1.hash(&mut hasher); } @@ -663,7 +660,7 @@ impl ConstraintSystem { hasher.update(&self.lookups.len().to_le_bytes()); for argument in self.lookups.iter() { hasher.update(&argument.input_columns.len().to_le_bytes()); - hasher.update(&argument.table_columns.len().to_le_bytes()); + assert_eq!(argument.input_columns.len(), argument.table_columns.len()); for (input, table) in argument .input_columns .iter() @@ -673,7 +670,5 @@ impl ConstraintSystem { table.hash(&mut hasher); } } - - hasher.finalize().as_bytes().try_into().unwrap() } } diff --git a/src/plonk/permutation.rs b/src/plonk/permutation.rs index 054ba23..abf6af9 100644 --- a/src/plonk/permutation.rs +++ b/src/plonk/permutation.rs @@ -11,6 +11,7 @@ pub(crate) mod keygen; pub(crate) mod prover; pub(crate) mod verifier; +use blake2b_simd::State as Blake2bState; use std::io; /// A permutation argument. @@ -68,7 +69,13 @@ impl VerifyingKey { Ok(VerifyingKey { commitments }) } - pub(crate) fn hash>(&self, transcript: &mut T) -> io::Result<()> { + pub(crate) fn hash>( + &self, + hasher: &mut Blake2bState, + transcript: &mut T, + ) -> io::Result<()> { + hasher.update(b"num_commitments"); + hasher.update(&self.commitments.len().to_le_bytes()); for commitment in &self.commitments { transcript.common_point(*commitment)?; } diff --git a/src/poly.rs b/src/poly.rs index 45088f4..1954d16 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -6,7 +6,6 @@ use crate::arithmetic::parallelize; use blake2b_simd::State as Blake2bState; use ff::Field; -use std::convert::TryInto; use std::fmt::Debug; use std::marker::PhantomData; use std::ops::{Add, Deref, DerefMut, Index, IndexMut, Mul, RangeFrom, RangeFull, Sub}; @@ -232,8 +231,7 @@ impl Rotation { } /// Hash Rotation into a Blake2bState - pub fn hash(&self, hasher: &mut Blake2bState) -> [u8; 64] { + pub fn hash(&self, hasher: &mut Blake2bState) { hasher.update(&format!("{:?}", self).as_bytes()); - hasher.finalize().as_bytes().try_into().unwrap() } } diff --git a/src/poly/domain.rs b/src/poly/domain.rs index c9a9dff..6298a27 100644 --- a/src/poly/domain.rs +++ b/src/poly/domain.rs @@ -380,8 +380,8 @@ impl EvaluationDomain { self.quotient_poly_degree as usize } - /// Hashes the constants in the domain which influence the proof into a u64 - pub fn hash(&self, hasher: &mut Blake2bState) -> [u8; 64] { + /// Hashes the constants in the domain which influence the proof into a Blake2bState + pub fn hash(&self, hasher: &mut Blake2bState) { hasher.update(b"k"); hasher.update(&self.k.to_le_bytes()); @@ -390,7 +390,5 @@ impl EvaluationDomain { hasher.update(b"omega"); hasher.update(&self.omega.to_bytes()); - - hasher.finalize().as_bytes().try_into().unwrap() } }