Skip to content

Commit 3c681a3

Browse files
pablodeymoentropidelicjrchatrucMauroToscano
authored
Fibonacci Stark Prover (#59)
* prover sub crate created * working on fold function * merge * test working * fold test completed * next_fri_layer function * Dependencies removed * using iterator step_by * fmt * reordering fri functions * fri_decommit init * evaluate_vec in polynomial and reference in evaluate * using evaluate_vec * evaluate_vec changed to evaluate_slice * evaluate_slice changed * fri_commitment * fri continuation * comment moved * fri_decommit_layers * comments added * polynomial.rs merge confilct * adapting to the new code * conflicts solved * append in transcript * insert last_evaluation in transcript * beta from transcript.challenge() * test: generating subgroups * prover sub crate created * Save work in progress * Add first iteration of function to get composition polynomials from trace and air * Add test for get_composition_poly * Add get_coefficients function * Tidy up code * Add docs * Fix tests * Add u128_prime field and make get_composition_poly return a Polynomial data structure * Fixes from rebasing * Apply clippy suggestions * Make functions pub crate * Tidy up code * Tidy up code * Minor fixes * Use U384 instead of U128 * Tidy up code and remove unnecessary u128 field element module * generate_vec_roots * generate_vec_roots in lib * Return trace polynomial from get_composition_poly * coset_factor * Add coset evaluation and fri commitment steps * Add result to get_cp_and_tp * Change error description and module name * Add decommitment step * Start filling the stark proof struct * Small comments * Add first verifier step * Switch to hardcoded fibonacci trace * Start FRI verification step * More progress * Improve code, change field to 17 for testing purposes * Fix FRI operation * Go back to fibonacci example with test passing * Refactor functions that use fiat shamir to take in a transcript * Add TODO * Add comments * Moved field definition to lib, removed duplicated definitions * Renamed types * Simplified operations * Refactor roots of unity generator * Small refactor * Refactor roots of unity generator * Update comment * Extracted FRI * Refactor verify * Refactor clippy * Re ordered prover * cargo fmt * fix roots of unity * Remove air * Prover -> Stark * Move folders * Uncomment tests, remove unused code * Fix fri_functions tests * Remove fri_merkle_tree module, move to mod.rs * Clippy * Remove TODOs --------- Co-authored-by: Pablo Deymonnaz <[email protected]> Co-authored-by: Mariano Nicolini <[email protected]> Co-authored-by: Javier Chatruc <[email protected]> Co-authored-by: MauroFab <[email protected]>
1 parent 5ebbd30 commit 3c681a3

File tree

20 files changed

+819
-8
lines changed

20 files changed

+819
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
members = [
44
"math",
55
"crypto",
6+
"proving-system/stark",
67
]

crypto/src/fiat_shamir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mod transcript;
1+
pub mod transcript;

crypto/src/fiat_shamir/transcript.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
use sha3::{Digest, Sha3_256};
22

3-
struct Transcript {
3+
pub struct Transcript {
44
hasher: Sha3_256,
55
}
66

77
impl Transcript {
8-
#[allow(unused)]
9-
fn new() -> Self {
8+
#[allow(clippy::new_without_default)]
9+
pub fn new() -> Self {
1010
Self {
1111
hasher: Sha3_256::new(),
1212
}
1313
}
1414

1515
#[allow(unused)]
16-
fn append(&mut self, new_data: &[u8]) {
16+
pub fn append(&mut self, new_data: &[u8]) {
1717
self.hasher.update(&mut new_data.to_owned());
1818
}
1919

2020
#[allow(unused)]
21-
fn challenge(&mut self) -> [u8; 32] {
21+
pub fn challenge(&mut self) -> [u8; 32] {
2222
let mut result_hash = [0_u8; 32];
2323
result_hash.copy_from_slice(&self.hasher.finalize_reset());
2424
self.hasher.update(result_hash);

crypto/src/merkle_tree/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::hash::traits::IsCryptoHash;
88
use self::{merkle::MerkleTree, proof::Proof};
99

1010
pub mod merkle;
11-
mod proof;
11+
pub mod proof;
1212
mod utils;
1313

1414
pub type U64F = U64PrimeField<0xFFFF_FFFF_0000_0001_u64>;

crypto/src/merkle_tree/proof.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,29 @@ use lambdaworks_math::{
55
traits::ByteConversion,
66
};
77

8+
#[derive(Debug, Clone)]
89
pub struct Proof<F: IsField, H: IsCryptoHash<F>> {
910
pub value: FieldElement<F>,
1011
pub merkle_path: Vec<(FieldElement<F>, bool)>,
1112
pub hasher: H,
1213
}
1314

15+
impl<F: IsField, H: IsCryptoHash<F>> Proof<F, H> {
16+
pub fn verify(&self, root_hash: FieldElement<F>) -> bool {
17+
let mut hashed_value = self.hasher.hash_one(self.value.clone());
18+
19+
for (sibling_node, is_left) in self.merkle_path.iter().rev() {
20+
if *is_left {
21+
hashed_value = self.hasher.hash_two(hashed_value, sibling_node.clone());
22+
} else {
23+
hashed_value = self.hasher.hash_two(sibling_node.clone(), hashed_value);
24+
}
25+
}
26+
27+
root_hash == hashed_value
28+
}
29+
}
30+
1431
impl<F, H> ByteConversion for Proof<F, H>
1532
where
1633
F: IsField,

math/src/field/element.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ where
300300
&self.value
301301
}
302302

303+
// Returns the representative of the value stored
304+
pub fn representative(&self) -> F::BaseType {
305+
F::representative(self.value.clone())
306+
}
307+
303308
/// Returns the multiplicative inverse of `self`
304309
pub fn inv(&self) -> Self {
305310
Self {

math/src/field/extensions/cubic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ where
127127
fn from_base_type(x: [FieldElement<Q::BaseField>; 3]) -> [FieldElement<Q::BaseField>; 3] {
128128
x
129129
}
130+
131+
fn representative(_x: Self::BaseType) -> Self::BaseType {
132+
todo!()
133+
}
130134
}
131135

132136
#[cfg(test)]

math/src/field/extensions/quadratic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ where
105105
fn from_base_type(x: [FieldElement<Q::BaseField>; 2]) -> [FieldElement<Q::BaseField>; 2] {
106106
x
107107
}
108+
109+
fn representative(_x: Self::BaseType) -> Self::BaseType {
110+
todo!()
111+
}
108112
}
109113

110114
#[cfg(test)]

math/src/field/fields/u384_prime_field.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ where
151151
fn from_base_type(x: Self::BaseType) -> Self::BaseType {
152152
MontgomeryAlgorithms::cios(&x, &C::R2, &C::MODULUS, &C::MU)
153153
}
154+
155+
// TO DO: Add tests for representatives
156+
fn representative(x: Self::BaseType) -> Self::BaseType {
157+
MontgomeryAlgorithms::cios(&x, &U384::from_u64(1), &C::MODULUS, &C::MU)
158+
}
154159
}
155160

156161
impl<C> ByteConversion for FieldElement<MontgomeryBackendPrimeField<C>>

math/src/field/fields/u64_prime_field.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ impl<const MODULUS: u64> IsField for U64PrimeField<MODULUS> {
5656
fn from_base_type(x: u64) -> u64 {
5757
Self::from_u64(x)
5858
}
59+
60+
fn representative(x: u64) -> u64 {
61+
x
62+
}
5963
}
6064

6165
impl<const MODULUS: u64> Copy for U64FieldElement<MODULUS> {}

0 commit comments

Comments
 (0)