1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

Remove Sol abi differences

This commit is contained in:
Leo Alt 2021-07-16 12:12:06 +02:00
parent 2857ceb972
commit 939f1eb58c
9 changed files with 53 additions and 506 deletions

View file

@ -52,16 +52,6 @@ pub fn subcommand() -> App<'static, 'static> {
.possible_values(constants::SCHEMES) .possible_values(constants::SCHEMES)
.default_value(constants::G16), .default_value(constants::G16),
) )
.arg(
Arg::with_name("solidity-abi")
.short("a")
.long("solidity-abi")
.help("Flag for setting the version of the ABI Encoder used in the contract")
.takes_value(true)
.possible_values(&["v1", "v2"])
.default_value("v1")
.required(false),
)
} }
pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> {
@ -99,9 +89,7 @@ fn cli_export_verifier<T: SolidityCompatibleField, S: SolidityCompatibleScheme<T
let vk = serde_json::from_reader(reader) let vk = serde_json::from_reader(reader)
.map_err(|why| format!("Could not deserialize verification key: {}", why))?; .map_err(|why| format!("Could not deserialize verification key: {}", why))?;
let abi = SolidityAbi::from(sub_matches.value_of("solidity-abi").unwrap())?; let verifier = S::export_solidity_verifier(vk);
let verifier = S::export_solidity_verifier(vk, abi);
//write output file //write output file
let output_path = Path::new(sub_matches.value_of("output").unwrap()); let output_path = Path::new(sub_matches.value_of("output").unwrap());

View file

@ -4,7 +4,6 @@ const solc = require('solc');
const contractPath = process.argv[2] const contractPath = process.argv[2]
const proofPath = process.argv[3] const proofPath = process.argv[3]
const format = process.argv[4] const format = process.argv[4]
const abiVersion = process.argv[5];
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
// -----Compile contract----- // -----Compile contract-----
@ -86,15 +85,11 @@ console.log(jsonInterface);
} }
} }
return abiVersion == "v1" ? verifyTx(proof, account, correct).on('receipt', handleReceipt)
verifyTx_ABIV1(proof, account, correct).on('receipt', handleReceipt) .catch(handleError);
.catch(handleError)
:
verifyTx_ABIV2(proof, account, correct).on('receipt', handleReceipt)
.catch(handleError);
} }
function verifyTx_ABIV2(proof, account, correct) { function verifyTx(proof, account, correct) {
var args = proof[0]; var args = proof[0];
args = proof[1].length > 0 ? [args, proof[1]] : [args]; args = proof[1].length > 0 ? [args, proof[1]] : [args];
@ -104,18 +99,6 @@ console.log(jsonInterface);
}); });
} }
function verifyTx_ABIV1(proof, account, correct) {
var args = proof[0];
args = proof[1].length > 0 ? [...args, proof[1]] : args;
return contract.methods.verifyTx(
...args
).send({
from: account,
gas: 5000000
});
}
function getProof(correct) { function getProof(correct) {
let json = JSON.parse(fs.readFileSync(proofPath)); let json = JSON.parse(fs.readFileSync(proofPath));
let inputs = json["inputs"]; let inputs = json["inputs"];

View file

@ -295,36 +295,31 @@ mod integration {
.unwrap(); .unwrap();
if scheme != &"marlin" { if scheme != &"marlin" {
for abi_version in &["v1", "v2"] { // EXPORT-VERIFIER
// EXPORT-VERIFIER assert_cli::Assert::command(&[
assert_cli::Assert::command(&[ "../target/release/zokrates",
"../target/release/zokrates", "export-verifier",
"export-verifier", "-i",
"-i", verification_key_path.to_str().unwrap(),
verification_key_path.to_str().unwrap(), "-o",
"-o", verification_contract_path.to_str().unwrap(),
verification_contract_path.to_str().unwrap(), "--proving-scheme",
"--proving-scheme", scheme,
scheme, ])
"-a", .succeeds()
abi_version, .unwrap();
])
.succeeds()
.unwrap();
// TEST VERIFIER // TEST VERIFIER
assert_cli::Assert::command(&[ assert_cli::Assert::command(&[
"node", "node",
"test.js", "test.js",
verification_contract_path.to_str().unwrap(), verification_contract_path.to_str().unwrap(),
proof_path.to_str().unwrap(), proof_path.to_str().unwrap(),
scheme, scheme,
abi_version, ])
]) .current_dir(concat!(env!("OUT_DIR"), "/contract"))
.current_dir(concat!(env!("OUT_DIR"), "/contract")) .succeeds()
.succeeds() .unwrap();
.unwrap();
}
} }
} }
} }

