From 9a2a1fdd13e23cb2b3dde6f2a2f5ac570a62d771 Mon Sep 17 00:00:00 2001 From: ashWhiteHat Date: Tue, 10 Oct 2023 20:48:33 +0900 Subject: [PATCH] Refactor Matrix vectors and Logical operation (#28) * Refactor Matrix vectors operation * Refacotr NIFS logical operation --- src/ccs/mod.rs | 1 + src/ccs/r1cs.rs | 1 + src/folding/nova/nifs.rs | 26 ++++++++---------------- src/utils/vec.rs | 44 ++++++++-------------------------------- 4 files changed, 20 insertions(+), 52 deletions(-) diff --git a/src/ccs/mod.rs b/src/ccs/mod.rs index b96ca61..3f1270c 100644 --- a/src/ccs/mod.rs +++ b/src/ccs/mod.rs @@ -91,6 +91,7 @@ impl CCS { M: vec![r1cs.A, r1cs.B, r1cs.C], } } + pub fn to_r1cs(self) -> R1CS { R1CS:: { l: self.l, diff --git a/src/ccs/r1cs.rs b/src/ccs/r1cs.rs index b4397f4..e31df3e 100644 --- a/src/ccs/r1cs.rs +++ b/src/ccs/r1cs.rs @@ -53,6 +53,7 @@ pub struct RelaxedR1CS { pub u: F, pub E: Vec, } + impl RelaxedR1CS { /// check that a RelaxedR1CS structure is satisfied by a z vector. Only for testing. pub fn check_relation(&self, z: &[F]) -> Result<(), Error> { diff --git a/src/folding/nova/nifs.rs b/src/folding/nova/nifs.rs index 65167e8..1628ddc 100644 --- a/src/folding/nova/nifs.rs +++ b/src/folding/nova/nifs.rs @@ -136,16 +136,11 @@ where cmT: &C, ) -> Result<(), Error> { let r2 = r * r; - if ci3.cmE != (ci1.cmE + cmT.mul(r) + ci2.cmE.mul(r2)) { - return Err(Error::NotSatisfied); - } - if ci3.u != ci1.u + r * ci2.u { - return Err(Error::NotSatisfied); - } - if ci3.cmW != (ci1.cmW + ci2.cmW.mul(r)) { - return Err(Error::NotSatisfied); - } - if ci3.x != vec_add(&ci1.x, &vec_scalar_mul(&ci2.x, &r))? { + if ci3.cmE != (ci1.cmE + cmT.mul(r) + ci2.cmE.mul(r2)) + || ci3.u != ci1.u + r * ci2.u + || ci3.cmW != (ci1.cmW + ci2.cmW.mul(r)) + || ci3.x != vec_add(&ci1.x, &vec_scalar_mul(&ci2.x, &r))? + { return Err(Error::NotSatisfied); } Ok(()) @@ -175,13 +170,10 @@ where // cm_proofs should have length 3: [cmE_proof, cmW_proof, cmT_proof] return Err(Error::NotExpectedLength); } - if !Pedersen::verify(pedersen_params, tr, ci.cmE, cm_proofs[0].clone()) { - return Err(Error::CommitmentVerificationFail); - } - if !Pedersen::verify(pedersen_params, tr, ci.cmW, cm_proofs[1].clone()) { - return Err(Error::CommitmentVerificationFail); - } - if !Pedersen::verify(pedersen_params, tr, cmT, cm_proofs[2].clone()) { + if !Pedersen::verify(pedersen_params, tr, ci.cmE, cm_proofs[0].clone()) + || !Pedersen::verify(pedersen_params, tr, ci.cmW, cm_proofs[1].clone()) + || !Pedersen::verify(pedersen_params, tr, cmT, cm_proofs[2].clone()) + { return Err(Error::CommitmentVerificationFail); } Ok(()) diff --git a/src/utils/vec.rs b/src/utils/vec.rs index 34382c3..341c548 100644 --- a/src/utils/vec.rs +++ b/src/utils/vec.rs @@ -48,39 +48,22 @@ pub fn vec_add(a: &[F], b: &[F]) -> Result, Error> { if a.len() != b.len() { return Err(Error::NotSameLength); } - let mut r: Vec = vec![F::zero(); a.len()]; - for i in 0..a.len() { - r[i] = a[i] + b[i]; - } - Ok(r) + Ok(a.iter().zip(b.iter()).map(|(x, y)| *x + y).collect()) } pub fn vec_sub(a: &[F], b: &[F]) -> Result, Error> { if a.len() != b.len() { return Err(Error::NotSameLength); } - let mut r: Vec = vec![F::zero(); a.len()]; - for i in 0..a.len() { - r[i] = a[i] - b[i]; - } - Ok(r) + Ok(a.iter().zip(b.iter()).map(|(x, y)| *x - y).collect()) } pub fn vec_scalar_mul(vec: &[F], c: &F) -> Vec { - let mut result = vec![F::zero(); vec.len()]; - for (i, a) in vec.iter().enumerate() { - result[i] = *a * c; - } - result + vec.iter().map(|a| *a * c).collect() } pub fn is_zero_vec(vec: &[F]) -> bool { - for e in vec { - if !e.is_zero() { - return false; - } - } - true + vec.iter().all(|a| a.is_zero()) } pub fn mat_vec_mul(M: &Vec>, z: &[F]) -> Result, Error> { @@ -107,7 +90,6 @@ pub fn mat_vec_mul_sparse(matrix: &SparseMatrix, vector: &[F]) res[row_i] += value * vector[col_i]; } } - res } @@ -127,21 +109,12 @@ pub mod tests { dense_matrix_to_sparse(to_F_dense_matrix(M)) } pub fn to_F_dense_matrix(M: Vec>) -> Vec> { - let mut R: Vec> = vec![Vec::new(); M.len()]; - for i in 0..M.len() { - R[i] = vec![F::zero(); M[i].len()]; - for j in 0..M[i].len() { - R[i][j] = F::from(M[i][j] as u64); - } - } - R + M.iter() + .map(|m| m.iter().map(|r| F::from(*r as u64)).collect()) + .collect() } pub fn to_F_vec(z: Vec) -> Vec { - let mut r: Vec = vec![F::zero(); z.len()]; - for i in 0..z.len() { - r[i] = F::from(z[i] as u64); - } - r + z.iter().map(|c| F::from(*c as u64)).collect() } #[test] @@ -192,6 +165,7 @@ pub mod tests { to_F_vec(vec![7, 16, 27, 40, 55, 72]) ); } + #[test] fn test_vec_add() { let a: Vec = to_F_vec::(vec![1, 2, 3, 4, 5, 6]);