-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::io::{Read, Write}; | ||
use pasta_curves::{pallas, vesta}; | ||
use pasta_curves::vesta::Affine; | ||
use rand::rngs::OsRng; | ||
use halo2_proofs::plonk; | ||
use halo2_proofs::plonk::{Circuit, SingleVerifier, VerifyingKey}; | ||
use halo2_proofs::poly::commitment::Params; | ||
use halo2_proofs::transcript::{Blake2bRead, Blake2bWrite}; | ||
|
||
/// A proof structure | ||
#[derive(Clone, Debug)] | ||
pub struct Proof(Vec<u8>); | ||
impl AsRef<[u8]> for Proof { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Proof { | ||
/// Creates a proof for the given circuits and instances. | ||
pub fn create<C>( | ||
vk: &VerifyingKey<Affine>, | ||
params: &Params<Affine>, | ||
circuit: C, | ||
) | ||
-> Result<Self, plonk::Error> | ||
where | ||
C: Circuit<pallas::Base>, | ||
{ | ||
let pk = plonk::keygen_pk(¶ms, vk.clone(), &circuit).unwrap(); | ||
Check warning on line 30 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check warning on line 30 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 30 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 30 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
|
||
|
||
let mut transcript = Blake2bWrite::<_, vesta::Affine, _>::init(vec![]); | ||
plonk::create_proof( | ||
¶ms, | ||
Check warning on line 34 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check warning on line 34 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 34 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 34 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
|
||
&pk, | ||
&[circuit], | ||
&[&[]], | ||
OsRng, | ||
&mut transcript, | ||
)?; | ||
let proof = transcript.finalize(); | ||
|
||
Ok(Proof(proof)) | ||
} | ||
|
||
/// Verifies this proof with the instances. | ||
pub fn verify( | ||
&self, | ||
vk: &VerifyingKey<Affine>, | ||
params: &Params<Affine>, | ||
) -> Result<(), plonk::Error> { | ||
|
||
let strategy = SingleVerifier::new(¶ms); | ||
Check warning on line 53 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check warning on line 53 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 53 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 53 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
|
||
let mut transcript = Blake2bRead::init(&self.0[..]); | ||
plonk::verify_proof(¶ms, &vk, strategy, &[&[]], &mut transcript) | ||
Check warning on line 55 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check warning on line 55 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check warning on line 55 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check warning on line 55 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 55 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 55 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 55 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
Check failure on line 55 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)this expression creates a reference which is immediately dereferenced by the compiler
|
||
} | ||
pub fn new(bytes: Vec<u8>) -> Self { | ||
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)missing documentation for an associated function
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Bitrot check
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Bitrot check
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Book tests
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Book tests
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Intra-doc links
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Intra-doc links
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Build target wasm32-wasi
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Build target wasm32-wasi
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)missing documentation for an associated function
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest with beta features
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest with beta features
Check failure on line 57 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on ubuntu-latest with beta features
|
||
Proof(bytes) | ||
} | ||
} | ||
|
||
pub(crate) fn test_proof_size<C>( | ||
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)function `test_proof_size` is never used
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Bitrot check
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Bitrot check
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Book tests
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Book tests
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Build target wasm32-wasi
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Build target wasm32-wasi
Check failure on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)function is never used: `test_proof_size`
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest with beta features
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest with beta features
Check warning on line 62 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on ubuntu-latest with beta features
|
||
k: u32, | ||
circuit: C, | ||
params: Params<Affine>, | ||
vk: VerifyingKey<Affine>, | ||
) where | ||
C: Circuit<pallas::Base>, | ||
{ | ||
// Test that the proof size is as expected. | ||
let circuit_cost = | ||
halo2_proofs::dev::CircuitCost::<pasta_curves::vesta::Point, _>::measure(k, &circuit); | ||
let expected_proof_size = usize::from(circuit_cost.proof_size(1)); | ||
|
||
|
||
let proof = Proof::create(&vk,¶ms,circuit).unwrap(); | ||
|
||
assert!(proof.verify(&vk, ¶ms).is_ok()); | ||
assert_eq!(proof.as_ref().len(), expected_proof_size); | ||
} | ||
pub fn write_test_case<W: Write>( | ||
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Bitrot check
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Bitrot check
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Book tests
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Book tests
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Intra-doc links
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Intra-doc links
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Build target wasm32-wasi
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Build target wasm32-wasi
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest with beta features
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest with beta features
Check failure on line 81 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on ubuntu-latest with beta features
|
||
mut w: W, | ||
proof: &Proof, | ||
) -> std::io::Result<()> { | ||
Check failure on line 84 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)missing documentation for a function
|
||
w.write_all(proof.as_ref())?; | ||
Ok(()) | ||
} | ||
Check failure on line 87 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)missing documentation for a function
|
||
|
||
pub fn read_test_case<R: Read>(mut r: R) -> std::io::Result<Proof> { | ||
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (beta)missing documentation for a function
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Bitrot check
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Bitrot check
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Book tests
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Book tests
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Intra-doc links
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Intra-doc links
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Build target wasm32-wasi
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Build target wasm32-wasi
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Clippy (MSRV)missing documentation for a function
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest with beta features
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on macOS-latest with beta features
Check failure on line 89 in halo2_gadgets/src/utilities/test_circuit.rs GitHub Actions / Test on ubuntu-latest with beta features
|
||
let mut proof_bytes = vec![]; | ||
r.read_to_end(&mut proof_bytes)?; | ||
let proof = Proof::new(proof_bytes); | ||
|
||
Ok(proof) | ||
} |