Skip to content

Commit

Permalink
Refactor Matrix vectors and Logical operation (#28)
Browse files Browse the repository at this point in the history
* Refactor Matrix vectors operation

* Refacotr NIFS logical operation
  • Loading branch information
ashWhiteHat authored Oct 10, 2023
1 parent 13e471a commit 9a2a1fd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 52 deletions.
1 change: 1 addition & 0 deletions src/ccs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl<C: CurveGroup> CCS<C> {
M: vec![r1cs.A, r1cs.B, r1cs.C],
}
}

pub fn to_r1cs(self) -> R1CS<C::ScalarField> {
R1CS::<C::ScalarField> {
l: self.l,
Expand Down
1 change: 1 addition & 0 deletions src/ccs/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct RelaxedR1CS<F: PrimeField> {
pub u: F,
pub E: Vec<F>,
}

impl<F: PrimeField> RelaxedR1CS<F> {
/// check that a RelaxedR1CS structure is satisfied by a z vector. Only for testing.
pub fn check_relation(&self, z: &[F]) -> Result<(), Error> {
Expand Down
26 changes: 9 additions & 17 deletions src/folding/nova/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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(())
Expand Down
44 changes: 9 additions & 35 deletions src/utils/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,22 @@ pub fn vec_add<F: PrimeField>(a: &[F], b: &[F]) -> Result<Vec<F>, Error> {
if a.len() != b.len() {
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> {
if a.len() != b.len() {
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> {
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 {
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> {
Expand All @@ -107,7 +90,6 @@ pub fn mat_vec_mul_sparse<F: PrimeField>(matrix: &SparseMatrix<F>, vector: &[F])
res[row_i] += value * vector[col_i];
}
}

res
}

Expand All @@ -127,21 +109,12 @@ pub mod tests {
dense_matrix_to_sparse(to_F_dense_matrix(M))
}
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> {
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]
Expand Down Expand Up @@ -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<Fr> = to_F_vec::<Fr>(vec![1, 2, 3, 4, 5, 6]);
Expand Down

0 comments on commit 9a2a1fd

Please sign in to comment.