Skip to content

Commit 4bd3554

Browse files
only 1 execution, at witness generation, instead of 2, using precomputed values for the "non -vectorized" memory size (idea by Lambdaclass)
Co-authored-by: Robin Salen <[email protected]>
1 parent 983db67 commit 4bd3554

29 files changed

+1091
-917
lines changed

Cargo.lock

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ multilinear-toolkit.workspace = true
111111
# p3-poseidon2-air = { path = "../zk/Plonky3/poseidon2-air" }
112112
# p3-dft = { path = "../zk/Plonky3/dft" }
113113
# p3-challenger = { path = "../zk/Plonky3/challenger" }
114-
# p3-util = { path = "../zk/Plonky3/util" }
114+
115+
# [patch."https://github.com/TomWambsgans/whir-p3.git"]
116+
# whir-p3 = { path = "../zk/whir/fork-whir-p3" }
115117

116118
[dev-dependencies]
117119
criterion = { version = "0.7", default-features = false, features = ["cargo_bench_support"] }

TODO.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
- opti compute_eval_eq when scalar = ONE
2929
- Dmitry's range check, bonus: we can spare 2 memory cells if the value being range check is small (using the zeros present by conventio on the public memory)
3030
- Make everything "padding aware" (including WHIR, logup*, AIR, etc)
31+
- Opti WHIR: in sumcheck we know more than f(0) + f(1), we know f(0) and f(1)
32+
- Opti WHIR https://github.com/tcoratger/whir-p3/issues/303 and https://github.com/tcoratger/whir-p3/issues/306
33+
- Avoid committing to extra columns / adding some constraints in poseidon16 AIR, for "compression", and use instead sumcheck
3134

3235
About "the packed pcs" (similar to SP1 Jagged PCS, slightly less efficient, but simpler (no sumchecks)):
3336
- The best strategy is probably to pack as much as possible (the cost increasing the density = additional inner evaluations), if we can fit below a power of 2 - epsilon (epsilon = 20% for instance, tbd), if the sum of the non zero data is just above a power of 2, no packed technique, even the best, can help us, so we should spread aniway (to reduce the pressure of inner evaluations)

