Skip to content
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

Feat: Native Poseidon2 chip #1219

Merged
merged 53 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
522bbf9
Constraints for inside level
TlatoaniHJ Jan 10, 2025
ecd1129
Make optimizations to inside row constraints
TlatoaniHJ Jan 13, 2025
dc3a36e
Finish all constraints
TlatoaniHJ Jan 13, 2025
b6b1fb1
Make records and split out into files
TlatoaniHJ Jan 13, 2025
81a6766
Some stuff
TlatoaniHJ Jan 13, 2025
48ec9d9
Write row hash part of execute
TlatoaniHJ Jan 13, 2025
9a8a480
Finish execute and add opcode
TlatoaniHJ Jan 13, 2025
c631312
Fill in interaction
TlatoaniHJ Jan 13, 2025
92b29d8
Finish (almost) trace generation for incorporate sibling
TlatoaniHJ Jan 13, 2025
dfd5073
Commit some stuff
TlatoaniHJ Jan 14, 2025
6e53d2f
Finish top level trace generation
TlatoaniHJ Jan 14, 2025
1c8c29d
Finish trace generation for inside row
TlatoaniHJ Jan 14, 2025
4c84f49
Finish implementation
TlatoaniHJ Jan 14, 2025
0353599
Implement verify_batch for test
TlatoaniHJ Jan 14, 2025
6fac595
Write test (only positive for now)
TlatoaniHJ Jan 14, 2025
78ba3f6
Some cleanup
TlatoaniHJ Jan 14, 2025
e15f8a8
Fix handling of initial cell
TlatoaniHJ Jan 14, 2025
851a8bd
Get execute and trace generation working
TlatoaniHJ Jan 14, 2025
582735d
Fix some bugs causing interaction to be unbalanced
TlatoaniHJ Jan 14, 2025
65467c6
Fix constraints
TlatoaniHJ Jan 15, 2025
97b17c6
Test passing
TlatoaniHJ Jan 15, 2025
648ee8d
Remove debugging statements
TlatoaniHJ Jan 15, 2025
235eea4
Cleanup
TlatoaniHJ Jan 15, 2025
ffb6286
Add negative test
TlatoaniHJ Jan 15, 2025
7866767
Add a test with multiple operations (failing)
TlatoaniHJ Jan 15, 2025
2a052eb
Fix failing test
TlatoaniHJ Jan 15, 2025
38cd631
Add compiler stuff
TlatoaniHJ Jan 15, 2025
02feb4f
Make fibonacci_small pass (without ext verify_batch)
TlatoaniHJ Jan 15, 2025
d2b0942
Remove debugging statements
TlatoaniHJ Jan 15, 2025
657c80d
Make address space constant
TlatoaniHJ Jan 15, 2025
04b6192
Get varying open value size working for felt
TlatoaniHJ Jan 15, 2025
d1a43e7
Fully integrate
TlatoaniHJ Jan 15, 2025
bfacc6a
Merge branch 'main' into feat/native-p2-chip
TlatoaniHJ Jan 15, 2025
823deea
Fix merge main
TlatoaniHJ Jan 15, 2025
2e0db3e
Fix magical test
TlatoaniHJ Jan 15, 2025
d924444
Fix behavior for rolling_hash not multiple of 8
TlatoaniHJ Jan 15, 2025
6fb7d15
Optimize specific columns
TlatoaniHJ Jan 16, 2025
7a0ee20
Passing old p2 tests
TlatoaniHJ Jan 17, 2025
8f8d335
Add simple perm functionality
TlatoaniHJ Jan 17, 2025
87ec833
Support COMP_POS2
TlatoaniHJ Jan 17, 2025
8069c0c
Merge branch 'main' into feat/native-p2-chip
TlatoaniHJ Jan 17, 2025
2c70598
Remove old Poseidon2 chip
TlatoaniHJ Jan 17, 2025
ae3a551
Fix magical test
TlatoaniHJ Jan 17, 2025
a724e49
Fix lint
TlatoaniHJ Jan 17, 2025
c247440
Document VERIFY_BATCH opcode
TlatoaniHJ Jan 17, 2025
5482566
Rename VerifyBatchChip to NativePoseidon2Chip
TlatoaniHJ Jan 17, 2025
76def78
Rustfmt
TlatoaniHJ Jan 17, 2025
57c4d8f
merge
yi-sun Jan 21, 2025
522734e
chore: move poseidon2 opcode and test into native-compiler/circuit
jonathanpwang Jan 21, 2025
9b0b7f4
chore: add some comments
jonathanpwang Jan 21, 2025
dd0fa74
chore: remove magic bus idx 7
jonathanpwang Jan 21, 2025
3f64254
nit: cleanup std::array::
jonathanpwang Jan 21, 2025
3c5c4a3
chore: update stark-backend rev
jonathanpwang Jan 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/vm/src/arch/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ impl<F: PrimeField32> VmChipTestBuilder<F> {
self.memory.write(address_space, pointer, value);
}

