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

Test/stark #85

Merged
merged 25 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/air.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod autogenerated;
mod composition;
mod global_values;
mod constants;
mod public_input;
mod public_memory;
mod diluted;
mod global_values;
mod pedersen;
mod autogenerated;
mod public_input;
mod public_memory;
mod traces;

#[cfg(test)]
Expand Down
10 changes: 6 additions & 4 deletions src/air/autogenerated.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use cairo_verifier::air::global_values::GlobalValues;
use cairo_verifier::common::math::{Felt252Div, pow};
use cairo_verifier::air::constants::{
CONSTRAINT_DEGREE, NUM_COLUMNS_FIRST, NUM_COLUMNS_SECOND, MASK_SIZE
use cairo_verifier::{
air::{
global_values::GlobalValues,
constants::{CONSTRAINT_DEGREE, NUM_COLUMNS_FIRST, NUM_COLUMNS_SECOND, MASK_SIZE}
},
common::math::{Felt252Div, pow},
};

fn eval_composition_polynomial_inner(
Expand Down
24 changes: 12 additions & 12 deletions src/air/composition.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use cairo_verifier::air::global_values::{EcPoint, InteractionElements, GlobalValues};
use cairo_verifier::air::constants::{
PUBLIC_MEMORY_STEP, DILUTED_N_BITS, DILUTED_SPACING, PEDERSEN_BUILTIN_RATIO,
PEDERSEN_BUILTIN_REPETITIONS, segments
use cairo_verifier::{
air::{
global_values::{EcPoint, InteractionElements, GlobalValues},
constants::{
PUBLIC_MEMORY_STEP, DILUTED_N_BITS, DILUTED_SPACING, PEDERSEN_BUILTIN_RATIO,
PEDERSEN_BUILTIN_REPETITIONS, segments
},
public_input::{PublicInput, PublicInputTrait}, diluted::get_diluted_product,
pedersen::{eval_pedersen_x, eval_pedersen_y},
autogenerated::{eval_composition_polynomial_inner, eval_oods_polynomial_inner}
},
common::{math::{Felt252Div, Felt252PartialOrd, pow}, asserts::assert_range_u128},
};
use cairo_verifier::air::public_input::{PublicInput, PublicInputTrait};
use cairo_verifier::air::diluted::get_diluted_product;
use cairo_verifier::air::pedersen::{eval_pedersen_x, eval_pedersen_y};
use cairo_verifier::air::autogenerated::{
eval_composition_polynomial_inner, eval_oods_polynomial_inner
};
use cairo_verifier::common::math::{Felt252Div, Felt252PartialOrd, pow};
use cairo_verifier::common::asserts::assert_range_u128;

const SHIFT_POINT_X: felt252 = 0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804;
const SHIFT_POINT_Y: felt252 = 0x3ca0cfe4b3bc6ddf346d49d06ea0ed34e621062c0e056c1d0405d266e10268a;
Expand Down
7 changes: 3 additions & 4 deletions src/air/public_input.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::debug::PrintTrait;
use core::{pedersen::PedersenTrait, hash::{HashStateTrait, HashStateExTrait, Hash}};
use cairo_verifier::{
common::{
flip_endianness::FlipEndiannessTrait, array_append::ArrayAppendTrait, blake2s::blake2s,
Expand All @@ -13,7 +13,6 @@ use cairo_verifier::{
},
domains::StarkDomains
};
use core::{pedersen::PedersenTrait, hash::{HashStateTrait, HashStateExTrait, Hash}};

#[derive(Drop, Copy, PartialEq)]
struct SegmentInfo {
Expand Down Expand Up @@ -156,11 +155,11 @@ impl PublicInputImpl of PublicInputTrait {
(prod, total_length)
}

fn validate(self: @PublicInput, domains: StarkDomains) {
fn validate(self: @PublicInput, domains: @StarkDomains) {
assert_range_u128_le(*self.log_n_steps, constants::MAX_LOG_N_STEPS);
let n_steps = pow(2, *self.log_n_steps);
assert(
n_steps * constants::CPU_COMPONENT_HEIGHT == domains.trace_domain_size,
n_steps * constants::CPU_COMPONENT_HEIGHT == *domains.trace_domain_size,
'Wrong trace size'
);

Expand Down
4 changes: 2 additions & 2 deletions src/air/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod test_autogenerated;
mod test_public_input;
mod test_traces;
mod test_diluted;
mod test_pedersen;
mod test_public_input;
mod test_traces;
mod test_composition;
11 changes: 4 additions & 7 deletions src/air/tests/test_autogenerated.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use core::array::ArrayTrait;
use cairo_verifier::air::autogenerated::{
eval_composition_polynomial_inner, eval_oods_polynomial_inner
use cairo_verifier::air::{
autogenerated::{eval_composition_polynomial_inner, eval_oods_polynomial_inner},
constants::{MASK_SIZE, N_CONSTRAINTS, NUM_COLUMNS_FIRST, NUM_COLUMNS_SECOND, CONSTRAINT_DEGREE},
global_values::{GlobalValues, EcPoint}
};
use cairo_verifier::air::constants::{
MASK_SIZE, N_CONSTRAINTS, NUM_COLUMNS_FIRST, NUM_COLUMNS_SECOND, CONSTRAINT_DEGREE
};
use cairo_verifier::air::global_values::{GlobalValues, EcPoint};

// test generated based on cairo0-verifier run on fib proof from stone-prover
#[test]
Expand Down
1 change: 0 additions & 1 deletion src/air/tests/test_diluted.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use cairo_verifier::air::diluted::get_diluted_product;
use debug::PrintTrait;

#[test]
#[available_gas(9999999999)]
Expand Down
1 change: 0 additions & 1 deletion src/air/tests/test_pedersen.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use cairo_verifier::air::pedersen::{eval_pedersen_x, eval_pedersen_y};
use debug::PrintTrait;

#[test]
#[available_gas(9999999999)]
Expand Down
6 changes: 4 additions & 2 deletions src/air/tests/test_public_input.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use cairo_verifier::air::public_input::PublicInputTrait;
use cairo_verifier::air::{public_memory::AddrValue, public_input::{PublicInput, SegmentInfo}};
use cairo_verifier::air::{
public_memory::AddrValue, public_input::{PublicInput, SegmentInfo},
public_input::PublicInputTrait
};

// test generated based on cairo0-verifier run on fib proof from stone-prover
#[test]
Expand Down
3 changes: 1 addition & 2 deletions src/air/tests/test_traces.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use cairo_verifier::air::traces::TracesConfigTrait;
use cairo_verifier::{
channel::channel::{Channel, ChannelImpl},
air::{
Expand All @@ -7,7 +6,7 @@ use cairo_verifier::{
traces_commit, traces_decommit, TracesCommitment, TracesDecommitment, TracesWitness,
TracesUnsentCommitment, TracesConfig
},
global_values::InteractionElements,
global_values::InteractionElements, traces::TracesConfigTrait,
},
table_commitment::table_commitment::{
TableCommitment, TableCommitmentConfig, TableDecommitment, TableCommitmentWitness
Expand Down
24 changes: 12 additions & 12 deletions src/air/traces.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use core::traits::Into;
use cairo_verifier::channel::channel::ChannelTrait;
use cairo_verifier::table_commitment::table_commitment::{
TableCommitment, TableDecommitment, TableCommitmentWitness, table_commit, table_decommit,
TableCommitmentConfig
use cairo_verifier::{
air::{
public_input::PublicInput, constants::{NUM_COLUMNS_FIRST, NUM_COLUMNS_SECOND},
global_values::InteractionElements,
},
channel::channel::{Channel, ChannelTrait},
table_commitment::table_commitment::{
TableCommitment, TableDecommitment, TableCommitmentWitness, table_commit, table_decommit,
TableCommitmentConfig
},
vector_commitment::vector_commitment::VectorCommitmentConfigTrait,
common::asserts::assert_in_range
};
use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfigTrait;
use cairo_verifier::air::{
public_input::PublicInput, constants::{NUM_COLUMNS_FIRST, NUM_COLUMNS_SECOND}
};
use cairo_verifier::channel::channel::Channel;
use cairo_verifier::air::global_values::InteractionElements;
use cairo_verifier::common::asserts::assert_in_range;

// A protocol component (see stark.cairo for details about protocol components) for the traces
// of the CPU AIR.
Expand Down
8 changes: 4 additions & 4 deletions src/channel/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod test_random_uint256_to_prover;
mod test_random_felts_to_prover;
mod test_read_truncated_hash_from_prover;
mod test_random_uint256_to_prover;
mod test_read_felt_from_prover;
mod test_read_uint64_from_prover;
mod test_read_felts_from_prover;
mod test_read_felt_vector_from_prover;
mod test_read_felts_from_prover;
mod test_read_truncated_hash_from_prover;
mod test_read_uint64_from_prover;
2 changes: 1 addition & 1 deletion src/common.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod array_extend;
mod array_print;
mod array_split;
mod asserts;
mod bit_reverse;
mod blake2s_u8;
mod blake2s;
mod consts;
Expand All @@ -11,7 +12,6 @@ mod from_span;
mod horner_eval;
mod math;
mod merge_sort;
mod bit_reverse;
mod powers_array;

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion src/common/array_append.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::traits::Into;
use cairo_verifier::common::flip_endianness::FlipEndiannessU32;

// 2^8 = 256
Expand Down
4 changes: 1 addition & 3 deletions src/common/blake2s.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::integer::u32_wrapping_add;
use cairo_verifier::common::array_append::ArrayAppendTrait;
use cairo_verifier::common::flip_endianness::FlipEndiannessTrait;

use cairo_verifier::common::{array_append::ArrayAppendTrait, flip_endianness::FlipEndiannessTrait};

fn blake2s(data: Array<u32>) -> u256 {
let mut state = blake2s_init();
Expand Down
4 changes: 1 addition & 3 deletions src/common/blake2s_u8.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::integer::u32_wrapping_add;
use cairo_verifier::common::array_append::ArrayAppendTrait;
use cairo_verifier::common::flip_endianness::FlipEndiannessTrait;

use cairo_verifier::common::{array_append::ArrayAppendTrait, flip_endianness::FlipEndiannessTrait};

fn blake2s(data: Array<u8>) -> u256 {
let mut state = blake2s_init();
Expand Down
8 changes: 1 addition & 7 deletions src/common/math.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ fn pow(base: felt252, exp: felt252) -> felt252 {
res
}

fn mul_inverse(x: felt252) -> felt252 {
// From Fermat's little theorem, a ^ (p - 1) = 1 when p is prime and a != 0. Since a ^ (p - 1) = a · a ^ (p - 2) we have that
// a ^ (p - 2) is the multiplicative inverse of a modulo p.
pow(x, STARK_PRIME_MINUS_TWO)
}

impl Felt252Div of Div<felt252> {
fn div(lhs: felt252, rhs: felt252) -> felt252 {
lhs * mul_inverse(rhs)
felt252_div(lhs, rhs.try_into().unwrap())
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/common/merge_sort.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use cairo_verifier::common::array_extend::ArrayExtendTrait;
use cairo_verifier::common::array_split::ArraySplitTrait;
use core::debug::PrintTrait;
use cairo_verifier::common::array_print::{ArrayPrintTrait, SpanPrintTrait};
use cairo_verifier::common::{array_extend::ArrayExtendTrait, array_split::ArraySplitTrait};

// Merge Sort
/// # Arguments
Expand Down
6 changes: 3 additions & 3 deletions src/common/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod test_blake2s;
mod test_array_append;
mod test_bit_reverse;
mod test_blake2s_u8;
mod test_blake2s;
mod test_flip_endianness;
mod test_from_span;
mod test_horner_eval;
mod test_array_append;
mod test_math;
mod test_bit_reverse;
mod test_merge_sort;
mod test_powers_array;
// notice: this test takes a lot of time and computation
Expand Down
4 changes: 1 addition & 3 deletions src/common/tests/test_blake2s_big.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use cairo_verifier::common::blake2s::blake2s;
use cairo_verifier::common::tests::test_blake2s::{get_arr_v1, get_arr_v2};

use cairo_verifier::common::{blake2s::blake2s, tests::test_blake2s::{get_arr_v1, get_arr_v2}};

#[test]
#[available_gas(9999999999)]
Expand Down
4 changes: 1 addition & 3 deletions src/common/tests/test_blake2s_big_u8.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use cairo_verifier::common::blake2s_u8::blake2s;
use cairo_verifier::common::tests::test_blake2s_u8::{get_arr_v1, get_arr_v2};

use cairo_verifier::common::{blake2s_u8::blake2s, tests::test_blake2s_u8::{get_arr_v1, get_arr_v2}};

#[test]
#[available_gas(9999999999)]
Expand Down
54 changes: 1 addition & 53 deletions src/common/tests/test_math.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cairo_verifier::common::math::{pow, mul_inverse, DivRemFelt252};
use cairo_verifier::common::math::{pow, DivRemFelt252};

#[test]
#[available_gas(9999999999)]
Expand Down Expand Up @@ -26,58 +26,6 @@ fn test_pow_2() {
);
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_1() {
let x = 9751091999414713;
let inv_x = mul_inverse(x);
assert(x * inv_x == 1, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_2() {
let x = 97199414713;
let inv_x = mul_inverse(x);
assert(x * inv_x == 1, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_3() {
let x = 92011457780;
let inv_x = mul_inverse(x);
assert(x * inv_x == 1, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_4() {
let x = 3;
let inv_x = mul_inverse(x);
assert(
inv_x == 1206167596222043737899107594365023368541035738443865566657697352045290673494,
'Invalid value'
);
assert(x * inv_x == 1, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_5() {
let x = 193456804421077096570009938751278224656090409051406060084;
let inv_inv_x = mul_inverse(mul_inverse(x));
assert(x == inv_inv_x, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_6() {
let x = 19345680409051406060084;
let inv_inv_x = mul_inverse(mul_inverse(x));
assert(x == inv_inv_x, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_div_rem_felt252() {
Expand Down
2 changes: 1 addition & 1 deletion src/deserialization.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod fri;
mod pow;
mod stark;
mod table;
mod traces;
mod vector;
mod table;
10 changes: 5 additions & 5 deletions src/deserialization/table.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use cairo_verifier::table_commitment::table_commitment::{
TableCommitmentConfig, TableCommitmentWitness, TableDecommitment
};
use cairo_verifier::deserialization::vector::{
VectorCommitmentConfigWithSerde, VectorCommitmentWitnessWithSerde
use cairo_verifier::{
table_commitment::table_commitment::{
TableCommitmentConfig, TableCommitmentWitness, TableDecommitment
},
deserialization::vector::{VectorCommitmentConfigWithSerde, VectorCommitmentWitnessWithSerde}
};

#[derive(Drop, Serde)]
Expand Down
Loading
Loading