From 6057dfc4e3de935b0b7195097449647ed2518eb7 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Fri, 22 Dec 2023 08:29:34 +0100 Subject: [PATCH] code checked for correctness 2/2 --- src/fri/fri_config.cairo | 27 +++++++++++++-------------- src/fri/fri_first_layer.cairo | 2 +- src/fri/fri_formula.cairo | 8 ++++---- src/fri/fri_last_layer.cairo | 2 +- src/fri/fri_layer.cairo | 4 ++-- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/fri/fri_config.cairo b/src/fri/fri_config.cairo index 1f4016216..63fb9d379 100644 --- a/src/fri/fri_config.cairo +++ b/src/fri/fri_config.cairo @@ -27,32 +27,31 @@ struct FriConfig { fn fri_config_validate( config: FriConfig, log_n_cosets: felt252, n_verifier_friendly_commitment_layers: felt252 ) -> felt252 { - assert(0_u256 <= config.log_last_layer_degree_bound.into(), 'Invalid value'); - assert( - config.log_last_layer_degree_bound.try_into().unwrap() <= MAX_LAST_LAYER_LOG_DEGREE_BOUND, - 'Invalid value' - ); + let n_layers = config.n_layers.try_into().unwrap(); + let log_last_layer_degree_bound = config.log_last_layer_degree_bound.try_into().unwrap(); - assert(2_u256 <= config.n_layers.into(), 'Invalid value'); - assert(config.n_layers.try_into().unwrap() <= MAX_FRI_LAYERS + 1, 'Invalid value'); + assert(log_last_layer_degree_bound >= 0, 'Must be non negative value'); + assert(log_last_layer_degree_bound <= MAX_LAST_LAYER_LOG_DEGREE_BOUND, 'Value too big'); - assert(*(config.fri_step_sizes[0]) == 0, 'Invalid value'); + assert(*config.fri_step_sizes.at(0) == 0, 'Invalid value'); + + assert(n_layers >= 2, 'Value too small'); + assert(n_layers <= MAX_FRI_LAYERS + 1, 'Value too big'); - let len: u32 = config.n_layers.try_into().unwrap(); let mut i: u32 = 1; let mut sum_of_step_sizes: felt252 = 0; let mut log_input_size = config.log_input_size; loop { - if i == len { + if i == n_layers { break; } - let fri_step: felt252 = *(config.fri_step_sizes.at(i)); - let table_commitment = *(config.inner_layers.at(i)); + let fri_step: felt252 = *config.fri_step_sizes.at(i); + let table_commitment = *config.inner_layers.at(i); let fri_step_u32: u32 = fri_step.try_into().unwrap(); - assert(1_u32 <= fri_step_u32, 'Invalid value'); - assert(fri_step_u32 <= MAX_FRI_STEP + 1, 'Invalid value'); + assert(fri_step_u32 >= 1, 'Value too small'); + assert(fri_step_u32 <= MAX_FRI_STEP + 1, 'Value too big'); assert(table_commitment.n_columns == fri_step * fri_step, 'Invalid value'); i += 1; diff --git a/src/fri/fri_first_layer.cairo b/src/fri/fri_first_layer.cairo index 7b7a625f0..e00de97d8 100644 --- a/src/fri/fri_first_layer.cairo +++ b/src/fri/fri_first_layer.cairo @@ -20,7 +20,7 @@ fn gather_first_layer_queries( } // Translate the coset to the homogenous group to have simple FRI equations. - let shifted_x_value = *(x_values.at(i)) * FIELD_GENERATOR_INVERSE; + let shifted_x_value = (*x_values.at(i)) * FIELD_GENERATOR_INVERSE; fri_queries .append( diff --git a/src/fri/fri_formula.cairo b/src/fri/fri_formula.cairo index 4f2698178..9702c438f 100644 --- a/src/fri/fri_formula.cairo +++ b/src/fri/fri_formula.cairo @@ -14,7 +14,7 @@ fn fri_formula2(f_x: felt252, f_minus_x: felt252, eval_point: felt252, x_inv: fe // Function to fold 4 elements into one using 2 layers of FRI. fn fri_formula4(values: Span, eval_point: felt252, x_inv: felt252) -> felt252 { - assert(values.len() == 4, 'Values length not equal 4'); + assert(values.len() == 4, 'Values length invalid'); // Applying the first layer of folding. let g0 = fri_formula2(*values[0], *values[1], eval_point, x_inv); let g1 = fri_formula2(*values[2], *values[3], eval_point, x_inv * OMEGA_4); @@ -25,7 +25,7 @@ fn fri_formula4(values: Span, eval_point: felt252, x_inv: felt252) -> f // Function to fold 8 elements into one using 3 layers of FRI. fn fri_formula8(values: Span, eval_point: felt252, x_inv: felt252) -> felt252 { - assert(values.len() == 8, 'Values length not equal 8'); + assert(values.len() == 8, 'Values length invalid'); // Applying the first two layers of folding. let g0 = fri_formula4(values.slice(0, 4), eval_point, x_inv); let g1 = fri_formula4(values.slice(4, 4), eval_point, x_inv * OMEGA_8); @@ -42,7 +42,7 @@ fn fri_formula8(values: Span, eval_point: felt252, x_inv: felt252) -> f // Function to fold 16 elements into one using 4 layers of FRI. fn fri_formula16(values: Span, eval_point: felt252, x_inv: felt252) -> felt252 { - assert(values.len() == 16, 'Values length not equal 16'); + assert(values.len() == 16, 'Values length invalid'); // Applying the first three layers of folding. let g0 = fri_formula8(values.slice(0, 8), eval_point, x_inv); let g1 = fri_formula8(values.slice(8, 8), eval_point, x_inv * OMEGA_16); @@ -72,7 +72,7 @@ fn fri_formula( } else if (coset_size == 16) { return fri_formula16(values, eval_point, x_inv); } else { - assert(values.len() == 2, 'Values length not equal 2'); + assert(values.len() == 2, 'Values length invalid'); return fri_formula2(*values[0], *values[1], eval_point, x_inv); } } diff --git a/src/fri/fri_last_layer.cairo b/src/fri/fri_last_layer.cairo index 316c78d6e..577a710f2 100644 --- a/src/fri/fri_last_layer.cairo +++ b/src/fri/fri_last_layer.cairo @@ -18,7 +18,7 @@ fn verify_last_layer(queries: Span, coefficients: Span= 0_u256, 'Must be non negative value'); verify_indices.append(coset_index); @@ -108,7 +108,7 @@ fn compute_next_layer( // Verify that at least one query was consumed. let coset_elements_len = coset_elements.len(); - assert(0 <= coset_elements_len, 'Invalid value'); + assert(coset_elements_len >= 0, 'Must be non negative value'); let coset_elements_span = coset_elements.span();