Skip to content

Commit

Permalink
feat(avm): gas specific range check (#9874)
Browse files Browse the repository at this point in the history
Please read [contributing guidelines](CONTRIBUTING.md) and remove this
line.
  • Loading branch information
IlyasRidhuan authored Nov 12, 2024
1 parent 52ae4e1 commit 1a9c5ce
Show file tree
Hide file tree
Showing 19 changed files with 28,798 additions and 829 deletions.
24 changes: 16 additions & 8 deletions barretenberg/cpp/pil/avm/gas.pil
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,21 @@ namespace main(256);

// ========= Initialize Range Check Gadget ===============================
// We range check that the absolute value of the differences between each row of l2 and da gas are 32 bits.
#[PERM_RNG_GAS_L2]
range_check.gas_l2_rng_chk {range_check.clk, range_check.value}
is
is_gas_accounted {main.clk, main.abs_l2_rem_gas };
pol commit l2_gas_u16_r0;
pol commit l2_gas_u16_r1;
// Do we need DA?
main.abs_l2_rem_gas = l2_gas_u16_r0 + l2_gas_u16_r1 * 2**16;

#[PERM_RNG_GAS_DA]
range_check.gas_da_rng_chk {range_check.clk, range_check.value}
is
is_gas_accounted {main.clk, main.abs_da_rem_gas };
#[LOOKUP_L2_GAS_RNG_CHK_0]
is_gas_accounted { l2_gas_u16_r0 } in main.sel_rng_16 { main.clk };
#[LOOKUP_L2_GAS_RNG_CHK_1]
is_gas_accounted { l2_gas_u16_r1 } in main.sel_rng_16 { main.clk };

pol commit da_gas_u16_r0;
pol commit da_gas_u16_r1;
main.abs_da_rem_gas = da_gas_u16_r0 + da_gas_u16_r1 * 2**16;

#[LOOKUP_DA_GAS_RNG_CHK_0]
is_gas_accounted { da_gas_u16_r0 } in main.sel_rng_16 { main.clk };
#[LOOKUP_DA_GAS_RNG_CHK_1]
is_gas_accounted { da_gas_u16_r1 } in main.sel_rng_16 { main.clk };
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.main_bin_op_id.set_if_valid_index(i, rows[i].main_bin_op_id);
polys.main_call_ptr.set_if_valid_index(i, rows[i].main_call_ptr);
polys.main_da_gas_remaining.set_if_valid_index(i, rows[i].main_da_gas_remaining);
polys.main_da_gas_u16_r0.set_if_valid_index(i, rows[i].main_da_gas_u16_r0);
polys.main_da_gas_u16_r1.set_if_valid_index(i, rows[i].main_da_gas_u16_r1);
polys.main_da_out_of_gas.set_if_valid_index(i, rows[i].main_da_out_of_gas);
polys.main_dyn_da_gas_op_cost.set_if_valid_index(i, rows[i].main_dyn_da_gas_op_cost);
polys.main_dyn_gas_multiplier.set_if_valid_index(i, rows[i].main_dyn_gas_multiplier);
Expand Down Expand Up @@ -282,6 +284,8 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.main_l1_to_l2_msg_exists_write_offset.set_if_valid_index(
i, rows[i].main_l1_to_l2_msg_exists_write_offset);
polys.main_l2_gas_remaining.set_if_valid_index(i, rows[i].main_l2_gas_remaining);
polys.main_l2_gas_u16_r0.set_if_valid_index(i, rows[i].main_l2_gas_u16_r0);
polys.main_l2_gas_u16_r1.set_if_valid_index(i, rows[i].main_l2_gas_u16_r1);
polys.main_l2_out_of_gas.set_if_valid_index(i, rows[i].main_l2_out_of_gas);
polys.main_mem_addr_a.set_if_valid_index(i, rows[i].main_mem_addr_a);
polys.main_mem_addr_b.set_if_valid_index(i, rows[i].main_mem_addr_b);
Expand Down Expand Up @@ -820,6 +824,10 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.lookup_byte_lengths_counts.set_if_valid_index(i, rows[i].lookup_byte_lengths_counts);
polys.lookup_byte_operations_counts.set_if_valid_index(i, rows[i].lookup_byte_operations_counts);
polys.lookup_opcode_gas_counts.set_if_valid_index(i, rows[i].lookup_opcode_gas_counts);
polys.lookup_l2_gas_rng_chk_0_counts.set_if_valid_index(i, rows[i].lookup_l2_gas_rng_chk_0_counts);
polys.lookup_l2_gas_rng_chk_1_counts.set_if_valid_index(i, rows[i].lookup_l2_gas_rng_chk_1_counts);
polys.lookup_da_gas_rng_chk_0_counts.set_if_valid_index(i, rows[i].lookup_da_gas_rng_chk_0_counts);
polys.lookup_da_gas_rng_chk_1_counts.set_if_valid_index(i, rows[i].lookup_da_gas_rng_chk_1_counts);
polys.kernel_output_lookup_counts.set_if_valid_index(i, rows[i].kernel_output_lookup_counts);
polys.lookup_into_kernel_counts.set_if_valid_index(i, rows[i].lookup_into_kernel_counts);
polys.lookup_cd_value_counts.set_if_valid_index(i, rows[i].lookup_cd_value_counts);
Expand Down
Loading

0 comments on commit 1a9c5ce

Please sign in to comment.