Skip to content

Commit

Permalink
Merge pull request #646 from zcash/fix-pow5-pad
Browse files Browse the repository at this point in the history
`halo2_gadgets::poseidon`: Fix loading of padding words.
  • Loading branch information
str4d authored Jun 27, 2023
2 parents 88ede7f + f4293c2 commit eec65ea
Showing 1 changed file with 49 additions and 19 deletions.
68 changes: 49 additions & 19 deletions halo2_gadgets/src/poseidon/pow5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,24 +341,30 @@ impl<

// Load the input into this region.
let load_input_word = |i: usize| {
let constraint_var = match input.0[i].clone() {
Some(PaddedWord::Message(word)) => word,
Some(PaddedWord::Padding(padding_value)) => region.assign_fixed(
|| format!("load pad_{}", i),
config.rc_b[i],
1,
|| Value::known(padding_value),
)?,
let (cell, value) = match input.0[i].clone() {
Some(PaddedWord::Message(word)) => (word.cell(), word.value().copied()),
Some(PaddedWord::Padding(padding_value)) => {
let cell = region
.assign_fixed(
|| format!("load pad_{}", i),
config.rc_b[i],
1,
|| Value::known(padding_value),
)?
.cell();
(cell, Value::known(padding_value))
}
_ => panic!("Input is not padded"),
};
constraint_var
.copy_advice(
|| format!("load input_{}", i),
&mut region,
config.state[i],
1,
)
.map(StateWord)
let var = region.assign_advice(
|| format!("load input_{}", i),
config.state[i],
1,
|| value,
)?;
region.constrain_equal(cell, var.cell())?;

Ok(StateWord(var))
};
let input: Result<Vec<_>, Error> = (0..RATE).map(load_input_word).collect();
let input = input?;
Expand Down Expand Up @@ -597,9 +603,11 @@ mod tests {
circuit::{Layouter, SimpleFloorPlanner, Value},
dev::MockProver,
pasta::Fp,
plonk::{Circuit, ConstraintSystem, Error},
plonk::{self, Circuit, ConstraintSystem, Error, SingleVerifier},
poly::commitment::Params,
transcript::{Blake2bRead, Blake2bWrite, Challenge255},
};
use pasta_curves::pallas;
use pasta_curves::{pallas, EqAffine};
use rand::rngs::OsRng;

use super::{PoseidonInstructions, Pow5Chip, Pow5Config, StateWord};
Expand Down Expand Up @@ -840,7 +848,29 @@ mod tests {
_spec: PhantomData,
};
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()))
assert_eq!(prover.verify(), Ok(()));

let params = Params::new(k);
let vk = plonk::keygen_vk(&params, &circuit).unwrap();
let pk = plonk::keygen_pk(&params, vk, &circuit).unwrap();

let mut transcript = Blake2bWrite::<_, EqAffine, _>::init(vec![]);
plonk::create_proof(
&params,
&pk,
&[circuit],
&[&[]],
&mut OsRng,
&mut transcript,
)
.unwrap();
let proof = transcript.finalize();

let strategy = SingleVerifier::new(&params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]);
assert!(
plonk::verify_proof(&params, pk.get_vk(), strategy, &[&[]], &mut transcript).is_ok()
);
}

#[test]
Expand Down

0 comments on commit eec65ea

Please sign in to comment.