View file

@ -1,7 +1,5 @@
use crate::proof_system::scheme::{NonUniversalScheme, Scheme}; use crate::proof_system::scheme::{NonUniversalScheme, Scheme};
use crate::proof_system::solidity::{ use crate::proof_system::solidity::{SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB};
SolidityAbi, SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
};
use crate::proof_system::{ use crate::proof_system::{
G1Affine, G2Affine, G2AffineFq, SolidityCompatibleField, SolidityCompatibleScheme, G1Affine, G2Affine, G2AffineFq, SolidityCompatibleField, SolidityCompatibleScheme,
}; };
@ -49,20 +47,11 @@ impl Scheme<Bw6_761Field> for GM17 {
} }
impl<T: SolidityCompatibleField + NotBw6_761Field> SolidityCompatibleScheme<T> for GM17 { impl<T: SolidityCompatibleField + NotBw6_761Field> SolidityCompatibleScheme<T> for GM17 {
fn export_solidity_verifier( fn export_solidity_verifier(vk: <GM17 as Scheme<T>>::VerificationKey) -> String {
vk: <GM17 as Scheme<T>>::VerificationKey, let (mut template_text, solidity_pairing_lib) = (
abi: SolidityAbi, String::from(CONTRACT_TEMPLATE),
) -> String { String::from(SOLIDITY_PAIRING_LIB),
let (mut template_text, solidity_pairing_lib) = match abi { );
SolidityAbi::V1 => (
String::from(CONTRACT_TEMPLATE),
String::from(SOLIDITY_PAIRING_LIB),
),
SolidityAbi::V2 => (
String::from(CONTRACT_TEMPLATE_V2),
String::from(SOLIDITY_PAIRING_LIB_V2),
),
};
// replace things in template // replace things in template
let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap(); let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap();
@ -158,7 +147,7 @@ impl<T: SolidityCompatibleField + NotBw6_761Field> SolidityCompatibleScheme<T> f
} }
} }
const CONTRACT_TEMPLATE_V2: &str = r#" const CONTRACT_TEMPLATE: &str = r#"
contract Verifier { contract Verifier {
using Pairing for *; using Pairing for *;
struct VerifyingKey { struct VerifyingKey {
@ -219,71 +208,3 @@ contract Verifier {
} }
} }
"#; "#;
const CONTRACT_TEMPLATE: &str = r#"
contract Verifier {
using Pairing for *;
struct VerifyingKey {
Pairing.G2Point h;
Pairing.G1Point g_alpha;
Pairing.G2Point h_beta;
Pairing.G1Point g_gamma;
Pairing.G2Point h_gamma;
Pairing.G1Point[] query;
}
struct Proof {
Pairing.G1Point a;
Pairing.G2Point b;
Pairing.G1Point c;
}
function verifyingKey() pure internal returns (VerifyingKey memory vk) {
vk.h = Pairing.G2Point(<%vk_h%>);
vk.g_alpha = Pairing.G1Point(<%vk_g_alpha%>);
vk.h_beta = Pairing.G2Point(<%vk_h_beta%>);
vk.g_gamma = Pairing.G1Point(<%vk_g_gamma%>);
vk.h_gamma = Pairing.G2Point(<%vk_h_gamma%>);
vk.query = new Pairing.G1Point[](<%vk_query_length%>);
<%vk_query_pts%>
}
function verify(uint[] memory input, Proof memory proof) internal view returns (uint) {
uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
VerifyingKey memory vk = verifyingKey();
require(input.length + 1 == vk.query.length);
// Compute the linear combination vk_x
Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0);
for (uint i = 0; i < input.length; i++) {
require(input[i] < snark_scalar_field);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.query[i + 1], input[i]));
}
vk_x = Pairing.addition(vk_x, vk.query[0]);
/**
* e(A*G^{alpha}, B*H^{beta}) = e(G^{alpha}, H^{beta}) * e(G^{psi}, H^{gamma})
* * e(C, H)
* where psi = \sum_{i=0}^l input_i pvk.query[i]
*/
if (!Pairing.pairingProd4(vk.g_alpha, vk.h_beta, vk_x, vk.h_gamma, proof.c, vk.h, Pairing.negate(Pairing.addition(proof.a, vk.g_alpha)), Pairing.addition(proof.b, vk.h_beta))) return 1;
/**
* e(A, H^{gamma}) = e(G^{gamma}, b)
*/
if (!Pairing.pairingProd2(proof.a, vk.h_gamma, Pairing.negate(vk.g_gamma), proof.b)) return 2;
return 0;
}
function verifyTx(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c<%input_argument%>
) public view returns (bool r) {
Proof memory proof;
proof.a = Pairing.G1Point(a[0], a[1]);
proof.b = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
proof.c = Pairing.G1Point(c[0], c[1]);
uint[] memory inputValues = new uint[](<%vk_input_length%>);
<%input_loop%>
if (verify(inputValues, proof) == 0) {
return true;
} else {
return false;
}
}
}
"#;

