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

[WIP] sync-halo2-lib-0.4.0 #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ff = "0.13.0"
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
lazy_static = "1.4.0"
thiserror = "1.0"
Expand All @@ -15,7 +16,7 @@ rand_xorshift = "0.3.0"
rand = "0.8"

[patch."https://github.com/privacy-scaling-explorations/halo2.git"]
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "develop" }
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "sync-halo2-lib-0.4.0" }

[features]
default = ["halo2_proofs/parallel_syn", "short"]
Expand Down
63 changes: 31 additions & 32 deletions src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! The hash circuit base on poseidon.

use crate::poseidon::primitives::{ConstantLengthIden3, Domain, Hash, Spec, VariableLengthIden3};
use ff::{FromUniformBytes, PrimeField};
use halo2_proofs::circuit::AssignedCell;
use halo2_proofs::halo2curves::bn256::Fr;
use halo2_proofs::plonk::Fixed;
use halo2_proofs::{arithmetic::FieldExt, circuit::AssignedCell};
use log;
use std::time::Instant;

Expand Down Expand Up @@ -45,7 +46,7 @@ pub use chip_long::*;
pub use chip_short::*;

/// indicate an field can be hashed in merkle tree (2 Fields to 1 Field)
pub trait Hashable: Hashablebase {
pub trait Hashable: Hashablebase + FromUniformBytes<64> + Ord {
/// the spec type used in circuit for this hashable field
type SpecType: Spec<Self, 3, 2>;
/// the domain type used for hash calculation
Expand All @@ -54,7 +55,7 @@ pub trait Hashable: Hashablebase {
/// execute hash for any sequence of fields
#[deprecated]
fn hash(inp: [Self; 2]) -> Self {
Self::hash_with_domain(inp, Self::zero())
Self::hash_with_domain(inp, Self::ZERO)
}

/// execute hash for any sequence of fields, with domain being specified
Expand Down Expand Up @@ -126,7 +127,7 @@ use std::fmt::Debug as DebugT;

/// The config for poseidon hash circuit
#[derive(Clone, Debug)]
pub struct SpongeConfig<F: FieldExt, PC: Chip<F> + Clone + DebugT> {
pub struct SpongeConfig<F: PrimeField, PC: Chip<F> + Clone + DebugT> {
permute_config: PC::Config,
hash_table: [Column<Advice>; 6],
hash_table_aux: [Column<Advice>; 6],
Expand Down Expand Up @@ -201,13 +202,11 @@ impl<F: Hashable, PC: PermuteChip<F, F::SpecType, 3, 2>> SpongeConfig<F, PC> {
vec![
q_enable.clone()
* s_continue.clone()
* (Expression::Constant(F::one()) - s_continue.clone()),
q_enable.clone() * ctrl * (Expression::Constant(F::one()) - ctrl_bool.clone()),
q_enable.clone()
* s_continue.clone()
* (Expression::Constant(F::one()) - ctrl_bool),
* (Expression::Constant(F::ONE) - s_continue.clone()),
q_enable.clone() * ctrl * (Expression::Constant(F::ONE) - ctrl_bool.clone()),
q_enable.clone() * s_continue.clone() * (Expression::Constant(F::ONE) - ctrl_bool),
q_enable
* (Expression::Constant(F::one())
* (Expression::Constant(F::ONE)
- s_continue
- meta.query_advice(header_mark, Rotation::cur())),
]
Expand All @@ -230,7 +229,7 @@ impl<F: Hashable, PC: PermuteChip<F, F::SpecType, 3, 2>> SpongeConfig<F, PC> {
* (ctrl
+ Expression::Constant(F::from_u128(step as u128 * HASHABLE_DOMAIN_SPEC))
- ctrl_prev),
q_enable * (Expression::Constant(F::one()) - ctrl_bool),
q_enable * (Expression::Constant(F::ONE) - ctrl_bool),
]
});

Expand All @@ -251,7 +250,7 @@ impl<F: Hashable, PC: PermuteChip<F, F::SpecType, 3, 2>> SpongeConfig<F, PC> {
vec![
q_enable.clone() * s_continue_hash.clone() * (hash_ind - hash_prev.clone()),
q_enable
* (Expression::Constant(F::one()) - s_continue_hash)
* (Expression::Constant(F::ONE) - s_continue_hash)
* (hash_out - hash_prev),
]
});
Expand Down Expand Up @@ -284,7 +283,7 @@ impl<F: Hashable, PC: PermuteChip<F, F::SpecType, 3, 2>> SpongeConfig<F, PC> {
// hash output: must inherit prev state or apply current control flag (for new hash)
ret.push(
q_enable.clone()
* (Expression::Constant(F::one()) - s_continue_hash.clone())
* (Expression::Constant(F::ONE) - s_continue_hash.clone())
* (inp_hash.clone() - inp_hash_init),
);
ret.push(q_enable * s_continue_hash * (inp_hash - inp_hash_prev - doman_spec));
Expand Down Expand Up @@ -320,7 +319,7 @@ pub struct PoseidonHashTable<F> {
pub checks: Vec<Option<F>>,
}

impl<F: FieldExt> PoseidonHashTable<F> {
impl<F: PrimeField> PoseidonHashTable<F> {
/// Add common inputs
#[deprecated]
pub fn constant_inputs<'d>(&mut self, src: impl IntoIterator<Item = &'d [F; 2]>) {
Expand Down Expand Up @@ -412,7 +411,7 @@ impl<F: Hashable> PoseidonHashTable<F> {

/// Represent the chip for Poseidon hash table
#[derive(Debug)]
pub struct SpongeChip<'d, F: FieldExt, const STEP: usize, PC: Chip<F> + Clone + DebugT>
pub struct SpongeChip<'d, F: PrimeField, const STEP: usize, PC: Chip<F> + Clone + DebugT>
where
PC::Config: Sync,
{
Expand Down Expand Up @@ -457,7 +456,7 @@ where
),
] {
for col in cols {
region.assign_advice(|| tip, *col, 0, || Value::known(F::zero()))?;
region.assign_advice(|| tip, *col, 0, || Value::known(F::ZERO))?;
}
}

Expand Down Expand Up @@ -506,7 +505,7 @@ where

let mut is_new_sponge = true;
let mut process_start = 0;
let mut state: [F; 3] = [F::zero(); 3];
let mut state: [F; 3] = [F::ZERO; 3];
let mut last_offset = 0;

for (i, ((inp, control), (domain, check))) in inputs_i
Expand All @@ -515,7 +514,7 @@ where
.enumerate()
{
let control = control.copied().unwrap_or(0);
let domain = domain.copied().unwrap_or_else(F::zero);
let domain = domain.copied().unwrap_or(F::ZERO);
let offset = i + begin_offset;
last_offset = offset;

Expand All @@ -528,7 +527,7 @@ where

let inp = inp
.map(|[a, b]| [*a, *b])
.unwrap_or_else(|| [F::zero(), F::zero()]);
.unwrap_or_else(|| [F::ZERO, F::ZERO]);

state.iter_mut().skip(1).zip(inp).for_each(|(s, inp)| {
if is_new_sponge {
Expand Down Expand Up @@ -556,7 +555,7 @@ where
|| "assign q_enable",
self.config.q_enable,
offset,
|| Value::known(F::one()),
|| Value::known(F::ONE),
)?;

let c_start = [0; 3]
Expand Down Expand Up @@ -593,17 +592,17 @@ where
(
"state beginning flag",
config.hash_table[5],
if is_new_sponge { F::one() } else { F::zero() },
if is_new_sponge { F::ONE } else { F::ZERO },
),
(
"state input control_aux",
config.control_aux,
control_as_flag.invert().unwrap_or_else(F::zero),
control_as_flag.invert().unwrap_or(F::ZERO),
),
(
"state continue control",
config.s_sponge_continue,
if is_new_sponge { F::zero() } else { F::one() },
if is_new_sponge { F::ZERO } else { F::ONE },
),
] {
region.assign_advice(
Expand Down Expand Up @@ -662,7 +661,7 @@ where
// any advice that we access in this region can be used
config.hash_table_aux[0],
data.len() - 1,
|| Value::known(F::zero()),
|| Value::known(F::ZERO),
)?;
*is_first_pass = false;
return Ok((states_in, states_out));
Expand All @@ -672,11 +671,11 @@ where

let mut is_new_sponge = true;
let mut process_start = 0;
let mut state = [F::zero(); 3];
let mut state = [F::ZERO; 3];

for (i, ((inp, control), (domain, check))) in data.iter().enumerate() {
let control = control.copied().unwrap_or(0u64);
let domain = domain.copied().unwrap_or_else(F::zero);
let domain = domain.copied().unwrap_or(F::ZERO);
let offset = i;

let control_as_flag = F::from_u128(control as u128 * HASHABLE_DOMAIN_SPEC);
Expand All @@ -688,7 +687,7 @@ where

let inp = inp
.map(|[a, b]| [*a, *b])
.unwrap_or_else(|| [F::zero(), F::zero()]);
.unwrap_or_else(|| [F::ZERO, F::ZERO]);

state.iter_mut().skip(1).zip(inp).for_each(|(s, inp)| {
if is_new_sponge {
Expand Down Expand Up @@ -716,7 +715,7 @@ where
|| "assign q_enable",
self.config.q_enable,
offset,
|| Value::known(F::one()),
|| Value::known(F::ONE),
)?;

let c_start = [0; 3]
Expand Down Expand Up @@ -753,17 +752,17 @@ where
(
"state beginning flag",
config.hash_table[5],
if is_new_sponge { F::one() } else { F::zero() },
if is_new_sponge { F::ONE } else { F::ZERO },
),
(
"state input control_aux",
config.control_aux,
control_as_flag.invert().unwrap_or_else(F::zero),
control_as_flag.invert().unwrap_or(F::ZERO),
),
(
"state continue control",
config.s_sponge_continue,
if is_new_sponge { F::zero() } else { F::one() },
if is_new_sponge { F::ZERO } else { F::ONE },
),
] {
region.assign_advice(
Expand Down Expand Up @@ -978,7 +977,7 @@ where
}
}

impl<F: FieldExt, const STEP: usize, PC: Chip<F> + Clone + DebugT> Chip<F>
impl<F: PrimeField, const STEP: usize, PC: Chip<F> + Clone + DebugT> Chip<F>
for SpongeChip<'_, F, STEP, PC>
where
PC::Config: Sync,
Expand Down
38 changes: 23 additions & 15 deletions src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::convert::TryInto;
use std::fmt;
use std::marker::PhantomData;

use ff::{Field, FromUniformBytes};
use halo2_proofs::{
arithmetic::{Field, FieldExt},
circuit::{AssignedCell, Chip, Layouter},
plonk::{ConstraintSystem, Error},
};
Expand All @@ -30,8 +30,12 @@ pub enum PaddedWord<F: Field> {
}

/// This trait is the interface to chips that implement a permutation.
pub trait PermuteChip<F: FieldExt, S: Spec<F, T, RATE>, const T: usize, const RATE: usize>:
Chip<F> + Clone + DebugT + PoseidonInstructions<F, S, T, RATE>
pub trait PermuteChip<
F: FromUniformBytes<64> + Ord,
S: Spec<F, T, RATE>,
const T: usize,
const RATE: usize,
>: Chip<F> + Clone + DebugT + PoseidonInstructions<F, S, T, RATE>
{
/// Configure the permutation chip.
fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config;
Expand All @@ -41,8 +45,12 @@ pub trait PermuteChip<F: FieldExt, S: Spec<F, T, RATE>, const T: usize, const RA
}

/// The set of circuit instructions required to use the Poseidon permutation.
pub trait PoseidonInstructions<F: FieldExt, S: Spec<F, T, RATE>, const T: usize, const RATE: usize>:
Chip<F>
pub trait PoseidonInstructions<
F: FromUniformBytes<64> + Ord,
S: Spec<F, T, RATE>,
const T: usize,
const RATE: usize,
>: Chip<F>
{
/// Variable representing the word over which the Poseidon permutation operates.
type Word: Clone
Expand Down Expand Up @@ -76,7 +84,7 @@ pub trait PoseidonInstructions<F: FieldExt, S: Spec<F, T, RATE>, const T: usize,
///
/// [`Hash`]: self::Hash
pub trait PoseidonSpongeInstructions<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
const T: usize,
Expand All @@ -102,7 +110,7 @@ pub trait PoseidonSpongeInstructions<
/// A word over which the Poseidon permutation operates.
#[derive(Debug)]
pub struct Word<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonInstructions<F, S, T, RATE>,
S: Spec<F, T, RATE>,
const T: usize,
Expand All @@ -112,7 +120,7 @@ pub struct Word<
}

impl<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonInstructions<F, S, T, RATE>,
S: Spec<F, T, RATE>,
const T: usize,
Expand All @@ -131,7 +139,7 @@ impl<
}

fn poseidon_sponge<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand All @@ -153,7 +161,7 @@ fn poseidon_sponge<
/// A Poseidon sponge.
#[derive(Debug)]
pub struct Sponge<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
M: SpongeMode,
Expand All @@ -168,7 +176,7 @@ pub struct Sponge<
}

impl<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand Down Expand Up @@ -241,7 +249,7 @@ impl<
}

impl<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand Down Expand Up @@ -272,7 +280,7 @@ impl<
/// A Poseidon hash function, built around a sponge.
#[derive(Debug)]
pub struct Hash<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand All @@ -283,7 +291,7 @@ pub struct Hash<
}

impl<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand All @@ -298,7 +306,7 @@ impl<
}

impl<
F: FieldExt,
F: FromUniformBytes<64> + Ord,
PoseidonChip: PoseidonSpongeInstructions<F, S, ConstantLength<L>, T, RATE>,
S: Spec<F, T, RATE>,
const T: usize,
Expand Down
Loading