2021-07-26 14:53:45 +00:00
|
|
|
use ark_circom::{CircomBuilder, CircomConfig};
|
2021-07-26 09:45:07 +00:00
|
|
|
use ark_std::rand::thread_rng;
|
|
|
|
use color_eyre::Result;
|
|
|
|
|
|
|
|
use ark_bn254::Bn254;
|
|
|
|
use ark_groth16::{
|
|
|
|
create_random_proof as prove, generate_random_parameters, prepare_verifying_key, verify_proof,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn groth16_proof() -> Result<()> {
|
2021-07-26 14:53:45 +00:00
|
|
|
let cfg = CircomConfig::<Bn254>::new(
|
2021-07-26 09:45:07 +00:00
|
|
|
"./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 = generate_random_parameters::<Bn254, _, _>(circom, &mut rng)?;
|
|
|
|
|
|
|
|
let circom = builder.build()?;
|
|
|
|
|
|
|
|
let inputs = circom.get_public_inputs().unwrap();
|
|
|
|
|
|
|
|
let proof = prove(circom, ¶ms, &mut rng)?;
|
|
|
|
|
|
|
|
let pvk = prepare_verifying_key(¶ms.vk);
|
|
|
|
|
|
|
|
let verified = verify_proof(&pvk, &proof, &inputs)?;
|
|
|
|
|
|
|
|
assert!(verified);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-18 10:03:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn groth16_proof_wrong_input() {
|
|
|
|
let cfg = CircomConfig::<Bn254>::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 faild
|
|
|
|
builder.push_input("foo", 11);
|
|
|
|
|
|
|
|
// create an empty instance for setting it up
|
|
|
|
let circom = builder.setup();
|
|
|
|
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
let _params = generate_random_parameters::<Bn254, _, _>(circom, &mut rng).unwrap();
|
|
|
|
|
2022-08-29 22:06:27 +00:00
|
|
|
let _ = builder.build().unwrap_err();
|
2021-11-18 10:03:37 +00:00
|
|
|
}
|
2021-11-29 08:02:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "circom-2")]
|
|
|
|
fn groth16_proof_circom2() -> Result<()> {
|
|
|
|
let cfg = CircomConfig::<Bn254>::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 = generate_random_parameters::<Bn254, _, _>(circom, &mut rng)?;
|
|
|
|
|
|
|
|
let circom = builder.build()?;
|
|
|
|
|
|
|
|
let inputs = circom.get_public_inputs().unwrap();
|
|
|
|
|
|
|
|
let proof = prove(circom, ¶ms, &mut rng)?;
|
|
|
|
|
|
|
|
let pvk = prepare_verifying_key(¶ms.vk);
|
|
|
|
|
|
|
|
let verified = verify_proof(&pvk, &proof, &inputs)?;
|
|
|
|
|
|
|
|
assert!(verified);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-17 15:23:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "circom-2")]
|
|
|
|
fn witness_generation_circom2() -> Result<()> {
|
|
|
|
let cfg = CircomConfig::<Bn254>::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(())
|
|
|
|
}
|