Skip to content
Closed
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ thiserror = "1.0"
bitvec = "1"

[patch."https://github.com/privacy-scaling-explorations/halo2.git"]
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "scroll-dev-0220" }
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "halo2-ecc-snark-verifier-0220" }

[features]
# Use an implementation using fewer rows (8) per permutation.
short = []
# printout the layout of circuits for demo and some unittests
print_layout = ["halo2_proofs/dev-graph"]
# print_layout = ["halo2_proofs/dev-graph"]

[dev-dependencies]
rand = "0.8"
Expand Down
56 changes: 28 additions & 28 deletions src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! The hash circuit base on poseidon.

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

mod chip_long {
use super::{SpongeChip, SpongeConfig};
Expand Down Expand Up @@ -42,7 +43,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 Down Expand Up @@ -103,7 +104,7 @@ use std::fmt::Debug as DebugT;

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

Expand All @@ -230,7 +229,7 @@ impl<Fp: Hashable, PC: PermuteChip<Fp, Fp::SpecType, 3, 2>> SpongeConfig<Fp, PC>
vec![
s_enable.clone() * s_continue_hash.clone() * (hash_ind - hash_prev.clone()),
s_enable
* (Expression::Constant(Fp::one()) - s_continue_hash)
* (Expression::Constant(Fp::ONE) - s_continue_hash)
* (hash_out - hash_prev),
]
});
Expand Down Expand Up @@ -262,7 +261,7 @@ impl<Fp: Hashable, PC: PermuteChip<Fp, Fp::SpecType, 3, 2>> SpongeConfig<Fp, PC>
// hash output: must inherit prev state or apply current control flag (for new hash)
ret.push(
s_enable.clone()
* (Expression::Constant(Fp::one()) - s_continue_hash.clone())
* (Expression::Constant(Fp::ONE) - s_continue_hash.clone())
* (inp_hash.clone() - inp_hash_init),
);
ret.push(s_enable * s_continue_hash * (inp_hash - inp_hash_prev));
Expand Down Expand Up @@ -295,7 +294,7 @@ pub struct PoseidonHashTable<Fp> {
pub nil_msg_hash: Option<Fp>,
}

impl<Fp: FieldExt> PoseidonHashTable<Fp> {
impl<Fp: PrimeField> PoseidonHashTable<Fp> {
/// Add common inputs
pub fn constant_inputs<'d>(&mut self, src: impl IntoIterator<Item = &'d [Fp; 2]>) {
let mut new_inps: Vec<_> = src.into_iter().copied().collect();
Expand All @@ -313,7 +312,7 @@ impl<Fp: FieldExt> PoseidonHashTable<Fp> {
for (a, b, c) in src {
self.inputs.push([*a, *b]);
self.checks.push(Some(*c));
self.controls.push(Fp::zero());
self.controls.push(Fp::ZERO);
}
}

Expand Down Expand Up @@ -359,7 +358,7 @@ impl<Fp: Hashable> PoseidonHashTable<Fp> {

/// Represent the chip for Poseidon hash table
#[derive(Debug)]
pub struct SpongeChip<'d, Fp: FieldExt, const STEP: usize, PC: Chip<Fp> + Clone + DebugT> {
pub struct SpongeChip<'d, Fp: PrimeField, const STEP: usize, PC: Chip<Fp> + Clone + DebugT> {
calcs: usize,
nil_msg_hash: Option<Fp>,
mpt_only: bool,
Expand Down Expand Up @@ -405,7 +404,7 @@ impl<'d, Fp: Hashable, const STEP: usize, PC: PermuteChip<Fp, Fp::SpecType, 3, 2
),
] {
for col in cols {
region.assign_advice(|| tip, *col, 0, || Value::known(Fp::zero()))?;
region.assign_advice(|| tip, *col, 0, || Value::known(Fp::ZERO))?;
}
}

Expand All @@ -425,7 +424,7 @@ impl<'d, Fp: Hashable, const STEP: usize, PC: PermuteChip<Fp, Fp::SpecType, 3, 2
),
] {
for col in cols {
region.assign_advice(|| tip, *col, 1, || Value::known(Fp::zero()))?;
region.assign_advice(|| tip, *col, 1, || Value::known(Fp::ZERO))?;
}
}

Expand All @@ -446,7 +445,7 @@ impl<'d, Fp: Hashable, const STEP: usize, PC: PermuteChip<Fp, Fp::SpecType, 3, 2
|| "custom mark",
config.hash_table[4],
1,
|| Value::known(Fp::one()),
|| Value::known(Fp::ONE),
)?;

