|
|
use ark_circom::{CircomBuilder, CircomConfig};
use ark_std::rand::thread_rng;
use color_eyre::Result;
use ark_bn254::{Bn254, Fr};
use ark_crypto_primitives::snark::SNARK;
use ark_groth16::Groth16;
type GrothBn = Groth16<Bn254>;
#[tokio::test]
async fn groth16_proof() -> Result<()> {
let cfg = CircomConfig::<Fr>::new(
"./test-vectors/mycircuit.wasm",
"./test-vectors/mycircuit.r1cs",
)?;
let mut builder = CircomBuilder::new(cfg);
builder.push_input("a", 3);
builder.push_input("b", 11);
// create an empty instance for setting it up
let circom = builder.setup();
let mut rng = thread_rng();
let params = GrothBn::generate_random_parameters_with_reduction(circom, &mut rng)?;
let circom = builder.build()?;
let inputs = circom.get_public_inputs().unwrap();
let proof = GrothBn::prove(¶ms, circom, &mut rng)?;
let pvk = GrothBn::process_vk(¶ms.vk).unwrap();
let verified = GrothBn::verify_with_processed_vk(&pvk, &inputs, &proof)?;
assert!(verified);
Ok(())
}
#[tokio::test]
async fn groth16_proof_wrong_input() {
let cfg = CircomConfig::<Fr>::new(
"./test-vectors/mycircuit.wasm",
"./test-vectors/mycircuit.r1cs",
)
.unwrap();
let mut builder = CircomBuilder::new(cfg);
builder.push_input("a", 3);
// This isn't a public input to the circuit, should fail
builder.push_input("foo", 11);
// create an empty instance for setting it up
let circom = builder.setup();
let mut rng = thread_rng();
let _params = GrothBn::generate_random_parameters_with_reduction(circom, &mut rng).unwrap();
let _ = builder.build().unwrap_err();
}
#[tokio::test]
#[cfg(feature = "circom-2")]
async fn groth16_proof_circom2() -> Result<()> {
let cfg = CircomConfig::<Fr>::new(
"./test-vectors/circom2_multiplier2.wasm",
"./test-vectors/circom2_multiplier2.r1cs",
)?;
let mut builder = CircomBuilder::new(cfg);
builder.push_input("a", 3);
builder.push_input("b", 11);
// create an empty instance for setting it up
let circom = builder.setup();
let mut rng = thread_rng();
let params = GrothBn::generate_random_parameters_with_reduction(circom, &mut rng)?;
let circom = builder.build()?;
let inputs = circom.get_public_inputs().unwrap();
let proof = GrothBn::prove(¶ms, circom, &mut rng)?;
let pvk = GrothBn::process_vk(¶ms.vk).unwrap();
let verified = GrothBn::verify_with_processed_vk(&pvk, &inputs, &proof)?;
assert!(verified);
Ok(())
}
#[tokio::test]
#[cfg(feature = "circom-2")]
async fn witness_generation_circom2() -> Result<()> {
let cfg = CircomConfig::<Fr>::new(
"./test-vectors/circom2_multiplier2.wasm",
"./test-vectors/circom2_multiplier2.r1cs",
)?;
let mut builder = CircomBuilder::new(cfg);
builder.push_input("a", 3);
builder.push_input("b", 0x100000000u64 - 1);
assert!(builder.build().is_ok());
Ok(())
}
|