Skip to content

Commit 1683ed3

Browse files
committed
Add compress table
1 parent 9dbd8db commit 1683ed3

File tree

6 files changed

+425
-0
lines changed

6 files changed

+425
-0
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ jobs:
9797
# cargo build --verbose --target ${{ env.target }} -p p3-fri-air
9898
# cargo build --verbose --target ${{ env.target }} -p p3-interpolation-air
9999
cargo build --verbose --target ${{ env.target }} -p p3-mmcs-air
100+
cargo build --verbose --target ${{ env.target }} -p p3-poseidon2-compress-air
100101
cargo build --verbose --target ${{ env.target }} -p p3-symmetric-air
101102
cargo build --verbose --target ${{ env.target }} -p p3-recursion
102103

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ members = [
99
"fri-air",
1010
"interpolation-air",
1111
"mmcs-air",
12+
"poseidon2-compress-air",
1213
"recursion",
1314
"symmetric-air",
1415
]
@@ -40,6 +41,8 @@ p3-koala-bear = { git = "https://github.com/Plonky3/Plonky3" }
4041
p3-matrix = { git = "https://github.com/Plonky3/Plonky3" }
4142
p3-maybe-rayon = { git = "https://github.com/Plonky3/Plonky3" }
4243
p3-merkle-tree = { git = "https://github.com/Plonky3/Plonky3" }
44+
p3-poseidon2 = { git = "https://github.com/Plonky3/Plonky3" }
45+
p3-poseidon2-air = { git = "https://github.com/Plonky3/Plonky3" }
4346
p3-symmetric = { git = "https://github.com/Plonky3/Plonky3" }
4447
p3-uni-stark = { git = "https://github.com/Plonky3/Plonky3" }
4548
p3-util = { git = "https://github.com/Plonky3/Plonky3" }
@@ -71,6 +74,7 @@ p3-field-air = { path = "field-air", version = "0.1.0" }
7174
p3-fri-air = { path = "fri-air", version = "0.1.0" }
7275
p3-interpolation-air = { path = "interpolation-air", version = "0.1.0" }
7376
p3-mmcs-air = { path = "mmcs-air", version = "0.1.0" }
77+
p3-poseidon2-circuit-air = { path = "poseidon2-compress-air", version = "0.1.0" }
7478
p3-recursion = { path = "recursion", version = "0.1.0" }
7579
p3-symmetric-air = { path = "symmetric-air", version = "0.1.0" }
7680

poseidon2-compress-air/Cargo.toml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[package]
2+
name = "p3-poseidon2-compress-air"
3+
description = "An AIR implementation of Poseidon2 modified for compression in the circuit builder."
4+
version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
homepage.workspace = true
9+
keywords.workspace = true
10+
categories.workspace = true
11+
12+
[dependencies]
13+
p3-air.workspace = true
14+
p3-circuit.workspace = true
15+
p3-field.workspace = true
16+
p3-matrix.workspace = true
17+
p3-maybe-rayon.workspace = true
18+
p3-poseidon2.workspace = true
19+
p3-poseidon2-air.workspace = true
20+
21+
rand.workspace = true
22+
tracing.workspace = true
23+
24+
[target.'cfg(target_family = "unix")'.dev-dependencies]
25+
tikv-jemallocator = "0.6"
26+
27+
[dev-dependencies]
28+
p3-baby-bear.workspace = true
29+
p3-challenger.workspace = true
30+
p3-commit.workspace = true
31+
p3-dft.workspace = true
32+
p3-fri.workspace = true
33+
p3-keccak.workspace = true
34+
p3-koala-bear.workspace = true
35+
p3-merkle-tree.workspace = true
36+
p3-symmetric.workspace = true
37+
p3-uni-stark.workspace = true
38+
39+
tracing-forest = { workspace = true, features = ["ansi", "smallvec"] }
40+
tracing-subscriber = { workspace = true, features = ["std", "env-filter"] }
41+
42+
[features]
43+
parallel = ["p3-maybe-rayon/parallel"]

