move p1_to_vec, p2_to_vec, pairing_to_vec to utils.rs

This commit is contained in:
poma 2020-01-24 13:53:28 +08:00
parent 1ef0e48957
commit 213aea092f
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
3 changed files with 76 additions and 96 deletions

@ -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 : &<Bn256 as Engine>::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 : &<Bn256 as Engine>::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();

@ -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 : &<Bn256 as Engine>::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 : &<Bn256 as Engine>::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),

@ -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<T: std::fmt::Display>(r: T) -> String {
BigUint::from_str_radix(&format!("{}", r)[2..], 16).unwrap().to_str_radix(10)
}
pub fn p1_to_vec(p: &G1Affine) -> Vec<String> {
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<Vec<String>> {
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<Vec<Vec<String>>> {
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()),
]
],
]
}