Commit Graph

325 Commits

Author SHA1 Message Date
Thibaut Schaeffer
01acfd736e Add plonky3 support (#1158)
plonky3 backend integration for witness columns only
2024-06-26 20:05:05 +00:00
chriseth
924b9b0660 Avoid creating an intermediate vector (#1457) 2024-06-26 16:42:10 +00:00
chriseth
9c4b3e92f9 Optimize queries. (#1455) 2024-06-18 16:43:27 +00:00
Thibaut Schaeffer
2708a63d1f Avoid parentheses in AlgebraicExpression display (#1444)
- Introduce structs in the analysed AST
- Port parenthesis minimisation from parsed AST to analysed AST 

Closes #1429
2024-06-17 10:05:28 +00:00
Georg Wiese
7a851317bc Implement permutation argument using extension field (#1306)
Makes the permutation argument sound on the Goldilocks field by
evaluating polynomials on the extension field introduced in #1310.

I also used the new `Constr::Permutation` variant!

A few test cases (also tested in CI):

#### No extension field
`cargo run pil test_data/std/permutation_via_challenges.asm -o output -f
--field bn254 --prove-with halo2-mock`

This still works and produces the same output as before, thanks to the
PIL evaluator removing multiplications by 0 etc:

```
    col witness stage(1) z;
    (std::protocols::permutation::is_first * (main.z - 1)) = 0;
    ((((1 - main.first_four) * ((std::protocols::permutation::beta1 - ((std::protocols::permutation::alpha1 * main.b1) + main.b2)) - 1)) + 1) * main.z') = (((main.first_four * ((std::protocols::permutation::beta1 - ((std::protocols::permutation::alpha1 * main.a1) + main.a2)) - 1)) + 1) * main.z);
```

#### With extension field
`cargo run pil test_data/std/permutation_via_challenges_ext.asm -o
output -f --field bn254 --prove-with halo2-mock`

The constraints are significantly more complex but seem correct to me:

```
    col witness stage(1) z1;
    col witness stage(1) z2;
    (std::protocols::permutation::is_first * (main.z1 - 1)) = 0;
    (std::protocols::permutation::is_first * main.z2) = 0;
    (((((1 - main.first_four) * ((std::protocols::permutation::beta1 - ((std::protocols::permutation::alpha1 * main.b1) + main.b2)) - 1)) + 1) * main.z1') + ((7 * ((1 - main.first_four) * (std::protocols::permutation::beta2 - (std::protocols::permutation::alpha2 * main.b1)))) * main.z2')) = ((((main.first_four * ((std::protocols::permutation::beta1 - ((std::protocols::permutation::alpha1 * main.a1) + main.a2)) - 1)) + 1) * main.z1) + ((7 * (main.first_four * (std::protocols::permutation::beta2 - (std::protocols::permutation::alpha2 * main.a1)))) * main.z2));
    ((((1 - main.first_four) * (std::protocols::permutation::beta2 - (std::protocols::permutation::alpha2 * main.b1))) * main.z1') + ((((1 - main.first_four) * ((std::protocols::permutation::beta1 - ((std::protocols::permutation::alpha1 * main.b1) + main.b2)) - 1)) + 1) * main.z2')) = (((main.first_four * (std::protocols::permutation::beta2 - (std::protocols::permutation::alpha2 * main.a1))) * main.z1) + (((main.first_four * ((std::protocols::permutation::beta1 - ((std::protocols::permutation::alpha1 * main.a1) + main.a2)) - 1)) + 1) * main.z2));
```

#### On Goldilocks

Running the first example on GL fails, because using the permutation
argument without the extension field would not be sound. The second
example works, but because we don't support challenges on GL yet, it
doesn't actually run the second-phase witness generation.

---------

Co-authored-by: chriseth <chris@ethereum.org>
2024-06-12 11:15:50 +00:00
chriseth
4ab45dc302 Process pc lookup first. (#1427) 2024-06-10 10:47:22 +00:00
Georg Wiese
f2457f2573 Witgen: Pass range constraints to callee (#1389)
*Cherry-picked b1a07bd9a7 from #1380, and
extended on it.*

Fixes #1382.

With this PR, a lookup like `selector { byte_lower + 256 * byte_upper }
in { <some other machine> }` works, even if the range constraints on
`byte_lower` and `byte_upper` are not "global". For example, they could
be implemented as `selector { byte_lower } in { BYTES }` (i.e.,
`byte_lower` is only range constrained when the machine call is active).

To make this work, I changed the `Machine::process_plookup` interface
like this:
```diff
    fn process_plookup<'b, Q: QueryCallback<T>>(
        &mut self,
        mutable_state: &'b mut MutableState<'a, 'b, T, Q>,
        identity_id: u64,
-       args: &[AffineExpression<&'a AlgebraicReference, T>],
+       caller_rows: &'b RowPair<'b, 'a, T>,
    ) -> EvalResult<'a, T>;
```

The `RowPair` passed by the caller contains all range constraints known
at runtime. The LHS of the lookup (or permutation) is no longer
evaluated by the caller but by the callee. For this, the callee needs to
remember the identity associated with the `identity_id` (before this PR,
most machines just remembered the RHS, not the full identity). I don't
expect there to be any performance implications, because we only invoke
one machine (since #1154).

### Benchmark results

```
executor-benchmark/keccak
                        time:   [14.609 s 14.645 s 14.678 s]
                        change: [-2.5984% -2.3127% -2.0090%] (p = 0.00 < 0.05)
                        Performance has improved.

executor-benchmark/many_chunks_chunk_0
                        time:   [39.299 s 39.380 s 39.452 s]
                        change: [-3.9505% -3.6909% -3.4063%] (p = 0.00 < 0.05)
                        Performance has improved.
```

---------

Co-authored-by: Leo <leo@powdrlabs.com>
2024-05-28 18:40:30 +00:00
chriseth
9ee58fe7d0 Store contents in SourceRef and change to offset instead of line/col. (#1396) 2024-05-28 15:33:38 +00:00
Gastón Zanitti
0d9669e26c SourceRef in expressions (#1357)
This PR solves issue https://github.com/powdr-labs/powdr/issues/1293.

Includes PRs that modify some Expression in order to match fields. It is
recommended to review/merge those first:
- #1351 
- #1352
- #1353
- #1354
- #1355


All comments are welcome :)

---------

Co-authored-by: chriseth <chris@ethereum.org>
2024-05-26 16:52:07 +00:00
Georg Wiese
a3087ea364 Witgen: Handle machine calls with side effects (#1388)
Cherry-picked ef6a72fcfa from #1380.

With this PR, we track whether a call to a machine led to some side
effect (e.g. added a block). In that case, the processed identity should
count has having led to some progress, even if no values were returned
to the calling machine. An example would be writing values to memory,
which does not return any values and hence does not change the state of
the caller.
2024-05-21 16:09:06 +00:00
Georg Wiese
cb8969368b BlockMachine: Don't look into other fixed columns for period detection (#1383)
Fixes an issue that @leonardoalt had on his `binary-mux2` branch.

There are two ways to have a block machine that is connected via a
permutation:
1. Use permutations `<sel> { ... } is (sub.sel * sub.LATCH) { ... }`.
This makes sure only rows where `sub.LATCH` is `1` can be selected. This
is what we do when we compile from ASM to PIL.
2. Use permutations `<sel> { ... } is sub.sel { ... }`, but also a
constraint `(1 - sub.LATCH) * sub.sel = 0`. This achieves something
similar.

The problem is that in the second case, detecting the block size is
harder, because the latch doesn't appear anywhere in the selector. So we
used to look into *all* fixed columns to detect the period. But this
includes fixed columns that might have a larger period (as is the case
for the multiplexer machine).

This PR simply removes support for the second approach. I think this is
fine in practice, as I don't see a disadvantage of the first approach
and when you come from ASM everything works as expected. I did need to
adjust `test_data/pil/block_lookup_or_permutation.pil`, which used the
second approach.
2024-05-17 16:24:08 +00:00
Leo
34d04ed873 move internal dependencies to shared version (#1379)
Trying this out which will make releases much easier
2024-05-15 10:46:34 +00:00
Gastón Zanitti
3f2792f98f Number struct in Expressions (#1353)
This PR is part of issue https://github.com/powdr-labs/powdr/pull/1345.
In particular, it adds the struct Number to Expressions to homogenize
the structure before including source references.
2024-05-10 08:06:01 +00:00
chriseth
62c3eadc4c Re-enable non-inlined format args (#1362)
Fixes https://github.com/powdr-labs/powdr/issues/1360
2024-05-08 16:27:22 +00:00
Georg Wiese
f307f513ad Fix accessing challenges from hints (#1331)
This PR attempts various issues around using challenges in hints, which
is blocking #1306:
1. Hints of later-phase witness columns are now removed in witgen, as
these columns don't need to be computed yet anyway and the hint might be
accessing a challenge that does not exist.
2. The query callback is now cloned for each phase of witness generation
(because otherwise it was only available in the first phase).
3. `SymbolicEvaluator` no longer panics when encountering challenges,
but returns an error. This evaluator is used to detect patterns in
identities, like `A' - A = 0`. This means that we can't detect patterns
in identities that involve challenges, but at least it doesn't panic.
4. `witgen::query_processor::Symbols` can now evaluate challenges.
5. `witgen::query_processor::Symbols` now also looks up intermediate
"polynomials" (which includes challenges). This is necessary because we
don't currently inline intermediate polynomials in hints (which we do
for identities).

I added a test that demonstrates that challenges can now be used in
hints.
2024-05-07 14:34:05 +00:00
chriseth
ed064f8821 Connection identity test. (#1258) 2024-05-07 14:04:07 +00:00
Georg Wiese
34cdaae63e Evaluate nested expressions (#1346)
Something I came across in #1306. This let's us do something like `let
foo: fe = eval(some_column * some_challenge)`.
2024-05-07 10:12:18 +00:00
Georg Wiese
98275f827f Witness generation with copy constraints (#1276)
This PR adds witness generation support for copy constraints: Whenever a
cell value is determined, this value is copied to all cells in its
equivalence class. This allows us to do witgen for arbitrary Plonkish
circuits (which would be detected as block machines) *as long as the
circuit is topologically sorted* (because otherwise, our row-by-row
solving strategy does not work.

Copy constraints are currently only supported in the language as
`connect` identities, as opposed to lists of cell pairs that belong to
the same equivalence class. Connecting this to the PIL input should be
part of another PR.
2024-04-30 18:01:33 +00:00
chriseth
88946edfa5 Make query functions enum-exhaustive. (#1327) 2024-04-30 17:59:41 +00:00
chriseth
2b63dd8032 Add degree builtin. (#1259) 2024-04-29 12:08:26 +00:00
Georg Wiese
d67eda4300 Improve error message if the machine rows are exhausted (#1292)
@leonardoalt got a failing assertion in one of his tests because the
rows in the Poseidon machine where exhausted. With this PR, this
situation is detected earlier, leading to a better error message:

```
=> Error: Table rows exhausted for machine Secondary machine 3: main_poseidon_gl (BlockMachine)
```
2024-04-19 16:00:47 +00:00
Georg Wiese
5e125ff765 Refactor: Make global range constraints part of FixedData (#1279)
The global range constraints are now part of `FixedData`. Also, I got
rid of the row factory, anyone with a reference to `FixedData` can now
just call `Row::fresh(fixed_data, row_index)`. This simplifies things
and should help with #1276.
2024-04-18 13:46:10 +00:00
chriseth
b2e0beef36 Enum pattern (#1216) 2024-04-17 13:11:09 +00:00
Lucas Clemente Vella
9d104c2088 Removing pub and "test_" from test functions. (#1274)
One is unecessary, the other is redundant.

Co-authored-by: Lucas Clemente Vella <lucas.vella@powdrlabs.com>
2024-04-15 20:01:47 +00:00
Leo
5d162d8fda remove unaligned memory check in double sorted memory machine in witgen (#1271)
This is only needed for RISCV, but witgen is more general than RISCV and
should allow memory accesses anywhere, especially since we have the std
memory machine.

The RISCV executor also checks this so it won't go unchecked.
2024-04-15 12:44:20 +00:00
Thibaut Schaeffer
ef2409b008 Upgrade rust to 1.77 (#1253)
Required for #1158
2024-04-04 20:17:48 +00:00
chriseth
99d25fd74f Rename generic args. (#1230) 2024-04-03 13:05:23 +00:00
chriseth
f46d59dfe1 Basic version of patterns. (#1205)
Depends on #1187 

Implements part of https://github.com/powdr-labs/powdr/issues/982

Will document in https://github.com/powdr-labs/powdr/pull/1214
2024-04-03 09:30:42 +00:00
chriseth
28f0bb9c1d Statements in blocks and function annotations (#1187)
Adds statements at block level and introduces function kinds to be
either pure, constr or query.

closes https://github.com/powdr-labs/powdr/issues/960

---------

Co-authored-by: Leo <leo@powdrlabs.com>
2024-04-02 08:23:39 +00:00
chriseth
989087634c Some dependency cleanup. (#1209) 2024-03-28 12:23:35 +00:00
Georg Wiese
96893cc143 Add Write-once memory to STD (#1202)
Fixes #844

This PR adds a new machine to the STD: `WriteOnceMemory`. This can be
used in our RISC-V machine for bootloader inputs (#1203).

Most of the issues mentioned in the issue were fixed in the meantime or
had a simple workaround (like defining `let LATCH = 1`). The only
remaining issues were in the machine detection, which I fixed here.

I also re-factor two existing tests.
2024-03-26 18:50:30 +00:00
Georg Wiese
c1bc7184ed Witgen: Remember which machine answers to which lookup / permutation (#1154)
With this PR, we remember the mapping from Lookup / Permutation Identity
to Machine. This is cleaner, lets us save some work when calling a
machine and allows us to fail earlier if no machine can answer the call
(at machine extraction time, rather than runtime.

Changes:
- `Machine::process_plookup()`: Instead of passing in `right` and
`kind`, machines now only receive an `IdentityId`. If the machine needs
to access any of the expressions, references to it have to be stored
with the machine.
- `Machine::identities()`: New function that lets us ask the machine for
which identities it feels respondible.
- The `Machines` struct now stores the mapping from `IdentityId` to
machine index, allowing us to replace the loop of trying all machines
with a simple call to `Machines::call()`.
- The identity kind is now also stored in `Identity::id`. Previously,
this only stored a `usize` which is not necessarily unique. As mentioned
above, this change could be merged as part of a separate PR.

#### Benchmark Results:

```
executor-benchmark/keccak
                        time:   [8.7242 s 8.9755 s 9.3876 s]
                        change: [-4.7814% +0.8652% +7.4252%] (p = 0.79 > 0.05)
                        No change in performance detected.

executor-benchmark/many_chunks_chunk_0
                        time:   [38.731 s 39.313 s 40.081 s]
                        change: [-5.6906% -3.9986% -1.8421%] (p = 0.00 < 0.05)
                        Performance has improved.
```
2024-03-25 19:27:09 +00:00
Georg Wiese
795eb51900 Make sure Identity IDs are unique (#1196)
This PR changes that `Identity` IDs are now globally unique, by doing to
things:
1. When dispensing IDs, we now have only one ID counter, instead of one
per type.
2. Fixed a bug in the condenser, where it would previously assign the
same ID when evaluating a `|| -> constr[]`.
2024-03-25 16:29:47 +00:00
Georg Wiese
f9318ba5a8 Implement multi-phase witgen 2024-03-25 10:27:15 +01:00
chriseth
f7d7649e87 Fix cache issue. 2024-03-22 17:22:13 +01:00
chriseth
a9412a4883 Test for generic cache. 2024-03-22 16:35:27 +01:00
Georg Wiese
80fafbe19c Spell-check 2024-03-20 16:12:55 +01:00
chriseth
e1753fd88e Challenges and stages. 2024-03-20 12:40:51 +01:00
Georg Wiese
a04e3f1360 Merge pull request #1157 from powdr-labs/witgen-fix-latch-on-first-row
Fix Witgen for machines connected by permutation with latch on first row
2024-03-19 23:05:41 +00:00
Georg Wiese
be7a48cb84 Witgen: Don't derive non-unique witness when there might be overflow 2024-03-19 14:13:17 +01:00
Leo Alt
efe11f40fc enum declarations 2024-03-18 22:09:44 +01:00
Georg Wiese
6281554a45 Fix Witgen for machines connected by permutation with latch on first row 2024-03-13 13:57:54 +01:00
Georg Wiese
d7afe3d6d8 Merge pull request #1134 from powdr-labs/memory-machine-separate-selectors
Memory machine: Don't use selectors to distinguish operations
2024-03-12 11:54:20 +00:00
Georg Wiese
d4477779a2 Memory machine: Don't use selectors to distinguish operations 2024-03-12 12:13:36 +01:00
chriseth
dc05b9c56e Use only one type type. 2024-03-11 18:01:40 +01:00
chriseth
349f9aa461 Parse numbers as integers. 2024-03-08 15:48:49 +01:00
chriseth
f0c9f0c41b Delay evaluation. 2024-03-08 10:49:05 +01:00
chriseth
4024698b2a Use ibig crate. 2024-03-08 07:31:27 +01:00
chriseth
2015953698 Use evaluation cache for symbols. 2024-03-07 13:14:03 +01:00
chriseth
f66d7a983c Merge pull request #1116 from powdr-labs/remove_custom
Remove custom type from evaluator
2024-03-05 20:05:55 +00:00