Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Dec 20, 2024
1 parent 93af7b5 commit c031c3a
Showing 2 changed files with 39 additions and 22 deletions.
35 changes: 24 additions & 11 deletions groth16_verifier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![no_std]
use soroban_sdk::{
contract, contractimpl, contracttype, crypto::bls12_381::{Fr, G1Affine, G2Affine}, vec, BytesN, Env, Vec
contract, contractimpl, contracttype,
crypto::bls12_381::{Fr, G1Affine, G2Affine},
vec, BytesN, Env, Vec,
};

#[derive(Clone)]
@@ -16,10 +18,10 @@ pub struct VerificationKey {
#[derive(Clone)]
#[contracttype]
pub struct Proof {
pub a: BytesN<96>,
pub a: BytesN<96>,
pub neg_a: BytesN<96>,
pub b: BytesN<192>,
pub c: BytesN<96>,
pub b: BytesN<192>,
pub c: BytesN<96>,
}

#[contract]
@@ -29,24 +31,35 @@ pub struct Groth16Verifier;
impl Groth16Verifier {
pub fn verify_proof(env: Env, vk: VerificationKey, proof: Proof, pub_signals: Vec<Fr>) -> bool {
let bls = env.crypto().bls12_381();

// vk_x = IC0 + pub_signals[0]*IC1
// Here we have only one public input. If there were more, we'd sum them similarly.
let mut vk_x = G1Affine::from_bytes(vk.ic.get(0).unwrap());
if let Some(s) = pub_signals.get(0) {
let term = bls.g1_mul(&G1Affine::from_bytes(vk.ic.get(1).unwrap()), &s);
vk_x = bls.g1_add(&vk_x, &term);
}

// We need to compute the pairing:
// e(-A, B) * e(alpha, beta) * e(vk_x, gamma) * e(C, delta) == 1
let neg_a = G1Affine::from_bytes(proof.neg_a);
let vp1 = vec![&env, neg_a, G1Affine::from_bytes(vk.alpha), vk_x, G1Affine::from_bytes(proof.c)];
let vp2 = vec![&env, G2Affine::from_bytes(proof.b), G2Affine::from_bytes(vk.beta), G2Affine::from_bytes(vk.gamma), G2Affine::from_bytes(vk.delta)];

let vp1 = vec![
&env,
neg_a,
G1Affine::from_bytes(vk.alpha),
vk_x,
G1Affine::from_bytes(proof.c),
];
let vp2 = vec![
&env,
G2Affine::from_bytes(proof.b),
G2Affine::from_bytes(vk.beta),
G2Affine::from_bytes(vk.gamma),
G2Affine::from_bytes(vk.delta),
];

bls.pairing_check(vp1, vp2)
}
}


mod test;
mod test;
26 changes: 15 additions & 11 deletions groth16_verifier/src/test.rs
Original file line number Diff line number Diff line change
@@ -3,22 +3,22 @@ extern crate std;

use ark_bls12_381::{Fq, Fq2};
use ark_serialize::CanonicalSerialize;
use core::str::FromStr;
use soroban_sdk::{
crypto::bls12_381::{Fr, G1Affine, G2Affine},
Env, Vec, U256,
Env, Vec, U256,
};
use core::str::FromStr;

use crate::{Groth16Verifier, Groth16VerifierClient, Proof, VerificationKey};

fn g1_from_coords(env: &Env, x: &str, y: &str) -> G1Affine {
fn g1_from_coords(env: &Env, x: &str, y: &str) -> G1Affine {
let ark_g1 = ark_bls12_381::G1Affine::new(Fq::from_str(x).unwrap(), Fq::from_str(y).unwrap());
let mut buf = [0u8; 96];
ark_g1.serialize_uncompressed(&mut buf[..]).unwrap();
G1Affine::from_array(env, &buf)
}

fn neg_g1_from_coords(env: &Env, x: &str, y: &str) -> G1Affine {
fn neg_g1_from_coords(env: &Env, x: &str, y: &str) -> G1Affine {
let ark_g1 = ark_bls12_381::G1Affine::new(Fq::from_str(x).unwrap(), -Fq::from_str(y).unwrap());
let mut buf = [0u8; 96];
ark_g1.serialize_uncompressed(&mut buf[..]).unwrap();
@@ -65,18 +65,22 @@ fn test() {
let ic0y = "3455508165409829148751617737772894557887792278044850553785496869183933597103951941805834639972489587640583544390358";

let ic1x = "2645559270376031734407122278942646687260452979296081924477586893972449945444985371392950465676350735694002713633589";
let ic1y = "2241039659097418315097403108596818813895651201896886552939297756980670248638746432560267634304593609165964274111037";
let ic1y = "2241039659097418315097403108596818813895651201896886552939297756980670248638746432560267634304593609165964274111037";


let vk = VerificationKey {
alpha: g1_from_coords(&env, alphax, alphay).to_bytes(),
beta: g2_from_coords(&env, betax1, betax2, betay1, betay2).to_bytes(),
gamma: g2_from_coords(&env, gammax1, gammax2, gammay1, gammay2).to_bytes(),
delta: g2_from_coords(&env, deltax1, deltax2, deltay1, deltay2).to_bytes(),
ic: Vec::from_array(&env, [g1_from_coords(&env, ic0x, ic0y).to_bytes(), g1_from_coords(&env, ic1x, ic1y).to_bytes()])
ic: Vec::from_array(
&env,
[
g1_from_coords(&env, ic0x, ic0y).to_bytes(),
g1_from_coords(&env, ic1x, ic1y).to_bytes(),
],
),
};


let pi_ax = "314442236668110257304682488877371582255161413673331360366570443799415414639292047869143313601702131653514009114222";
let pi_ay = "2384632327855835824635705027009217874826122107057894594162233214798350178691568018290025994699762298534539543934607";
let pi_bx1 = "428844167033934720609657613212495751617651348480870890908850335525890280786532876634895457032623422366474694342656";
@@ -96,8 +100,8 @@ fn test() {
let output = Vec::from_array(&env, [Fr::from_u256(U256::from_u32(&env, 33))]);

let client = create_client(&env);
env.budget().reset_default();
let res= client.verify_proof(&vk, &proof, &output);
env.budget().reset_default();
let res = client.verify_proof(&vk, &proof, &output);
assert_eq!(res, true);
env.budget().print();
}
}

0 comments on commit c031c3a

Please sign in to comment.