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: poseidon2 input pack (byte-input-padding-only) #1352

Open
wants to merge 90 commits into
base: main
Choose a base branch
from

Conversation

reshmem
Copy link
Contributor

@reshmem reshmem commented Mar 10, 2024

  1. Padding of byte-input only (7 * RATE)
  2. Bit Padding Schema was used (thanks to @codeblooded1729)
  3. No padding of FE (every FE in sponge-preimage is un-packed to 7 bytes that anchored inside memory-stark)
  4. Currently most tight packing of 7 bytes per 1 field element is used (it still possible to configure it to 1..7)
  5. CTL from poseidon2-preimage-pack to memory
  6. CTL from poseidon2-sponge to poseidon2-preimage-pack + reduce_with_powers to make 1 field element out of 7 bytes

Me and @codeblooded1729 think that this implementation will be performant especially in case of data-input length more than 256 bytes.

For small input, like 32 bytes performance can be decreased .

Benchmarking shows 15-20% increase in performance overall. For very small input, the results are very noisy since poseidon2 is part of mozak-stark and its run-time is less than other starks.

@reshmem reshmem changed the title [WIP] fear: poseidon2 input pack [WIP] feat: poseidon2 input pack Mar 11, 2024
@reshmem reshmem changed the title [WIP] feat: poseidon2 input pack feat: poseidon2 input pack (byte-input-padding-only) Mar 19, 2024
@reshmem reshmem requested a review from codeblooded1729 March 19, 2024 20:54
value: value.preimage[i],
..Default::default()
// each Field element in preimage represents packed data (packed bytes)
(0..Poseidon2Permutation::<F>::RATE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

TODO(Matthias): this is all very suspicious.

Base automatically changed from matthias/enable-poseidon-always-remove-feature to main April 13, 2024 06:41
matthiasgoergens and others added 3 commits April 13, 2024 16:30
Basically, the bug allows applying sponge permutation on a given chunk of packed field elements in arbitrary order. That is, prover, instead of supplying given chunk c: [F; 8] in the intended order [c_1, c_2 ...c_8]` to Poseidon2Sponge, can instead supply it in any way he wants!

Here is how:
Say prover packs two field elements out of address region (0...14) with content [1, 2 ... 14]
So corresponding packed values would be
let packed_1 = Poseidon2PreimagePack<F> {
    pub clk: some_clk,
    pub byte_addr: 0,
    pub fe_addr: 0,
    pub bytes: [1, 2 ..7], // 2261738553347342
    pub is_executed: 1,
}
let packed_2 = Poseidon2PreimagePack<F> {
    pub clk: some_clk,
    pub byte_addr: 8,
    pub fe_addr: 1,
    pub bytes: [8, 9, .. 14], // 283686952306183
    pub is_executed: 1,
}
The idea is that there is no constraint tying byte_addr with fe_addr.
That is, the malicious prover can flip the above fe_addrs by (packed_1.fe_addr, packed_2.fe_addr) = (packed_2.fe_addr, packed_1.fe_addr).
It won't be detected anywhere!
Hence when supplying the above to Poseidon2Sponge, he can emulate that he is supplying 283686952306183 before 2261738553347342.
Simply because the former pack has fe_addr=0, and the latter pack has fe_addr=1 after modification.
So instead of sponge permutation acting on [2261738553347342, 283686952306183...],
it acts on [283686952306183, 2261738553347342...], hence leading to different output.
In the former codebase, this can't happen because of CTL against Memory stark, which ensures that input to Poseidon2Sponge is coming in correct order (according to their addresses). But in the current implementation, there is no restriction, because fe_addr is not a real address as you pointed out!
I think there is an easy way out though. We change the interpretation of input_addr in Poseidon2Sponge
pub struct Poseidon2Sponge<T> {
    pub clk: T,
    pub ops: Ops<T>,
    pub input_addr: T,
    pub output_addr: T,
    pub input_len: T,
    pub preimage: [T; WIDTH],
    pub output: [T; WIDTH],
    pub gen_output: T,
    pub input_addr_padded: T,
}
Earlier this said "Hey, I am taking 8 bytes in region input_addr...input_addr+8, feeding it to current sponge preimage , apply sponge permutation, and I get output .  I have also ordered the trace in increasing order of input_addr which you can check. Hence, you can be sure I am feeding it in correct order."
@reshmem reshmem force-pushed the roman/poseidon-input-pack branch from ed728a8 to 2c07fb9 Compare April 14, 2024 09:57
matthiasgoergens added a commit that referenced this pull request Apr 15, 2024
@supragya
Copy link
Contributor

Would recomment @codeblooded1729 to take a look. Although part of SDK, this was last touched (and mostly coded up by @codeblooded1729). Not many touch-points here for me!

reshmem added 4 commits April 16, 2024 15:22
# Conflicts:
#	circuits/src/generation/mod.rs
#	circuits/src/stark/mozak_stark.rs
#	sdk/Cargo.toml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants