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

Public Input Commitment for Step Circuit #6

Merged
merged 18 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ halo2curves = { git = "https://github.com/timoftime/halo2curves", branch = "dev/
# halo2curves = { path = "../halo2curves" }

[patch."https://github.com/axiom-crypto/halo2-lib"]
halo2-base = { git = "https://github.com/timoftime/halo2-lib", rev = "d3455345c40fdee4462e400b55b94559c6d494e9", default-features = false, features = [
halo2-base = { git = "https://github.com/timoftime/halo2-lib", rev = "03c7baedeae208b21359e542b8683644b3eb1ac2", default-features = false, features = [
"halo2-pse",
"display",
] }
halo2-ecc = { git = "https://github.com/timoftime/halo2-lib", rev = "d3455345c40fdee4462e400b55b94559c6d494e9", default-features = false }
poseidon = { git = "https://github.com/timoftime/halo2-lib", rev = "d3455345c40fdee4462e400b55b94559c6d494e9", default-features = false }
halo2-ecc = { git = "https://github.com/timoftime/halo2-lib", rev = "03c7baedeae208b21359e542b8683644b3eb1ac2", default-features = false }
poseidon = { git = "https://github.com/timoftime/halo2-lib", rev = "03c7baedeae208b21359e542b8683644b3eb1ac2", default-features = false }

# halo2-base = { path = "../halo2-lib/halo2-base", default-features = false, features = [
# "halo2-pse",
Expand Down
4 changes: 3 additions & 1 deletion lightclient-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ snark-verifier-sdk = { git = "https://github.com/axiom-crypto/snark-verifier.git
"loader_evm",
"halo2-pse",
] }

# crypto
num-bigint = { version = "0.4", features = ["rand"] }
pasta_curves = "0.4.1"
Expand Down Expand Up @@ -60,5 +59,8 @@ hex = "0.4"
rayon = "1.7.0"
ark-std = { version = "0.4.0", features = ["print-trace"] }

poseidon_native = { git = "https://github.com/axiom-crypto/halo2.git", branch = "axiom/dev", package = "poseidon" }
# [dev-dependencies]

[features]
default = []
2 changes: 1 addition & 1 deletion lightclient-circuits/config/sync_step_k20.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "strategy": "Vertical", "k": 20, "num_advice_per_phase": [17, 0, 0], "num_lookup_advice_per_phase": [2, 0, 0], "num_fixed": 1 }
{ "strategy": "Vertical", "k": 20, "num_advice_per_phase": [18, 0, 0], "num_lookup_advice_per_phase": [2, 0, 0], "num_fixed": 1 }
2 changes: 1 addition & 1 deletion lightclient-circuits/src/gadget/crypto/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
util::{ThreadBuilderBase, ThreadBuilderConfigBase},
};

use super::sha256::{assign_threads_sha, SpreadConfig, FIRST_PHASE};
use super::sha256_flex::{assign_threads_sha, SpreadConfig, FIRST_PHASE};