Ok(2)
Expand Down Expand Up @@ -486,11 +485,11 @@ impl<'d, Fp: Hashable, const STEP: usize, PC: PermuteChip<Fp, Fp::SpecType, 3, 2

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

for (i, ((inp, control), check)) in inputs_i.zip(controls_i).zip(checks_i).enumerate() {
let control = control.copied().unwrap_or_else(Fp::zero);
let control = control.copied().unwrap_or(Fp::ZERO);
let offset = i + begin_offset;
last_offset = offset;

Expand All @@ -501,7 +500,7 @@ impl<'d, Fp: Hashable, const STEP: usize, PC: PermuteChip<Fp, Fp::SpecType, 3, 2

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

state.iter_mut().skip(1).zip(inp).for_each(|(s, inp)| {
if is_new_sponge {
Expand Down Expand Up @@ -560,17 +559,17 @@ impl<'d, Fp: Hashable, const STEP: usize, PC: PermuteChip<Fp, Fp::SpecType, 3, 2
(
"state beginning flag",
config.hash_table[4],
if is_new_sponge { Fp::one() } else { Fp::zero() },
if is_new_sponge { Fp::ONE } else { Fp::ZERO },
),
(
"state input control_aux",
config.control_aux,
control.invert().unwrap_or_else(Fp::zero),
control.invert().unwrap_or(Fp::ZERO),
),
(
"state continue control",
config.s_sponge_continue,
if is_new_sponge { Fp::zero() } else { Fp::one() },
if is_new_sponge { Fp::ZERO } else { Fp::ONE },
),
] {
region.assign_advice(
Expand Down Expand Up @@ -666,7 +665,7 @@ impl<'d, Fp: Hashable, const STEP: usize, PC: PermuteChip<Fp, Fp::SpecType, 3, 2
}
}

impl<Fp: FieldExt, const STEP: usize, PC: Chip<Fp> + Clone + DebugT> Chip<Fp>
impl<Fp: PrimeField, const STEP: usize, PC: Chip<Fp> + Clone + DebugT> Chip<Fp>
for SpongeChip<'_, Fp, STEP, PC>
{
type Config = SpongeConfig<Fp, PC>;
Expand All @@ -687,6 +686,7 @@ mod tests {
use crate::poseidon::{Pow5Chip, SeptidonChip};

use super::*;
use halo2_proofs::ff::Field;
use halo2_proofs::halo2curves::group::ff::PrimeField;
use halo2_proofs::{circuit::SimpleFloorPlanner, plonk::Circuit};

Expand Down Expand Up @@ -722,7 +722,7 @@ mod tests {
Fr::from_str_vartime("1").unwrap(),
Fr::from_str_vartime("2").unwrap(),
Fr::from_str_vartime("3").unwrap(),
Fr::zero(),
Fr::ZERO,
];

let supposed_bytes = 50u64;
Expand Down Expand Up @@ -852,7 +852,7 @@ mod tests {
Fr::from_str_vartime("2").unwrap(),
];

let message2 = [Fr::from_str_vartime("50331648").unwrap(), Fr::zero()];
let message2 = [Fr::from_str_vartime("50331648").unwrap(), Fr::ZERO];

let k = 8;
let circuit = TestCircuit::<PC>::new( PoseidonHashTable {
Expand All @@ -866,7 +866,7 @@ mod tests {

let circuit = TestCircuit::<PC>::new(PoseidonHashTable {
inputs: vec![message1, message2, message1],
controls: vec![Fr::from_u128(64), Fr::from_u128(32), Fr::zero()],
controls: vec![Fr::from_u128(64), Fr::from_u128(32), Fr::ZERO],
checks: Vec::new(),
..Default::default()
});
Expand Down
38 changes: 23 additions & 15 deletions src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::fmt;
use std::marker::PhantomData;

use halo2_proofs::{
arithmetic::{Field, FieldExt},
circuit::{AssignedCell, Chip, Layouter},
ff::{Field, FromUniformBytes},
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 + fmt::Debug + From<AssignedCell<F, F>> + Into<AssignedCell<F, F>>;
Expand All @@ -59,7 +67,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 @@ -85,7 +93,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 @@ -95,7 +103,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 @@ -114,7 +122,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 @@ -136,7 +144,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 @@ -151,7 +159,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 @@ -224,7 +232,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 @@ -255,7 +263,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 @@ -266,7 +274,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 @@ -281,7 +289,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