Skip to content

Commit

Permalink
Public IO in PSE Fork (#30)
Browse files Browse the repository at this point in the history
* implemented for prove

* integrate for verification

* clean up unused imports & code

* add pub value to add circuit
  • Loading branch information
jp4g authored Jun 9, 2023
1 parent 7b7a6e8 commit 9890df4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/add/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Test integer addition: 3 + 4 = 7
fn main(mut x: u32, y: u32, z: u32) {
fn main(mut x: u32, y: u32, z: pub u32) {
x += y;
assert(x == z);

x *= 8;
assert(x>9);
}
}
23 changes: 21 additions & 2 deletions src/pse_halo2/acvm_interop/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::pse_halo2::halo2_plonk_api::{halo2_keygen, halo2_prove, halo2_verify}

use crate::pse_halo2::PseHalo2;

use crate::noir_field_to_halo2_field;

impl ProofSystemCompiler for PseHalo2 {
type Error = BackendError;

Expand Down Expand Up @@ -70,13 +72,23 @@ impl ProofSystemCompiler for PseHalo2 {
)
.unwrap();

let instance: Vec<Fr> = circuit
.public_inputs()
.indices()
.iter()
.map(|index| match witness_values.get_index(*index) {
Some(val) => noir_field_to_halo2_field(*val),
None => noir_field_to_halo2_field(FieldElement::zero()),
})
.collect();

let translator = NoirHalo2Translator::<Fr> {
circuit: circuit.clone(),
witness_values,
_marker: PhantomData::<Fr>,
};

let proof = halo2_prove(translator, &params, &pk);
let proof = halo2_prove(translator, &params, &pk, &instance[..]);

Ok(proof)
}
Expand All @@ -103,7 +115,12 @@ impl ProofSystemCompiler for PseHalo2 {
)
.unwrap();

Ok(halo2_verify(&params, &vk, proof).is_ok())
let instance: Vec<Fr> = _public_inputs
.into_iter()
.map(|(_, el)| noir_field_to_halo2_field(el))
.collect();

Ok(halo2_verify(&params, &vk, proof, &instance[..]).is_ok())
}

fn np_language(&self) -> Language {
Expand Down Expand Up @@ -153,3 +170,5 @@ impl ProofSystemCompiler for PseHalo2 {
panic!("vk_as_fields not supported in this backend");
}
}

noir_field_to_halo2_field!(Fr);
15 changes: 13 additions & 2 deletions src/pse_halo2/halo2_plonk_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,27 @@ pub fn halo2_prove(
circuit: NoirHalo2Translator<Fr>,
params: &ParamsKZG<Bn256>,
pk: &ProvingKey<<G1 as CofactorCurve>::Affine>,
public_inputs: &[Fr],
) -> Vec<u8> {
let rng = OsRng;
let mut transcript: Blake2bWrite<Vec<u8>, _, Challenge255<_>> =
Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);

create_proof::<
KZGCommitmentScheme<Bn256>,
ProverGWC<'_, Bn256>,
Challenge255<G1Affine>,
_,
Blake2bWrite<Vec<u8>, G1Affine, Challenge255<_>>,
_,
>(params, pk, &[circuit], &[&[&[]]], rng, &mut transcript)
>(
params,
pk,
&[circuit],
&[&[public_inputs]],
rng,
&mut transcript,
)
.expect("proof generation should not fail");
transcript.finalize()
}
Expand All @@ -71,16 +80,18 @@ pub fn halo2_verify(
params: &ParamsKZG<Bn256>,
vk: &VerifyingKey<<G1 as CofactorCurve>::Affine>,
proof: &[u8],
public_inputs: &[Fr],
) -> Result<(), Error> {
let strategy = SingleStrategy::new(params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(proof);

verify_proof::<
KZGCommitmentScheme<Bn256>,
VerifierGWC<'_, Bn256>,
Challenge255<G1Affine>,
Blake2bRead<&[u8], G1Affine, Challenge255<G1Affine>>,
SingleStrategy<'_, Bn256>,
>(params, vk, strategy, &[&[&[]]], &mut transcript)
>(params, vk, strategy, &[&[public_inputs]], &mut transcript)
}

#[derive(Clone)]
Expand Down

0 comments on commit 9890df4

Please sign in to comment.