View file

@ -1,7 +1,5 @@
use crate::proof_system::scheme::{NonUniversalScheme, Scheme}; use crate::proof_system::scheme::{NonUniversalScheme, Scheme};
use crate::proof_system::solidity::{ use crate::proof_system::solidity::{SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB};
SolidityAbi, SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
};
use crate::proof_system::{G1Affine, G2Affine, SolidityCompatibleField, SolidityCompatibleScheme}; use crate::proof_system::{G1Affine, G2Affine, SolidityCompatibleField, SolidityCompatibleScheme};
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -33,20 +31,11 @@ impl<T: Field> Scheme<T> for G16 {
impl<T: Field> NonUniversalScheme<T> for G16 {} impl<T: Field> NonUniversalScheme<T> for G16 {}
impl<T: SolidityCompatibleField> SolidityCompatibleScheme<T> for G16 { impl<T: SolidityCompatibleField> SolidityCompatibleScheme<T> for G16 {
fn export_solidity_verifier( fn export_solidity_verifier(vk: <G16 as Scheme<T>>::VerificationKey) -> String {
vk: <G16 as Scheme<T>>::VerificationKey, let (mut template_text, solidity_pairing_lib) = (
abi: SolidityAbi, String::from(CONTRACT_TEMPLATE),
) -> String { String::from(SOLIDITY_PAIRING_LIB),
let (mut template_text, solidity_pairing_lib) = match abi { );
SolidityAbi::V1 => (
String::from(CONTRACT_TEMPLATE),
String::from(SOLIDITY_PAIRING_LIB),
),
SolidityAbi::V2 => (
String::from(CONTRACT_TEMPLATE_V2),
String::from(SOLIDITY_PAIRING_LIB_V2),
),
};
let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap(); let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap();
let vk_gamma_abc_len_regex = Regex::new(r#"(<%vk_gamma_abc_length%>)"#).unwrap(); let vk_gamma_abc_len_regex = Regex::new(r#"(<%vk_gamma_abc_length%>)"#).unwrap();
@ -140,7 +129,7 @@ impl<T: SolidityCompatibleField> SolidityCompatibleScheme<T> for G16 {
} }
} }
const CONTRACT_TEMPLATE_V2: &str = r#" const CONTRACT_TEMPLATE: &str = r#"
contract Verifier { contract Verifier {
using Pairing for *; using Pairing for *;
struct VerifyingKey { struct VerifyingKey {
@ -194,64 +183,3 @@ contract Verifier {
} }
} }
"#; "#;
const CONTRACT_TEMPLATE: &str = r#"
contract Verifier {
using Pairing for *;
struct VerifyingKey {
Pairing.G1Point alpha;
Pairing.G2Point beta;
Pairing.G2Point gamma;
Pairing.G2Point delta;
Pairing.G1Point[] gamma_abc;
}
struct Proof {
Pairing.G1Point a;
Pairing.G2Point b;
Pairing.G1Point c;
}
function verifyingKey() pure internal returns (VerifyingKey memory vk) {
vk.alpha = Pairing.G1Point(<%vk_alpha%>);
vk.beta = Pairing.G2Point(<%vk_beta%>);
vk.gamma = Pairing.G2Point(<%vk_gamma%>);
vk.delta = Pairing.G2Point(<%vk_delta%>);
vk.gamma_abc = new Pairing.G1Point[](<%vk_gamma_abc_length%>);
<%vk_gamma_abc_pts%>
}
function verify(uint[] memory input, Proof memory proof) internal view returns (uint) {
uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
VerifyingKey memory vk = verifyingKey();
require(input.length + 1 == vk.gamma_abc.length);
// Compute the linear combination vk_x
Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0);
for (uint i = 0; i < input.length; i++) {
require(input[i] < snark_scalar_field);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.gamma_abc[i + 1], input[i]));
}
vk_x = Pairing.addition(vk_x, vk.gamma_abc[0]);
if(!Pairing.pairingProd4(
proof.a, proof.b,
Pairing.negate(vk_x), vk.gamma,
Pairing.negate(proof.c), vk.delta,
Pairing.negate(vk.alpha), vk.beta)) return 1;
return 0;
}
function verifyTx(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c<%input_argument%>
) public view returns (bool r) {
Proof memory proof;
proof.a = Pairing.G1Point(a[0], a[1]);
proof.b = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
proof.c = Pairing.G1Point(c[0], c[1]);
uint[] memory inputValues = new uint[](<%vk_input_length%>);
<%input_loop%>
if (verify(inputValues, proof) == 0) {
return true;
} else {
return false;
}
}
}
"#;

View file

@ -1,7 +1,5 @@
use crate::proof_system::scheme::{NonUniversalScheme, Scheme}; use crate::proof_system::scheme::{NonUniversalScheme, Scheme};
use crate::proof_system::solidity::{ use crate::proof_system::solidity::{SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB};
SolidityAbi, SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
};
use crate::proof_system::{G1Affine, G2Affine, SolidityCompatibleField, SolidityCompatibleScheme}; use crate::proof_system::{G1Affine, G2Affine, SolidityCompatibleField, SolidityCompatibleScheme};
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -42,20 +40,11 @@ impl<T: Field> Scheme<T> for PGHR13 {
impl<T: Field> NonUniversalScheme<T> for PGHR13 {} impl<T: Field> NonUniversalScheme<T> for PGHR13 {}
impl<T: SolidityCompatibleField> SolidityCompatibleScheme<T> for PGHR13 { impl<T: SolidityCompatibleField> SolidityCompatibleScheme<T> for PGHR13 {
fn export_solidity_verifier( fn export_solidity_verifier(vk: <PGHR13 as Scheme<T>>::VerificationKey) -> String {
vk: <PGHR13 as Scheme<T>>::VerificationKey, let (mut template_text, solidity_pairing_lib) = (
abi: SolidityAbi, String::from(CONTRACT_TEMPLATE),
) -> String { String::from(SOLIDITY_PAIRING_LIB),
let (mut template_text, solidity_pairing_lib) = match abi { );
SolidityAbi::V1 => (
String::from(CONTRACT_TEMPLATE),
String::from(SOLIDITY_PAIRING_LIB),
),
SolidityAbi::V2 => (
String::from(CONTRACT_TEMPLATE_V2),
String::from(SOLIDITY_PAIRING_LIB_V2),
),
};
// replace things in template // replace things in template
let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap(); let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap();
@ -156,7 +145,7 @@ impl<T: SolidityCompatibleField> SolidityCompatibleScheme<T> for PGHR13 {
} }
} }
const CONTRACT_TEMPLATE_V2: &str = r#"contract Verifier { const CONTRACT_TEMPLATE: &str = r#"contract Verifier {
using Pairing for *; using Pairing for *;
struct VerifyingKey { struct VerifyingKey {
Pairing.G2Point a; Pairing.G2Point a;
@ -228,92 +217,3 @@ const CONTRACT_TEMPLATE_V2: &str = r#"contract Verifier {
} }
} }
"#; "#;
const CONTRACT_TEMPLATE: &str = r#"contract Verifier {
using Pairing for *;
struct VerifyingKey {
Pairing.G2Point a;
Pairing.G1Point b;
Pairing.G2Point c;
Pairing.G2Point gamma;
Pairing.G1Point gamma_beta_1;
Pairing.G2Point gamma_beta_2;
Pairing.G2Point z;
Pairing.G1Point[] ic;
}
struct Proof {
Pairing.G1Point a;
Pairing.G1Point a_p;
Pairing.G2Point b;
Pairing.G1Point b_p;
Pairing.G1Point c;
Pairing.G1Point c_p;
Pairing.G1Point h;
Pairing.G1Point k;
}
function verifyingKey() pure internal returns (VerifyingKey memory vk) {
vk.a = Pairing.G2Point(<%vk_a%>);
vk.b = Pairing.G1Point(<%vk_b%>);
vk.c = Pairing.G2Point(<%vk_c%>);
vk.gamma = Pairing.G2Point(<%vk_g%>);
vk.gamma_beta_1 = Pairing.G1Point(<%vk_gb1%>);
vk.gamma_beta_2 = Pairing.G2Point(<%vk_gb2%>);
vk.z = Pairing.G2Point(<%vk_z%>);
vk.ic = new Pairing.G1Point[](<%vk_ic_length%>);
<%vk_ic_pts%>
}
function verify(uint[] memory input, Proof memory proof) internal view returns (uint) {
uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
VerifyingKey memory vk = verifyingKey();
require(input.length + 1 == vk.ic.length);
// Compute the linear combination vk_x
Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0);
for (uint i = 0; i < input.length; i++) {
require(input[i] < snark_scalar_field);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.ic[i + 1], input[i]));
}
vk_x = Pairing.addition(vk_x, vk.ic[0]);
if (!Pairing.pairingProd2(proof.a, vk.a, Pairing.negate(proof.a_p), Pairing.P2())) return 1;
if (!Pairing.pairingProd2(vk.b, proof.b, Pairing.negate(proof.b_p), Pairing.P2())) return 2;
if (!Pairing.pairingProd2(proof.c, vk.c, Pairing.negate(proof.c_p), Pairing.P2())) return 3;
if (!Pairing.pairingProd3(
proof.k, vk.gamma,
Pairing.negate(Pairing.addition(vk_x, Pairing.addition(proof.a, proof.c))), vk.gamma_beta_2,
Pairing.negate(vk.gamma_beta_1), proof.b
)) return 4;
if (!Pairing.pairingProd3(
Pairing.addition(vk_x, proof.a), proof.b,
Pairing.negate(proof.h), vk.z,
Pairing.negate(proof.c), Pairing.P2()
)) return 5;
return 0;
}
function verifyTx(
uint[2] memory a,
uint[2] memory a_p,
uint[2][2] memory b,
uint[2] memory b_p,
uint[2] memory c,
uint[2] memory c_p,
uint[2] memory h,
uint[2] memory k<%input_argument%>
) public view returns (bool r) {
Proof memory proof;
proof.a = Pairing.G1Point(a[0], a[1]);
proof.a_p = Pairing.G1Point(a_p[0], a_p[1]);
proof.b = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
proof.b_p = Pairing.G1Point(b_p[0], b_p[1]);
proof.c = Pairing.G1Point(c[0], c[1]);
proof.c_p = Pairing.G1Point(c_p[0], c_p[1]);
proof.h = Pairing.G1Point(h[0], h[1]);
proof.k = Pairing.G1Point(k[0], k[1]);
uint[] memory inputValues = new uint[](<%vk_input_length%>);
<%input_loop%>
if (verify(inputValues, proof) == 0) {
return true;
} else {
return false;
}
}
}
"#;