#[derive(Debug, Clone)]
pub struct SHAConfig<F: Field, CustomConfig: ThreadBuilderConfigBase<F>> {
Expand Down
4 changes: 2 additions & 2 deletions lightclient-circuits/src/gadget/crypto/ecc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eth_types::{Field, AppCurveExt};
use eth_types::{AppCurveExt, Field};
use halo2_base::Context;
use halo2_ecc::{bls12_381::FpChip, bigint::ProperCrtUint, fields::FieldChip};
use halo2_ecc::{bigint::ProperCrtUint, bls12_381::FpChip, fields::FieldChip};

// Calculates y^2 = x^3 + 4 (the curve equation)
pub fn calculate_ysquared<F: Field, C: AppCurveExt>(
Expand Down
4 changes: 2 additions & 2 deletions lightclient-circuits/src/gadget/crypto/hash2curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use super::{
util::{fp2_sgn0, i2osp, strxor},
Fp2Point, G1Point, G2Point, HashInstructions,
};
use super::{ShaContexts, ShaThreadBuilder, AssignedHashResult};
use super::{AssignedHashResult, ShaContexts, ShaThreadBuilder};

const G2_EXT_DEGREE: usize = 2;

Expand Down Expand Up @@ -625,7 +625,7 @@ mod test {
use std::vec;
use std::{cell::RefCell, marker::PhantomData};

use crate::gadget::crypto::sha256::{SpreadChip, SpreadConfig};
use crate::gadget::crypto::sha256_flex::{SpreadChip, SpreadConfig};
use crate::gadget::crypto::ShaCircuitBuilder;
use crate::gadget::crypto::{Sha256Chip, ShaThreadBuilder};
use crate::util::{print_fq2_dev, Challenges, IntoWitness};
Expand Down
6 changes: 3 additions & 3 deletions lightclient-circuits/src/gadget/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod builder;
mod ecc;
mod hash2curve;
mod sha256;
mod sha256_flex;
mod sha256_wide;
mod util;
mod ecc;

pub use builder::{SHAConfig, ShaCircuitBuilder};
use eth_types::{AppCurveExt, Field, HashCurveExt};
Expand All @@ -17,7 +17,7 @@ use halo2_ecc::{
use halo2_proofs::plonk::Error;
pub use hash2curve::{HashToCurveCache, HashToCurveChip};
use lazy_static::lazy_static;
pub use sha256::{Sha256Chip, ShaContexts, ShaThreadBuilder};
pub use sha256_flex::{Sha256Chip, ShaContexts, ShaThreadBuilder};
pub use sha256_wide::{Sha256ChipWide, ShaBitThreadBuilder};

pub use ecc::calculate_ysquared;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sha2::digest::generic_array::GenericArray;
use std::collections::HashMap;
use std::{cell::RefCell, char::MAX};

use crate::gadget::crypto::sha256::compression::{sha256_compression, INIT_STATE};
use crate::gadget::crypto::sha256_flex::compression::{sha256_compression, INIT_STATE};
use crate::util::{AssignedValueCell, ThreadBuilderBase};
use crate::witness::HashInput;
use halo2_base::safe_types::RangeChip;
Expand All @@ -35,7 +35,7 @@ pub use self::builder::ShaContexts;
pub(super) use self::builder::{assign_threads_sha, FIRST_PHASE};
pub use self::spread::SpreadChip;

use super::{HashInstructions, AssignedHashResult};
use super::{AssignedHashResult, HashInstructions};

const SHA256_CONTEXT_ID: usize = usize::MAX;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use halo2_base::{
};
use halo2_proofs::{
circuit::{self, Region, Value},
plonk::{Advice, Column, Selector, Error},
plonk::{Advice, Column, Error, Selector},
};
use itertools::Itertools;

Expand Down Expand Up @@ -137,7 +137,6 @@ impl<F: Field> ThreadBuilderBase<F> for ShaThreadBuilder<F> {
region: &mut Region<F>,
break_points: &mut MultiPhaseThreadBreakPoints,
) -> Result<(), Error> {

let break_points_gate = mem::take(&mut break_points[FIRST_PHASE]);
// warning: we currently take all contexts from phase 0, which means you can't read the values
// from these contexts later in phase 1. If we want to read, should clone here
Expand All @@ -161,7 +160,6 @@ impl<F: Field> ThreadBuilderBase<F> for ShaThreadBuilder<F> {
}
}


impl<F: Field> ShaThreadBuilder<F> {
pub fn sha_contexts_pair(&mut self) -> (&mut Context<F>, ShaContexts<F>) {
if self.threads_dense.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::cell::RefMut;

use crate::util::ThreadBuilderBase;

use super::ShaThreadBuilder;
use super::builder::ShaContexts;
use super::spread::{self, SpreadChip, SpreadConfig};
use super::util::{bits_le_to_fe, fe_to_bits_le};
use super::ShaThreadBuilder;
use eth_types::Field;
use halo2_base::halo2_proofs::halo2curves::FieldExt;
use halo2_base::halo2_proofs::{
Expand Down Expand Up @@ -155,13 +155,7 @@ pub fn sha256_compression<'a, 'b: 'a, F: Field>(
// let f_spread = state_to_spread_u32(ctx, range, ctx_spread, &f)?;
// let g_spread = state_to_spread_u32(ctx, range, ctx_spread, &g)?;
let sigma_term = sigma_upper1(thread_pool, spread_chip, &e_spread)?;
let ch_term = ch(
thread_pool,
spread_chip,
&e_spread,
&f_spread,
&g_spread,
)?;
let ch_term = ch(thread_pool, spread_chip, &e_spread, &f_spread, &g_spread)?;
// println!(
// "idx {} sigma {:?} ch {:?}",
// idx,
Expand Down Expand Up @@ -191,13 +185,7 @@ pub fn sha256_compression<'a, 'b: 'a, F: Field>(
// let b_spread = state_to_spread_u32(ctx, range, ctx_spread, &b)?;
// let c_spread = state_to_spread_u32(ctx, range, ctx_spread, &c)?;
let sigma_term = sigma_upper0(thread_pool, spread_chip, &a_spread)?;
let maj_term = maj(
thread_pool,
spread_chip,
&a_spread,
&b_spread,
&c_spread,
)?;
let maj_term = maj(thread_pool, spread_chip, &a_spread, &b_spread, &c_spread)?;
let add = gate.add(
thread_pool.main(),
QuantumCell::Existing(sigma_term),
Expand Down Expand Up @@ -243,7 +231,11 @@ pub fn sha256_compression<'a, 'b: 'a, F: Field>(
.copied()
.zip(pre_state_words.iter().copied())
.map(|(x, y)| {
let add = gate.add(thread_pool.main(), QuantumCell::Existing(x), QuantumCell::Existing(y));
let add = gate.add(
thread_pool.main(),
QuantumCell::Existing(x),
QuantumCell::Existing(y),
);
// println!(
// "pre {:?} new {:?} add {:?}",
// y.value(),
Expand Down Expand Up @@ -338,10 +330,14 @@ fn ch<'a, 'b: 'a, F: Field>(
QuantumCell::Existing(x_neg_hi),
QuantumCell::Existing(z_hi),
);
let (p_lo_even, p_lo_odd) = spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &p_lo)?;
let (p_hi_even, p_hi_odd) = spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &p_hi)?;
let (q_lo_even, q_lo_odd) = spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &q_lo)?;
let (q_hi_even, q_hi_odd) = spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &q_hi)?;
let (p_lo_even, p_lo_odd) =
spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &p_lo)?;
let (p_hi_even, p_hi_odd) =
spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &p_hi)?;
let (q_lo_even, q_lo_odd) =
spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &q_lo)?;
let (q_hi_even, q_hi_odd) =
spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &q_hi)?;
{
let even_spread = spread_chip.spread(thread_pool, &p_lo_even)?;
let odd_spread = spread_chip.spread(thread_pool, &p_lo_odd)?;
Expand Down Expand Up @@ -431,8 +427,10 @@ fn maj<'a, 'b: 'a, F: Field>(
QuantumCell::Existing(y_hi),
QuantumCell::Existing(z_hi),
);
let (m_lo_even, m_lo_odd) = spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &m_lo)?;
let (m_hi_even, m_hi_odd) = spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &m_hi)?;
let (m_lo_even, m_lo_odd) =
spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &m_lo)?;
let (m_hi_even, m_hi_odd) =
spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &m_hi)?;
{
let even_spread = spread_chip.spread(thread_pool, &m_lo_even)?;
let odd_spread = spread_chip.spread(thread_pool, &m_lo_odd)?;
Expand Down Expand Up @@ -648,10 +646,30 @@ fn sigma_generic<'a, 'b: 'a, F: Field>(
// let assigned_b_spread = spread_config.spread(ctx, range, &assigned_b)?;
// let assigned_c_spread = spread_config.spread(ctx, range, &assigned_c)?;
// let assigned_d_spread = spread_config.spread(ctx, range, &assigned_d)?;
sum = gate.mul_add(thread_pool.main(), QuantumCell::Constant(coeffs[0]), assigned_a, sum);
sum = gate.mul_add(thread_pool.main(), QuantumCell::Constant(coeffs[1]), assigned_b, sum);
sum = gate.mul_add(thread_pool.main(), QuantumCell::Constant(coeffs[2]), assigned_c, sum);
sum = gate.mul_add(thread_pool.main(), QuantumCell::Constant(coeffs[3]), assigned_d, sum);
sum = gate.mul_add(
thread_pool.main(),
QuantumCell::Constant(coeffs[0]),
assigned_a,
sum,
);
sum = gate.mul_add(
thread_pool.main(),
QuantumCell::Constant(coeffs[1]),
assigned_b,
sum,
);
sum = gate.mul_add(
thread_pool.main(),
QuantumCell::Constant(coeffs[2]),
assigned_c,
sum,
);
sum = gate.mul_add(
thread_pool.main(),
QuantumCell::Constant(coeffs[3]),
assigned_d,
sum,
);
sum
};
let (r_lo, r_hi) = {
Expand All @@ -671,8 +689,10 @@ fn sigma_generic<'a, 'b: 'a, F: Field>(
(assigned_lo, assigned_hi)
};

