Skip to content

Commit

Permalink
Impl multi-variable lagrange (#5)
Browse files Browse the repository at this point in the history
1. bugfix for Fervalds algorithm
2. Impl multi-variable lagrange.
  • Loading branch information
SuccinctPaul authored Aug 2, 2023
1 parent aa4e12f commit 584fa37
Show file tree
Hide file tree
Showing 27 changed files with 757 additions and 160 deletions.
3 changes: 2 additions & 1 deletion 1_IP/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// The interactive proofs case(1.2.1) in chapter 1
/// In this case, business store data in cloud_provider. Later, before business wants wanna do some computation on cloud_provider, the business check the `data` first.
/// In this case, business store data in cloud_provider. Later, before business wants wanna do some
/// computation on cloud_provider, the business check the `data` first.
mod prover;
mod utils;
mod verify;
Expand Down
14 changes: 7 additions & 7 deletions 2_Freivalds_Algorithm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::matrix::Matrix;
use crate::prover::Prover;
use crate::utils::gen_x;
use rand_core::OsRng;

mod matrix;
/// For matrix A and B, C = A · B.
///
/// How can one verify that two matrices were multiplied correctly.
/// First,choose a random `r∈Fp`,and let x=(1,r,r2,...,rn−1).
/// Then compute `y=Cx` and `z=A·Bx`,outputting YES if y = z and NO otherwise.
mod matrix;
mod prover;
mod utils;
mod verifier;

use crate::matrix::Matrix;
use crate::prover::Prover;
use crate::utils::gen_x;
use rand_core::OsRng;

#[test]
fn completeness() {
let n: usize = std::env::var("n")
Expand All @@ -26,7 +26,7 @@ fn completeness() {
let c = alice.matrix_multiplication();

let x = gen_x(OsRng, n);
// z=A·Bx
// z=A·(Bx)
let z = alice.hash(&x);

// verify
Expand Down
17 changes: 16 additions & 1 deletion 2_Freivalds_Algorithm/src/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bls12_381::Scalar;
use ff::Field;
use rand_core::{OsRng, RngCore};
use rand_core::OsRng;
use std::ops::AddAssign;

/// This define `matrix` (rows * cols) (m × n)
Expand Down Expand Up @@ -98,6 +98,7 @@ impl Matrix {
mod test {
use crate::matrix::Matrix;
use bls12_381::Scalar;
use ff::PrimeField;

#[test]
fn test_random_matrix() {
Expand Down Expand Up @@ -154,4 +155,18 @@ mod test {
assert_eq!(row_1, res);
println!("{:#?}", res);
}

#[test]
fn test() {
let n = 2;
let A = Matrix::random(n, n);
let B = Matrix::random(n, n);
let x = vec![Scalar::from_u128(3), Scalar::from_u128(5)];

// A*B*x
let res1 = Matrix::mul(&A, &B).matrix_mul_vec(&x);
// A*(B*x)
let res2 = A.matrix_mul_vec(&B.matrix_mul_vec(&x));
assert_eq!(res1, res2);
}
}
8 changes: 4 additions & 4 deletions 2_Freivalds_Algorithm/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl Prover {
}

pub fn hash(&self, x: &Vec<Scalar>) -> Vec<Scalar> {
// tmp = Ax
let tmp = self.a.matrix_mul_vec(x);
// z = B temp
self.b.matrix_mul_vec(&tmp)
// tmp = Bx
let tmp = self.b.matrix_mul_vec(x);
// z = A temp = A(Bx)
self.a.matrix_mul_vec(&tmp)
}
}
10 changes: 5 additions & 5 deletions 2_Reed_Solomon_Fingerprinting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::prover::Prover;
/// The Reed-Solomon Fingerprinting case(2.1) in chapter 2
/// In this case, we'll check whether Alice and Bob has the same file by checking RS-fingerprint.
mod prover;
mod utils;
mod verify;

use crate::prover::Prover;
use crate::utils::{dump_field_data, read_from_file};
use crate::verify::Verifier;
use bls12_381::Scalar;
use ff::Field;
use rand_core::{OsRng, RngCore};

mod prover;
mod utils;
mod verify;

#[derive(Default, Eq, PartialEq)]
pub(crate) struct Person {
data: Vec<Scalar>,
Expand Down
Loading

0 comments on commit 584fa37

Please sign in to comment.