-
Notifications
You must be signed in to change notification settings - Fork 2
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: Add helper function for LDE calculation #12
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4bc7873
bugfix: fix bug caused by not handling mont form of stone
jaehunkim 4a3cb30
chore: clippy issue
jaehunkim 0658d40
feat: Add helper function for LDE calculation
jaehunkim a53ccd0
feat: implemented element reordering for stone
jaehunkim 5f37935
Merge branch 'master' into dev-lde
jaehunkim b12282b
fix: simplify order changing methods
jaehunkim 92cab4b
Merge branch 'master' of https://github.com/zksecurity/pumice into de…
jaehunkim 92ac599
fix: use densepolynomial for evaluating low degree extension
jaehunkim 1f24ae2
chore: clippy fix
jaehunkim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "fri" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
ark-ff.workspace = true | ||
ark-poly.workspace = true | ||
felt = { path = "../felt" } | ||
randomness = { path = "../randomness" } | ||
channel = { path = "../channel" } | ||
sha3.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
use ark_ff::PrimeField; | ||
use ark_poly::{ | ||
univariate::DensePolynomial, DenseUVPolynomial, EvaluationDomain, Radix2EvaluationDomain, | ||
}; | ||
|
||
use crate::stone_domain::change_order_of_elements_in_domain; | ||
|
||
#[allow(dead_code)] | ||
pub struct MultiplicativeLDE<F: PrimeField> { | ||
pub ldes: Vec<DensePolynomial<F>>, | ||
pub base: Radix2EvaluationDomain<F>, | ||
pub reversed_order: bool, | ||
} | ||
|
||
#[allow(dead_code)] | ||
impl<F: PrimeField> MultiplicativeLDE<F> { | ||
pub fn new(base: Radix2EvaluationDomain<F>, reversed_order: bool) -> Self { | ||
Self { | ||
ldes: vec![], | ||
base, | ||
reversed_order, | ||
} | ||
} | ||
|
||
// Adds an evaluation on coset that was used to build the LDE. | ||
// Future eval invocations will add the lde of that evaluation to the results. | ||
pub fn add_eval(&mut self, evaluation: &[F]) { | ||
assert_eq!( | ||
evaluation.len(), | ||
self.base.size(), | ||
"length of evaluation must be equal to base size" | ||
); | ||
|
||
let new_lde = if self.reversed_order { | ||
let evaluation_order_changed = change_order_of_elements_in_domain(evaluation); | ||
self.base.ifft(&evaluation_order_changed) | ||
} else { | ||
self.base.ifft(evaluation) | ||
}; | ||
|
||
self.ldes | ||
.push(DensePolynomial::from_coefficients_slice(&new_lde)); | ||
} | ||
|
||
pub fn add_coeff(&mut self, coeffs: &[F]) { | ||
assert!( | ||
coeffs.len() == self.base.size(), | ||
"length of coeffs must be equal to base size" | ||
); | ||
self.ldes | ||
.push(DensePolynomial::from_coefficients_slice(coeffs)); | ||
} | ||
|
||
// Evaluates the low degree extension of the evaluation that were previously added on a given coset. | ||
// The results are ordered according to the order that the LDEs were added. | ||
pub fn eval(&self, offset: F) -> Vec<Vec<F>> { | ||
let eval_domain = self.base.get_coset(offset).unwrap(); | ||
let mut evals: Vec<Vec<F>> = vec![]; | ||
for lde_poly in self.ldes.iter() { | ||
let evals_lde = lde_poly.evaluate_over_domain_by_ref(eval_domain); | ||
evals.push(evals_lde.evals); | ||
} | ||
|
||
evals | ||
} | ||
|
||
pub fn coeffs(&self, index: usize) -> &[F] { | ||
debug_assert!(index < self.ldes.len()); | ||
self.ldes[index].coeffs() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use ark_poly::{ | ||
domain::EvaluationDomain, univariate::DensePolynomial, DenseUVPolynomial, Polynomial, | ||
Radix2EvaluationDomain, | ||
}; | ||
use channel::{fs_prover_channel::FSProverChannel, Channel}; | ||
use felt::Felt252; | ||
use randomness::{keccak256::PrngKeccak256, Prng}; | ||
use sha3::Sha3_256; | ||
|
||
use super::MultiplicativeLDE; | ||
|
||
type TestProverChannel = FSProverChannel<Felt252, PrngKeccak256, Sha3_256>; | ||
|
||
fn generate_prover_channel() -> TestProverChannel { | ||
let prng = PrngKeccak256::new_with_seed(&[0u8; 4]); | ||
TestProverChannel::new(prng) | ||
} | ||
|
||
fn gen_random_field_element(prover_channel: &mut TestProverChannel) -> Felt252 { | ||
prover_channel.draw_felem() | ||
} | ||
|
||
fn setup_test_environment() -> ( | ||
Radix2EvaluationDomain<Felt252>, | ||
DensePolynomial<Felt252>, | ||
Vec<Felt252>, | ||
Felt252, | ||
) { | ||
let log_n = 4; | ||
let n = 1 << log_n; | ||
let mut test_prover_channel = generate_prover_channel(); | ||
|
||
let offset = gen_random_field_element(&mut test_prover_channel); | ||
let domain = Radix2EvaluationDomain::<Felt252>::new(n) | ||
.unwrap() | ||
.get_coset(offset) | ||
.unwrap(); | ||
|
||
let coeffs: Vec<Felt252> = (0..n) | ||
.map(|_| gen_random_field_element(&mut test_prover_channel)) | ||
.collect(); | ||
|
||
let poly = DensePolynomial::from_coefficients_vec(coeffs); | ||
let src: Vec<Felt252> = domain.elements().map(|x| poly.evaluate(&x)).collect(); | ||
|
||
let eval_domain_offset = gen_random_field_element(&mut test_prover_channel); | ||
|
||
(domain, poly, src, eval_domain_offset) | ||
} | ||
|
||
#[test] | ||
fn lde_naked_test() { | ||
let (domain, poly, src, eval_domain_offset) = setup_test_environment(); | ||
|
||
let dst_domain = domain.get_coset(eval_domain_offset).unwrap(); | ||
|
||
let lde_coeffs = domain.ifft(&src); | ||
let lde_result = dst_domain.fft(&lde_coeffs); | ||
|
||
for (x, &result) in dst_domain.elements().zip(lde_result.iter()) { | ||
let expected = poly.evaluate(&x); | ||
assert_eq!(expected, result, "LDE result mismatch at x = {:?}", x); | ||
} | ||
} | ||
|
||
#[test] | ||
fn multiplicative_lde_test() { | ||
let (domain, poly, src, eval_domain_offset) = setup_test_environment(); | ||
let mut lde = MultiplicativeLDE::new(domain, false); | ||
|
||
lde.add_eval(&src); | ||
let evals = lde.eval(eval_domain_offset); | ||
let eval_domain = domain.get_coset(eval_domain_offset).unwrap(); | ||
|
||
for (x, &result) in eval_domain.elements().zip(evals[0].iter()) { | ||
let expected = poly.evaluate(&x); | ||
assert_eq!(expected, result, "LDE result mismatch at x = {:?}", x); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod lde; | ||
mod stone_domain; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use ark_ff::FftField; | ||
use ark_poly::{EvaluationDomain, Radix2EvaluationDomain}; | ||
|
||
#[allow(dead_code)] | ||
pub fn get_field_element_at_index<F: FftField, E: EvaluationDomain<F>>( | ||
domain: &E, | ||
index: usize, | ||
) -> F { | ||
let log_len = domain.size().trailing_zeros() as usize; | ||
domain | ||
.elements() | ||
.nth(translate_index(index, log_len)) | ||
.unwrap() | ||
} | ||
|
||
// change order of elements in domain | ||
pub fn change_order_of_elements_in_domain<F: FftField>(elements: &[F]) -> Vec<F> { | ||
// get smallest power of two that is greater than elements.len() | ||
let size = elements.len().next_power_of_two(); | ||
// byte size of usize - log_len | ||
let log_len = size.trailing_zeros() as usize; | ||
// byte size of usize - log_len | ||
println!("log_len: {}", log_len); | ||
let mut new_elements = Vec::with_capacity(size); | ||
for i in 0..size { | ||
println!("i: {}", i); | ||
println!("translate_index(i): {}", translate_index(i, log_len)); | ||
new_elements.push(elements[translate_index(i, log_len)]) | ||
} | ||
|
||
new_elements | ||
} | ||
|
||
fn translate_index(index: usize, log_len: usize) -> usize { | ||
let sft = std::mem::size_of::<usize>() * 8 - log_len; | ||
index.reverse_bits() >> sft | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn make_fft_domains<F: FftField>( | ||
domain_size_log: usize, | ||
offset: F, | ||
) -> Vec<Radix2EvaluationDomain<F>> { | ||
let mut current_offset = offset; | ||
let mut domains = Vec::with_capacity(domain_size_log + 1); | ||
for i in (0..=domain_size_log).rev() { | ||
let domain = Radix2EvaluationDomain::<F>::new(1 << i) | ||
.unwrap() | ||
.get_coset(current_offset) | ||
.unwrap(); | ||
domains.push(domain); | ||
current_offset = current_offset * current_offset; | ||
} | ||
domains | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use felt::Felt252; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_change_order_of_elements_in_domain() { | ||
let elements = vec![ | ||
Felt252::from(0u64), | ||
Felt252::from(4u64), | ||
Felt252::from(2u64), | ||
Felt252::from(6u64), | ||
Felt252::from(1u64), | ||
Felt252::from(5u64), | ||
Felt252::from(3u64), | ||
Felt252::from(7u64), | ||
]; | ||
let new_elements = change_order_of_elements_in_domain(&elements); | ||
assert_eq!( | ||
new_elements, | ||
vec![ | ||
Felt252::from(0u64), | ||
Felt252::from(1u64), | ||
Felt252::from(2u64), | ||
Felt252::from(3u64), | ||
Felt252::from(4u64), | ||
Felt252::from(5u64), | ||
Felt252::from(6u64), | ||
Felt252::from(7u64), | ||
] | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a multiplicative group of order 8:
Typically, the elements are ordered as g^0, g^1, g^2, g^3, ...
However, in Stone's method, they use two types of element ordering:
One is the normal ordering, and the other is where
elements are ordered as g^0, g^4, g^2, g^6, ...