Commit Graph

142 Commits

Author SHA1 Message Date
Steve Wang
fba0ba8d08 modul asm only (#1410)
Only the asm file changes that's ready to merge. To be paired with
another PR that will be updated and merged later: #1404
2024-05-30 15:19:56 +00:00
Georg Wiese
0105da5b4d Add test for parallel memory accesses (#1403)
This simulates one approach we could go for when moving registers to
memory. The memory machine remains completely unchanged, but the step is
increased by more than 1 in each step of the main machine. This way,
from the point of view of memory, all the memory operations happen at
different time steps, which allows for:
- Reading from the same address twice
- Writing to the same address that we read from (which from the point of
view of memory should happen *after* the read)

The only downside I see with this approach is that this makes the
differences of time steps between memory accesses bigger: Before it was
at most the degree, now it is some small constant times the degree (in
this example 3). The way the memory machine is currently built, the
difference can be at most $2^{32} - 1$, so I think this is fine in
practice. E.g., for a degree $2^{30}$ machine we could do up to 4
parallel reads / writes.
2024-05-29 13:18:44 +00:00
Georg Wiese
1be743b641 Add multiple_signatures test (#1397)
This tests that we can re-use operation IDs (if we want the same
constraints), but switching around the inputs and outputs.
2024-05-28 18:41:06 +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
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
07642d056b Add book example that uses publics in asm (#1373)
Fixes https://github.com/powdr-labs/powdr/issues/1225
2024-05-14 12:37:51 +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
Leandro Pacheco
98e844c9a8 fix book example (#1344) 2024-05-03 13:27:26 +00:00
chriseth
3e43e33796 Constr as user-defined enum (#1252)
Turns some of the built-in types into user types in the prelude.
2024-05-01 10:29:19 +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
75968f286a Btree data structure (#1299)
Co-authored-by: Georg Wiese <georgwiese@gmail.com>
2024-04-30 09:40:58 +00:00
Leandro Pacheco
cea207ff3f Machine properties using with syntax (#1267)
This implements issue #1251.
Basically `machine Foo(a,b) { ... }` is now `machine Foo with latch: a,
operation_id: b { ... }`
2024-04-25 16:02:01 +00:00
Georg Wiese
46e8bb8ee5 Move STD machines into submodule (#1201)
Moves all machines in the standard library to `std::machines`. That way,
it is separated from the PIL utilities in STD.
2024-04-25 12:14:33 +00:00
Georg Wiese
a917e4f35a Add permutation to PIL STD (#1297)
First part of #1296 

This PR adds a `permutation()` function to the standard library. The
code is inspired by the `permutation_via_challenges` test (removed now),
[this
comment](https://github.com/powdr-labs/powdr/issues/424#issuecomment-1931686047)
by @chriseth and [this Halo2
implementation](https://github.com/privacy-scaling-explorations/halo2/blob/main/halo2_proofs/examples/shuffle.rs).
2024-04-25 07:32:35 +00:00
Steve Wang
056630049b keccak.asm (#1231)
Adapted from:
https://github.com/Zokrates/ZoKrates/blob/develop/zokrates_stdlib/stdlib/hashes/keccak/keccak.zok

Line reference comments are for the .zok file linked above.
2024-04-24 15:12:58 +00:00
chriseth
6096afb218 Patterns in let statements and function parameters (#1214)
Co-authored-by: Leo <leo@powdrlabs.com>
2024-04-03 18:39:19 +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
Leandro Pacheco
c5b0767fc4 allow PC to be updated in instruction mapping declarations (#1221) 2024-04-01 17:25:49 +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
chriseth
c85eb1e813 Remove all unreferenced definitions. (#1197)
Fixes https://github.com/powdr-labs/powdr/issues/966

---------

Co-authored-by: Leandro Pacheco <contact@leandropacheco.com>
2024-03-26 11:34:40 +00:00
Leandro Pacheco
31343690de test using call_selectors with no incoming permutation (#1198) 2024-03-25 19:10:26 +00:00
Georg Wiese
a18577fed2 Add read/write memory to standard library (#1129)
With the recent changes by @pacheco, we can extract our [memory
machine](https://github.com/powdr-labs/powdr/blob/main/riscv/src/compiler.rs#L687-L841)
as a separate machine and add it to the standard library.

The result should be the same as calling the function linked above with
`with_bootloader=false`, except that the memory alignment stuff is not
inlined. For this reason, the machine is not yet used by the RISC-V
machine, but it could be after #1077 is implemented.

[This](eb320dca0c) shows the diff from
what we have in `compiler.rs`.

<!--

Please follow this protocol when creating or reviewing PRs in this
repository:

- Leave the PR as draft until review is required.
- When reviewing a PR, every reviewer should assign themselves as soon
as they
start, so that other reviewers know the PR is covered. You should not be
discouraged from reviewing a PR with assignees, but you will know it is
not
  strictly needed.
- Unless the PR is very small, help the reviewers by not making forced
pushes, so
that GitHub properly tracks what has been changed since the last review;
use
  "merge" instead of "rebase". It can be squashed after approval.
- Once the comments have been addressed, explicitly let the reviewer
know the PR
  is ready again.

-->
2024-03-25 17:33:55 +00:00
Georg Wiese
f9318ba5a8 Implement multi-phase witgen 2024-03-25 10:27:15 +01:00
schaeff
8065794633 allow expression in degree statement 2024-03-22 19:20:29 +01:00
Georg Wiese
d2bd95cfd5 Merge pull request #1179 from powdr-labs/challenges-halo2
Implement challenges in Halo2 backend
2024-03-21 18:30:12 +00:00
Georg Wiese
0036acfa5e Implement challenges in Halo2 backend 2024-03-21 19:06:04 +01:00
Georg Wiese
10aeae6cb5 Allow and use permutation in all std machines 2024-03-21 17:20:18 +01:00
chriseth
ae8f8654b1 Merge pull request #1118 from powdr-labs/challenges
Challenges
2024-03-20 12:07:06 +00:00
chriseth
e1753fd88e Challenges and stages. 2024-03-20 12:40:51 +01:00
Leandro Pacheco
edaf536500 Allow machines to be linked via permutation
by instructions and links using `~` instead of `=`.
Block machines must declare `call_selectors`.
2024-03-20 08:17:21 -03: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
Leandro Pacheco
ff8fda1563 Expressions in instr/link params
allow for `link` and RHS of `instr` params to use full on expressions
2024-03-18 14:04:08 -03:00
Georg Wiese
f87a760071 Hints for arithmetic machine 2024-03-18 11:57:40 +01:00
schaeff
995f1fd4c2 remove ordering assumption on register declarations 2024-03-12 19:23:08 +01:00
Georg Wiese
d4477779a2 Memory machine: Don't use selectors to distinguish operations 2024-03-12 12:13:36 +01:00
chriseth
dc96509070 Make expressions and constraints native values. 2024-03-05 11:22:12 +01:00
Leo
88c9d735b7 Merge pull request #1070 from powdr-labs/link_params
Move `link` params to RHS
2024-03-04 13:40:25 +00:00
Leandro Pacheco
f3e1802787 move link params to RHS and simplify the code
- added more checks for pc machines / constrained machines
- improve example and docs
2024-03-04 09:55:37 -03:00
chriseth
df3d3e285a Implement sqrt 2024-03-04 12:51:34 +01:00
chriseth
97ed14c48b Type checking. 2024-03-01 17:36:54 +01:00
Leo
da66acac0c Merge pull request #1108 from powdr-labs/fix-halo2-soundness-bug
Fix Soundness Bug in Halo2 implementation
2024-03-01 08:40:12 +00:00
Georg Wiese
6ad188ccd3 Fix Soundness Bug in Halo2 implementation 2024-02-29 18:03:27 +01:00
chriseth
2a04e6a916 Merge pull request #1075 from powdr-labs/block-machine-permutation
Witness generation for block machines connected via permutations
2024-02-29 15:47:08 +00:00
Georg Wiese
4eb8644e79 Witness generation for block machines connected via permutations 2024-02-29 16:04:58 +01:00
Georg Wiese
50a181dab3 Add tests with invalid witnesses 2024-02-28 17:49:41 +01:00
Georg Wiese
3ce5a9f98a Add tests for Shift and Binary machines 2024-02-26 11:24:03 +01:00