Skip to content

Commit

Permalink
feat(avm): support aliases in bb-pilcom (#7904)
Browse files Browse the repository at this point in the history
Supports generating cpp variables for `pol x = sth` PIL aliases.
* Cuts down 10k+ lines from poseidon2 and others
* Improves `check_circuit` 62%! (a bit unexpected!)
  * I think what we see here is how much faster poseidon2 gets with this change. We see it in check_circuit and not in sumcheck because poseidon2 is skippable, which is enabled in sumcheck, but not in check circuit (and the test was on the token transfer). I'm pretty sure that if I prepare a 100% poseidon trace we'll see great improvements.
* Improves sumcheck (~20%)
* bugfix: Reduces the degree of some relations by 1 (max goes from 7 to 6)

BEFORE (16 cores)
```
prove/all_ms: 24789
prove/check_circuit_ms: 16416
prove/create_composer_ms: 0
prove/create_prover_ms: 2580
prove/create_verifier_ms: 0
prove/execute_log_derivative_inverse_commitments_round_ms: 824
prove/execute_log_derivative_inverse_round_ms: 583
prove/execute_pcs_rounds_ms: 232
prove/execute_relation_check_rounds_ms: 2181
prove/execute_wire_commitments_round_ms: 240
prove/gen_trace_ms: 1592
```

AFTER (16 cores)
```
prove/all_ms: 13272
prove/check_circuit_ms: 6278
prove/create_composer_ms: 0
prove/create_prover_ms: 1937
prove/create_verifier_ms: 0
prove/execute_log_derivative_inverse_commitments_round_ms: 776
prove/execute_log_derivative_inverse_round_ms: 584
prove/execute_pcs_rounds_ms: 202
prove/execute_relation_check_rounds_ms: 1742
prove/execute_wire_commitments_round_ms: 169
prove/gen_trace_ms: 1471
```

On 1 cpu, the change is not as big but it still shaves 10s from check_circuit.

BEFORE (1 cpu)

```
prove/all_ms: 86497
prove/check_circuit_ms: 38114
prove/create_composer_ms: 0
prove/create_prover_ms: 2045
prove/create_verifier_ms: 0
prove/execute_log_derivative_inverse_commitments_round_ms: 8561
prove/execute_log_derivative_inverse_round_ms: 7069
prove/execute_pcs_rounds_ms: 1899
prove/execute_relation_check_rounds_ms: 26534
prove/execute_wire_commitments_round_ms: 542
prove/gen_trace_ms: 1593
```

AFTER (1 cpu)

```
prove/all_ms: 74197
prove/check_circuit_ms: 27675
prove/create_composer_ms: 0
prove/create_prover_ms: 2093
prove/create_verifier_ms: 0
prove/execute_log_derivative_inverse_commitments_round_ms: 8913
prove/execute_log_derivative_inverse_round_ms: 7263
prove/execute_pcs_rounds_ms: 1900
prove/execute_relation_check_rounds_ms: 24044
prove/execute_wire_commitments_round_ms: 552
prove/gen_trace_ms: 1618
```

----
TL;DR: Had to do a major refactor of the codegen. Will come back later to clean some of it up.

I didn't want to, but I started pulling the thread and had to end up doing a lot of refactor/cleanup. The new version of the codegen is (imperceptibly) slower (some things are traversed several times), but I think cleaner.

VM Builder
* Separated computation of columns
* Cleaned up lots of args that were passed to handlebars but not used anymore
* Separated computation of shifts

Lookups and permutations
* Separated the NAME of the lookup/perm and the name of the inverse column

Relation builder
* Most important work is here
* Recurse expressions and look for aliases
* Filtering only the used aliases per file was a bit tough: had to take the transitive closure of the alises used in the relations
* Fixed degree of constant polynomial (was 1, but I think should be 0)
* Fixed BB hacks, not needed anymore after #7859
* Will have to come back to set up a better way to walk expressions: a visitor (I think powdr has something like this)
  • Loading branch information
fcarreiro authored Aug 12, 2024
1 parent 0d95d9e commit 09e317d
Show file tree
Hide file tree
Showing 100 changed files with 3,877 additions and 12,673 deletions.
15 changes: 6 additions & 9 deletions barretenberg/cpp/pil/avm/main.pil
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,17 @@ namespace main(256);

//===== CONTROL_FLOW_CONSISTENCY ============================================
pol INTERNAL_CALL_STACK_SELECTORS = (sel_first + sel_op_internal_call + sel_op_internal_return + sel_op_halt);
pol SEL_ALU_R_TAG = sel_op_add + sel_op_sub + sel_op_mul + sel_op_div + sel_op_not + sel_op_eq
+ sel_op_lt + sel_op_lte + sel_op_shr + sel_op_shl;
pol SEL_ALU_W_TAG = sel_op_cast;
pol SEL_ALL_ALU = SEL_ALU_R_TAG + SEL_ALU_W_TAG;
pol SEL_ALL_CTRL_FLOW = sel_op_jump + sel_op_jumpi + sel_op_internal_call + sel_op_internal_return;

pol SEL_ALL_LEFTGAS = sel_op_dagasleft + sel_op_l2gasleft;
pol SEL_ALL_BINARY = sel_op_and + sel_op_or + sel_op_xor;
pol SEL_ALL_GADGET = sel_op_radix_le + sel_op_sha256 + sel_op_poseidon2 + sel_op_keccak + sel_op_pedersen;
pol SEL_ALL_MEMORY = sel_op_cmov + sel_op_mov;
pol SEL_ALL_MEM_SLICE = sel_op_calldata_copy + sel_op_external_return;
pol OPCODE_SELECTORS = sel_op_fdiv + SEL_ALU_ALL + SEL_ALL_BINARY + SEL_ALL_MEMORY + SEL_ALL_GADGET
pol OPCODE_SELECTORS = sel_op_fdiv + SEL_ALL_ALU + SEL_ALL_BINARY + SEL_ALL_MEMORY + SEL_ALL_GADGET
+ KERNEL_INPUT_SELECTORS + KERNEL_OUTPUT_SELECTORS + SEL_ALL_LEFTGAS + SEL_ALL_MEM_SLICE;

// TODO: sel_gas_accounting_active is activating gas accounting on a given row. All opcode with selectors
Expand Down Expand Up @@ -559,15 +562,9 @@ namespace main(256);
#[MOV_MAIN_SAME_TAG]
(sel_op_mov + sel_op_cmov) * (r_in_tag - w_in_tag) = 0;

//===== ALU CONSTRAINTS =====================================================
pol SEL_ALU_R_TAG = sel_op_add + sel_op_sub + sel_op_mul + sel_op_div + sel_op_not + sel_op_eq
+ sel_op_lt + sel_op_lte + sel_op_shr + sel_op_shl;
pol SEL_ALU_W_TAG = sel_op_cast;
pol SEL_ALU_ALL = SEL_ALU_R_TAG + SEL_ALU_W_TAG;

// Predicate to activate the copy of intermediate registers to ALU table. If tag_err == 1,
// the operation is not copied to the ALU table.
sel_alu = SEL_ALU_ALL * (1 - tag_err) * (1 - op_err);
sel_alu = SEL_ALL_ALU * (1 - tag_err) * (1 - op_err);

// Dispatch the correct in_tag for alu
SEL_ALU_R_TAG * (alu_in_tag - r_in_tag) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class AvmCircuitBuilder {
using Polynomial = Flavor::Polynomial;
using ProverPolynomials = Flavor::ProverPolynomials;

static constexpr size_t num_fixed_columns = 703;
static constexpr size_t num_polys = 703 + 74;
std::vector<Row> rows;

void set_trace(std::vector<Row>&& trace) { rows = std::move(trace); }
Expand Down
Loading

0 comments on commit 09e317d

Please sign in to comment.