View file

@ -5,22 +5,7 @@ pub trait SolidityCompatibleField: Field {}
impl SolidityCompatibleField for Bn128Field {} impl SolidityCompatibleField for Bn128Field {}
pub trait SolidityCompatibleScheme<T: SolidityCompatibleField>: Scheme<T> { pub trait SolidityCompatibleScheme<T: SolidityCompatibleField>: Scheme<T> {
fn export_solidity_verifier(vk: Self::VerificationKey, abi: SolidityAbi) -> String; fn export_solidity_verifier(vk: Self::VerificationKey) -> String;
}
pub enum SolidityAbi {
V1,
V2,
}
impl SolidityAbi {
pub fn from(v: &str) -> Result<Self, &str> {
match v {
"v1" => Ok(SolidityAbi::V1),
"v2" => Ok(SolidityAbi::V2),
_ => Err("Invalid ABI version"),
}
}
} }
pub const SOLIDITY_G2_ADDITION_LIB: &str = r#"// SPDX-License-Identifier: LGPL-3.0-only pub const SOLIDITY_G2_ADDITION_LIB: &str = r#"// SPDX-License-Identifier: LGPL-3.0-only
@ -421,155 +406,6 @@ library BN256G2 {
} }
"#; "#;
pub const SOLIDITY_PAIRING_LIB_V2: &str = r#"// This file is MIT Licensed.
//
// Copyright 2017 Christian Reitwiessner
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
pragma solidity ^0.8.0;
library Pairing {
struct G1Point {
uint X;
uint Y;
}
// Encoding of field elements is: X[0] * z + X[1]
struct G2Point {
uint[2] X;
uint[2] Y;
}
/// @return the generator of G1
function P1() pure internal returns (G1Point memory) {
return G1Point(1, 2);
}
/// @return the generator of G2
function P2() pure internal returns (G2Point memory) {
return G2Point(
[10857046999023057135944570762232829481370756359578518086990519993285655852781,
11559732032986387107991004021392285783925812861821192530917403151452391805634],
[8495653923123431417604973247489272438418190587263600148770280649306958101930,
4082367875863433681332203403145435568316851327593401208105741076214120093531]
);
}
/// @return the negation of p, i.e. p.addition(p.negate()) should be zero.
function negate(G1Point memory p) pure internal returns (G1Point memory) {
// The prime q in the base field F_q for G1
uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
if (p.X == 0 && p.Y == 0)
return G1Point(0, 0);
return G1Point(p.X, q - (p.Y % q));
}
/// @return r the sum of two points of G1
function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) {
uint[4] memory input;
input[0] = p1.X;
input[1] = p1.Y;
input[2] = p2.X;
input[3] = p2.Y;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60)
// Use "invalid" to make gas estimation work
switch success case 0 { invalid() }
}
require(success);
}
/// @return r the sum of two points of G2
function addition(G2Point memory p1, G2Point memory p2) internal view returns (G2Point memory r) {
(r.X[0], r.X[1], r.Y[0], r.Y[1]) = BN256G2.ECTwistAdd(p1.X[0],p1.X[1],p1.Y[0],p1.Y[1],p2.X[0],p2.X[1],p2.Y[0],p2.Y[1]);
}
/// @return r the product of a point on G1 and a scalar, i.e.
/// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p.
function scalar_mul(G1Point memory p, uint s) internal view returns (G1Point memory r) {
uint[3] memory input;
input[0] = p.X;
input[1] = p.Y;
input[2] = s;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60)
// Use "invalid" to make gas estimation work
switch success case 0 { invalid() }
}
require (success);
}
/// @return the result of computing the pairing check
/// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1
/// For example pairing([P1(), P1().negate()], [P2(), P2()]) should
/// return true.
function pairing(G1Point[] memory p1, G2Point[] memory p2) internal view returns (bool) {
require(p1.length == p2.length);
uint elements = p1.length;
uint inputSize = elements * 6;
uint[] memory input = new uint[](inputSize);
for (uint i = 0; i < elements; i++)
{
input[i * 6 + 0] = p1[i].X;
input[i * 6 + 1] = p1[i].Y;
input[i * 6 + 2] = p2[i].X[1];
input[i * 6 + 3] = p2[i].X[0];
input[i * 6 + 4] = p2[i].Y[1];
input[i * 6 + 5] = p2[i].Y[0];
}
uint[1] memory out;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20)
// Use "invalid" to make gas estimation work
switch success case 0 { invalid() }
}
require(success);
return out[0] != 0;
}
/// Convenience method for a pairing check for two pairs.
function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](2);
G2Point[] memory p2 = new G2Point[](2);
p1[0] = a1;
p1[1] = b1;
p2[0] = a2;
p2[1] = b2;
return pairing(p1, p2);
}
/// Convenience method for a pairing check for three pairs.
function pairingProd3(
G1Point memory a1, G2Point memory a2,
G1Point memory b1, G2Point memory b2,
G1Point memory c1, G2Point memory c2
) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](3);
G2Point[] memory p2 = new G2Point[](3);
p1[0] = a1;
p1[1] = b1;
p1[2] = c1;
p2[0] = a2;
p2[1] = b2;
p2[2] = c2;
return pairing(p1, p2);
}
/// Convenience method for a pairing check for four pairs.
function pairingProd4(
G1Point memory a1, G2Point memory a2,
G1Point memory b1, G2Point memory b2,
G1Point memory c1, G2Point memory c2,
G1Point memory d1, G2Point memory d2
) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](4);
G2Point[] memory p2 = new G2Point[](4);
p1[0] = a1;
p1[1] = b1;
p1[2] = c1;
p1[3] = d1;
p2[0] = a2;
p2[1] = b2;
p2[2] = c2;
p2[3] = d2;
return pairing(p1, p2);
}
}
"#;
pub const SOLIDITY_PAIRING_LIB: &str = r#"// This file is MIT Licensed. pub const SOLIDITY_PAIRING_LIB: &str = r#"// This file is MIT Licensed.
// //
// Copyright 2017 Christian Reitwiessner // Copyright 2017 Christian Reitwiessner