pub fn write_usize<const N: usize>(
&mut self,
address_space: usize,
pointer: usize,
value: [usize; N],
) {
self.memory
.write(address_space, pointer, value.map(F::from_canonical_usize));
}

pub fn write_heap<const NUM_LIMBS: usize>(
&mut self,
register: usize,
Expand Down
9 changes: 9 additions & 0 deletions docs/specs/ISA.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,15 @@ and only support `CHUNK = 8` and `WIDTH = 16` in BabyBear Poseidon2 above. For t
is read in two batches of size `CHUNK`, and, similarly, the output is written in either one or two batches of
size `CHUNK`, depending on the output size of the corresponding opcode.

### Verification

We have the following special opcode tailored to optimize verification. Due to already having a large number of operands,
the address space is fixed to be `AS::Native = 5`.

| Name | Operands | Description |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **VERIFY_BATCH** `[CHUNK, PID]` <br/><br/> Here `CHUNK` and `PID` are **constants** that determine different opcodes. `PID` is an internal identifier for particular Poseidon2 constants dependent on the field (see below). | `a,b,c,d,e,f,g` | Computes `mmcs::verify_batch`. In the native address space, `[a], [b], [d], [e], [f]` should be the array start pointers for the dimensions array, the opened values array (which contains more arrays), the proof (which contains arrays of length `CHUNK`) and the commitment (which is an array of length `CHUNK`). `[c]` should be the length of the opened values array (and so should be equal to the length of the dimensions array as well). `g` should be the reciprocal of the size (in field elements) of the values contained in the opened values array: if the opened values array contains field elements, `g` should be 1; if the opened values array contains extension field elements, `g` should be 1/4. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This explanation is probably not going to be that helpful, if we could point to a rust function or a more detailed writeup, that would be better.


## Phantom Sub-Instructions

As mentioned in [System](#system), the **PHANTOM** instruction has different behavior based on the operand `c`.
Expand Down
1 change: 1 addition & 0 deletions extensions/native/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ openvm-instructions = { workspace = true }
openvm-rv32im-circuit = { workspace = true }
openvm-native-compiler = { workspace = true }


strum.workspace = true
itertools.workspace = true
tracing.workspace = true
Expand Down
19 changes: 12 additions & 7 deletions extensions/native/circuit/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use openvm_instructions::{
use openvm_native_compiler::{
CastfOpcode, FieldArithmeticOpcode, FieldExtensionOpcode, FriOpcode, NativeBranchEqualOpcode,
NativeJalOpcode, NativeLoadStore4Opcode, NativeLoadStoreOpcode, NativePhantom,
BLOCK_LOAD_STORE_SIZE,
VerifyBatchOpcode, BLOCK_LOAD_STORE_SIZE,
};
use openvm_poseidon2_air::Poseidon2Config;
use openvm_rv32im_circuit::{
Expand All @@ -31,6 +31,7 @@ use strum::IntoEnumIterator;

use crate::{
adapters::{convert_adapter::ConvertAdapterChip, *},
chip::NativePoseidon2Chip,
phantom::*,
*,
};
Expand Down Expand Up @@ -85,8 +86,8 @@ pub enum NativeExecutor<F: PrimeField32> {
Jal(NativeJalChip<F>),
FieldArithmetic(FieldArithmeticChip<F>),
FieldExtension(FieldExtensionChip<F>),
Poseidon2(NativePoseidon2Chip<F>),
FriReducedOpening(FriReducedOpeningChip<F>),
VerifyBatch(NativePoseidon2Chip<F, 1>),
}

#[derive(From, ChipUsageGetter, Chip, AnyEnum, BytesStateful)]
Expand Down Expand Up @@ -198,18 +199,22 @@ impl<F: PrimeField32> VmExtension<F> for Native {
FriOpcode::iter().map(VmOpcode::with_default_offset),
)?;

let poseidon2_chip = NativePoseidon2Chip::new(
let verify_batch_chip = NativePoseidon2Chip::new(
execution_bus,
program_bus,
memory_bridge,
Poseidon2Config::default(),
VerifyBatchOpcode::default_offset(),
Poseidon2Opcode::default_offset(),
builder.system_config().max_constraint_degree,
offline_memory.clone(),
Poseidon2Config::default(),
);
inventory.add_executor(
poseidon2_chip,
Poseidon2Opcode::iter().map(VmOpcode::with_default_offset),
verify_batch_chip,
[
VmOpcode::with_default_offset(VerifyBatchOpcode::VERIFY_BATCH),
VmOpcode::with_default_offset(Poseidon2Opcode::PERM_POS2),
VmOpcode::with_default_offset(Poseidon2Opcode::COMP_POS2),
],
)?;

builder.add_phantom_sub_executor(
Expand Down
Loading