Generalise Pow5T3 chip to be generic over WIDTH, RATE.

This commit is contained in:
therealyingtong 2021-11-02 15:52:11 +01:00
parent 0417e233c3
commit fe1bc97ab4
5 changed files with 562 additions and 588 deletions

View File

@ -40,7 +40,7 @@ use gadget::{
chip::{EccChip, EccConfig},
FixedPoint, FixedPointBaseField, FixedPointShort, NonIdentityPoint, Point,
},
poseidon::{Hash as PoseidonHash, Pow5T3Chip as PoseidonChip, Pow5T3Config as PoseidonConfig},
poseidon::{Hash as PoseidonHash, Pow5Chip as PoseidonChip, Pow5Config as PoseidonConfig},
sinsemilla::{
chip::{SinsemillaChip, SinsemillaConfig, SinsemillaHashDomains},
commit_ivk::CommitIvkConfig,
@ -82,7 +82,7 @@ pub struct Config {
q_add: Selector,
advices: [Column<Advice>; 10],
ecc_config: EccConfig,
poseidon_config: PoseidonConfig<pallas::Base>,
poseidon_config: PoseidonConfig<pallas::Base, 3, 2>,
merkle_config_1: MerkleConfig,
merkle_config_2: MerkleConfig,
sinsemilla_config_1: SinsemillaConfig,

View File

@ -1,7 +1,7 @@
use pasta_curves::pallas;
use ecc::chip::EccChip;
use poseidon::Pow5T3Chip as PoseidonChip;
use poseidon::Pow5Chip as PoseidonChip;
use sinsemilla::{chip::SinsemillaChip, merkle::chip::MerkleChip};
pub(crate) mod ecc;
@ -30,7 +30,7 @@ impl super::Config {
MerkleChip::construct(self.merkle_config_2.clone())
}
pub(super) fn poseidon_chip(&self) -> PoseidonChip<pallas::Base> {
pub(super) fn poseidon_chip(&self) -> PoseidonChip<pallas::Base, 3, 2> {
PoseidonChip::construct(self.poseidon_config.clone())
}
}

View File

@ -9,8 +9,8 @@ use halo2::{
plonk::Error,
};
mod pow5t3;
pub use pow5t3::{Pow5T3Chip, Pow5T3Config, StateWord};
mod pow5;
pub use pow5::{Pow5Chip, Pow5Config, StateWord};
use crate::circuit::gadget::utilities::CellValue;
use crate::primitives::poseidon::{ConstantLength, Domain, Spec, Sponge, SpongeState, State};

View File

@ -1,3 +1,4 @@
use std::convert::TryInto;
use std::iter;
use halo2::{
@ -11,11 +12,9 @@ use super::{PoseidonDuplexInstructions, PoseidonInstructions};
use crate::circuit::gadget::utilities::{CellValue, Var};
use crate::primitives::poseidon::{Domain, Mds, Spec, SpongeState, State};
const WIDTH: usize = 3;
/// Configuration for an [`Pow5T3Chip`].
/// Configuration for a [`Pow5Chip`].
#[derive(Clone, Debug)]
pub struct Pow5T3Config<F: FieldExt> {
pub struct Pow5Config<F: FieldExt, const WIDTH: usize, const RATE: usize> {
pub(in crate::circuit) state: [Column<Advice>; WIDTH],
partial_sbox: Column<Advice>,
rc_a: [Column<Fixed>; WIDTH],
@ -34,11 +33,11 @@ pub struct Pow5T3Config<F: FieldExt> {
/// A Poseidon chip using an $x^5$ S-Box, with a width of 3, suitable for a 2:1 reduction.
#[derive(Debug)]
pub struct Pow5T3Chip<F: FieldExt> {
config: Pow5T3Config<F>,
pub struct Pow5Chip<F: FieldExt, const WIDTH: usize, const RATE: usize> {
config: Pow5Config<F, WIDTH, RATE>,
}
impl<F: FieldExt> Pow5T3Chip<F> {
impl<F: FieldExt, const WIDTH: usize, const RATE: usize> Pow5Chip<F, WIDTH, RATE> {
/// Configures this chip for use in a circuit.
///
/// # Side-effects
@ -48,14 +47,15 @@ impl<F: FieldExt> Pow5T3Chip<F> {
// TODO: Does the rate need to be hard-coded here, or only the width? It probably
// needs to be known wherever we implement the hashing gadget, but it isn't strictly
// necessary for the permutation.
pub fn configure<S: Spec<F, WIDTH, 2>>(
pub fn configure<S: Spec<F, WIDTH, RATE>>(
meta: &mut ConstraintSystem<F>,
spec: S,
state: [Column<Advice>; WIDTH],
partial_sbox: Column<Advice>,
rc_a: [Column<Fixed>; WIDTH],
rc_b: [Column<Fixed>; WIDTH],
) -> Pow5T3Config<F> {
) -> Pow5Config<F, WIDTH, RATE> {
assert_eq!(RATE, WIDTH - 1);
// Generate constants for the Poseidon permutation.
// This gadget requires R_F and R_P to be even.
assert!(S::full_rounds() & 1 == 0);
@ -87,123 +87,96 @@ impl<F: FieldExt> Pow5T3Chip<F> {
};
meta.create_gate("full round", |meta| {
let cur_0 = meta.query_advice(state[0], Rotation::cur());
let cur_1 = meta.query_advice(state[1], Rotation::cur());
let cur_2 = meta.query_advice(state[2], Rotation::cur());
let next = [
meta.query_advice(state[0], Rotation::next()),
meta.query_advice(state[1], Rotation::next()),
meta.query_advice(state[2], Rotation::next()),
];
let rc_0 = meta.query_fixed(rc_a[0], Rotation::cur());
let rc_1 = meta.query_fixed(rc_a[1], Rotation::cur());
let rc_2 = meta.query_fixed(rc_a[2], Rotation::cur());
let s_full = meta.query_selector(s_full);
let full_round = |next_idx: usize| {
s_full.clone()
* (pow_5(cur_0.clone() + rc_0.clone()) * m_reg[next_idx][0]
+ pow_5(cur_1.clone() + rc_1.clone()) * m_reg[next_idx][1]
+ pow_5(cur_2.clone() + rc_2.clone()) * m_reg[next_idx][2]
- next[next_idx].clone())
};
vec![
("state[0]", full_round(0)),
("state[1]", full_round(1)),
("state[2]", full_round(2)),
]
(0..WIDTH)
.map(|next_idx| {
let state_next = meta.query_advice(state[next_idx], Rotation::next());
let expr = (0..WIDTH).fold(-state_next, |acc, idx| {
let state_cur = meta.query_advice(state[idx], Rotation::cur());
let rc_a = meta.query_fixed(rc_a[idx], Rotation::cur());
acc + pow_5(state_cur + rc_a) * m_reg[next_idx][idx]
});
s_full.clone() * expr
})
.collect::<Vec<_>>()
});
meta.create_gate("partial rounds", |meta| {
let cur_0 = meta.query_advice(state[0], Rotation::cur());
let cur_1 = meta.query_advice(state[1], Rotation::cur());
let cur_2 = meta.query_advice(state[2], Rotation::cur());
let mid_0 = meta.query_advice(partial_sbox, Rotation::cur());
let next_0 = meta.query_advice(state[0], Rotation::next());
let next_1 = meta.query_advice(state[1], Rotation::next());
let next_2 = meta.query_advice(state[2], Rotation::next());
let rc_a0 = meta.query_fixed(rc_a[0], Rotation::cur());
let rc_a1 = meta.query_fixed(rc_a[1], Rotation::cur());
let rc_a2 = meta.query_fixed(rc_a[2], Rotation::cur());
let rc_b0 = meta.query_fixed(rc_b[0], Rotation::cur());
let rc_b1 = meta.query_fixed(rc_b[1], Rotation::cur());
let rc_b2 = meta.query_fixed(rc_b[2], Rotation::cur());
let s_partial = meta.query_selector(s_partial);
let partial_round_linear = |idx: usize, rc_b: Expression<F>| {
s_partial.clone()
* (mid_0.clone() * m_reg[idx][0]
+ (cur_1.clone() + rc_a1.clone()) * m_reg[idx][1]
+ (cur_2.clone() + rc_a2.clone()) * m_reg[idx][2]
+ rc_b
- (next_0.clone() * m_inv[idx][0]
+ next_1.clone() * m_inv[idx][1]
+ next_2.clone() * m_inv[idx][2]))
use halo2::plonk::VirtualCells;
let mid = |idx: usize, meta: &mut VirtualCells<F>| {
let mid = mid_0.clone() * m_reg[idx][0];
(1..WIDTH).fold(mid, |acc, cur_idx| {
let cur = meta.query_advice(state[cur_idx], Rotation::cur());
let rc_a = meta.query_fixed(rc_a[cur_idx], Rotation::cur());
acc + (cur + rc_a) * m_reg[idx][cur_idx]
})
};
vec![
(
"state[0] round a",
let next = |idx: usize, meta: &mut VirtualCells<F>| {
let next_0 = meta.query_advice(state[0], Rotation::next());
let next_0 = next_0 * m_inv[idx][0];
(1..WIDTH).fold(next_0, |acc, next_idx| {
let next = meta.query_advice(state[next_idx], Rotation::next());
acc + next * m_inv[idx][next_idx]
})
};
let partial_round_linear = |idx: usize, meta: &mut VirtualCells<F>| {
let expr = {
let rc_b = meta.query_fixed(rc_b[idx], Rotation::cur());
mid(idx, meta) + rc_b - next(idx, meta)
};
s_partial.clone() * expr
};
std::iter::empty()
// state[0] round a
.chain(Some(
s_partial.clone() * (pow_5(cur_0 + rc_a0) - mid_0.clone()),
),
(
"state[0] round b",
s_partial.clone()
* (pow_5(
mid_0.clone() * m_reg[0][0]
+ (cur_1.clone() + rc_a1.clone()) * m_reg[0][1]
+ (cur_2.clone() + rc_a2.clone()) * m_reg[0][2]
+ rc_b0,
) - (next_0.clone() * m_inv[0][0]
+ next_1.clone() * m_inv[0][1]
+ next_2.clone() * m_inv[0][2])),
),
("state[1]", partial_round_linear(1, rc_b1)),
("state[2]", partial_round_linear(2, rc_b2)),
]
))
// state[0] round b
.chain(Some(
s_partial.clone() * (pow_5(mid(0, meta) + rc_b0) - next(0, meta)),
))
.chain((1..WIDTH).map(|idx| partial_round_linear(idx, meta)))
.collect::<Vec<_>>()
});
meta.create_gate("pad-and-add", |meta| {
let initial_state_0 = meta.query_advice(state[0], Rotation::prev());
let initial_state_1 = meta.query_advice(state[1], Rotation::prev());
let initial_state_2 = meta.query_advice(state[2], Rotation::prev());
let input_0 = meta.query_advice(state[0], Rotation::cur());
let input_1 = meta.query_advice(state[1], Rotation::cur());
let output_state_0 = meta.query_advice(state[0], Rotation::next());
let output_state_1 = meta.query_advice(state[1], Rotation::next());
let output_state_2 = meta.query_advice(state[2], Rotation::next());
let initial_state_rate = meta.query_advice(state[RATE], Rotation::prev());
let output_state_rate = meta.query_advice(state[RATE], Rotation::next());
let s_pad_and_add = meta.query_selector(s_pad_and_add);
let pad_and_add = |initial_state, input, output_state| {
let pad_and_add = |idx: usize| {
let initial_state = meta.query_advice(state[idx], Rotation::prev());
let input = meta.query_advice(state[idx], Rotation::cur());
let output_state = meta.query_advice(state[idx], Rotation::next());
// We pad the input by storing the required padding in fixed columns and
// then constraining the corresponding input columns to be equal to it.
s_pad_and_add.clone() * (initial_state + input - output_state)
};
vec![
(
"state[0]",
pad_and_add(initial_state_0, input_0, output_state_0),
),
(
"state[1]",
pad_and_add(initial_state_1, input_1, output_state_1),
),
(0..RATE)
.map(pad_and_add)
// The capacity element is never altered by the input.
(
"state[2]",
s_pad_and_add * (initial_state_2 - output_state_2),
),
]
.chain(Some(
s_pad_and_add.clone() * (initial_state_rate - output_state_rate),
))
.collect::<Vec<_>>()
});
Pow5T3Config {
Pow5Config {
state,
partial_sbox,
rc_a,
@ -220,13 +193,13 @@ impl<F: FieldExt> Pow5T3Chip<F> {
}
}
pub fn construct(config: Pow5T3Config<F>) -> Self {
Pow5T3Chip { config }
pub fn construct(config: Pow5Config<F, WIDTH, RATE>) -> Self {
Pow5Chip { config }
}
}
impl<F: FieldExt> Chip<F> for Pow5T3Chip<F> {
type Config = Pow5T3Config<F>;
impl<F: FieldExt, const WIDTH: usize, const RATE: usize> Chip<F> for Pow5Chip<F, WIDTH, RATE> {
type Config = Pow5Config<F, WIDTH, RATE>;
type Loaded = ();
fn config(&self) -> &Self::Config {
@ -238,7 +211,9 @@ impl<F: FieldExt> Chip<F> for Pow5T3Chip<F> {
}
}
impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonInstructions<F, S, WIDTH, 2> for Pow5T3Chip<F> {
impl<F: FieldExt, S: Spec<F, WIDTH, RATE>, const WIDTH: usize, const RATE: usize>
PoseidonInstructions<F, S, WIDTH, RATE> for Pow5Chip<F, WIDTH, RATE>
{
type Word = StateWord<F>;
fn permute(
@ -252,7 +227,7 @@ impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonInstructions<F, S, WIDTH, 2> for
|| "permute state",
|mut region| {
// Load the initial state into this region.
let state = Pow5T3State::load(&mut region, config, initial_state)?;
let state = Pow5State::load(&mut region, config, initial_state)?;
let state = (0..config.half_full_rounds).fold(Ok(state), |res, r| {
res.and_then(|state| state.full_round(&mut region, config, r, r))
@ -286,46 +261,52 @@ impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonInstructions<F, S, WIDTH, 2> for
}
}
impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonDuplexInstructions<F, S, WIDTH, 2>
for Pow5T3Chip<F>
impl<F: FieldExt, S: Spec<F, WIDTH, RATE>, const WIDTH: usize, const RATE: usize>
PoseidonDuplexInstructions<F, S, WIDTH, RATE> for Pow5Chip<F, WIDTH, RATE>
{
fn initial_state(
&self,
layouter: &mut impl Layouter<F>,
domain: &impl Domain<F, WIDTH, 2>,
domain: &impl Domain<F, WIDTH, RATE>,
) -> Result<State<Self::Word, WIDTH>, Error> {
let config = self.config();
layouter.assign_region(
let state = layouter.assign_region(
|| format!("initial state for domain {:?}", domain),
|mut region| {
let mut load_state_word = |i: usize, value: F| {
let mut state = Vec::with_capacity(WIDTH);
let mut load_state_word = |i: usize, value: F| -> Result<_, Error> {
let var = region.assign_advice_from_constant(
|| format!("state_{}", i),
config.state[i],
0,
value,
)?;
Ok(StateWord {
state.push(StateWord {
var,
value: Some(value),
})
});
Ok(())
};
Ok([
load_state_word(0, F::zero())?,
load_state_word(1, F::zero())?,
load_state_word(2, domain.initial_capacity_element())?,
])
for i in 0..RATE {
load_state_word(i, F::zero())?;
}
load_state_word(RATE, domain.initial_capacity_element())?;
Ok(state)
},
)
)?;
Ok(state.try_into().unwrap())
}
fn pad_and_add(
&self,
layouter: &mut impl Layouter<F>,
domain: &impl Domain<F, WIDTH, 2>,
domain: &impl Domain<F, WIDTH, RATE>,
initial_state: &State<Self::Word, WIDTH>,
input: &SpongeState<Self::Word, 2>,
input: &SpongeState<Self::Word, RATE>,
) -> Result<State<Self::Word, WIDTH>, Error> {
let config = self.config();
layouter.assign_region(
@ -334,7 +315,7 @@ impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonDuplexInstructions<F, S, WIDTH,
config.s_pad_and_add.enable(&mut region, 1)?;
// Load the initial state into this region.
let mut load_state_word = |i: usize| {
let load_state_word = |i: usize| {
let value = initial_state[i].value;
let var = region.assign_advice(
|| format!("load state_{}", i),
@ -345,16 +326,14 @@ impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonDuplexInstructions<F, S, WIDTH,
region.constrain_equal(initial_state[i].var, var)?;
Ok(StateWord { var, value })
};
let initial_state = [
load_state_word(0)?,
load_state_word(1)?,
load_state_word(2)?,
];
let initial_state: Result<Vec<_>, Error> =
(0..WIDTH).map(load_state_word).collect();
let initial_state = initial_state?;
let padding_values = domain.padding();
// Load the input and padding into this region.
let mut load_input_word = |i: usize| {
let load_input_word = |i: usize| {
let (constraint_var, value) = match (input[i], padding_values[i]) {
(Some(word), None) => (word.var, word.value),
(None, Some(padding_value)) => {
@ -378,10 +357,11 @@ impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonDuplexInstructions<F, S, WIDTH,
Ok(StateWord { var, value })
};
let input = [load_input_word(0)?, load_input_word(1)?];
let input: Result<Vec<_>, Error> = (0..RATE).map(load_input_word).collect();
let input = input?;
// Constrain the output.
let mut constrain_output_word = |i: usize| {
let constrain_output_word = |i: usize| {
let value = initial_state[i].value.and_then(|initial_word| {
input
.get(i)
@ -399,17 +379,19 @@ impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonDuplexInstructions<F, S, WIDTH,
Ok(StateWord { var, value })
};
Ok([
constrain_output_word(0)?,
constrain_output_word(1)?,
constrain_output_word(2)?,
])
let output: Result<Vec<_>, Error> = (0..WIDTH).map(constrain_output_word).collect();
output.map(|output| output.try_into().unwrap())
},
)
}
fn get_output(state: &State<Self::Word, WIDTH>) -> SpongeState<Self::Word, 2> {
[Some(state[0]), Some(state[1])]
fn get_output(state: &State<Self::Word, WIDTH>) -> SpongeState<Self::Word, RATE> {
state[..RATE]
.iter()
.map(|word| Some(*word))
.collect::<Vec<_>>()
.try_into()
.unwrap()
}
}
@ -438,83 +420,73 @@ impl<F: FieldExt> From<CellValue<F>> for StateWord<F> {
}
#[derive(Debug)]
struct Pow5T3State<F: FieldExt>([StateWord<F>; WIDTH]);
struct Pow5State<F: FieldExt, const WIDTH: usize>([StateWord<F>; WIDTH]);
impl<F: FieldExt> Pow5T3State<F> {
fn full_round(
impl<F: FieldExt, const WIDTH: usize> Pow5State<F, WIDTH> {
fn full_round<const RATE: usize>(
self,
region: &mut Region<F>,
config: &Pow5T3Config<F>,
config: &Pow5Config<F, WIDTH, RATE>,
round: usize,
offset: usize,
) -> Result<Self, Error> {
Self::round(region, config, round, offset, config.s_full, |_| {
let q_0 = self.0[0]
.value
.map(|v| v + config.round_constants[round][0]);
let q_1 = self.0[1]
.value
.map(|v| v + config.round_constants[round][1]);
let q_2 = self.0[2]
.value
.map(|v| v + config.round_constants[round][2]);
let r_0 = q_0.map(|v| v.pow(&config.alpha));
let r_1 = q_1.map(|v| v.pow(&config.alpha));
let r_2 = q_2.map(|v| v.pow(&config.alpha));
let q = self
.0
.iter()
.enumerate()
.map(|(idx, word)| word.value.map(|v| v + config.round_constants[round][idx]));
let r: Option<Vec<F>> = q.map(|q| q.map(|q| q.pow(&config.alpha))).collect();
let m = &config.m_reg;
let r = r_0.and_then(|r_0| r_1.and_then(|r_1| r_2.map(|r_2| [r_0, r_1, r_2])));
let state = m.iter().map(|m_i| {
r.as_ref().map(|r| {
r.iter()
.enumerate()
.fold(F::zero(), |acc, (j, r_j)| acc + m_i[j] * r_j)
})
});
Ok((
round + 1,
[
r.map(|r| m[0][0] * r[0] + m[0][1] * r[1] + m[0][2] * r[2]),
r.map(|r| m[1][0] * r[0] + m[1][1] * r[1] + m[1][2] * r[2]),
r.map(|r| m[2][0] * r[0] + m[2][1] * r[1] + m[2][2] * r[2]),
],
))
Ok((round + 1, state.collect::<Vec<_>>().try_into().unwrap()))
})
}
fn partial_round(
fn partial_round<const RATE: usize>(
self,
region: &mut Region<F>,
config: &Pow5T3Config<F>,
config: &Pow5Config<F, WIDTH, RATE>,
round: usize,
offset: usize,
) -> Result<Self, Error> {
Self::round(region, config, round, offset, config.s_partial, |region| {
let m = &config.m_reg;
let p: Option<Vec<_>> = self.0.iter().map(|word| word.value).collect();
let p = self.0[0].value.and_then(|p_0| {
self.0[1]
.value
.and_then(|p_1| self.0[2].value.map(|p_2| [p_0, p_1, p_2]))
});
let r = p.map(|p| {
[
(p[0] + config.round_constants[round][0]).pow(&config.alpha),
p[1] + config.round_constants[round][1],
p[2] + config.round_constants[round][2],
]
let r: Option<Vec<_>> = p.map(|p| {
let r_0 = (p[0] + config.round_constants[round][0]).pow(&config.alpha);
let r_i = p[1..]
.iter()
.enumerate()
.map(|(i, p_i)| *p_i + config.round_constants[round][i + 1]);
std::iter::empty().chain(Some(r_0)).chain(r_i).collect()
});
region.assign_advice(
|| format!("round_{} partial_sbox", round),
config.partial_sbox,
offset,
|| r.map(|r| r[0]).ok_or(Error::SynthesisError),
|| r.as_ref().map(|r| r[0]).ok_or(Error::SynthesisError),
)?;
let p_mid = r.map(|r| {
[
m[0][0] * r[0] + m[0][1] * r[1] + m[0][2] * r[2],
m[1][0] * r[0] + m[1][1] * r[1] + m[1][2] * r[2],
m[2][0] * r[0] + m[2][1] * r[1] + m[2][2] * r[2],
]
});
let p_mid: Option<Vec<_>> = m
.iter()
.map(|m_i| {
r.as_ref().map(|r| {
r.iter()
.enumerate()
.fold(F::zero(), |acc, (j, r_j)| acc + m_i[j] * r_j)
})
})
.collect();
// Load the second round constants.
let mut load_round_constant = |i: usize| {
@ -529,31 +501,36 @@ impl<F: FieldExt> Pow5T3State<F> {
load_round_constant(i)?;
}
let r_mid = p_mid.map(|p| {
[
(p[0] + config.round_constants[round + 1][0]).pow(&config.alpha),
p[1] + config.round_constants[round + 1][1],
p[2] + config.round_constants[round + 1][2],
]
let r_mid: Option<Vec<_>> = p_mid.map(|p| {
let r_0 = (p[0] + config.round_constants[round + 1][0]).pow(&config.alpha);
let r_i = p[1..]
.iter()
.enumerate()
.map(|(i, p_i)| *p_i + config.round_constants[round + 1][i + 1]);
std::iter::empty().chain(Some(r_0)).chain(r_i).collect()
});
Ok((
round + 2,
[
r_mid.map(|r| m[0][0] * r[0] + m[0][1] * r[1] + m[0][2] * r[2]),
r_mid.map(|r| m[1][0] * r[0] + m[1][1] * r[1] + m[1][2] * r[2]),
r_mid.map(|r| m[2][0] * r[0] + m[2][1] * r[1] + m[2][2] * r[2]),
],
))
let state: Vec<Option<_>> = m
.iter()
.map(|m_i| {
r_mid.as_ref().map(|r| {
r.iter()
.enumerate()
.fold(F::zero(), |acc, (j, r_j)| acc + m_i[j] * r_j)
})
})
.collect();
Ok((round + 2, state.try_into().unwrap()))
})
}
fn load(
fn load<const RATE: usize>(
region: &mut Region<F>,
config: &Pow5T3Config<F>,
config: &Pow5Config<F, WIDTH, RATE>,
initial_state: &State<StateWord<F>, WIDTH>,
) -> Result<Self, Error> {
let mut load_state_word = |i: usize| {
let load_state_word = |i: usize| {
let value = initial_state[i].value;
let var = region.assign_advice(
|| format!("load state_{}", i),
@ -565,16 +542,13 @@ impl<F: FieldExt> Pow5T3State<F> {
Ok(StateWord { var, value })
};
Ok(Pow5T3State([
load_state_word(0)?,
load_state_word(1)?,
load_state_word(2)?,
]))
let state: Result<Vec<_>, _> = (0..WIDTH).map(load_state_word).collect();
state.map(|state| Pow5State(state.try_into().unwrap()))
}
fn round(
fn round<const RATE: usize>(
region: &mut Region<F>,
config: &Pow5T3Config<F>,
config: &Pow5Config<F, WIDTH, RATE>,
round: usize,
offset: usize,
round_gate: Selector,
@ -599,7 +573,7 @@ impl<F: FieldExt> Pow5T3State<F> {
// Compute the next round's state.
let (next_round, next_state) = round_fn(region)?;
let mut next_state_word = |i: usize| {
let next_state_word = |i: usize| {
let value = next_state[i];
let var = region.assign_advice(
|| format!("round_{} state_{}", next_round, i),
@ -610,11 +584,8 @@ impl<F: FieldExt> Pow5T3State<F> {
Ok(StateWord { var, value })
};
Ok(Pow5T3State([
next_state_word(0)?,
next_state_word(1)?,
next_state_word(2)?,
]))
let next_state: Result<Vec<_>, _> = (0..WIDTH).map(next_state_word).collect();
next_state.map(|next_state| Pow5State(next_state.try_into().unwrap()))
}
}
@ -630,7 +601,7 @@ mod tests {
};
use pasta_curves::pallas;
use super::{PoseidonInstructions, Pow5T3Chip, Pow5T3Config, StateWord, WIDTH};
use super::{PoseidonInstructions, Pow5Chip, Pow5Config, StateWord};
use crate::{
circuit::gadget::{
poseidon::Hash,
@ -639,17 +610,20 @@ mod tests {
primitives::poseidon::{self, ConstantLength, P128Pow5T3 as OrchardNullifier, Spec},
};
const WIDTH: usize = 3;
const RATE: usize = 2;
struct PermuteCircuit {}
impl Circuit<Fp> for PermuteCircuit {
type Config = Pow5T3Config<Fp>;
type Config = Pow5Config<Fp, WIDTH, RATE>;
type FloorPlanner = SimpleFloorPlanner;
fn without_witnesses(&self) -> Self {
PermuteCircuit {}
}
fn configure(meta: &mut ConstraintSystem<Fp>) -> Pow5T3Config<Fp> {
fn configure(meta: &mut ConstraintSystem<Fp>) -> Pow5Config<Fp, WIDTH, RATE> {
let state = [
meta.advice_column(),
meta.advice_column(),
@ -668,12 +642,12 @@ mod tests {
meta.fixed_column(),
];
Pow5T3Chip::configure(meta, OrchardNullifier, state, partial_sbox, rc_a, rc_b)
Pow5Chip::configure(meta, OrchardNullifier, state, partial_sbox, rc_a, rc_b)
}
fn synthesize(
&self,
config: Pow5T3Config<Fp>,
config: Pow5Config<Fp, WIDTH, RATE>,
mut layouter: impl Layouter<Fp>,
) -> Result<(), Error> {
let initial_state = layouter.assign_region(
@ -694,8 +668,8 @@ mod tests {
},
)?;
let chip = Pow5T3Chip::construct(config.clone());
let final_state = <Pow5T3Chip<_> as PoseidonInstructions<
let chip = Pow5Chip::construct(config.clone());
let final_state = <Pow5Chip<_, WIDTH, RATE> as PoseidonInstructions<
Fp,
OrchardNullifier,
WIDTH,
@ -705,7 +679,7 @@ mod tests {
// For the purpose of this test, compute the real final state inline.
let mut expected_final_state = [Fp::zero(), Fp::one(), Fp::from_u64(2)];
let (round_constants, mds, _) = OrchardNullifier.constants();
poseidon::permute::<_, OrchardNullifier, WIDTH, 2>(
poseidon::permute::<_, OrchardNullifier, WIDTH, RATE>(
&mut expected_final_state,
&mds,
&round_constants,
@ -749,14 +723,14 @@ mod tests {
}
impl Circuit<Fp> for HashCircuit {
type Config = Pow5T3Config<Fp>;
type Config = Pow5Config<Fp, WIDTH, RATE>;
type FloorPlanner = SimpleFloorPlanner;
fn without_witnesses(&self) -> Self {
Self::default()
}
fn configure(meta: &mut ConstraintSystem<Fp>) -> Pow5T3Config<Fp> {
fn configure(meta: &mut ConstraintSystem<Fp>) -> Pow5Config<Fp, WIDTH, RATE> {
let state = [
meta.advice_column(),
meta.advice_column(),
@ -777,15 +751,15 @@ mod tests {
meta.enable_constant(rc_b[0]);
Pow5T3Chip::configure(meta, OrchardNullifier, state, partial_sbox, rc_a, rc_b)
Pow5Chip::configure(meta, OrchardNullifier, state, partial_sbox, rc_a, rc_b)
}
fn synthesize(
&self,
config: Pow5T3Config<Fp>,
config: Pow5Config<Fp, WIDTH, RATE>,
mut layouter: impl Layouter<Fp>,
) -> Result<(), Error> {
let chip = Pow5T3Chip::construct(config.clone());
let chip = Pow5Chip::construct(config.clone());
let message = layouter.assign_region(
|| "load message",

View File

@ -10831,6 +10831,15 @@ PinnedVerificationKey {
Sum(
Sum(
Sum(
Negated(
Advice {
query_index: 22,
column_index: 6,
rotation: Rotation(
1,
),
},
),
Scaled(
Product(
Product(
@ -10922,97 +10931,6 @@ PinnedVerificationKey {
),
0x0ab5e5b874a68de7b3d59fbdc8c9ead497d7a0ab23850b56323f2486d7e11b63,
),
Scaled(
Product(
Product(
Product(
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
Product(
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
0x31916628e58a5abb293f0f0d886c7954240d4a7cbf7357368eca5596e996ab5e,
),
),
Scaled(
Product(
@ -11020,15 +10938,15 @@ PinnedVerificationKey {
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11036,15 +10954,15 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11054,15 +10972,15 @@ PinnedVerificationKey {
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11070,15 +10988,15 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11088,32 +11006,114 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
0x07c045d5f5e9e5a6d803952bbb364fdfa0a3b71a5fb1573519d1cf25d8e8345d,
0x31916628e58a5abb293f0f0d886c7954240d4a7cbf7357368eca5596e996ab5e,
),
),
Negated(
Advice {
query_index: 22,
column_index: 6,
rotation: Rotation(
1,
Scaled(
Product(
Product(
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
),
},
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
0x07c045d5f5e9e5a6d803952bbb364fdfa0a3b71a5fb1573519d1cf25d8e8345d,
),
),
),
@ -11176,6 +11176,15 @@ PinnedVerificationKey {
Sum(
Sum(
Sum(
Negated(
Advice {
query_index: 18,
column_index: 7,
rotation: Rotation(
1,
),
},
),
Scaled(
Product(
Product(
@ -11267,97 +11276,6 @@ PinnedVerificationKey {
),
0x233162630ebf9ed7f8e24f66822c2d9f3a0a464048bd770ad049cdc8d085167c,
),
Scaled(
Product(
Product(
Product(
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
Product(
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
0x25cae2599892a8b0b36664548d60957d78f8365c85bbab07402270113e047a2e,
),
),
Scaled(
Product(
@ -11365,15 +11283,15 @@ PinnedVerificationKey {
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11381,15 +11299,15 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11399,15 +11317,15 @@ PinnedVerificationKey {
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11415,15 +11333,15 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11433,32 +11351,114 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
0x22f5b5e1e6081c9774938717989a19579aad3d8262efd83ff84d806f685f747a,
0x25cae2599892a8b0b36664548d60957d78f8365c85bbab07402270113e047a2e,
),
),
Negated(
Advice {
query_index: 18,
column_index: 7,
rotation: Rotation(
1,
Scaled(
Product(
Product(
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
),
},
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
0x22f5b5e1e6081c9774938717989a19579aad3d8262efd83ff84d806f685f747a,
),
),
),
@ -11521,6 +11521,15 @@ PinnedVerificationKey {
Sum(
Sum(
Sum(
Negated(
Advice {
query_index: 19,
column_index: 8,
rotation: Rotation(
1,
),
},
),
Scaled(
Product(
Product(
@ -11612,97 +11621,6 @@ PinnedVerificationKey {
),
0x2e29dd59c64b1037f333aa91c383346421680eabc56bc15dfee7a9944f84dbe4,
),
Scaled(
Product(
Product(
Product(
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
Product(
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
),
Sum(
Advice {
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
0x1d1aab4ec1cd678892d15e7dceef1665cbeaf48b3a0624c3c771effa43263664,
),
),
Scaled(
Product(
@ -11710,15 +11628,15 @@ PinnedVerificationKey {
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11726,15 +11644,15 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11744,15 +11662,15 @@ PinnedVerificationKey {
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11760,15 +11678,15 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
@ -11778,32 +11696,114 @@ PinnedVerificationKey {
),
Sum(
Advice {
query_index: 8,
column_index: 8,
query_index: 7,
column_index: 7,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
query_index: 5,
column_index: 6,
rotation: Rotation(
0,
),
},
),
),
0x3bf763086a18936451e0cbead65516b975872c39b59a31f615639415f6e85ef1,
0x1d1aab4ec1cd678892d15e7dceef1665cbeaf48b3a0624c3c771effa43263664,
),
),
Negated(
Advice {
query_index: 19,
column_index: 8,
rotation: Rotation(
1,
Scaled(
Product(
Product(
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
Product(
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
),
},
Sum(
Advice {
query_index: 8,
column_index: 8,
rotation: Rotation(
0,
),
},
Fixed {
query_index: 6,
column_index: 7,
rotation: Rotation(
0,
),
},
),
),
0x3bf763086a18936451e0cbead65516b975872c39b59a31f615639415f6e85ef1,
),
),
),