-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
circom interface with tests #7
Open
arasuarun
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
arasuarun:circom_interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9aa9e2f
circom interface with tests
bbe9577
incorporated comments
231408f
added simplified interface fucntion
67e834a
removed struct from interface
e175fc0
Merge branch 'microsoft:main' into circom_interface
arasuarun af8a5b3
moved to new verifier
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pragma circom 2.0.6; | ||
|
||
template cube() { | ||
signal input x; | ||
signal input y; | ||
signal x_sq <== x * x; | ||
y === x_sq * x; | ||
} | ||
|
||
component main { public [x, y] } = cube(); |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
use std::path::PathBuf; | ||
|
||
use crate::{ | ||
errors::SpartanError, | ||
traits::{self, snark::RelaxedR1CSSNARKTrait, Group}, | ||
ProverKey, VerifierKey, SNARK, | ||
}; | ||
|
||
use bellpepper_core::{Circuit, ConstraintSystem, SynthesisError}; | ||
use ff::PrimeField; | ||
|
||
use circom_scotia::{r1cs::R1CS, reader::load_r1cs, witness::WitnessCalculator}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct SpartanCircuit<F: PrimeField> { | ||
r1cs: R1CS<F>, | ||
witness: Option<Vec<F>>, // this is actually z = [1 || x || w] | ||
} | ||
|
||
#[allow(dead_code)] | ||
impl<F: PrimeField> SpartanCircuit<F> { | ||
pub fn new(r1cs_path: PathBuf) -> Self { | ||
SpartanCircuit { | ||
r1cs: load_r1cs(r1cs_path), | ||
witness: None, | ||
} | ||
} | ||
|
||
pub fn compute_witness(&mut self, input: Vec<(String, Vec<F>)>, wtns_path: PathBuf) { | ||
let mut witness_calculator = WitnessCalculator::new(wtns_path).unwrap(); | ||
let witness = witness_calculator | ||
.calculate_witness(input.clone(), true) | ||
.expect("msg"); | ||
|
||
self.witness = Some(witness); | ||
} | ||
} | ||
|
||
impl<F: PrimeField> Circuit<F> for SpartanCircuit<F> { | ||
fn synthesize<CS: ConstraintSystem<F>>(self, cs: &mut CS) -> Result<(), SynthesisError> { | ||
let _ = circom_scotia::synthesize(cs, self.r1cs.clone(), self.witness).unwrap(); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn setup<G: Group, S: RelaxedR1CSSNARKTrait<G>>( | ||
circuit: SpartanCircuit<<G as Group>::Scalar>, | ||
) -> (ProverKey<G, S>, VerifierKey<G, S>) { | ||
SNARK::<G, S, SpartanCircuit<<G as Group>::Scalar>>::setup(circuit).unwrap() | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn generate_proof<G: Group, S: RelaxedR1CSSNARKTrait<G>>( | ||
pk: ProverKey<G, S>, | ||
circuit: &mut SpartanCircuit<<G as Group>::Scalar>, | ||
input: Vec<(String, Vec<<G as Group>::Scalar>)>, | ||
wtns_path: PathBuf, | ||
) -> Result<SNARK<G, S, SpartanCircuit<<G as traits::Group>::Scalar>>, SpartanError> { | ||
circuit.compute_witness(input, wtns_path); | ||
SNARK::prove(&pk, circuit.clone()) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn create_snark<G: Group, S: RelaxedR1CSSNARKTrait<G>>( | ||
r1cs_path: PathBuf, | ||
wtns_path: PathBuf, | ||
input: Vec<(String, Vec<<G as Group>::Scalar>)>, | ||
) -> ( | ||
ProverKey<G, S>, | ||
VerifierKey<G, S>, | ||
Result<SNARK<G, S, SpartanCircuit<<G as traits::Group>::Scalar>>, SpartanError>, | ||
) { | ||
let mut circuit = SpartanCircuit::new(r1cs_path); | ||
let (pk, vk) = | ||
SNARK::<G, S, SpartanCircuit<<G as Group>::Scalar>>::setup(circuit.clone()).unwrap(); | ||
circuit.compute_witness(input, wtns_path); | ||
let proof = SNARK::prove(&pk, circuit.clone()); | ||
(pk, vk, proof) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::{create_snark, generate_proof, setup, SpartanCircuit}; | ||
use crate::{provider::bn256_grumpkin::bn256, traits::Group}; | ||
use std::env::current_dir; | ||
|
||
#[test] | ||
fn test_spartan_snark() { | ||
type G = bn256::Point; | ||
type EE = crate::provider::ipa_pc::EvaluationEngine<G>; | ||
type S = crate::spartan::snark::RelaxedR1CSSNARK<G, EE>; | ||
|
||
let root = current_dir().unwrap().join("examples/cube"); | ||
let r1cs_path = root.join("cube.r1cs"); | ||
let wtns_path = root.join("cube.wasm"); | ||
|
||
let arg_x = ("x".into(), vec![<G as Group>::Scalar::from(2)]); | ||
let arg_y = ("y".into(), vec![<G as Group>::Scalar::from(8)]); | ||
let input = vec![arg_x, arg_y]; | ||
|
||
let (_, vk, res) = create_snark::<G, S>(r1cs_path, wtns_path, input); | ||
assert!(res.is_ok()); | ||
|
||
let snark = res.unwrap(); | ||
assert!(snark.verify(&vk).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_spartan_snark_fail() { | ||
type G = bn256::Point; | ||
type EE = crate::provider::ipa_pc::EvaluationEngine<G>; | ||
type S = crate::spartan::snark::RelaxedR1CSSNARK<G, EE>; | ||
|
||
let root = current_dir().unwrap().join("examples/cube"); | ||
let r1cs_path = root.join("cube.r1cs"); | ||
let wtns_path = root.join("cube.wasm"); | ||
let mut circuit = SpartanCircuit::new(r1cs_path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a simplification here. We can just pass |
||
|
||
let (pk, vk) = setup(circuit.clone()); | ||
|
||
// setting y to 9 shouldn't satisfy | ||
let arg_x = ("x".into(), vec![<G as Group>::Scalar::from(2)]); | ||
let arg_y = ("y".into(), vec![<G as Group>::Scalar::from(9)]); | ||
let input = vec![arg_x, arg_y]; | ||
|
||
let res = generate_proof::<G, S>(pk, &mut circuit, input, wtns_path); | ||
assert!(res.is_ok()); | ||
|
||
let snark = res.unwrap(); | ||
// check that it fails | ||
assert!(snark.verify(&vk).is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
// private modules | ||
mod bellpepper; | ||
mod circom; | ||
mod constants; | ||
mod digest; | ||
mod r1cs; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method seems not that useful. In other words, we can just have two methods, one for setup and another for prove. The setup takes the r1cs file path and the prove takes the witness file path (in addition to pk).