Browse Source

Refactor Matrix vectors and Logical operation (#28)

* Refactor Matrix vectors operation

* Refacotr NIFS logical operation
main
ashWhiteHat 1 year ago
committed by GitHub
parent
commit
9a2a1fdd13
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 52 deletions
  1. +1
    -0
      src/ccs/mod.rs
  2. +1
    -0
      src/ccs/r1cs.rs
  3. +9
    -17
      src/folding/nova/nifs.rs
  4. +9
    -35
      src/utils/vec.rs

+ 1
- 0
src/ccs/mod.rs

@ -91,6 +91,7 @@ impl CCS {
M: vec![r1cs.A, r1cs.B, r1cs.C], M: vec![r1cs.A, r1cs.B, r1cs.C],
} }
} }
pub fn to_r1cs(self) -> R1CS<C::ScalarField> { pub fn to_r1cs(self) -> R1CS<C::ScalarField> {
R1CS::<C::ScalarField> { R1CS::<C::ScalarField> {
l: self.l, l: self.l,

+ 1
- 0
src/ccs/r1cs.rs

@ -53,6 +53,7 @@ pub struct RelaxedR1CS {
pub u: F, pub u: F,
pub E: Vec<F>, pub E: Vec<F>,
} }
impl<F: PrimeField> RelaxedR1CS<F> { impl<F: PrimeField> RelaxedR1CS<F> {
/// check that a RelaxedR1CS structure is satisfied by a z vector. Only for testing. /// check that a RelaxedR1CS structure is satisfied by a z vector. Only for testing.
pub fn check_relation(&self, z: &[F]) -> Result<(), Error> { pub fn check_relation(&self, z: &[F]) -> Result<(), Error> {

+ 9
- 17
src/folding/nova/nifs.rs

@ -136,16 +136,11 @@ where
cmT: &C, cmT: &C,
) -> Result<(), Error> { ) -> Result<(), Error> {
let r2 = r * r; 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); return Err(Error::NotSatisfied);
} }
Ok(()) Ok(())
@ -175,13 +170,10 @@ where
// cm_proofs should have length 3: [cmE_proof, cmW_proof, cmT_proof] // cm_proofs should have length 3: [cmE_proof, cmW_proof, cmT_proof]
return Err(Error::NotExpectedLength); 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); return Err(Error::CommitmentVerificationFail);
} }
Ok(()) Ok(())

+ 9
- 35
src/utils/vec.rs

@ -48,39 +48,22 @@ pub fn vec_add(a: &[F], b: &[F]) -> Result, Error> {
if a.len() != b.len() { if a.len() != b.len() {
return Err(Error::NotSameLength); return Err(Error::NotSameLength);
} }
let mut r: Vec<F> = 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<F: PrimeField>(a: &[F], b: &[F]) -> Result<Vec<F>, Error> { pub fn vec_sub<F: PrimeField>(a: &[F], b: &[F]) -> Result<Vec<F>, Error> {
if a.len() != b.len() { if a.len() != b.len() {
return Err(Error::NotSameLength); return Err(Error::NotSameLength);
} }
let mut r: Vec<F> = 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<F: PrimeField>(vec: &[F], c: &F) -> Vec<F> { pub fn vec_scalar_mul<F: PrimeField>(vec: &[F], c: &F) -> Vec<F> {
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<F: PrimeField>(vec: &[F]) -> bool { pub fn is_zero_vec<F: PrimeField>(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<F: PrimeField>(M: &Vec<Vec<F>>, z: &[F]) -> Result<Vec<F>, Error> { pub fn mat_vec_mul<F: PrimeField>(M: &Vec<Vec<F>>, z: &[F]) -> Result<Vec<F>, Error> {
@ -107,7 +90,6 @@ pub fn mat_vec_mul_sparse(matrix: &SparseMatrix, vector: &[F])
res[row_i] += value * vector[col_i]; res[row_i] += value * vector[col_i];
} }
} }
res res
} }
@ -127,21 +109,12 @@ pub mod tests {
dense_matrix_to_sparse(to_F_dense_matrix(M)) dense_matrix_to_sparse(to_F_dense_matrix(M))
} }
pub fn to_F_dense_matrix<F: PrimeField>(M: Vec<Vec<usize>>) -> Vec<Vec<F>> { pub fn to_F_dense_matrix<F: PrimeField>(M: Vec<Vec<usize>>) -> Vec<Vec<F>> {
let mut R: Vec<Vec<F>> = 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<F: PrimeField>(z: Vec<usize>) -> Vec<F> { pub fn to_F_vec<F: PrimeField>(z: Vec<usize>) -> Vec<F> {
let mut r: Vec<F> = 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] #[test]
@ -192,6 +165,7 @@ pub mod tests {
to_F_vec(vec![7, 16, 27, 40, 55, 72]) to_F_vec(vec![7, 16, 27, 40, 55, 72])
); );
} }
#[test] #[test]
fn test_vec_add() { fn test_vec_add() {
let a: Vec<Fr> = to_F_vec::<Fr>(vec![1, 2, 3, 4, 5, 6]); let a: Vec<Fr> = to_F_vec::<Fr>(vec![1, 2, 3, 4, 5, 6]);

Loading…
Cancel
Save