crates/lean_compiler/src/b_compile_intermediate.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,19 +707,24 @@ fn compile_poseidon(
707707
compiler: &Compiler,
708708
over_16: bool, // otherwise over_24
709709
) -> Result<(), String> {
710-
assert_eq!(args.len(), 3);
711-
712710
let low_level_arg_a = IntermediateValue::from_simple_expr(&args[0], compiler);
713711
let low_level_arg_b = IntermediateValue::from_simple_expr(&args[1], compiler);
714712
let low_level_res = IntermediateValue::from_simple_expr(&args[2], compiler);
715713

716714
if over_16 {
715+
assert_eq!(args.len(), 4);
716+
let is_compression = args[3].as_constant().unwrap().naive_eval().unwrap();
717+
assert!(is_compression.is_zero() || is_compression.is_one());
718+
let is_compression = is_compression.is_one();
719+
717720
instructions.push(IntermediateInstruction::Poseidon2_16 {
718721
arg_a: low_level_arg_a,
719722
arg_b: low_level_arg_b,
720723
res: low_level_res,
724+
is_compression,
721725
});
722726
} else {
727+
assert_eq!(args.len(), 3);
723728
instructions.push(IntermediateInstruction::Poseidon2_24 {
724729
arg_a: low_level_arg_a,
725730
arg_b: low_level_arg_b,

crates/lean_compiler/src/c_compile_final.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,17 @@ fn compile_block(
261261
IntermediateValue::Constant(ConstExpression::Value(ConstantValue::Scalar(1)));
262262
codegen_jump(hints, low_level_bytecode, one, dest, updated_fp)
263263
}
264-
IntermediateInstruction::Poseidon2_16 { arg_a, arg_b, res } => {
264+
IntermediateInstruction::Poseidon2_16 {
265+
arg_a,
266+
arg_b,
267+
res,
268+
is_compression,
269+
} => {
265270
low_level_bytecode.push(Instruction::Poseidon2_16 {
266271
arg_a: try_as_mem_or_constant(&arg_a).unwrap(),
267272
arg_b: try_as_mem_or_constant(&arg_b).unwrap(),
268273
res: try_as_mem_or_fp(&res).unwrap(),
274+
is_compression,
269275
});
270276
}
271277
IntermediateInstruction::Poseidon2_24 { arg_a, arg_b, res } => {

crates/lean_compiler/src/ir/instruction.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ pub enum IntermediateInstruction {
3131
Poseidon2_16 {
3232
arg_a: IntermediateValue, // vectorized pointer, of size 1
3333
arg_b: IntermediateValue, // vectorized pointer, of size 1
34-
res: IntermediateValue, // vectorized pointer, of size 2
34+
res: IntermediateValue, // vectorized pointer, of size 1 if `is_compression`, else size 2
35+
is_compression: bool,
3536
},
3637
Poseidon2_24 {
3738
arg_a: IntermediateValue, // vectorized pointer, of size 2 (2 first inputs)
@@ -168,8 +169,16 @@ impl Display for IntermediateInstruction {
168169
write!(f, "jump_if_not_zero {condition} to {dest}")
169170
}
170171
}
171-
Self::Poseidon2_16 { arg_a, arg_b, res } => {
172-
write!(f, "{res} = poseidon2_16({arg_a}, {arg_b})")
172+
Self::Poseidon2_16 {
173+
arg_a,
174+
arg_b,
175+
res,
176+
is_compression,
177+
} => {
178+
write!(
179+
f,
180+
"{res} = poseidon2_16({arg_a}, {arg_b}, is_compression={is_compression})"
181+
)
173182
}
174183
Self::Poseidon2_24 { arg_a, arg_b, res } => {
175184
write!(f, "{res} = poseidon2_24({arg_a}, {arg_b})")

crates/lean_compiler/src/precompiles.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Display for PrecompileName {
2727

2828
pub const POSEIDON_16: Precompile = Precompile {
2929
name: PrecompileName::Poseidon16,
30-
n_inputs: 3,
30+
n_inputs: 4,
3131
};
3232

3333
pub const POSEIDON_24: Precompile = Precompile {

crates/lean_prover/src/common.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub fn get_base_dims(
3333
ColDims::padded(n_cycles, F::ZERO), // mem_addr_c
3434
ColDims::padded(n_poseidons_16, F::from_usize(ZERO_VEC_PTR)), // poseidon16 index a
3535
ColDims::padded(n_poseidons_16, F::from_usize(ZERO_VEC_PTR)), // poseidon16 index b
36-
ColDims::padded(n_poseidons_16, F::from_usize(POSEIDON_16_NULL_HASH_PTR)), // poseidon16 index res
3736
ColDims::padded(n_poseidons_24, F::from_usize(ZERO_VEC_PTR)), // poseidon24 index a
3837
ColDims::padded(n_poseidons_24, F::from_usize(ZERO_VEC_PTR)), // poseidon24 index b
3938
ColDims::padded(n_poseidons_24, F::from_usize(POSEIDON_24_NULL_HASH_PTR)), // poseidon24 index res
@@ -139,7 +138,7 @@ pub fn add_memory_statements_for_dot_product_precompile(
139138

140139
pub struct PrecompileFootprint {
141140
pub global_challenge: EF,
142-
pub p16_powers: [EF; 5],
141+
pub p16_powers: [EF; 6],
143142
pub p24_powers: [EF; 5],
144143
pub dot_product_powers: [EF; 6],
145144
pub multilinear_eval_powers: [EF; 6],
@@ -164,6 +163,7 @@ impl PrecompileFootprint {
164163
(nu_a * self.p16_powers[2]
165164
+ nu_b * self.p16_powers[3]
166165
+ nu_c * self.p16_powers[4]
166+
+ mul_point_f_and_ef(point[COL_INDEX_AUX], self.p16_powers[5])
167167
+ self.p16_powers[1])
168168
* point[COL_INDEX_POSEIDON_16]
169169
+ (nu_a * self.p24_powers[2]
@@ -383,10 +383,10 @@ pub fn add_poseidon_lookup_statements_on_indexes(
383383
log_n_p24: usize,
384384
index_lookup_point: &MultilinearPoint<EF>,
385385
inner_values: &[EF],
386-
p16_indexe_statements: [&mut Vec<Evaluation<EF>>; 3], // a, b, res
386+
p16_indexe_statements: [&mut Vec<Evaluation<EF>>; 4], // a, b, res_1, res_2
387387
p24_indexe_statements: [&mut Vec<Evaluation<EF>>; 3], // a, b, res
388388
) {
389-
assert_eq!(inner_values.len(), 6);
389+
assert_eq!(inner_values.len(), 7);
390390
let mut idx_point_right_p16 = MultilinearPoint(index_lookup_point[3..].to_vec());
391391
let mut idx_point_right_p24 =
392392
MultilinearPoint(index_lookup_point[3 + log_n_p16.abs_diff(log_n_p24)..].to_vec());
@@ -402,7 +402,7 @@ pub fn add_poseidon_lookup_statements_on_indexes(
402402
for (i, stmt) in p24_indexe_statements.into_iter().enumerate() {
403403
stmt.push(Evaluation::new(
404404
idx_point_right_p24.clone(),
405-
inner_values[i + 3],
405+
inner_values[i + 4],
406406
));
407407
}
408408
}

0 commit comments

Comments
 (0)