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

Erabinov/reduces #1271

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
50b4f23
profiling setup
erabinov Jul 30, 2024
868385c
removed unnecessary
erabinov Jul 30, 2024
82b2615
Update domain.rs
erabinov Jul 30, 2024
2dcefe3
count reduce max bits
erabinov Jul 30, 2024
56a159a
removed over-reduce
erabinov Jul 31, 2024
5c51f21
push
jtguibas Jul 31, 2024
db58cb8
hm
jtguibas Jul 31, 2024
e764454
hm
jtguibas Jul 31, 2024
41e68c1
hm
jtguibas Jul 31, 2024
39d2d15
hm
jtguibas Jul 31, 2024
1532817
boom
jtguibas Jul 31, 2024
fccb5ed
hm
jtguibas Jul 31, 2024
546356f
hm
jtguibas Jul 31, 2024
a82de5c
hm
jtguibas Jul 31, 2024
0c5dc86
hm
jtguibas Jul 31, 2024
e6a00cf
more optimizations
jtguibas Jul 31, 2024
2e5f2b6
hm
jtguibas Jul 31, 2024
37ed86d
hm
jtguibas Jul 31, 2024
97f67ed
hm
jtguibas Jul 31, 2024
339df54
hm
jtguibas Jul 31, 2024
925e30f
weird
jtguibas Jul 31, 2024
cac4737
fixed inv e
jtguibas Jul 31, 2024
fb25af3
hm
jtguibas Jul 31, 2024
990b60f
hm
jtguibas Jul 31, 2024
3e1a837
acched proof
jtguibas Jul 31, 2024
4ecafa6
hm
jtguibas Jul 31, 2024
ca3de79
fix bug
jtguibas Jul 31, 2024
28567d9
Wtf
jtguibas Jul 31, 2024
c12d461
reduce counts hashmap
erabinov Jul 31, 2024
148a3b9
precomputed x powers
erabinov Aug 1, 2024
769ce8d
wip
erabinov Aug 1, 2024
9323408
babybear range check fix
erabinov Aug 1, 2024
417b3e8
h
erabinov Aug 2, 2024
50e5d1d
optimistic reduction
erabinov Aug 2, 2024
8b76205
MulEF first attempt
erabinov Aug 2, 2024
0bba967
semi-working with range-checker environment variable
erabinov Aug 2, 2024
bb8e7a8
moved quotient binary decomp
erabinov Aug 2, 2024
8319f8c
zero() and one(), remove unnecessary tobits
erabinov Aug 3, 2024
5d6bc51
zero() and one(), remove unnecessary tobits
erabinov Aug 3, 2024
1c95894
sound range check?
erabinov Aug 3, 2024
4179a5a
commit for testing
erabinov Aug 5, 2024
3201bab
added opcode for felt2var in circuit
erabinov Aug 5, 2024
d5561fc
correct felt2var
erabinov Aug 6, 2024
83e882f
two effective reduces
erabinov Aug 6, 2024
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
52 changes: 26 additions & 26 deletions prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,40 +765,40 @@ pub mod tests {
let core_proof = prover.prove_core(&pk, &stdin, opts, context)?;
let public_values = core_proof.public_values.clone();

tracing::info!("verify core");
prover.verify(&core_proof.proof, &vk)?;
// tracing::info!("verify core");
// prover.verify(&core_proof.proof, &vk)?;

if test_kind == Test::Core {
return Ok(());
}
// if test_kind == Test::Core {
// return Ok(());
// }

tracing::info!("compress");
let compressed_proof = prover.compress(&vk, core_proof, vec![], opts)?;
// tracing::info!("compress");
// let compressed_proof = prover.compress(&vk, core_proof, vec![], opts)?;

tracing::info!("verify compressed");
prover.verify_compressed(&compressed_proof, &vk)?;
// tracing::info!("verify compressed");
// prover.verify_compressed(&compressed_proof, &vk)?;

if test_kind == Test::Compress {
return Ok(());
}
// if test_kind == Test::Compress {
// return Ok(());
// }

tracing::info!("shrink");
let shrink_proof = prover.shrink(compressed_proof, opts)?;
// tracing::info!("shrink");
// let shrink_proof = prover.shrink(compressed_proof, opts)?;

tracing::info!("verify shrink");
prover.verify_shrink(&shrink_proof, &vk)?;
// tracing::info!("verify shrink");
// prover.verify_shrink(&shrink_proof, &vk)?;

if test_kind == Test::Shrink {
return Ok(());
}
// if test_kind == Test::Shrink {
// return Ok(());
// }

tracing::info!("wrap bn254");
let wrapped_bn254_proof = prover.wrap_bn254(shrink_proof, opts)?;
let bytes = bincode::serialize(&wrapped_bn254_proof).unwrap();
// tracing::info!("wrap bn254");
// let wrapped_bn254_proof = prover.wrap_bn254(shrink_proof, opts)?;
// let bytes = bincode::serialize(&wrapped_bn254_proof).unwrap();

// Save the proof.
let mut file = File::create("proof-with-pis.bin").unwrap();
file.write_all(bytes.as_slice()).unwrap();
// // Save the proof.
// let mut file = File::create("proof-with-pis.bin").unwrap();
// file.write_all(bytes.as_slice()).unwrap();

// Load the proof.
let mut file = File::open("proof-with-pis.bin").unwrap();
Expand Down Expand Up @@ -828,7 +828,7 @@ pub mod tests {
let plonk_bn254_proof = prover.wrap_plonk_bn254(wrapped_bn254_proof, &artifacts_dir);
println!("{:?}", plonk_bn254_proof);

prover.verify_plonk_bn254(&plonk_bn254_proof, &vk, &public_values, &artifacts_dir)?;
// prover.verify_plonk_bn254(&plonk_bn254_proof, &vk, &public_values, &artifacts_dir)?;

Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions recursion/circuit-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ rand = "0.8.5"

[features]
native-gnark = ["sp1-recursion-gnark-ffi/native"]

[[bin]]
name = "circuit-architecture-sweep"
path = "scripts/circuit_architecture_sweep.rs"
70 changes: 35 additions & 35 deletions recursion/circuit-v2/scripts/circuit_architecture_sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,49 @@ fn machine_with_dummy<const DEGREE: usize, const COL_PADDING: usize>(

fn main() {
// Test the performance of the full architecture with different degrees.
let machine_maker_3 = || machine_with_all_chips::<3>(16, 16, 16);
let machine_maker_5 = || machine_with_all_chips::<5>(16, 16, 16);
let machine_maker_9 = || machine_with_all_chips::<9>(16, 16, 16);
let machine_maker_17 = || machine_with_all_chips::<17>(16, 16, 16);
test_machine(machine_maker_3);
test_machine(machine_maker_5);
test_machine(machine_maker_9);
test_machine(machine_maker_17);
// let machine_maker_3 = || machine_with_all_chips::<3>(16, 16, 16);
// let machine_maker_5 = || machine_with_all_chips::<5>(16, 16, 16);
// let machine_maker_9 = || machine_with_all_chips::<9>(16, 16, 16);
// let machine_maker_17 = || machine_with_all_chips::<17>(16, 16, 16);
// test_machine(machine_maker_3);
// test_machine(machine_maker_5);
// test_machine(machine_maker_9);
// test_machine(machine_maker_17);

// Test the performance of the machine with the full architecture for different numbers of rows
// in the precompiles. Degree is set to 9.
let machine_maker = |i| machine_with_all_chips::<9>(i, i, i);
for i in 1..=5 {
test_machine(|| machine_maker(i));
}
// let machine_maker = |i| machine_with_all_chips::<9>(i, i, i);
// for i in 1..=5 {
// test_machine(|| machine_maker(i));
// }

// Test the performance of the dummy machine for different numbers of columns in the dummy table.
// Degree is kept fixed at 9.
test_machine(|| machine_with_dummy::<9, 1>(16));
test_machine(|| machine_with_dummy::<9, 50>(16));
test_machine(|| machine_with_dummy::<9, 100>(16));
test_machine(|| machine_with_dummy::<9, 150>(16));
test_machine(|| machine_with_dummy::<9, 200>(16));
test_machine(|| machine_with_dummy::<9, 250>(16));
test_machine(|| machine_with_dummy::<9, 300>(16));
test_machine(|| machine_with_dummy::<9, 350>(16));
test_machine(|| machine_with_dummy::<9, 400>(16));
test_machine(|| machine_with_dummy::<9, 450>(16));
test_machine(|| machine_with_dummy::<9, 500>(16));
test_machine(|| machine_with_dummy::<9, 550>(16));
test_machine(|| machine_with_dummy::<9, 600>(16));
test_machine(|| machine_with_dummy::<9, 650>(16));
test_machine(|| machine_with_dummy::<9, 700>(16));
test_machine(|| machine_with_dummy::<9, 750>(16));
// test_machine(|| machine_with_dummy::<9, 1>(16));
// test_machine(|| machine_with_dummy::<9, 50>(16));
// test_machine(|| machine_with_dummy::<9, 100>(16));
// test_machine(|| machine_with_dummy::<9, 150>(16));
// test_machine(|| machine_with_dummy::<9, 200>(16));
// test_machine(|| machine_with_dummy::<9, 250>(16));
// test_machine(|| machine_with_dummy::<9, 300>(16));
// test_machine(|| machine_with_dummy::<9, 350>(16));
// test_machine(|| machine_with_dummy::<9, 400>(16));
// test_machine(|| machine_with_dummy::<9, 450>(16));
// test_machine(|| machine_with_dummy::<9, 500>(16));
// test_machine(|| machine_with_dummy::<9, 550>(16));
// test_machine(|| machine_with_dummy::<9, 600>(16));
// test_machine(|| machine_with_dummy::<9, 650>(16));
// test_machine(|| machine_with_dummy::<9, 700>(16));
test_machine(|| machine_with_dummy::<5, 130>(18));

// Test the performance of the dummy machine for different heights of the dummy table.
for i in 4..=7 {
test_machine(|| machine_with_dummy::<9, 1>(i));
}
// for i in 4..=7 {
// test_machine(|| machine_with_dummy::<9, 1>(i));
// }

// Change the degree for the dummy table, keeping other parameters fixed.
test_machine(|| machine_with_dummy::<3, 500>(16));
test_machine(|| machine_with_dummy::<5, 500>(16));
test_machine(|| machine_with_dummy::<9, 500>(16));
test_machine(|| machine_with_dummy::<17, 500>(16));
// test_machine(|| machine_with_dummy::<3, 500>(16));
// test_machine(|| machine_with_dummy::<5, 500>(16));
// test_machine(|| machine_with_dummy::<9, 500>(16));
// test_machine(|| machine_with_dummy::<17, 500>(16));
}
12 changes: 9 additions & 3 deletions recursion/circuit-v2/src/build_wrap_v2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Borrow;
use std::iter::once;
use std::{borrow::Borrow, os};

use p3_baby_bear::{BabyBear, DiffusionMatrixBabyBear};
use p3_bn254_fr::Bn254Fr;
Expand Down Expand Up @@ -27,9 +27,11 @@ use sp1_recursion_circuit::{
use sp1_recursion_compiler::{
config::OuterConfig,
constraints::{Constraint, ConstraintCompiler},
ir::{Builder, Config, Ext, Felt, Usize, Var, Witness},
ir::{Builder, Config, DslIr, Ext, Felt, Usize, Var, Witness},
};
use sp1_recursion_core::{
air::RecursionPublicValues, range_check, stark::config::BabyBearPoseidon2Outer,
};
use sp1_recursion_core::{air::RecursionPublicValues, stark::config::BabyBearPoseidon2Outer};
use sp1_recursion_program::types::QuotientDataValues;

type OuterSC = BabyBearPoseidon2Outer;
Expand Down Expand Up @@ -159,6 +161,10 @@ where
// builder.assert_felt_eq(*expected_elm, *calculated_elm);
// }

builder
.operations
.push(DslIr::CycleTracker("Hello world".to_string()));

let mut backend = ConstraintCompiler::<OuterConfig>::default();
backend.emit(builder.operations)
}
Expand Down
3 changes: 1 addition & 2 deletions recursion/circuit/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ pub fn reduce_32<C: Config>(builder: &mut Builder<C>, vals: &[Felt<C::F>]) -> Va
let mut power = C::N::one();
let result: Var<C::N> = builder.eval(C::N::zero());
for val in vals.iter() {
let bits = builder.num2bits_f_circuit(*val);
let val = builder.bits2num_v_circuit(&bits);
let val = builder.felt2var_circuit(*val);
builder.assign(result, result + val * power);
power *= C::N::from_canonical_u64(1u64 << 32);
}
Expand Down
50 changes: 40 additions & 10 deletions recursion/circuit/src/fri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,26 @@ pub fn verify_shape_and_sample_challenges<C: Config>(
challenger.check_witness(builder, config.proof_of_work_bits, proof.pow_witness);

let log_max_height = proof.commit_phase_commits.len() + config.log_blowup;

let mut precomputed_generator_powers: Vec<Felt<_>> = vec![
builder.eval(SymbolicFelt::from_f(C::F::one())),
builder.eval(SymbolicFelt::from_f(C::F::two_adic_generator(
log_max_height,
))),
];

let mut cur = precomputed_generator_powers[1];
for _ in 2..log_max_height {
cur = builder.eval(cur * cur);
precomputed_generator_powers.push(cur);
}

let query_indices: Vec<Var<_>> = (0..config.num_queries)
.map(|_| challenger.sample_bits(builder, log_max_height))
.collect();

FriChallenges {
precomputed_generator_powers,
query_indices,
betas,
}
Expand All @@ -60,6 +75,7 @@ pub fn verify_two_adic_pcs<C: Config>(
challenger: &mut MultiField32ChallengerVariable<C>,
rounds: Vec<TwoAdicPcsRoundVariable<C>>,
) {
let mut counter = 0;
let alpha = challenger.sample_ext(builder);

let fri_challenges =
Expand Down Expand Up @@ -125,16 +141,22 @@ pub fn verify_two_adic_pcs<C: Config>(
let two_adic_generator_exp =
builder.exp_f_bits(two_adic_generator, rev_reduced_index);
let x: Felt<_> = builder.eval(g * two_adic_generator_exp);
builder.operations.push(DslIr::ReduceF(x));

for (z, ps_at_z) in izip!(mat_points, mat_values) {
let mut acc: Ext<C::F, C::EF> =
builder.eval(SymbolicExt::from_f(C::EF::zero()));
counter += 1;
for (p_at_x, &p_at_z) in izip!(mat_opening.clone(), ps_at_z) {
let pow = log_height_pow[log_height];

// Fill in any missing powers of alpha.
(alpha_pows.len()..pow + 1).for_each(|_| {
alpha_pows.push(builder.eval(*alpha_pows.last().unwrap() * alpha));
let alpha = builder.eval(*alpha_pows.last().unwrap() * alpha);
builder.operations.push(DslIr::ReduceE(alpha));
alpha_pows.push(alpha);
});

acc = builder.eval(acc + (alpha_pows[pow] * (p_at_z - p_at_x[0])));
log_height_pow[log_height] += 1;
}
Expand All @@ -146,6 +168,8 @@ pub fn verify_two_adic_pcs<C: Config>(
})
.collect::<Vec<_>>();

println!("Counter: {}", counter);

verify_challenges(
builder,
config,
Expand All @@ -163,6 +187,8 @@ pub fn verify_challenges<C: Config>(
reduced_openings: Vec<[Ext<C::F, C::EF>; 32]>,
) {
let log_max_height = proof.commit_phase_commits.len() + config.log_blowup;
println!("Log max height: {}", log_max_height);
println!("Num queries: {}", config.num_queries);
for (&index, query_proof, ro) in izip!(
&challenges.query_indices,
&proof.query_proofs,
Expand All @@ -176,6 +202,7 @@ pub fn verify_challenges<C: Config>(
challenges.betas.clone(),
ro,
log_max_height,
challenges.precomputed_generator_powers.clone(),
);

builder.assert_ext_eq(folded_eval, proof.final_poly);
Expand All @@ -190,14 +217,12 @@ pub fn verify_query<C: Config>(
betas: Vec<Ext<C::F, C::EF>>,
reduced_openings: [Ext<C::F, C::EF>; 32],
log_max_height: usize,
precomputed_generator_powers: Vec<Felt<C::F>>,
) -> Ext<C::F, C::EF> {
let mut folded_eval: Ext<C::F, C::EF> = builder.eval(SymbolicExt::from_f(C::EF::zero()));
let two_adic_generator = builder.eval(SymbolicExt::from_f(C::EF::two_adic_generator(
log_max_height,
)));
let index_bits = builder.num2bits_v_circuit(index, 32);
let rev_reduced_index = builder.reverse_bits_len_circuit(index_bits.clone(), log_max_height);
let mut x = builder.exp_e_bits(two_adic_generator, rev_reduced_index);
let mut x = builder.exp_f_bits_precomputed(rev_reduced_index, &precomputed_generator_powers);

let mut offset = 0;
for (log_folded_height, commit, step, beta) in izip!(
Expand All @@ -212,6 +237,9 @@ pub fn verify_query<C: Config>(
let index_sibling: Var<_> = builder.eval(one - index_bits.clone()[offset]);
let index_pair = &index_bits[(offset + 1)..];

builder.operations.push(DslIr::ReduceE(folded_eval));
builder.operations.push(DslIr::ReduceE(step.sibling_value));

let evals_ext = [
builder.select_ef(index_sibling, folded_eval, step.sibling_value),
builder.select_ef(index_sibling, step.sibling_value, folded_eval),
Expand All @@ -234,13 +262,15 @@ pub fn verify_query<C: Config>(
step.opening_proof.clone(),
);

let xs_new = builder.eval(x * C::EF::two_adic_generator(1));
let xs_new = builder.eval(x * C::F::two_adic_generator(1));
let xs = [
builder.select_ef(index_sibling, x, xs_new),
builder.select_ef(index_sibling, xs_new, x),
builder.select_f(index_sibling, x, xs_new),
builder.select_f(index_sibling, xs_new, x),
];
folded_eval = builder
.eval(evals_ext[0] + (beta - xs[0]) * (evals_ext[1] - evals_ext[0]) / (xs[1] - xs[0]));
let one: Felt<_> = builder.eval(C::F::one());
let inv: Felt<_> = builder.eval(one / (xs[1] - xs[0]));
folded_eval =
builder.eval(evals_ext[0] + (beta - xs[0]) * (evals_ext[1] - evals_ext[0]) * inv);
x = builder.eval(x * x);
offset += 1;
}
Expand Down
6 changes: 5 additions & 1 deletion recursion/circuit/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use sp1_core::{
};
use sp1_recursion_compiler::config::OuterConfig;
use sp1_recursion_compiler::constraints::{Constraint, ConstraintCompiler};
use sp1_recursion_compiler::ir::{Builder, Config, Ext, Felt, Var};
use sp1_recursion_compiler::ir::{Builder, Config, DslIr, Ext, Felt, Var};
use sp1_recursion_compiler::ir::{Usize, Witness};
use sp1_recursion_compiler::prelude::SymbolicVar;
use sp1_recursion_core::air::{RecursionPublicValues, NUM_PV_ELMS_TO_HASH};
Expand Down Expand Up @@ -358,6 +358,10 @@ pub fn build_wrap_circuit(
builder.assert_felt_eq(*expected_elm, *calculated_elm);
}

builder
.operations
.push(DslIr::CycleTracker("Hello World".to_string()));

let mut backend = ConstraintCompiler::<OuterConfig>::default();
backend.emit(builder.operations)
}
Expand Down
1 change: 1 addition & 0 deletions recursion/circuit/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct FriQueryProofVariable<C: Config> {
/// Reference: https://github.com/Plonky3/Plonky3/blob/4809fa7bedd9ba8f6f5d3267b1592618e3776c57/fri/src/verifier.rs#L22
#[derive(Clone)]
pub struct FriChallenges<C: Config> {
pub precomputed_generator_powers: Vec<Felt<C::F>>,
pub query_indices: Vec<Var<C::N>>,
pub betas: Vec<Ext<C::F, C::EF>>,
}
Expand Down
Loading
Loading