diff --git a/phase2/src/bin/export_keys.rs b/phase2/src/bin/export_keys.rs index a7f3340..bc41091 100644 --- a/phase2/src/bin/export_keys.rs +++ b/phase2/src/bin/export_keys.rs @@ -11,11 +11,13 @@ use std::fs; use std::fs::OpenOptions; use serde::{Deserialize, Serialize}; use phase2::parameters::MPCParameters; -use phase2::utils::repr_to_big; +use phase2::utils::{ + p1_to_vec, + p2_to_vec, + pairing_to_vec, +}; use bellman_ce::pairing::{ Engine, - CurveAffine, - ff::PrimeField, bn256::{ Bn256, } @@ -85,62 +87,6 @@ fn main() { h: vec![], }; - let p1_to_vec = |p : &::G1Affine| { - vec![ - repr_to_big(p.get_x().into_repr()), - repr_to_big(p.get_y().into_repr()), - if p.is_zero() { "0".to_string() } else { "1".to_string() } - ] - }; - let p2_to_vec = |p : &::G2Affine| { - vec![ - vec![ - repr_to_big(p.get_x().c0.into_repr()), - repr_to_big(p.get_x().c1.into_repr()), - ], - vec![ - repr_to_big(p.get_y().c0.into_repr()), - repr_to_big(p.get_y().c1.into_repr()), - ], - if p.is_zero() { - vec!["0".to_string(), "0".to_string()] - } else { - vec!["1".to_string(), "0".to_string()] - } - ] - }; - let pairing_to_vec = |p : bellman_ce::pairing::bn256::Fq12| { - vec![ - vec![ - vec![ - repr_to_big(p.c0.c0.c0.into_repr()), - repr_to_big(p.c0.c0.c1.into_repr()), - ], - vec![ - repr_to_big(p.c0.c1.c0.into_repr()), - repr_to_big(p.c0.c1.c1.into_repr()), - ], - vec![ - repr_to_big(p.c0.c2.c0.into_repr()), - repr_to_big(p.c0.c2.c1.into_repr()), - ] - ], - vec![ - vec![ - repr_to_big(p.c1.c0.c0.into_repr()), - repr_to_big(p.c1.c0.c1.into_repr()), - ], - vec![ - repr_to_big(p.c1.c1.c0.into_repr()), - repr_to_big(p.c1.c1.c1.into_repr()), - ], - vec![ - repr_to_big(p.c1.c2.c0.into_repr()), - repr_to_big(p.c1.c2.c1.into_repr()), - ] - ], - ] - }; let a = params.a.clone(); for e in a.iter() { proving_key.a.push(p1_to_vec(e)); @@ -200,7 +146,7 @@ fn main() { let vk_gamma_2 = params.vk.gamma_g2.clone(); verification_key.vk_gamma_2 = p2_to_vec(&vk_gamma_2); verification_key.vk_delta_2 = p2_to_vec(&vk_delta_2); - verification_key.vk_alfabeta_12 = pairing_to_vec(Bn256::pairing(vk_alfa_1, vk_beta_2)); + verification_key.vk_alfabeta_12 = pairing_to_vec(&Bn256::pairing(vk_alfa_1, vk_beta_2)); let pk_json = serde_json::to_string(&proving_key).unwrap(); fs::write(pk_filename, pk_json.as_bytes()).unwrap(); diff --git a/phase2/src/bin/prove.rs b/phase2/src/bin/prove.rs index 176b950..90a4755 100644 --- a/phase2/src/bin/prove.rs +++ b/phase2/src/bin/prove.rs @@ -10,18 +10,13 @@ use std::fs::OpenOptions; use serde::{Deserialize, Serialize}; use phase2::parameters::MPCParameters; use phase2::circom_circuit::CircomCircuit; -use phase2::utils::repr_to_big; -use bellman_ce::groth16::{prepare_verifying_key, create_random_proof, verify_proof}; -use bellman_ce::pairing::{ - Engine, - CurveAffine, - ff::{ - PrimeField, - }, - bn256::{ - Bn256, - }, +use phase2::utils::{ + repr_to_big, + p1_to_vec, + p2_to_vec, }; +use bellman_ce::groth16::{prepare_verifying_key, create_random_proof, verify_proof}; +use bellman_ce::pairing::ff::PrimeField; #[derive(Serialize, Deserialize)] struct ProofJson { @@ -73,31 +68,6 @@ fn main() { ).unwrap(); assert!(result, "Proof is correct"); - let p1_to_vec = |p : &::G1Affine| { - vec![ - repr_to_big(p.get_x().into_repr()), - repr_to_big(p.get_y().into_repr()), - if p.is_zero() { "0".to_string() } else { "1".to_string() } - ] - }; - let p2_to_vec = |p : &::G2Affine| { - vec![ - vec![ - repr_to_big(p.get_x().c0.into_repr()), - repr_to_big(p.get_x().c1.into_repr()), - ], - vec![ - repr_to_big(p.get_y().c0.into_repr()), - repr_to_big(p.get_y().c1.into_repr()), - ], - if p.is_zero() { - vec!["0".to_string(), "0".to_string()] - } else { - vec!["1".to_string(), "0".to_string()] - } - ] - }; - let proof = ProofJson { protocol: "groth".to_string(), pi_a: p1_to_vec(&proof.a), diff --git a/phase2/src/utils.rs b/phase2/src/utils.rs index 1a002b2..818f565 100644 --- a/phase2/src/utils.rs +++ b/phase2/src/utils.rs @@ -16,8 +16,13 @@ use bellman_ce::pairing::{ CurveAffine, CurveProjective, Wnaf, + Engine, bn256::{ + Bn256, G2, + G1Affine, + G2Affine, + Fq12, } }; use rand::{ @@ -118,3 +123,62 @@ pub fn hash_to_g2(mut digest: &[u8]) -> G2 pub fn repr_to_big(r: T) -> String { BigUint::from_str_radix(&format!("{}", r)[2..], 16).unwrap().to_str_radix(10) } + +pub fn p1_to_vec(p: &G1Affine) -> Vec { + return vec![ + repr_to_big(p.get_x().into_repr()), + repr_to_big(p.get_y().into_repr()), + if p.is_zero() { "0".to_string() } else { "1".to_string() } + ] +} + +pub fn p2_to_vec(p: &G2Affine) -> Vec> { + return vec![ + vec![ + repr_to_big(p.get_x().c0.into_repr()), + repr_to_big(p.get_x().c1.into_repr()), + ], + vec![ + repr_to_big(p.get_y().c0.into_repr()), + repr_to_big(p.get_y().c1.into_repr()), + ], + if p.is_zero() { + vec!["0".to_string(), "0".to_string()] + } else { + vec!["1".to_string(), "0".to_string()] + } + ] +} + +pub fn pairing_to_vec(p: &Fq12) -> Vec>> { + return vec![ + vec![ + vec![ + repr_to_big(p.c0.c0.c0.into_repr()), + repr_to_big(p.c0.c0.c1.into_repr()), + ], + vec![ + repr_to_big(p.c0.c1.c0.into_repr()), + repr_to_big(p.c0.c1.c1.into_repr()), + ], + vec![ + repr_to_big(p.c0.c2.c0.into_repr()), + repr_to_big(p.c0.c2.c1.into_repr()), + ] + ], + vec![ + vec![ + repr_to_big(p.c1.c0.c0.into_repr()), + repr_to_big(p.c1.c0.c1.into_repr()), + ], + vec![ + repr_to_big(p.c1.c1.c0.into_repr()), + repr_to_big(p.c1.c1.c1.into_repr()), + ], + vec![ + repr_to_big(p.c1.c2.c0.into_repr()), + repr_to_big(p.c1.c2.c1.into_repr()), + ] + ], + ] +} \ No newline at end of file