let (r_lo_even, r_lo_odd) = spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &r_lo)?;
let (r_hi_even, r_hi_odd) = spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &r_hi)?;
let (r_lo_even, r_lo_odd) =
spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &r_lo)?;
let (r_hi_even, r_hi_odd) =
spread_chip.decompose_even_and_odd_unchecked(thread_pool.main(), &r_hi)?;

{
let even_spread = spread_chip.spread(thread_pool, &r_lo_even)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::marker::PhantomData;

use crate::util::{ThreadBuilderBase, ThreadBuilderConfigBase};

use super::ShaThreadBuilder;
use super::builder::ShaContexts;
use super::ShaThreadBuilder;
use super::{compression::SpreadU32, util::*};
use eth_types::Field;
use halo2_base::gates::builder::FlexGateConfigParams;
Expand Down Expand Up @@ -92,10 +92,9 @@ impl<F: Field> SpreadConfig<F> {
}
}


impl<F: Field> ThreadBuilderConfigBase<F> for SpreadConfig<F> {
fn configure(meta: &mut ConstraintSystem<F>, params: FlexGateConfigParams) -> Self {
Self::configure(meta, 8, 2) // TODO configure num_advice_columns
Self::configure(meta, 8, 2) // TODO configure num_advice_columns
}

fn load(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
Expand Down Expand Up @@ -223,7 +222,9 @@ impl<'a, F: Field> SpreadChip<'a, F> {

let assigned_spread = ctx_base.load_witness(spread_value);
let assigned_spread_vanila = ctx_spread.load_witness(*assigned_spread.value());
thread_pool.main().constrain_equal(&assigned_spread_vanila, &assigned_spread);
thread_pool
.main()
.constrain_equal(&assigned_spread_vanila, &assigned_spread);

Ok(assigned_spread)
}
Expand Down
13 changes: 4 additions & 9 deletions lightclient-circuits/src/gadget/crypto/sha256_wide/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use eth_types::Field;
use halo2_base::{
gates::{
builder::{
CircuitBuilderStage, FlexGateConfigParams, GateThreadBuilder, KeygenAssignments,
ThreadBreakPoints, assign_threads_in,
assign_threads_in, CircuitBuilderStage, FlexGateConfigParams, GateThreadBuilder,
KeygenAssignments, ThreadBreakPoints,
},
flex_gate::FlexGateConfig,
},
Expand Down Expand Up @@ -182,14 +182,9 @@ impl<F: Field> ThreadBuilderBase<F> for ShaBitThreadBuilder<F> {
break_points_gate,
);

self.sha_contexts().assign_in_region(
region,
config,
use_unknown,
None,
)
self.sha_contexts()
.assign_in_region(region, config, use_unknown, None)
}

}

impl<F: Field> ShaBitThreadBuilder<F> {
Expand Down
7 changes: 4 additions & 3 deletions lightclient-circuits/src/gadget/crypto/sha256_wide/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use eth_types::Field;
use halo2_base::{utils::{biguint_to_fe, fe_to_biguint}, AssignedValue};
use halo2_base::{
utils::{biguint_to_fe, fe_to_biguint},
AssignedValue,
};
use halo2_proofs::circuit::AssignedCell;
use itertools::Itertools;
use num_bigint::BigUint;
Expand Down Expand Up @@ -55,8 +58,6 @@ pub struct Sha256AssignedRows<F: Field> {
pub output_rlc: Vec<AssignedValue<F>>,
}



impl<F: Field> Sha256AssignedRows<F> {
pub fn new() -> Self {
Self {
Expand Down
2 changes: 1 addition & 1 deletion lightclient-circuits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub mod witness;
pub mod committee_update_circuit;
pub mod sync_step_circuit;

mod builder;
mod poseidon;
mod ssz_merkle;
mod builder;

pub use halo2_base::gates::builder::FlexGateConfigParams;
Loading
Loading