poseidon2-compress-air/src/air.rs

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
use core::borrow::Borrow;
2+
3+
use p3_air::{Air, AirBuilder, BaseAir};
4+
use p3_field::PrimeCharacteristicRing;
5+
use p3_matrix::Matrix;
6+
use p3_poseidon2::GenericPoseidon2LinearLayers;
7+
use p3_poseidon2_air::{Poseidon2Air, RoundConstants};
8+
9+
use crate::{Poseidon2CompressCols, num_cols};
10+
11+
/// Extends the Poseidon2 AIR with recursion circuit-specific columns and constraints.
12+
/// Assumes the field size is at least 16 bits.
13+
///
14+
/// It compresses `N` chunks of size 'CHUNK' of degree-`D` extension field elements into one chunk.
15+
#[derive(Debug)]
16+
pub struct Poseidon2CompressAir<
17+
F: PrimeCharacteristicRing,
18+
LinearLayers,
19+
const D: usize,
20+
const N: usize,
21+
const WIDTH: usize,
22+
const CHUNK: usize,
23+
const INPUT_EXTF: usize,
24+
const SBOX_DEGREE: u64,
25+
const SBOX_REGISTERS: usize,
26+
const HALF_FULL_ROUNDS: usize,
27+
const PARTIAL_ROUNDS: usize,
28+
> {
29+
p3_poseidon2: Poseidon2Air<
30+
F,
31+
LinearLayers,
32+
WIDTH,
33+
SBOX_DEGREE,
34+
SBOX_REGISTERS,
35+
HALF_FULL_ROUNDS,
36+
PARTIAL_ROUNDS,
37+
>,
38+
}
39+
40+
impl<
41+
F: PrimeCharacteristicRing,
42+
LinearLayers,
43+
const D: usize,
44+
const N: usize,
45+
const WIDTH: usize,
46+
const CHUNK: usize,
47+
const INPUT_EXTF: usize,
48+
const SBOX_DEGREE: u64,
49+
const SBOX_REGISTERS: usize,
50+
const HALF_FULL_ROUNDS: usize,
51+
const PARTIAL_ROUNDS: usize,
52+
>
53+
Poseidon2CompressAir<
54+
F,
55+
LinearLayers,
56+
D,
57+
N,
58+
WIDTH,
59+
CHUNK,
60+
INPUT_EXTF,
61+
SBOX_DEGREE,
62+
SBOX_REGISTERS,
63+
HALF_FULL_ROUNDS,
64+
PARTIAL_ROUNDS,
65+
>
66+
{
67+
pub const fn new(
68+
constants: RoundConstants<F, WIDTH, HALF_FULL_ROUNDS, PARTIAL_ROUNDS>,
69+
) -> Self {
70+
assert!(CHUNK * N == INPUT_EXTF);
71+
assert!(D * INPUT_EXTF == WIDTH);
72+
73+
Self {
74+
p3_poseidon2: Poseidon2Air::new(constants),
75+
}
76+
}
77+
}
78+
79+
impl<
80+
F: PrimeCharacteristicRing + Sync,
81+
LinearLayers: Sync,
82+
const D: usize,
83+
const N: usize,
84+
const WIDTH: usize,
85+
const CHUNK: usize,
86+
const INPUT_EXTF: usize,
87+
const SBOX_DEGREE: u64,
88+
const SBOX_REGISTERS: usize,
89+
const HALF_FULL_ROUNDS: usize,
90+
const PARTIAL_ROUNDS: usize,
91+
> BaseAir<F>
92+
for Poseidon2CompressAir<
93+
F,
94+
LinearLayers,
95+
D,
96+
N,
97+
WIDTH,
98+
CHUNK,
99+
INPUT_EXTF,
100+
SBOX_DEGREE,
101+
SBOX_REGISTERS,
102+
HALF_FULL_ROUNDS,
103+
PARTIAL_ROUNDS,
104+
>
105+
{
106+
fn width(&self) -> usize {
107+
num_cols::<
108+
WIDTH,
109+
CHUNK,
110+
INPUT_EXTF,
111+
SBOX_DEGREE,
112+
SBOX_REGISTERS,
113+
HALF_FULL_ROUNDS,
114+
PARTIAL_ROUNDS,
115+
>()
116+
}
117+
}
118+
119+
pub(crate) fn eval<
120+
AB: AirBuilder,
121+
LinearLayers: GenericPoseidon2LinearLayers<WIDTH>,
122+
const D: usize,
123+
const N: usize,
124+
const WIDTH: usize,
125+
const CHUNK: usize,
126+
const INPUT_EXTF: usize,
127+
const SBOX_DEGREE: u64,
128+
const SBOX_REGISTERS: usize,
129+
const HALF_FULL_ROUNDS: usize,
130+
const PARTIAL_ROUNDS: usize,
131+
>(
132+
air: &Poseidon2CompressAir<
133+
AB::F,
134+
LinearLayers,
135+
D,
136+
N,
137+
WIDTH,
138+
CHUNK,
139+
INPUT_EXTF,
140+
SBOX_DEGREE,
141+
SBOX_REGISTERS,
142+
HALF_FULL_ROUNDS,
143+
PARTIAL_ROUNDS,
144+
>,
145+
builder: &mut AB,
146+
_local: &Poseidon2CompressCols<
147+
AB::Var,
148+
WIDTH,
149+
CHUNK,
150+
INPUT_EXTF,
151+
SBOX_DEGREE,
152+
SBOX_REGISTERS,
153+
HALF_FULL_ROUNDS,
154+
PARTIAL_ROUNDS,
155+
>,
156+
) {
157+
air.p3_poseidon2.eval(builder);
158+
159+
// TODO: Add all lookups:
160+
// - Receive input columns from witness table.
161+
// - Send output columns to witness table.
162+
}
163+
164+
impl<
165+
AB: AirBuilder,
166+
LinearLayers: GenericPoseidon2LinearLayers<WIDTH>,
167+
const D: usize,
168+
const N: usize,
169+
const WIDTH: usize,
170+
const CHUNK: usize,
171+
const INPUT_EXTF: usize,
172+
const SBOX_DEGREE: u64,
173+
const SBOX_REGISTERS: usize,
174+
const HALF_FULL_ROUNDS: usize,
175+
const PARTIAL_ROUNDS: usize,
176+
> Air<AB>
177+
for Poseidon2CompressAir<
178+
AB::F,
179+
LinearLayers,
180+
D,
181+
N,
182+
WIDTH,
183+
CHUNK,
184+
INPUT_EXTF,
185+
SBOX_DEGREE,
186+
SBOX_REGISTERS,
187+
HALF_FULL_ROUNDS,
188+
PARTIAL_ROUNDS,
189+
>
190+
{
191+
#[inline]
192+
fn eval(&self, builder: &mut AB) {
193+
let main = builder.main();
194+
let local = main.row_slice(0).expect("The matrix is empty?");
195+
let local = (*local).borrow();
196+
197+
eval::<
198+
_,
199+
_,
200+
D,
201+
N,
202+
WIDTH,
203+
CHUNK,
204+
INPUT_EXTF,
205+
SBOX_DEGREE,
206+
SBOX_REGISTERS,
207+
HALF_FULL_ROUNDS,
208+
PARTIAL_ROUNDS,
209+
>(self, builder, local);
210+
}
211+
}

0 commit comments

Comments
 (0)