-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Finish M extension #434
Merged
Merged
Finish M extension #434
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e133257
trace new instructions
ncitron 7aed02d
rename toolchain tag file
ncitron fd39280
fix ci
ncitron faaa91b
add m extension support to tracer
ncitron 60ac0cf
surface error in toolchain download
ncitron 84ce384
remove unwrap in error handling
ncitron 9c83a33
Merge pull request #416 from a16z/ncitron/trace-m-extension
moodlezoup 347b9c3
Merge branch 'main' into feat/m-extension
moodlezoup e536276
Merge branch 'main' into feat/m-extension
moodlezoup 64b2b2f
Enable div and rem virtual sequences
moodlezoup 2394c54
Fix bytecode preprocessing for virtual instructions
moodlezoup 3c0f4b6
Fix some constraints
moodlezoup 1814e85
Fix virtual assert lte
moodlezoup fed3528
Working(?) non-uniform constraints
moodlezoup 0b3e369
Working uniform constraints (and many bug fixes along the way)
moodlezoup 6553b56
Remove unnecessary packing aux variables and constraints
moodlezoup 438d3b1
Refactor virtual_sequence and virtual_trace
moodlezoup 474a9cd
Merge remote-tracking branch 'origin/main' into feat/m-extension
moodlezoup 8ee380f
Add M extension page
moodlezoup aa07f27
Merge pull request #440 from a16z/feat/m-extension-wiki
moodlezoup fad60f6
remove clone
moodlezoup 8644f50
Merge branch 'main' into feat/m-extension
moodlezoup 660ebd1
Bump toolchain
moodlezoup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# M extension | ||
|
||
Jolt supports the RISC-V "M" extension for integer multiplication and division. | ||
The instructions included in this extension are described [here](https://msyksphinz-self.github.io/riscv-isadoc/html/rvm.html). | ||
For RV32, the M extension includes 8 instructions: `MUL`, `MULH`, `MULHSU`, `MULU`, `DIV`, `DIVU`, `REM`, and `REMU`. | ||
|
||
The [Jolt paper](https://eprint.iacr.org/2023/1217.pdf) describes how to handle the M extension instructions in Section 6, | ||
but our implementation deviates from the paper in a couple ways (described below). | ||
|
||
## Virtual sequences | ||
|
||
Section 6.1 of the Jolt paper introduces virtual instructions and registers –– some of the M extension | ||
instructions cannot be implemented as a single subtable decomposition, but rather must be split into | ||
a sequence of instructions which together compute the output and places it in the destination register. | ||
In our implementation, these sequences are captured by the `VirtualInstructionSequence` trait. | ||
|
||
The instructions that comprise such a sequence can be a combination of "real" RISC-V instructions and "virtual" | ||
instructions which only appear in the context of virtual sequences. | ||
We also introduce 32 virtual registers as "scratch space" where instructions in a virtual sequence | ||
can write intermediate values. | ||
|
||
## Deviations from the Jolt paper | ||
|
||
There are three inconsistencies between the virtual sequences provided in Section 6.3 | ||
of the Jolt paper, and the RISC-V specification. Namely: | ||
|
||
1. The Jolt prover (as described in the paper) would fail to produce a valid proof | ||
if it encountered a division by zero; since the divisor `y` is 0, the `ASSERT_LTU`/`ASSERT_LT_ABS` would | ||
always fail (for `DIVU` and `DIV`, respectively). | ||
1. The MLE provided for `ASSERT_LT_ABS` in Section 6.1.1 doesn't account for two's complement. | ||
1. The `ASSERT_EQ_SIGNS` instruction should always return true if the remainder is 0. | ||
|
||
To address these issues, our implementation of `DIVU`, `DIV`, `REMU`, and `REM` deviate from the | ||
Jolt paper in the following ways. | ||
|
||
### `DIVU` virtual sequence | ||
|
||
1. `ADVICE` --, --, --, $v_q$ `// store non-deterministic advice` $q$ `into `$v_q$ | ||
1. `ADVICE` --, --, --, $v_r$ `// store non-deterministic advice` $r$ `into `$v_r$ | ||
1. `MUL` $v_q$, $r_y$, --, $v_{qy}$ `// compute q * y` | ||
1. `ASSERT_VALID_UNSIGNED_REMAINDER` $v_r$, $r_y$, --, -- `// assert that y == 0 || r < y` | ||
1. `ASSERT_LTE` $v_{qy}$, $r_x$, --, -- `// assert q * y <= x` | ||
1. `ASSERT_VALID_DIV0` $r_y$, $v_q$, --, -- `// assert that y != 0 || q == 2 ** WORD_SIZE - 1` | ||
1. `ADD` $v_{qy}$, $v_r$, --, $v_0$ `// compute q * y + r` | ||
1. `ASSERT_EQ` $v_0$, $x$, --, -- | ||
1. `MOVE` $v_q$, --, --, `rd` | ||
|
||
### `REMU` virtual sequence | ||
|
||
1. `ADVICE` --, --, --, $v_q$ `// store non-deterministic advice` $q$ `into `$v_q$ | ||
1. `ADVICE` --, --, --, $v_r$ `// store non-deterministic advice` $r$ `into `$v_r$ | ||
1. `MUL` $v_q$, $r_y$, --, $v_{qy}$ `// compute q * y` | ||
1. `ASSERT_VALID_UNSIGNED_REMAINDER` $v_r$, $r_y$, --, -- `// assert that y == 0 || r < y` | ||
1. `ASSERT_LTE` $v_{qy}$, $r_x$, --, -- `// assert q * y <= x` | ||
1. `ADD` $v_{qy}$, $v_r$, --, $v_0$ `// compute q * y + r` | ||
1. `ASSERT_EQ` $v_0$, $x$, --, -- | ||
1. `MOVE` $v_r$, --, --, `rd` | ||
|
||
### `DIV` virtual sequence | ||
|
||
1. `ADVICE` --, --, --, $v_q$ `// store non-deterministic advice` $q$ `into `$v_q$ | ||
1. `ADVICE` --, --, --, $v_r$ `// store non-deterministic advice` $r$ `into `$v_r$ | ||
1. `ASSERT_VALID_SIGNED_REMAINDER` $v_r$, $r_y$, --, -- `// assert that r == 0 || y == 0 || (|r| < |y| && sign(r) == sign(y))` | ||
1. `ASSERT_VALID_DIV0` $r_y$, $v_q$, --, -- `// assert that y != 0 || q == 2 ** WORD_SIZE - 1` | ||
1. `MUL` $v_q$, $r_y$, --, $v_{qy}$ `// compute q * y` | ||
1. `ADD` $v_{qy}$, $v_r$, --, $v_0$ `// compute q * y + r` | ||
1. `ASSERT_EQ` $v_0$, $x$, --, -- | ||
1. `MOVE` $v_q$, --, --, `rd` | ||
|
||
### `REM` virtual sequence | ||
|
||
1. `ADVICE` --, --, --, $v_q$ `// store non-deterministic advice` $q$ `into `$v_q$ | ||
1. `ADVICE` --, --, --, $v_r$ `// store non-deterministic advice` $r$ `into `$v_r$ | ||
1. `ASSERT_VALID_SIGNED_REMAINDER` $v_r$, $r_y$, --, -- `// assert that r == 0 || y == 0 || (|r| < |y| && sign(r) == sign(y))` | ||
1. `MUL` $v_q$, $r_y$, --, $v_{qy}$ `// compute q * y` | ||
1. `ADD` $v_{qy}$, $v_r$, --, $v_0$ `// compute q * y + r` | ||
1. `ASSERT_EQ` $v_0$, $x$, --, -- | ||
1. `MOVE` $v_r$, --, --, `rd` | ||
|
||
## R1CS constraints | ||
|
||
### Ciruict flags | ||
|
||
With the M extension we introduce the following circuit flags: | ||
|
||
1. `is_virtual`: Is this instruction part of a virtual sequence? | ||
1. `is_assert`: Is this instruction an `ASSERT_*` instruction? | ||
1. `do_not_update_pc`: If this instruction is virtual and *not the last one in its sequence*, | ||
then we should *not* update the PC. | ||
This is because all instructions in virtual sequences are mapped to the same ELF address. | ||
|
||
### Uniform constraints | ||
|
||
The following constraints are enforced for every step of the execution trace: | ||
|
||
1. If the instruction is a `MUL`, `MULU`, or `MULHU`, the lookup query is the product | ||
of the two operands `x * y` (field multiplication of two 32-bit values). | ||
1. If the instruction is a `MOV` or `MOVSIGN`, the lookup query is a single operand `x` | ||
(read from the first source register `rs1`). | ||
1. If the instruction is an assert, the lookup output must be true. | ||
|
||
### Program counter constraints | ||
|
||
Each instruction in the preprocessed [bytecode](./bytecode.md) contains its (compressed) | ||
memory address as given by the ELF file. | ||
This is used to compute the expected program counter for each step in the program trace. | ||
|
||
If the `do_not_update_pc` flag is set, we constrain the next PC value to be equal to the current one. | ||
This handles the fact that all instructions in virtual sequences are mapped to the same ELF address. | ||
|
||
This also means we need some other mechanism to ensure that virtual sequences are executed in *order* and in *full*. | ||
If the current instruction is virtual, we can constrain the next instruction in the trace to be the | ||
next instruction in the bytecode. | ||
We observe that the virtual sequences used in the M extension don't involve jumps or branches, | ||
so this should always hold, *except* if we encounter a virtual instruction followed by a padding instruction. | ||
But that should never happend because an execution trace should always end with some return handling, | ||
which shouldn't involve a virtual sequence. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "muldiv" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
jolt-sdk = { path = "../../jolt-sdk", features = ["host"] } | ||
guest = { package = "muldiv-guest", path = "./guest" } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "muldiv-guest" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[[bin]] | ||
name = "guest" | ||
path = "./src/lib.rs" | ||
|
||
[features] | ||
guest = [] | ||
|
||
[dependencies] | ||
jolt = { package = "jolt-sdk", path = "../../../jolt-sdk" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![cfg_attr(feature = "guest", no_std)] | ||
#![no_main] | ||
|
||
#[jolt::provable] | ||
fn muldiv(a: u32, b: u32, c: u32) -> u32 { | ||
a * b / c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
pub fn main() { | ||
let (prove, verify) = guest::build_muldiv(); | ||
|
||
let (output, proof) = prove(12031293, 17, 92); | ||
let is_valid = verify(proof); | ||
|
||
println!("output: {}", output); | ||
println!("valid: {}", is_valid); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nightly-8af9d45d5e09a04832cc9b2e1df993fd1ce49d02 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this flag (and so we had to reverse the virtual sequence indexing) for the new non-uniform constraint, which ensures that virtual sequences are executed in full and in order