-
Notifications
You must be signed in to change notification settings - Fork 9
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: poseidon2
input pack (byte-input-padding-only)
#1352
Open
reshmem
wants to merge
90
commits into
main
Choose a base branch
from
roman/poseidon-input-pack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
90 commits
Select commit
Hold shift + click to select a range
9ef4635
comments ...
reshmem c04d848
comments ...
reshmem fc0101b
comments
reshmem 28ce14c
comments
reshmem 451ade7
WIP
reshmem fede12c
WIP
reshmem b704a0e
WIP
reshmem 1e58ec8
WIP - reduce_with_powers does not work
reshmem 558bf34
WIP - fix
reshmem 767f8b9
fix tests
reshmem 553d5d7
pack to 7 - max for 64-b field + clippy
reshmem 2cb1d44
cleanups
reshmem d762387
refactor...
reshmem 89bf4d2
refactor...
reshmem 99f416d
fix
reshmem 7202f0d
refactor...
reshmem f953edc
fix - bit-padded-input
reshmem a4cbd2e
fix input_addr_padding constraint
reshmem fe41a7f
Merge branch 'main' into roman/poseidon-input-pack
reshmem 9ebd02d
after merge from main
reshmem 1885a88
clippy...
reshmem 8501dfb
Merge branch 'main' into roman/poseidon-input-pack
reshmem 2966a7d
fix after merge from main
reshmem 5fc696d
Merge branch 'main' into roman/poseidon-input-pack
reshmem bf3682d
unpack logic in its own place
reshmem 528e59f
fixes...
reshmem 54c001c
fixes...
reshmem 0fd0ab8
fixes...
reshmem 7d9ce7e
Merge branch 'main' into roman/poseidon-input-pack
reshmem 448d33b
add LE encoding
reshmem 404a5c5
cleanup
reshmem d81382b
Merge branch 'main' into roman/poseidon-input-pack
reshmem 8258863
fixes after review
reshmem 28fcfcf
Merge branch 'main' into roman/poseidon-input-pack
reshmem 3831081
Merge branch 'main' into roman/poseidon-input-pack
reshmem 700582b
Update runner/src/poseidon2.rs
reshmem 6b41395
fix
reshmem 374e2c3
Merge branch 'main' into roman/poseidon-input-pack
reshmem 72b4422
Merge branch 'main' into roman/poseidon-input-pack
reshmem 63c2123
Clippy
matthiasgoergens eb225d8
Better conditional compilation
matthiasgoergens 45f8a63
pad_trace_with_default
matthiasgoergens e3e565e
Remove useless trace
matthiasgoergens 9ed5ac2
Clean up
matthiasgoergens e73298f
Fix typo
matthiasgoergens eaaa138
CLean up
matthiasgoergens a705624
Clean up
matthiasgoergens c0e5f13
Rename
matthiasgoergens 5701875
Further cleanup
matthiasgoergens c41654d
Clean up
matthiasgoergens 93f5e56
Simpler
matthiasgoergens cc10dda
Cleaner
matthiasgoergens cd61733
Remove counter-productive 'debug' option
matthiasgoergens f823c8b
Merge branch 'matthias/remove-useless-debug' into roman/poseidon-inpu…
matthiasgoergens 3932df1
Remove poseidon2-feature
matthiasgoergens aec4160
Debug helpers
matthiasgoergens aaa7346
Merge branch 'matthias/enable-poseidon-always-remove-feature' into ro…
matthiasgoergens 9f04532
Minimise diff
matthiasgoergens 9aa4ac0
Minimise diff
matthiasgoergens a29c569
Rename
matthiasgoergens 35eb2ce
Clean up
matthiasgoergens 9b7aeb7
Revert "Simpler"
matthiasgoergens 354c567
Revert "Clean up"
matthiasgoergens bc26da4
Reverted
matthiasgoergens 5f2f78f
Clean up
matthiasgoergens 712a8ed
refactor
matthiasgoergens f4de7be
Restore
matthiasgoergens 253183d
Fixed!
matthiasgoergens 5fab346
Clean up
matthiasgoergens de40025
Clean up
matthiasgoergens 4317767
Clippy
matthiasgoergens 0c37937
Keep length in the type system
matthiasgoergens c53434c
Merge remote-tracking branch 'origin/main' into roman/poseidon-input-…
matthiasgoergens d9aa994
Merge branch 'main' into roman/poseidon-input-pack
reshmem 2c07fb9
fix: issue with under-constraint byte-address
reshmem 0ab95e3
fix: after review
reshmem ec39911
fix: comment ..
reshmem 41c3e77
fix: failed example, refactored cyclic dep
reshmem 72ec4af
taplo
reshmem 6617711
Merge branch 'main' into roman/poseidon-input-pack
reshmem 5680070
fix...
reshmem aacb641
Cargo.lock
reshmem f853513
Merge branch 'main' into roman/poseidon-input-pack
reshmem 662b827
examples/Cargo.lock
reshmem 2793151
sdk/Cargo.lock
reshmem c0d9671
clippy
reshmem 09c8a56
Merge branch 'main' into roman/poseidon-input-pack
reshmem 8f1cbfb
Merge branch 'main' into roman/poseidon-input-pack
reshmem 0cf3377
fix after merge
reshmem ae3403a
taplo
reshmem 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
WIP
- Loading branch information
commit fede12cc7110018e6449723f319f54bb6edea002
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 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,67 @@ | ||
use plonky2::hash::hash_types::RichField; | ||
|
||
use super::MIN_TRACE_LENGTH; | ||
use crate::poseidon2_preimage_pack::columns::Poseidon2PreimagePack; | ||
use crate::poseidon2_sponge::columns::Poseidon2Sponge; | ||
|
||
fn pad_trace<F: RichField>( | ||
mut trace: Vec<Poseidon2PreimagePack<F>>, | ||
) -> Vec<Poseidon2PreimagePack<F>> { | ||
trace.resize( | ||
trace.len().next_power_of_two().max(MIN_TRACE_LENGTH), | ||
Poseidon2PreimagePack::default(), | ||
); | ||
trace | ||
} | ||
|
||
pub fn generate_poseidon2_preimage_pack_trace<F: RichField>( | ||
poseidon2_sponge_rows: &[Poseidon2Sponge<F>], | ||
) -> Vec<Poseidon2PreimagePack<F>> { | ||
let trace: Vec<Poseidon2PreimagePack<F>> = poseidon2_sponge_rows | ||
.iter() | ||
.flat_map(Into::<Vec<Poseidon2PreimagePack<F>>>::into) | ||
.collect(); | ||
let trace = pad_trace(trace); | ||
log::trace!("trace {:?}", trace); | ||
trace | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use mozak_runner::vm::Row; | ||
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig}; | ||
|
||
use crate::generation::poseidon2_sponge::generate_poseidon2_sponge_trace; | ||
use crate::generation::MIN_TRACE_LENGTH; | ||
use crate::test_utils::{create_poseidon2_test, Poseidon2Test}; | ||
const D: usize = 2; | ||
type C = PoseidonGoldilocksConfig; | ||
type F = <C as GenericConfig<D>>::F; | ||
#[test] | ||
fn generate_poseidon2_output_bytes_trace() { | ||
let data = "😇 Mozak is knowledge arguments based technology".to_string(); | ||
let input_start_addr = 1024; | ||
let output_start_addr = 2048; | ||
let (_program, record) = create_poseidon2_test(&[Poseidon2Test { | ||
data, | ||
input_start_addr, | ||
output_start_addr, | ||
}]); | ||
|
||
let step_rows = record.executed; | ||
|
||
let sponge_trace = generate_poseidon2_sponge_trace(&step_rows); | ||
let trace = super::generate_poseidon2_preimage_pack_trace(&sponge_trace); | ||
// for one sponge construct we have one row with gen_output = 1. | ||
// So we expect other padding data to make trace of len MIN_TRACE_LENGTH. | ||
assert_eq!(trace.len(), MIN_TRACE_LENGTH); | ||
} | ||
|
||
#[test] | ||
fn generate_poseidon2_trace_with_dummy() { | ||
let step_rows: Vec<Row<F>> = vec![]; | ||
let sponge_trace = generate_poseidon2_sponge_trace(&step_rows); | ||
let trace = super::generate_poseidon2_preimage_pack_trace(&sponge_trace); | ||
assert_eq!(trace.len(), MIN_TRACE_LENGTH); | ||
} | ||
} |
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,110 @@ | ||
use itertools::Itertools; | ||
use mozak_runner::poseidon2::MozakPoseidon2; | ||
use plonky2::field::types::Field; | ||
use plonky2::hash::hash_types::RichField; | ||
|
||
use crate::columns_view::{columns_view_impl, make_col_map, NumberOfColumns}; | ||
use crate::linear_combination::Column; | ||
use crate::poseidon2::columns::STATE_SIZE; | ||
use crate::poseidon2_sponge::columns::Poseidon2Sponge; | ||
|
||
// FIXME: MozakPoseidon2::DATA_CAPACITY_PER_FIELD_ELEMENT; | ||
pub const BYTES_COUNT: usize = MozakPoseidon2::DATA_CAPACITY_PER_FIELD_ELEMENT; | ||
|
||
#[repr(C)] | ||
#[derive(Clone, Copy, Eq, PartialEq, Debug, Default)] | ||
pub struct Poseidon2PreimagePack<F> { | ||
pub clk: F, | ||
pub addr: F, | ||
pub bytes: [F; MozakPoseidon2::DATA_CAPACITY_PER_FIELD_ELEMENT], | ||
pub is_executed: F, | ||
} | ||
|
||
columns_view_impl!(Poseidon2PreimagePack); | ||
make_col_map!(Poseidon2PreimagePack); | ||
|
||
pub const NUM_POSEIDON2_PREIMAGE_PACK_COLS: usize = Poseidon2PreimagePack::<()>::NUMBER_OF_COLUMNS; | ||
|
||
impl<F: RichField> From<&Poseidon2Sponge<F>> for Vec<Poseidon2PreimagePack<F>> { | ||
fn from(value: &Poseidon2Sponge<F>) -> Self { | ||
if (value.ops.is_init_permute + value.ops.is_permute).is_one() { | ||
assert!( | ||
MozakPoseidon2::FIELD_ELEMENTS_RATE < STATE_SIZE, | ||
"Packing RATE should be less than STATE_SIZE" | ||
reshmem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
// FIXME: 8 should be MozakPoseidon2::FE_RATE | ||
let preimage: [F; MozakPoseidon2::FIELD_ELEMENTS_RATE] = value.preimage | ||
[..MozakPoseidon2::FIELD_ELEMENTS_RATE] | ||
.try_into() | ||
.expect("Should succeed since preimage can't be empty"); | ||
let mut base_address = value.input_addr; | ||
let mut index = 0; | ||
// For each FE of preimage we have BYTES_COUNT bytes | ||
let result = preimage | ||
.iter() | ||
.map(|fe| { | ||
// Note: assumed `to_be_bytes`, otherwise another side of the array should be | ||
// taken | ||
let bytes: Vec<_> = fe.clone().to_canonical_u64().to_be_bytes() | ||
[MozakPoseidon2::BYTES_PER_FIELD_ELEMENT | ||
- MozakPoseidon2::DATA_CAPACITY_PER_FIELD_ELEMENT..] | ||
.into_iter() | ||
.map(|e| F::from_canonical_u8(*e)) | ||
.collect(); | ||
let addr = base_address; | ||
let i = index; | ||
index+=1; | ||
base_address = base_address + F::from_canonical_u64( u64::try_from(MozakPoseidon2::DATA_CAPACITY_PER_FIELD_ELEMENT).expect("Cast from usize to u64 for MozakPoseidon2::BYTES_PER_FIELD_ELEMENT should succeed")); | ||
Poseidon2PreimagePack { | ||
clk: value.clk, | ||
addr, | ||
bytes: <[F; MozakPoseidon2::DATA_CAPACITY_PER_FIELD_ELEMENT]>::try_from( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @matthiasgoergens - don't know how to make it less ugly |
||
bytes, | ||
) | ||
.unwrap(), | ||
is_executed: F::ONE - value.is_padded[i], | ||
} | ||
}) | ||
.collect_vec(); | ||
println!("poseidon-value: {:?}", value); | ||
println!("preimage-result: {:?}", result); | ||
return result; | ||
} | ||
vec![] | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn data_for_poseidon2_sponge<F: Field>() -> Vec<Column<F>> { | ||
let data = col_map().map(Column::from); | ||
vec![ | ||
data.clk, | ||
data.addr, | ||
Column::<F>::reduce_with_powers(&data.bytes, F::from_canonical_u16(1 << 8)), | ||
] | ||
} | ||
|
||
#[must_use] | ||
pub fn filter_for_poseidon2_sponge<F: Field>() -> Column<F> { | ||
col_map().map(Column::from).is_executed | ||
} | ||
|
||
#[must_use] | ||
pub fn data_for_input_memory<F: Field>(index: u8) -> Vec<Column<F>> { | ||
assert!( | ||
usize::try_from(index).unwrap() < BYTES_COUNT, | ||
"poseidon2-preimage data_for_input_memory: index can be 0..{:?}", | ||
BYTES_COUNT | ||
); | ||
let data = col_map().map(Column::from); | ||
vec![ | ||
data.clk, | ||
Column::constant(F::ZERO), // is_store | ||
Column::constant(F::ONE), // is_load | ||
data.bytes[index as usize].clone(), // value | ||
data.addr + F::from_canonical_u8(index), // address | ||
] | ||
} | ||
|
||
#[must_use] | ||
pub fn filter_for_input_memory<F: Field>() -> Column<F> { col_map().map(Column::from).is_executed } |
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,2 @@ | ||
pub mod columns; | ||
pub mod stark; |
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.
That's just
pad_with_default
?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.
Yes it is. Should I rename it ?
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.
No, you should use the existing
pad_with_default
instead of duplicating.