View file

@ -13,7 +13,7 @@ use zokrates_core::ir;
use zokrates_core::proof_system::bellman::Bellman; use zokrates_core::proof_system::bellman::Bellman;
use zokrates_core::proof_system::groth16::G16; use zokrates_core::proof_system::groth16::G16;
use zokrates_core::proof_system::{ use zokrates_core::proof_system::{
Backend, NonUniversalBackend, Proof, Scheme, SolidityAbi, SolidityCompatibleScheme, Backend, NonUniversalBackend, Proof, Scheme, SolidityCompatibleScheme,
}; };
use zokrates_core::typed_absy::abi::Abi; use zokrates_core::typed_absy::abi::Abi;
use zokrates_core::typed_absy::types::ConcreteSignature as Signature; use zokrates_core::typed_absy::types::ConcreteSignature as Signature;
@ -168,13 +168,9 @@ pub fn setup(program: &[u8]) -> Result<JsValue, JsValue> {
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn export_solidity_verifier(vk: JsValue, abi_version: JsValue) -> Result<JsValue, JsValue> { pub fn export_solidity_verifier(vk: JsValue) -> Result<JsValue, JsValue> {
let abi_version = SolidityAbi::from(abi_version.as_string().unwrap().as_str())
.map_err(|err| JsValue::from_str(err))?;
let verifier = <G16 as SolidityCompatibleScheme<Bn128Field>>::export_solidity_verifier( let verifier = <G16 as SolidityCompatibleScheme<Bn128Field>>::export_solidity_verifier(
vk.into_serde().unwrap(), vk.into_serde().unwrap(),
abi_version,
); );
Ok(JsValue::from_str(verifier.as_str())) Ok(JsValue::from_str(verifier.as_str()))

View file

@ -104,7 +104,7 @@ describe('tests', function() {
const artifacts = this.zokrates.compile(code); const artifacts = this.zokrates.compile(code);
const keypair = this.zokrates.setup(artifacts.program); const keypair = this.zokrates.setup(artifacts.program);
const verifier = this.zokrates.exportSolidityVerifier(keypair.vk, "v1"); const verifier = this.zokrates.exportSolidityVerifier(keypair.vk);
assert.ok(verifier.length > 0); assert.ok(verifier.length > 0);
}); });
}); });