Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run for higher epoch_limit #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 57 additions & 9 deletions versionA/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use once_cell::sync::Lazy;
type UniPoly_381 = DensePolynomial<<Bls12_381 as PairingEngine>::Fr>;
type KZG = KZG10<Bls12_381, UniPoly_381>;

const EPOCH_LIMIT: u8 = 1;
const EPOCH_LIMIT: u8 = 3;
const DEGREE: usize = EPOCH_LIMIT as usize;

static KEYS: Lazy<(Powers<Bls12_381>, VerifierKey<Bls12_381>)> = Lazy::new(|| {
Expand Down Expand Up @@ -95,25 +95,72 @@ impl RLN {
messages.push((message_hash, evaluation));

if messages.len() > self.limit as usize {
let key = Self::recover_key([messages[0], messages[1]]);
let key = Self::recover_key(messages);
let pubkey = KEYS.1.g.mul(key);
assert!(self.shares.get(&pubkey).is_some());

self.shares.remove(&pubkey).unwrap();
}
}

fn recover_key(shares: [(Fr, Fr); (EPOCH_LIMIT + 1) as usize]) -> Fr {
let (x1, y1) = shares[0];
let (x2, y2) = shares[1];
fn recover_key(shares: &Vec<(Fr, Fr)>) -> Fr {
let size = (EPOCH_LIMIT + 1) as usize;
let vec_x: Vec<Fr> = shares.iter().map(|a| {a.0}).collect();
let vec_y: Vec<Fr> = shares.iter().map(|a| {a.1}).collect();

let numerator = y2 * x1 - y1 * x2;
let denominator = x1 - x2;
let mut matrix: Vec<Vec<Fr>> = vec![vec![Fr::from(1); size]];
matrix.push(vec_x.clone());

for i in 2..size {
let next_row = matrix[i-1].iter().zip(&vec_x).map(|(&a, &b)| {a * b}).collect();
matrix.push(next_row);
}

let denominator = determinant(matrix.clone());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If determinant() takes slice - you won't need to clone matrix

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The determinant function manipulates the matrix, but I need the original matrix one more time.
I think this needs cloning here.

_ = std::mem::replace(&mut matrix[0], vec_y);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just

matrix[0] = vec_y;

?
I didn't test it, but am I missing something ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That simple code didn't work previously for some reason.
But, as I tested it now, it works properly.

let numerator = determinant(matrix);

numerator / denominator
}
}

fn determinant(mut matrix: Vec<Vec<Fr>>) -> Fr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here, don't need to take ownership of Vector, can just get slice

let n = matrix.len();
let mut det = Fr::from(1);

for i in 0..n {
let mut pivot_row = i;
for j in (i + 1)..n {
if matrix[j][i] != Fr::from(0) {
pivot_row = j;
break;
}
}

if pivot_row != i {
matrix.swap(i, pivot_row);
det = -det;
}

let pivot = matrix[i][i];

if pivot == Fr::from(0) {
return Fr::from(0);
}

det *= pivot;

for j in (i + 1)..n {
let factor = matrix[j][i] / pivot;
for k in (i + 1)..n {
matrix[j][k] = matrix[j][k] - factor * matrix[i][k];
}
}
}

det
}

struct User {
polynomial: UniPoly_381,
}
Expand Down Expand Up @@ -183,8 +230,9 @@ fn main() {
user.register(&mut rln);
assert!(rln.shares.get(&user.pubkey()).is_some());

user.send(Fr::rand(rng), &mut rln);
user.send(Fr::rand(rng), &mut rln);
for _ in 0..EPOCH_LIMIT+1 {
user.send(Fr::rand(rng), &mut rln);
}

assert!(rln.shares.get(&user.pubkey()).is_none());
}