Merge pull request #414 from petscheit/app_testing
Adds testing of contracts to testing pipeline
This commit is contained in:
commit
92234ae000
21 changed files with 3770 additions and 421 deletions
|
@ -83,6 +83,8 @@ jobs:
|
|||
integration_test:
|
||||
docker:
|
||||
- image: rustlang/rust:nightly-slim
|
||||
- image: trufflesuite/ganache-cli:next
|
||||
port: 8545:8545
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
|
|
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2221,6 +2221,7 @@ dependencies = [
|
|||
"assert_cli 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bincode 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
apt-get update -y
|
||||
apt-get install -y curl gnupg sudo build-essential
|
||||
apt-get install -y curl gnupg sudo build-essential git
|
||||
curl -sL https://deb.nodesource.com/setup_11.x | bash -
|
||||
apt-get install -y nodejs
|
||||
npm i -g solc
|
|
@ -25,6 +25,9 @@ glob = "0.2.11"
|
|||
assert_cli = "0.5"
|
||||
tempdir = "0.3"
|
||||
|
||||
[build-dependencies]
|
||||
fs_extra = "1.1.0"
|
||||
|
||||
[[bin]]
|
||||
name = "zokrates"
|
||||
path = "src/bin.rs"
|
||||
|
|
15
zokrates_cli/build.rs
Normal file
15
zokrates_cli/build.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use fs_extra::copy_items;
|
||||
use fs_extra::dir::CopyOptions;
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
// export js test folder to OUT_DIR
|
||||
export_stdlib();
|
||||
}
|
||||
|
||||
fn export_stdlib() {
|
||||
let out_dir = env::var("OUT_DIR").unwrap();
|
||||
let mut options = CopyOptions::new();
|
||||
options.overwrite = true;
|
||||
copy_items(&vec!["tests/contract"], out_dir, &options).unwrap();
|
||||
}
|
|
@ -45,6 +45,7 @@ fn cli() -> Result<(), String> {
|
|||
const WITNESS_DEFAULT_PATH: &str = "witness";
|
||||
const JSON_PROOF_PATH: &str = "proof.json";
|
||||
let default_scheme = env::var("ZOKRATES_PROVING_SCHEME").unwrap_or(String::from("g16"));
|
||||
let default_solidity_abi = "v1";
|
||||
|
||||
// cli specification using clap library
|
||||
let matches = App::new("ZoKrates")
|
||||
|
@ -145,6 +146,14 @@ fn cli() -> Result<(), String> {
|
|||
.takes_value(true)
|
||||
.required(false)
|
||||
.default_value(&default_scheme)
|
||||
).arg(Arg::with_name("abi")
|
||||
.short("a")
|
||||
.long("abi")
|
||||
.help("Flag for setting the version of the ABI Encoder used in the contract. Default is v1.")
|
||||
.takes_value(true)
|
||||
.possible_values(&["v1", "v2"])
|
||||
.default_value(&default_solidity_abi)
|
||||
.required(false)
|
||||
)
|
||||
)
|
||||
.subcommand(SubCommand::with_name("compute-witness")
|
||||
|
@ -238,7 +247,7 @@ fn cli() -> Result<(), String> {
|
|||
.value_name("FORMAT")
|
||||
.help("Format in which the proof should be printed. [remix, json]")
|
||||
.takes_value(true)
|
||||
.possible_values(&["remix", "json", "testingV1", "testingV2"])
|
||||
.possible_values(&["remix", "json"])
|
||||
.required(true)
|
||||
)
|
||||
)
|
||||
|
@ -416,7 +425,7 @@ fn cli() -> Result<(), String> {
|
|||
("export-verifier", Some(sub_matches)) => {
|
||||
{
|
||||
let scheme = get_scheme(sub_matches.value_of("proving-scheme").unwrap())?;
|
||||
|
||||
let is_abiv2 = sub_matches.value_of("abi").unwrap() == "v2";
|
||||
println!("Exporting verifier...");
|
||||
|
||||
// read vk file
|
||||
|
@ -425,7 +434,7 @@ fn cli() -> Result<(), String> {
|
|||
.map_err(|why| format!("couldn't open {}: {}", input_path.display(), why))?;
|
||||
let reader = BufReader::new(input_file);
|
||||
|
||||
let verifier = scheme.export_solidity_verifier(reader);
|
||||
let verifier = scheme.export_solidity_verifier(reader, is_abiv2);
|
||||
|
||||
//write output file
|
||||
let output_path = Path::new(sub_matches.value_of("output").unwrap());
|
||||
|
@ -506,20 +515,6 @@ fn cli() -> Result<(), String> {
|
|||
println!();
|
||||
println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||
}
|
||||
"testingV1" => {
|
||||
//used by testing pipeline to generate arguments for contract call
|
||||
for (_, value) in proof_object["proof"].as_object().unwrap().iter() {
|
||||
print!("{}", value);
|
||||
print!(",");
|
||||
}
|
||||
println!("{}", proof_object["inputs"]);
|
||||
}
|
||||
"testingV2" => {
|
||||
//used by testing pipeline to generate arguments for contract call
|
||||
print!("{}", proof_object["proof"]);
|
||||
print!(",");
|
||||
println!("{}", proof_object["inputs"]);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
def main(field[3] a, field b, private field[4] c) -> (field, field[3], field[4]):
|
||||
def main(private field[3] a, private field b, private field[4] c) -> (field, field[3], field[4]):
|
||||
return b, a, c
|
|
@ -1 +1 @@
|
|||
[42]
|
||||
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1]
|
|
@ -1,9 +1,11 @@
|
|||
import "BELLMAN/sha256round" as sha256
|
||||
|
||||
def main(field unused) -> (field[256]):
|
||||
def main(private field[256] expected) -> (field):
|
||||
|
||||
field[256] a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
field[256] b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1]
|
||||
field[256] IV = [0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1]
|
||||
|
||||
return sha256([a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15], a[16], a[17], a[18], a[19], a[20], a[21], a[22], a[23], a[24], a[25], a[26], a[27], a[28], a[29], a[30], a[31], a[32], a[33], a[34], a[35], a[36], a[37], a[38], a[39], a[40], a[41], a[42], a[43], a[44], a[45], a[46], a[47], a[48], a[49], a[50], a[51], a[52], a[53], a[54], a[55], a[56], a[57], a[58], a[59], a[60], a[61], a[62], a[63], a[64], a[65], a[66], a[67], a[68], a[69], a[70], a[71], a[72], a[73], a[74], a[75], a[76], a[77], a[78], a[79], a[80], a[81], a[82], a[83], a[84], a[85], a[86], a[87], a[88], a[89], a[90], a[91], a[92], a[93], a[94], a[95], a[96], a[97], a[98], a[99], a[100], a[101], a[102], a[103], a[104], a[105], a[106], a[107], a[108], a[109], a[110], a[111], a[112], a[113], a[114], a[115], a[116], a[117], a[118], a[119], a[120], a[121], a[122], a[123], a[124], a[125], a[126], a[127], a[128], a[129], a[130], a[131], a[132], a[133], a[134], a[135], a[136], a[137], a[138], a[139], a[140], a[141], a[142], a[143], a[144], a[145], a[146], a[147], a[148], a[149], a[150], a[151], a[152], a[153], a[154], a[155], a[156], a[157], a[158], a[159], a[160], a[161], a[162], a[163], a[164], a[165], a[166], a[167], a[168], a[169], a[170], a[171], a[172], a[173], a[174], a[175], a[176], a[177], a[178], a[179], a[180], a[181], a[182], a[183], a[184], a[185], a[186], a[187], a[188], a[189], a[190], a[191], a[192], a[193], a[194], a[195], a[196], a[197], a[198], a[199], a[200], a[201], a[202], a[203], a[204], a[205], a[206], a[207], a[208], a[209], a[210], a[211], a[212], a[213], a[214], a[215], a[216], a[217], a[218], a[219], a[220], a[221], a[222], a[223], a[224], a[225], a[226], a[227], a[228], a[229], a[230], a[231], a[232], a[233], a[234], a[235], a[236], a[237], a[238], a[239], a[240], a[241], a[242], a[243], a[244], a[245], a[246], a[247], a[248], a[249], a[250], a[251], a[252], a[253], a[254], a[255], b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15], b[16], b[17], b[18], b[19], b[20], b[21], b[22], b[23], b[24], b[25], b[26], b[27], b[28], b[29], b[30], b[31], b[32], b[33], b[34], b[35], b[36], b[37], b[38], b[39], b[40], b[41], b[42], b[43], b[44], b[45], b[46], b[47], b[48], b[49], b[50], b[51], b[52], b[53], b[54], b[55], b[56], b[57], b[58], b[59], b[60], b[61], b[62], b[63], b[64], b[65], b[66], b[67], b[68], b[69], b[70], b[71], b[72], b[73], b[74], b[75], b[76], b[77], b[78], b[79], b[80], b[81], b[82], b[83], b[84], b[85], b[86], b[87], b[88], b[89], b[90], b[91], b[92], b[93], b[94], b[95], b[96], b[97], b[98], b[99], b[100], b[101], b[102], b[103], b[104], b[105], b[106], b[107], b[108], b[109], b[110], b[111], b[112], b[113], b[114], b[115], b[116], b[117], b[118], b[119], b[120], b[121], b[122], b[123], b[124], b[125], b[126], b[127], b[128], b[129], b[130], b[131], b[132], b[133], b[134], b[135], b[136], b[137], b[138], b[139], b[140], b[141], b[142], b[143], b[144], b[145], b[146], b[147], b[148], b[149], b[150], b[151], b[152], b[153], b[154], b[155], b[156], b[157], b[158], b[159], b[160], b[161], b[162], b[163], b[164], b[165], b[166], b[167], b[168], b[169], b[170], b[171], b[172], b[173], b[174], b[175], b[176], b[177], b[178], b[179], b[180], b[181], b[182], b[183], b[184], b[185], b[186], b[187], b[188], b[189], b[190], b[191], b[192], b[193], b[194], b[195], b[196], b[197], b[198], b[199], b[200], b[201], b[202], b[203], b[204], b[205], b[206], b[207], b[208], b[209], b[210], b[211], b[212], b[213], b[214], b[215], b[216], b[217], b[218], b[219], b[220], b[221], b[222], b[223], b[224], b[225], b[226], b[227], b[228], b[229], b[230], b[231], b[232], b[233], b[234], b[235], b[236], b[237], b[238], b[239], b[240], b[241], b[242], b[243], b[244], b[245], b[246], b[247], b[248], b[249], b[250], b[251], b[252], b[253], b[254], b[255]], IV)
|
||||
expected == sha256([a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15], a[16], a[17], a[18], a[19], a[20], a[21], a[22], a[23], a[24], a[25], a[26], a[27], a[28], a[29], a[30], a[31], a[32], a[33], a[34], a[35], a[36], a[37], a[38], a[39], a[40], a[41], a[42], a[43], a[44], a[45], a[46], a[47], a[48], a[49], a[50], a[51], a[52], a[53], a[54], a[55], a[56], a[57], a[58], a[59], a[60], a[61], a[62], a[63], a[64], a[65], a[66], a[67], a[68], a[69], a[70], a[71], a[72], a[73], a[74], a[75], a[76], a[77], a[78], a[79], a[80], a[81], a[82], a[83], a[84], a[85], a[86], a[87], a[88], a[89], a[90], a[91], a[92], a[93], a[94], a[95], a[96], a[97], a[98], a[99], a[100], a[101], a[102], a[103], a[104], a[105], a[106], a[107], a[108], a[109], a[110], a[111], a[112], a[113], a[114], a[115], a[116], a[117], a[118], a[119], a[120], a[121], a[122], a[123], a[124], a[125], a[126], a[127], a[128], a[129], a[130], a[131], a[132], a[133], a[134], a[135], a[136], a[137], a[138], a[139], a[140], a[141], a[142], a[143], a[144], a[145], a[146], a[147], a[148], a[149], a[150], a[151], a[152], a[153], a[154], a[155], a[156], a[157], a[158], a[159], a[160], a[161], a[162], a[163], a[164], a[165], a[166], a[167], a[168], a[169], a[170], a[171], a[172], a[173], a[174], a[175], a[176], a[177], a[178], a[179], a[180], a[181], a[182], a[183], a[184], a[185], a[186], a[187], a[188], a[189], a[190], a[191], a[192], a[193], a[194], a[195], a[196], a[197], a[198], a[199], a[200], a[201], a[202], a[203], a[204], a[205], a[206], a[207], a[208], a[209], a[210], a[211], a[212], a[213], a[214], a[215], a[216], a[217], a[218], a[219], a[220], a[221], a[222], a[223], a[224], a[225], a[226], a[227], a[228], a[229], a[230], a[231], a[232], a[233], a[234], a[235], a[236], a[237], a[238], a[239], a[240], a[241], a[242], a[243], a[244], a[245], a[246], a[247], a[248], a[249], a[250], a[251], a[252], a[253], a[254], a[255], b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15], b[16], b[17], b[18], b[19], b[20], b[21], b[22], b[23], b[24], b[25], b[26], b[27], b[28], b[29], b[30], b[31], b[32], b[33], b[34], b[35], b[36], b[37], b[38], b[39], b[40], b[41], b[42], b[43], b[44], b[45], b[46], b[47], b[48], b[49], b[50], b[51], b[52], b[53], b[54], b[55], b[56], b[57], b[58], b[59], b[60], b[61], b[62], b[63], b[64], b[65], b[66], b[67], b[68], b[69], b[70], b[71], b[72], b[73], b[74], b[75], b[76], b[77], b[78], b[79], b[80], b[81], b[82], b[83], b[84], b[85], b[86], b[87], b[88], b[89], b[90], b[91], b[92], b[93], b[94], b[95], b[96], b[97], b[98], b[99], b[100], b[101], b[102], b[103], b[104], b[105], b[106], b[107], b[108], b[109], b[110], b[111], b[112], b[113], b[114], b[115], b[116], b[117], b[118], b[119], b[120], b[121], b[122], b[123], b[124], b[125], b[126], b[127], b[128], b[129], b[130], b[131], b[132], b[133], b[134], b[135], b[136], b[137], b[138], b[139], b[140], b[141], b[142], b[143], b[144], b[145], b[146], b[147], b[148], b[149], b[150], b[151], b[152], b[153], b[154], b[155], b[156], b[157], b[158], b[159], b[160], b[161], b[162], b[163], b[164], b[165], b[166], b[167], b[168], b[169], b[170], b[171], b[172], b[173], b[174], b[175], b[176], b[177], b[178], b[179], b[180], b[181], b[182], b[183], b[184], b[185], b[186], b[187], b[188], b[189], b[190], b[191], b[192], b[193], b[194], b[195], b[196], b[197], b[198], b[199], b[200], b[201], b[202], b[203], b[204], b[205], b[206], b[207], b[208], b[209], b[210], b[211], b[212], b[213], b[214], b[215], b[216], b[217], b[218], b[219], b[220], b[221], b[222], b[223], b[224], b[225], b[226], b[227], b[228], b[229], b[230], b[231], b[232], b[233], b[234], b[235], b[236], b[237], b[238], b[239], b[240], b[241], b[242], b[243], b[244], b[245], b[246], b[247], b[248], b[249], b[250], b[251], b[252], b[253], b[254], b[255]], IV)
|
||||
|
||||
return 1
|
|
@ -1,256 +1 @@
|
|||
~out_255 1
|
||||
~out_254 0
|
||||
~out_253 1
|
||||
~out_252 1
|
||||
~out_251 0
|
||||
~out_250 1
|
||||
~out_249 0
|
||||
~out_248 0
|
||||
~out_247 1
|
||||
~out_246 0
|
||||
~out_245 1
|
||||
~out_244 1
|
||||
~out_243 1
|
||||
~out_242 1
|
||||
~out_241 0
|
||||
~out_240 0
|
||||
~out_239 0
|
||||
~out_238 0
|
||||
~out_237 1
|
||||
~out_236 1
|
||||
~out_235 0
|
||||
~out_234 0
|
||||
~out_233 1
|
||||
~out_232 1
|
||||
~out_231 0
|
||||
~out_230 1
|
||||
~out_229 0
|
||||
~out_228 1
|
||||
~out_227 0
|
||||
~out_226 1
|
||||
~out_225 1
|
||||
~out_224 1
|
||||
~out_223 1
|
||||
~out_222 1
|
||||
~out_221 0
|
||||
~out_220 1
|
||||
~out_219 1
|
||||
~out_218 1
|
||||
~out_217 1
|
||||
~out_216 0
|
||||
~out_215 0
|
||||
~out_214 0
|
||||
~out_213 0
|
||||
~out_212 0
|
||||
~out_211 0
|
||||
~out_210 1
|
||||
~out_209 0
|
||||
~out_208 1
|
||||
~out_207 1
|
||||
~out_206 1
|
||||
~out_205 0
|
||||
~out_204 0
|
||||
~out_203 1
|
||||
~out_202 0
|
||||
~out_201 0
|
||||
~out_200 0
|
||||
~out_199 1
|
||||
~out_198 1
|
||||
~out_197 1
|
||||
~out_196 1
|
||||
~out_195 0
|
||||
~out_194 1
|
||||
~out_193 1
|
||||
~out_192 1
|
||||
~out_191 1
|
||||
~out_190 0
|
||||
~out_189 1
|
||||
~out_188 0
|
||||
~out_187 1
|
||||
~out_186 1
|
||||
~out_185 0
|
||||
~out_184 0
|
||||
~out_183 1
|
||||
~out_182 1
|
||||
~out_181 1
|
||||
~out_180 1
|
||||
~out_179 0
|
||||
~out_178 0
|
||||
~out_177 0
|
||||
~out_176 1
|
||||
~out_175 1
|
||||
~out_174 1
|
||||
~out_173 0
|
||||
~out_172 0
|
||||
~out_171 1
|
||||
~out_170 1
|
||||
~out_169 1
|
||||
~out_168 0
|
||||
~out_167 0
|
||||
~out_166 1
|
||||
~out_165 1
|
||||
~out_164 1
|
||||
~out_163 0
|
||||
~out_162 0
|
||||
~out_161 1
|
||||
~out_160 0
|
||||
~out_159 1
|
||||
~out_158 0
|
||||
~out_157 0
|
||||
~out_156 0
|
||||
~out_155 0
|
||||
~out_154 1
|
||||
~out_153 1
|
||||
~out_152 0
|
||||
~out_151 0
|
||||
~out_150 1
|
||||
~out_149 0
|
||||
~out_148 0
|
||||
~out_147 0
|
||||
~out_146 1
|
||||
~out_145 1
|
||||
~out_144 0
|
||||
~out_143 1
|
||||
~out_142 0
|
||||
~out_141 1
|
||||
~out_140 0
|
||||
~out_139 1
|
||||
~out_138 0
|
||||
~out_137 1
|
||||
~out_136 0
|
||||
~out_135 0
|
||||
~out_134 0
|
||||
~out_133 0
|
||||
~out_132 0
|
||||
~out_131 1
|
||||
~out_130 0
|
||||
~out_129 0
|
||||
~out_128 0
|
||||
~out_127 0
|
||||
~out_126 1
|
||||
~out_125 1
|
||||
~out_124 0
|
||||
~out_123 1
|
||||
~out_122 1
|
||||
~out_121 0
|
||||
~out_120 1
|
||||
~out_119 0
|
||||
~out_118 0
|
||||
~out_117 1
|
||||
~out_116 0
|
||||
~out_115 1
|
||||
~out_114 0
|
||||
~out_113 0
|
||||
~out_112 0
|
||||
~out_111 0
|
||||
~out_110 0
|
||||
~out_109 1
|
||||
~out_108 0
|
||||
~out_107 1
|
||||
~out_106 0
|
||||
~out_105 0
|
||||
~out_104 0
|
||||
~out_103 0
|
||||
~out_102 0
|
||||
~out_101 1
|
||||
~out_100 1
|
||||
~out_99 0
|
||||
~out_98 1
|
||||
~out_97 0
|
||||
~out_96 0
|
||||
~out_95 0
|
||||
~out_94 1
|
||||
~out_93 0
|
||||
~out_92 0
|
||||
~out_91 1
|
||||
~out_90 1
|
||||
~out_89 0
|
||||
~out_88 0
|
||||
~out_87 1
|
||||
~out_86 1
|
||||
~out_85 0
|
||||
~out_84 0
|
||||
~out_83 0
|
||||
~out_82 1
|
||||
~out_81 1
|
||||
~out_80 1
|
||||
~out_79 0
|
||||
~out_78 1
|
||||
~out_77 1
|
||||
~out_76 1
|
||||
~out_75 0
|
||||
~out_74 0
|
||||
~out_73 0
|
||||
~out_72 1
|
||||
~out_71 0
|
||||
~out_70 0
|
||||
~out_69 1
|
||||
~out_68 1
|
||||
~out_67 1
|
||||
~out_66 1
|
||||
~out_65 0
|
||||
~out_64 0
|
||||
~out_63 0
|
||||
~out_62 0
|
||||
~out_61 1
|
||||
~out_60 0
|
||||
~out_59 1
|
||||
~out_58 0
|
||||
~out_57 0
|
||||
~out_56 1
|
||||
~out_55 0
|
||||
~out_54 0
|
||||
~out_53 0
|
||||
~out_52 1
|
||||
~out_51 1
|
||||
~out_50 1
|
||||
~out_49 0
|
||||
~out_48 1
|
||||
~out_47 0
|
||||
~out_46 1
|
||||
~out_45 1
|
||||
~out_44 1
|
||||
~out_43 1
|
||||
~out_42 0
|
||||
~out_41 0
|
||||
~out_40 0
|
||||
~out_39 0
|
||||
~out_38 1
|
||||
~out_37 0
|
||||
~out_36 0
|
||||
~out_35 0
|
||||
~out_34 0
|
||||
~out_33 1
|
||||
~out_32 1
|
||||
~out_31 0
|
||||
~out_30 0
|
||||
~out_29 0
|
||||
~out_28 1
|
||||
~out_27 1
|
||||
~out_26 0
|
||||
~out_25 0
|
||||
~out_24 1
|
||||
~out_23 1
|
||||
~out_22 1
|
||||
~out_21 0
|
||||
~out_20 1
|
||||
~out_19 0
|
||||
~out_18 0
|
||||
~out_17 0
|
||||
~out_16 1
|
||||
~out_15 1
|
||||
~out_14 1
|
||||
~out_13 0
|
||||
~out_12 1
|
||||
~out_11 1
|
||||
~out_10 1
|
||||
~out_9 0
|
||||
~out_8 0
|
||||
~out_7 1
|
||||
~out_6 1
|
||||
~out_5 1
|
||||
~out_4 1
|
||||
~out_3 1
|
||||
~out_2 0
|
||||
~out_1 0
|
||||
~out_0 0
|
||||
~out_0 1
|
2988
zokrates_cli/tests/contract/npm-shrinkwrap.json
generated
Normal file
2988
zokrates_cli/tests/contract/npm-shrinkwrap.json
generated
Normal file
File diff suppressed because it is too large
Load diff
15
zokrates_cli/tests/contract/package.json
Normal file
15
zokrates_cli/tests/contract/package.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "zokrates-solidity-tester",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "test.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Paul Etscheit",
|
||||
"license": "LGPL-3.0-only",
|
||||
"dependencies": {
|
||||
"solc": "^0.5.9",
|
||||
"web3": "^1.0.0-beta.37"
|
||||
}
|
||||
}
|
188
zokrates_cli/tests/contract/test.js
Normal file
188
zokrates_cli/tests/contract/test.js
Normal file
|
@ -0,0 +1,188 @@
|
|||
var fs = require("fs");
|
||||
let Web3 = require('web3');
|
||||
const solc = require('solc');
|
||||
const contractPath = process.argv[2]
|
||||
const proofPath = process.argv[3]
|
||||
const format = process.argv[4]
|
||||
const abiVersion = process.argv[5];
|
||||
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
|
||||
|
||||
// -----Compile contract-----
|
||||
const source = fs.readFileSync(contractPath, 'UTF-8');
|
||||
let jsonContractSource = JSON.stringify({
|
||||
language: 'Solidity',
|
||||
sources: {
|
||||
[contractPath]: {
|
||||
content: source,
|
||||
},
|
||||
},
|
||||
settings: {
|
||||
outputSelection: {
|
||||
'*': {
|
||||
'*': ['abi', "evm.bytecode"],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
let jsonInterface = JSON.parse(solc.compile(jsonContractSource));
|
||||
(async () => {
|
||||
const accounts = await web3.eth.getAccounts();
|
||||
let abi = jsonInterface.contracts[contractPath]["Verifier"].abi
|
||||
let bytecode = jsonInterface.contracts[contractPath]["Verifier"].evm.bytecode
|
||||
|
||||
//There is a solc issue, that for unknown reasons wont link the BN256G2 Library automatically for gm17 v1 and v2 contracts. I dont know why this is happening,
|
||||
//the contracts compile and deploy without any issue on remix. To fix this, the the BN256G2 Library must be compiled and deployed by itself, after that,
|
||||
//the library placeholder must be replaced with the library address in the contracts bytecode
|
||||
if (format == "gm17") {
|
||||
let library = await deployLibrary();
|
||||
//replace lib placeholder with lib address in bytecode
|
||||
bytecode.object = bytecode.object.replace(/\_\_\$[a-f0-9]{34}\$\_\_/g, library["_address"].replace("0x", ""))
|
||||
}
|
||||
|
||||
let contract = new web3.eth.Contract(abi)
|
||||
.deploy({
|
||||
data: '0x' + bytecode.object
|
||||
})
|
||||
.send({
|
||||
from: accounts[0],
|
||||
gas: '2000000'
|
||||
})
|
||||
.on('receipt', (tx) => {
|
||||
if (tx.status == true) {
|
||||
console.log("Contract Deployed! Gas used: " + tx.gasUsed)
|
||||
}
|
||||
})
|
||||
.then(newContractInstance => {
|
||||
contract = newContractInstance;
|
||||
Promise.all([makeTransaction(accounts[0], true), makeTransaction(accounts[0], false)])
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
process.exit(1);
|
||||
})
|
||||
|
||||
function makeTransaction(account, correct) {
|
||||
let proof = getProof();
|
||||
|
||||
function handleReceipt(tx) {
|
||||
if (tx.status == true && !correct) {
|
||||
console.log("Verification has been successful with invalid proof data! THIS IS A BUG")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
if (tx.status == true) {
|
||||
console.log("Correct proof works! Gas used: " + tx.gasUsed)
|
||||
}
|
||||
}
|
||||
|
||||
function handleError(err, correct) {
|
||||
if (!correct) {
|
||||
console.log("False proof not verified! Success")
|
||||
} else {
|
||||
console.log(err);
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
if (abiVersion == "v1") {
|
||||
if (format == "g16" || format == "gm17") {
|
||||
return verifyTxG16_GM17_ABIV1(proof, account, correct).on('receipt', handleReceipt)
|
||||
.catch(handleError);
|
||||
} else if (format == "pghr13") {
|
||||
return verifyTxPGHR13_ABIV1(proof, account, correct).on('receipt', handleReceipt)
|
||||
.catch(handleError);
|
||||
}
|
||||
} else {
|
||||
return verifyTxABIV2(proof, account, correct).on('receipt', handleReceipt)
|
||||
.catch(handleError);
|
||||
}
|
||||
}
|
||||
|
||||
function verifyTxABIV2(proof, account, correct) {
|
||||
contract.methods.verifyTx(proof[0], proof[1]).send({
|
||||
from: account,
|
||||
gas: 5000000
|
||||
})
|
||||
}
|
||||
|
||||
function verifyTxG16_GM17_ABIV1(proof, account, correct) {
|
||||
return contract.methods.verifyTx(
|
||||
proof[0][0],
|
||||
proof[0][1],
|
||||
proof[0][2],
|
||||
proof[1]
|
||||
).send({
|
||||
from: account,
|
||||
gas: 5000000
|
||||
})
|
||||
}
|
||||
|
||||
function verifyTxPGHR13_ABIV1(proof, account, correct) {
|
||||
return contract.methods.verifyTx(
|
||||
proof[0][0],
|
||||
proof[0][1],
|
||||
proof[0][2],
|
||||
proof[0][3],
|
||||
proof[0][4],
|
||||
proof[0][5],
|
||||
proof[0][6],
|
||||
proof[0][7],
|
||||
proof[1]
|
||||
).send({
|
||||
from: account,
|
||||
gas: 5000000
|
||||
})
|
||||
}
|
||||
|
||||
function getProof(correct) {
|
||||
let json = JSON.parse(fs.readFileSync(proofPath));
|
||||
let inputs = json["inputs"];
|
||||
let proof = json["proof"]
|
||||
//falsifies proof to check if verification fails
|
||||
if (!correct) {
|
||||
proof["a"][0] = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
}
|
||||
if (abiVersion == "v1") {
|
||||
return [Object.values(proof), Object.values(inputs)];
|
||||
} else if (abiVersion == "v2") {
|
||||
return [proof, inputs]
|
||||
}
|
||||
}
|
||||
|
||||
//function used for deploying BN256G2 Library, used for gm17 only
|
||||
function deployLibrary() {
|
||||
let jsonContractSourceBin = JSON.stringify({
|
||||
language: 'Solidity',
|
||||
sources: {
|
||||
["BN256G2"]: {
|
||||
content: source,
|
||||
},
|
||||
},
|
||||
settings: {
|
||||
outputSelection: {
|
||||
'*': {
|
||||
'*': ['abi', "evm.bytecode"],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
let jsonInterfaceBin = JSON.parse(solc.compile(jsonContractSourceBin));
|
||||
let abiLib = jsonInterfaceBin.contracts["BN256G2"]["BN256G2"].abi
|
||||
let bytecodeLib = jsonInterfaceBin.contracts["BN256G2"]['BN256G2'].evm.bytecode
|
||||
return new web3.eth.Contract(abiLib)
|
||||
.deploy({
|
||||
data: '0x' + bytecodeLib.object
|
||||
})
|
||||
.send({
|
||||
from: accounts[0],
|
||||
gas: '2000000'
|
||||
})
|
||||
.on('receipt', (tx) => {
|
||||
if (tx.status == false) {
|
||||
console.log("Library couldn't be deployed");
|
||||
process.exit(1);
|
||||
}
|
||||
})
|
||||
}
|
||||
})();
|
|
@ -16,6 +16,9 @@ mod integration {
|
|||
#[test]
|
||||
#[ignore]
|
||||
fn test_compile_and_witness_dir() {
|
||||
// install nodejs dependencies for the verification contract tester
|
||||
install_nodejs_deps();
|
||||
|
||||
let dir = Path::new("./tests/code");
|
||||
assert!(dir.is_dir());
|
||||
for entry in fs::read_dir(dir).unwrap() {
|
||||
|
@ -32,6 +35,15 @@ mod integration {
|
|||
}
|
||||
}
|
||||
|
||||
fn install_nodejs_deps() {
|
||||
let out_dir = concat!(env!("OUT_DIR"), "/contract");
|
||||
|
||||
assert_cli::Assert::command(&["npm", "install"])
|
||||
.current_dir(out_dir)
|
||||
.succeeds()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn test_compile_and_witness(
|
||||
program_name: &str,
|
||||
program_path: &Path,
|
||||
|
@ -44,6 +56,7 @@ mod integration {
|
|||
let flattened_path = tmp_base.join(program_name).join("out");
|
||||
let witness_path = tmp_base.join(program_name).join("witness");
|
||||
let inline_witness_path = tmp_base.join(program_name).join("inline_witness");
|
||||
let proof_path = tmp_base.join(program_name).join("proof.json");
|
||||
let verification_key_path = tmp_base
|
||||
.join(program_name)
|
||||
.join("verification")
|
||||
|
@ -193,23 +206,6 @@ mod integration {
|
|||
.succeeds()
|
||||
.unwrap();
|
||||
|
||||
let mut verifier_file = File::open(&verification_contract_path).unwrap();
|
||||
let mut verifier_string = String::new();
|
||||
verifier_file.read_to_string(&mut verifier_string).unwrap();
|
||||
|
||||
let solc_json_input = format!(
|
||||
r#"{{"language": "Solidity", "sources": {{ "this": {{ "content": {:?} }} }} }}"#,
|
||||
verifier_string
|
||||
);
|
||||
|
||||
assert_cli::Assert::command(&["solcjs", "--standard-json"])
|
||||
.stdin(&solc_json_input)
|
||||
.succeeds()
|
||||
.stdout()
|
||||
.doesnt_contain(r#""severity":"error""#)
|
||||
.execute()
|
||||
.expect("solcjs not installed or not in scope.");
|
||||
|
||||
// GENERATE-PROOF
|
||||
assert_cli::Assert::command(&[
|
||||
"../target/release/zokrates",
|
||||
|
@ -222,9 +218,25 @@ mod integration {
|
|||
proving_key_path.to_str().unwrap(),
|
||||
"--proving-scheme",
|
||||
scheme,
|
||||
"-j",
|
||||
proof_path.to_str().unwrap(),
|
||||
])
|
||||
.succeeds()
|
||||
.unwrap();
|
||||
|
||||
// TEST VERIFIER
|
||||
|
||||
assert_cli::Assert::command(&[
|
||||
"node",
|
||||
"test.js",
|
||||
verification_contract_path.to_str().unwrap(),
|
||||
proof_path.to_str().unwrap(),
|
||||
scheme,
|
||||
"v1",
|
||||
])
|
||||
.current_dir(concat!(env!("OUT_DIR"), "/contract"))
|
||||
.succeeds()
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -97,11 +97,11 @@ void serializeVerificationKeyToFile(r1cs_se_ppzksnark_verification_key<libff::al
|
|||
|
||||
unsigned queryLength = vk.query.size();
|
||||
|
||||
ss << "\t\tvk.H = " << outputPointG2AffineAsHex(vk.H) << endl;
|
||||
ss << "\t\tvk.Galpha = " << outputPointG1AffineAsHex(vk.G_alpha) << endl;
|
||||
ss << "\t\tvk.Hbeta = " << outputPointG2AffineAsHex(vk.H_beta) << endl;
|
||||
ss << "\t\tvk.Ggamma = " << outputPointG1AffineAsHex(vk.G_gamma) << endl;
|
||||
ss << "\t\tvk.Hgamma = " << outputPointG2AffineAsHex(vk.H_gamma) << endl;
|
||||
ss << "\t\tvk.h = " << outputPointG2AffineAsHex(vk.H) << endl;
|
||||
ss << "\t\tvk.g_alpha = " << outputPointG1AffineAsHex(vk.G_alpha) << endl;
|
||||
ss << "\t\tvk.h_beta = " << outputPointG2AffineAsHex(vk.H_beta) << endl;
|
||||
ss << "\t\tvk.g_gamma = " << outputPointG1AffineAsHex(vk.G_gamma) << endl;
|
||||
ss << "\t\tvk.h_gamma = " << outputPointG2AffineAsHex(vk.H_gamma) << endl;
|
||||
ss << "\t\tvk.query.len() = " << queryLength << endl;
|
||||
for (size_t i = 0; i < queryLength; ++i)
|
||||
{
|
||||
|
@ -124,14 +124,14 @@ void exportProof(r1cs_se_ppzksnark_proof<libff::alt_bn128_pp> proof, const char*
|
|||
ss << "{" << "\n";
|
||||
ss << "\t\"proof\":" << "\n";
|
||||
ss << "\t{" << "\n";
|
||||
ss << "\t\t\"A\":" <<outputPointG1AffineAsHexJson(proof.A) << ",\n";
|
||||
ss << "\t\t\"B\":" << "\n";
|
||||
ss << "\t\t\"a\":" <<outputPointG1AffineAsHexJson(proof.A) << ",\n";
|
||||
ss << "\t\t\"b\":" << "\n";
|
||||
ss << "\t\t\t" << outputPointG2AffineAsHexJson(proof.B) << ",\n";
|
||||
ss << "\t\t\n";
|
||||
ss << "\t\t\"C\":" <<outputPointG1AffineAsHexJson(proof.C) << ",\n";
|
||||
ss << "\t\t\"c\":" <<outputPointG1AffineAsHexJson(proof.C) << "\n";
|
||||
ss << "\t}," << "\n";
|
||||
//add input to json
|
||||
ss << "\t\"input\":" << "[";
|
||||
ss << "\t\"inputs\":" << "[";
|
||||
for (int i = 1; i < public_inputs_length; i++) {
|
||||
if(i!=1){
|
||||
ss << ",";
|
||||
|
|
|
@ -97,15 +97,15 @@ void serializeVerificationKeyToFile(r1cs_ppzksnark_verification_key<libff::alt_b
|
|||
|
||||
unsigned icLength = vk.encoded_IC_query.rest.indices.size() + 1;
|
||||
|
||||
ss << "\t\tvk.A = " << outputPointG2AffineAsHex(vk.alphaA_g2) << endl;
|
||||
ss << "\t\tvk.B = " << outputPointG1AffineAsHex(vk.alphaB_g1) << endl;
|
||||
ss << "\t\tvk.C = " << outputPointG2AffineAsHex(vk.alphaC_g2) << endl;
|
||||
ss << "\t\tvk.a = " << outputPointG2AffineAsHex(vk.alphaA_g2) << endl;
|
||||
ss << "\t\tvk.b = " << outputPointG1AffineAsHex(vk.alphaB_g1) << endl;
|
||||
ss << "\t\tvk.c = " << outputPointG2AffineAsHex(vk.alphaC_g2) << endl;
|
||||
ss << "\t\tvk.gamma = " << outputPointG2AffineAsHex(vk.gamma_g2) << endl;
|
||||
ss << "\t\tvk.gammaBeta1 = " << outputPointG1AffineAsHex(vk.gamma_beta_g1) << endl;
|
||||
ss << "\t\tvk.gammaBeta2 = " << outputPointG2AffineAsHex(vk.gamma_beta_g2) << endl;
|
||||
ss << "\t\tvk.Z = " << outputPointG2AffineAsHex(vk.rC_Z_g2) << endl;
|
||||
ss << "\t\tvk.IC.len() = " << icLength << endl;
|
||||
ss << "\t\tvk.IC[0] = " << outputPointG1AffineAsHex(vk.encoded_IC_query.first) << endl;
|
||||
ss << "\t\tvk.gamma_beta_1 = " << outputPointG1AffineAsHex(vk.gamma_beta_g1) << endl;
|
||||
ss << "\t\tvk.gamma_beta_2 = " << outputPointG2AffineAsHex(vk.gamma_beta_g2) << endl;
|
||||
ss << "\t\tvk.z = " << outputPointG2AffineAsHex(vk.rC_Z_g2) << endl;
|
||||
ss << "\t\tvk.ic.len() = " << icLength << endl;
|
||||
ss << "\t\tvk.ic[0] = " << outputPointG1AffineAsHex(vk.encoded_IC_query.first) << endl;
|
||||
for (size_t i = 1; i < icLength; ++i)
|
||||
{
|
||||
auto vkICi = outputPointG1AffineAsHex(vk.encoded_IC_query.rest.values[i - 1]);
|
||||
|
@ -127,19 +127,19 @@ void exportProof(r1cs_ppzksnark_proof<libff::alt_bn128_pp> proof, const char* pr
|
|||
ss << "{" << "\n";
|
||||
ss << "\t\"proof\":" << "\n";
|
||||
ss << "\t{" << "\n";
|
||||
ss << "\t\t\"A\":" <<outputPointG1AffineAsHexJson(proof.g_A.g) << ",\n";
|
||||
ss << "\t\t\"A_p\":" <<outputPointG1AffineAsHexJson(proof.g_A.h) << ",\n";
|
||||
ss << "\t\t\"B\":" << "\n";
|
||||
ss << "\t\t\"a\":" <<outputPointG1AffineAsHexJson(proof.g_A.g) << ",\n";
|
||||
ss << "\t\t\"a_p\":" <<outputPointG1AffineAsHexJson(proof.g_A.h) << ",\n";
|
||||
ss << "\t\t\"b\":" << "\n";
|
||||
ss << "\t\t\t" << outputPointG2AffineAsHexJson(proof.g_B.g) << ",\n";
|
||||
ss << "\t\t\n";
|
||||
ss << "\t\t\"B_p\":" <<outputPointG1AffineAsHexJson(proof.g_B.h) << ",\n";
|
||||
ss << "\t\t\"C\":" <<outputPointG1AffineAsHexJson(proof.g_C.g) << ",\n";
|
||||
ss << "\t\t\"C_p\":" <<outputPointG1AffineAsHexJson(proof.g_C.h) << ",\n";
|
||||
ss << "\t\t\"H\":" <<outputPointG1AffineAsHexJson(proof.g_H) << ",\n";
|
||||
ss << "\t\t\"K\":" <<outputPointG1AffineAsHexJson(proof.g_K) << "\n";
|
||||
ss << "\t\t\"b_p\":" <<outputPointG1AffineAsHexJson(proof.g_B.h) << ",\n";
|
||||
ss << "\t\t\"c\":" <<outputPointG1AffineAsHexJson(proof.g_C.g) << ",\n";
|
||||
ss << "\t\t\"c_p\":" <<outputPointG1AffineAsHexJson(proof.g_C.h) << ",\n";
|
||||
ss << "\t\t\"h\":" <<outputPointG1AffineAsHexJson(proof.g_H) << ",\n";
|
||||
ss << "\t\t\"k\":" <<outputPointG1AffineAsHexJson(proof.g_K) << "\n";
|
||||
ss << "\t}," << "\n";
|
||||
//add input to json
|
||||
ss << "\t\"input\":" << "[";
|
||||
ss << "\t\"inputs\":" << "[";
|
||||
for (int i = 1; i < public_inputs_length; i++) {
|
||||
if(i!=1){
|
||||
ss << ",";
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use crate::ir;
|
||||
use crate::proof_system::bn128::utils::bellman::Computation;
|
||||
use crate::proof_system::bn128::utils::solidity::{SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB};
|
||||
use crate::proof_system::bn128::utils::solidity::{
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
|
||||
};
|
||||
use crate::proof_system::ProofSystem;
|
||||
use bellman::groth16::Parameters;
|
||||
use regex::Regex;
|
||||
|
@ -55,18 +57,29 @@ impl ProofSystem for G16 {
|
|||
true
|
||||
}
|
||||
|
||||
fn export_solidity_verifier(&self, reader: BufReader<File>) -> String {
|
||||
fn export_solidity_verifier(&self, reader: BufReader<File>, is_abiv2: bool) -> String {
|
||||
let mut lines = reader.lines();
|
||||
|
||||
let mut template_text = String::from(CONTRACT_TEMPLATE);
|
||||
let gamma_abc_template = String::from("vk.gammaABC[index] = Pairing.G1Point(points);"); //copy this for each entry
|
||||
let (mut template_text, solidity_pairing_lib) = if is_abiv2 {
|
||||
(
|
||||
String::from(CONTRACT_TEMPLATE_V2),
|
||||
String::from(SOLIDITY_PAIRING_LIB_V2),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
String::from(CONTRACT_TEMPLATE),
|
||||
String::from(SOLIDITY_PAIRING_LIB),
|
||||
)
|
||||
};
|
||||
|
||||
let gamma_abc_template = String::from("vk.gamma_abc[index] = Pairing.G1Point(points);"); //copy this for each entry
|
||||
|
||||
//replace things in template
|
||||
let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap();
|
||||
let vk_gamma_abc_len_regex = Regex::new(r#"(<%vk_gammaABC_length%>)"#).unwrap();
|
||||
let vk_gamma_abc_len_regex = Regex::new(r#"(<%vk_gamma_abc_length%>)"#).unwrap();
|
||||
let vk_gamma_abc_index_regex = Regex::new(r#"index"#).unwrap();
|
||||
let vk_gamma_abc_points_regex = Regex::new(r#"points"#).unwrap();
|
||||
let vk_gamma_abc_repeat_regex = Regex::new(r#"(<%vk_gammaABC_pts%>)"#).unwrap();
|
||||
let vk_gamma_abc_repeat_regex = Regex::new(r#"(<%vk_gamma_abc_pts%>)"#).unwrap();
|
||||
let vk_input_len_regex = Regex::new(r#"(<%vk_input_length%>)"#).unwrap();
|
||||
|
||||
for _ in 0..4 {
|
||||
|
@ -131,7 +144,7 @@ impl ProofSystem for G16 {
|
|||
|
||||
format!(
|
||||
"{}{}{}",
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, template_text
|
||||
SOLIDITY_G2_ADDITION_LIB, solidity_pairing_lib, template_text
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -150,7 +163,7 @@ mod serialize {
|
|||
vk.beta = {}
|
||||
vk.gamma = {}
|
||||
vk.delta = {}
|
||||
vk.gammaABC.len() = {}
|
||||
vk.gamma_abc.len() = {}
|
||||
{}",
|
||||
parse_g1_hex(&vk.alpha_g1),
|
||||
parse_g2_hex(&vk.beta_g2),
|
||||
|
@ -160,7 +173,7 @@ mod serialize {
|
|||
vk.ic
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, x)| format!("vk.gammaABC[{}] = {}", i, parse_g1_hex(x)))
|
||||
.map(|(i, x)| format!("vk.gamma_abc[{}] = {}", i, parse_g1_hex(x)))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
)
|
||||
|
@ -188,6 +201,63 @@ mod serialize {
|
|||
}
|
||||
}
|
||||
|
||||
const CONTRACT_TEMPLATE_V2: &str = r#"
|
||||
contract Verifier {
|
||||
using Pairing for *;
|
||||
struct VerifyingKey {
|
||||
Pairing.G1Point a;
|
||||
Pairing.G2Point b;
|
||||
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.a = Pairing.G1Point(<%vk_a%>);
|
||||
vk.b = Pairing.G2Point(<%vk_b%>);
|
||||
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 returns (uint) {
|
||||
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++)
|
||||
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.a), vk.b)) return 1;
|
||||
return 0;
|
||||
}
|
||||
event Verified(string s);
|
||||
function verifyTx(
|
||||
Proof memory proof,
|
||||
uint[<%vk_input_length%>] memory input
|
||||
) public returns (bool r) {
|
||||
uint[] memory inputValues = new uint[](input.length);
|
||||
for(uint i = 0; i < input.length; i++){
|
||||
inputValues[i] = input[i];
|
||||
}
|
||||
if (verify(inputValues, proof) == 0) {
|
||||
emit Verified("Transaction successfully verified.");
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
const CONTRACT_TEMPLATE: &str = r#"
|
||||
contract Verifier {
|
||||
using Pairing for *;
|
||||
|
@ -196,33 +266,33 @@ contract Verifier {
|
|||
Pairing.G2Point b;
|
||||
Pairing.G2Point gamma;
|
||||
Pairing.G2Point delta;
|
||||
Pairing.G1Point[] gammaABC;
|
||||
Pairing.G1Point[] gamma_abc;
|
||||
}
|
||||
struct Proof {
|
||||
Pairing.G1Point A;
|
||||
Pairing.G2Point B;
|
||||
Pairing.G1Point C;
|
||||
Pairing.G1Point a;
|
||||
Pairing.G2Point b;
|
||||
Pairing.G1Point c;
|
||||
}
|
||||
function verifyingKey() pure internal returns (VerifyingKey memory vk) {
|
||||
vk.a = Pairing.G1Point(<%vk_a%>);
|
||||
vk.b = Pairing.G2Point(<%vk_b%>);
|
||||
vk.gamma = Pairing.G2Point(<%vk_gamma%>);
|
||||
vk.delta = Pairing.G2Point(<%vk_delta%>);
|
||||
vk.gammaABC = new Pairing.G1Point[](<%vk_gammaABC_length%>);
|
||||
<%vk_gammaABC_pts%>
|
||||
vk.gamma_abc = new Pairing.G1Point[](<%vk_gamma_abc_length%>);
|
||||
<%vk_gamma_abc_pts%>
|
||||
}
|
||||
function verify(uint[] memory input, Proof memory proof) internal returns (uint) {
|
||||
VerifyingKey memory vk = verifyingKey();
|
||||
require(input.length + 1 == vk.gammaABC.length);
|
||||
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++)
|
||||
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.gammaABC[i + 1], input[i]));
|
||||
vk_x = Pairing.addition(vk_x, vk.gammaABC[0]);
|
||||
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,
|
||||
proof.a, proof.b,
|
||||
Pairing.negate(vk_x), vk.gamma,
|
||||
Pairing.negate(proof.C), vk.delta,
|
||||
Pairing.negate(proof.c), vk.delta,
|
||||
Pairing.negate(vk.a), vk.b)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -234,9 +304,9 @@ contract Verifier {
|
|||
uint[<%vk_input_length%>] memory input
|
||||
) public 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]);
|
||||
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[](input.length);
|
||||
for(uint i = 0; i < input.length; i++){
|
||||
inputValues[i] = input[i];
|
||||
|
|
|
@ -3,7 +3,9 @@ extern crate libc;
|
|||
use self::libc::{c_char, c_int};
|
||||
use ir;
|
||||
use proof_system::bn128::utils::libsnark::{prepare_generate_proof, prepare_setup};
|
||||
use proof_system::bn128::utils::solidity::{SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB};
|
||||
use proof_system::bn128::utils::solidity::{
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
|
||||
};
|
||||
use proof_system::ProofSystem;
|
||||
use regex::Regex;
|
||||
use std::fs::File;
|
||||
|
@ -105,10 +107,21 @@ impl ProofSystem for GM17 {
|
|||
}
|
||||
}
|
||||
|
||||
fn export_solidity_verifier(&self, reader: BufReader<File>) -> String {
|
||||
fn export_solidity_verifier(&self, reader: BufReader<File>, is_abiv2: bool) -> String {
|
||||
let mut lines = reader.lines();
|
||||
|
||||
let mut template_text = String::from(CONTRACT_TEMPLATE);
|
||||
let (mut template_text, solidity_pairing_lib) = if is_abiv2 {
|
||||
(
|
||||
String::from(CONTRACT_TEMPLATE_V2),
|
||||
String::from(SOLIDITY_PAIRING_LIB_V2),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
String::from(CONTRACT_TEMPLATE),
|
||||
String::from(SOLIDITY_PAIRING_LIB),
|
||||
)
|
||||
};
|
||||
|
||||
let query_template = String::from("vk.query[index] = Pairing.G1Point(points);"); //copy this for each entry
|
||||
|
||||
//replace things in template
|
||||
|
@ -178,33 +191,33 @@ impl ProofSystem for GM17 {
|
|||
|
||||
format!(
|
||||
"{}{}{}",
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, template_text
|
||||
SOLIDITY_G2_ADDITION_LIB, solidity_pairing_lib, template_text
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const CONTRACT_TEMPLATE: &str = r#"
|
||||
const CONTRACT_TEMPLATE_V2: &str = r#"
|
||||
contract Verifier {
|
||||
using Pairing for *;
|
||||
struct VerifyingKey {
|
||||
Pairing.G2Point H;
|
||||
Pairing.G1Point Galpha;
|
||||
Pairing.G2Point Hbeta;
|
||||
Pairing.G1Point Ggamma;
|
||||
Pairing.G2Point Hgamma;
|
||||
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;
|
||||
Pairing.G1Point a;
|
||||
Pairing.G2Point b;
|
||||
Pairing.G1Point c;
|
||||
}
|
||||
function verifyingKey() pure internal returns (VerifyingKey memory vk) {
|
||||
vk.H = Pairing.G2Point(<%vk_h%>);
|
||||
vk.Galpha = Pairing.G1Point(<%vk_g_alpha%>);
|
||||
vk.Hbeta = Pairing.G2Point(<%vk_h_beta%>);
|
||||
vk.Ggamma = Pairing.G1Point(<%vk_g_gamma%>);
|
||||
vk.Hgamma = Pairing.G2Point(<%vk_h_gamma%>);
|
||||
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%>
|
||||
}
|
||||
|
@ -221,24 +234,88 @@ contract Verifier {
|
|||
* * e(C, H)
|
||||
* where psi = \sum_{i=0}^l input_i pvk.query[i]
|
||||
*/
|
||||
if (!Pairing.pairingProd4(vk.Galpha, vk.Hbeta, vk_x, vk.Hgamma, proof.C, vk.H, Pairing.negate(Pairing.addition(proof.A, vk.Galpha)), Pairing.addition(proof.B, vk.Hbeta))) return 1;
|
||||
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.Hgamma, Pairing.negate(vk.Ggamma), proof.B)) return 2;
|
||||
if (!Pairing.pairingProd2(proof.a, vk.h_gamma, Pairing.negate(vk.g_gamma), proof.b)) return 2;
|
||||
return 0;
|
||||
}
|
||||
event Verified(string s);
|
||||
function verifyTx(
|
||||
uint[2] memory a,
|
||||
uint[2][2] memory b,
|
||||
uint[2] memory c,
|
||||
Proof memory proof,
|
||||
uint[<%vk_input_length%>] memory input
|
||||
) public 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[](input.length);
|
||||
for(uint i = 0; i < input.length; i++){
|
||||
inputValues[i] = input[i];
|
||||
}
|
||||
if (verify(inputValues, proof) == 0) {
|
||||
emit Verified("Transaction successfully verified.");
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
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 returns (uint) {
|
||||
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++)
|
||||
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;
|
||||
}
|
||||
event Verified(string s);
|
||||
function verifyTx(
|
||||
uint[2] memory a,
|
||||
uint[2][2] memory b,
|
||||
uint[2] memory c,
|
||||
uint[<%vk_input_length%>] memory input
|
||||
) public 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[](input.length);
|
||||
for(uint i = 0; i < input.length; i++){
|
||||
inputValues[i] = input[i];
|
||||
|
|
|
@ -3,7 +3,9 @@ extern crate libc;
|
|||
use self::libc::{c_char, c_int};
|
||||
use ir;
|
||||
use proof_system::bn128::utils::libsnark::{prepare_generate_proof, prepare_setup};
|
||||
use proof_system::bn128::utils::solidity::{SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB};
|
||||
use proof_system::bn128::utils::solidity::{
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
|
||||
};
|
||||
use proof_system::ProofSystem;
|
||||
|
||||
use regex::Regex;
|
||||
|
@ -111,11 +113,22 @@ impl ProofSystem for PGHR13 {
|
|||
}
|
||||
}
|
||||
|
||||
fn export_solidity_verifier(&self, reader: BufReader<File>) -> String {
|
||||
fn export_solidity_verifier(&self, reader: BufReader<File>, is_abiv2: bool) -> String {
|
||||
let mut lines = reader.lines();
|
||||
|
||||
let mut template_text = String::from(CONTRACT_TEMPLATE);
|
||||
let ic_template = String::from("vk.IC[index] = Pairing.G1Point(points);"); //copy this for each entry
|
||||
let (mut template_text, solidity_pairing_lib) = if is_abiv2 {
|
||||
(
|
||||
String::from(CONTRACT_TEMPLATE_V2),
|
||||
String::from(SOLIDITY_PAIRING_LIB_V2),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
String::from(CONTRACT_TEMPLATE),
|
||||
String::from(SOLIDITY_PAIRING_LIB),
|
||||
)
|
||||
};
|
||||
|
||||
let ic_template = String::from("vk.ic[index] = Pairing.G1Point(points);"); //copy this for each entry
|
||||
|
||||
//replace things in template
|
||||
let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap();
|
||||
|
@ -181,64 +194,139 @@ impl ProofSystem for PGHR13 {
|
|||
|
||||
format!(
|
||||
"{}{}{}",
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, template_text
|
||||
SOLIDITY_G2_ADDITION_LIB, solidity_pairing_lib, template_text
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const CONTRACT_TEMPLATE: &str = r#"contract Verifier {
|
||||
const CONTRACT_TEMPLATE_V2: &str = r#"contract Verifier {
|
||||
using Pairing for *;
|
||||
struct VerifyingKey {
|
||||
Pairing.G2Point A;
|
||||
Pairing.G1Point B;
|
||||
Pairing.G2Point C;
|
||||
Pairing.G2Point a;
|
||||
Pairing.G1Point b;
|
||||
Pairing.G2Point c;
|
||||
Pairing.G2Point gamma;
|
||||
Pairing.G1Point gammaBeta1;
|
||||
Pairing.G2Point gammaBeta2;
|
||||
Pairing.G2Point Z;
|
||||
Pairing.G1Point[] IC;
|
||||
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 K;
|
||||
Pairing.G1Point H;
|
||||
Pairing.G1Point a;
|
||||
Pairing.G1Point a_p;
|
||||
Pairing.G2Point b;
|
||||
Pairing.G1Point b_p;
|
||||
Pairing.G1Point c;
|
||||
Pairing.G1Point c_p;
|
||||
Pairing.G1Point k;
|
||||
Pairing.G1Point h;
|
||||
}
|
||||
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.a = Pairing.G2Point(<%vk_a%>);
|
||||
vk.b = Pairing.G1Point(<%vk_b%>);
|
||||
vk.c = Pairing.G2Point(<%vk_c%>);
|
||||
vk.gamma = Pairing.G2Point(<%vk_g%>);
|
||||
vk.gammaBeta1 = Pairing.G1Point(<%vk_gb1%>);
|
||||
vk.gammaBeta2 = Pairing.G2Point(<%vk_gb2%>);
|
||||
vk.Z = Pairing.G2Point(<%vk_z%>);
|
||||
vk.IC = new Pairing.G1Point[](<%vk_ic_length%>);
|
||||
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 returns (uint) {
|
||||
VerifyingKey memory vk = verifyingKey();
|
||||
require(input.length + 1 == vk.IC.length);
|
||||
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++)
|
||||
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;
|
||||
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.gammaBeta2,
|
||||
Pairing.negate(vk.gammaBeta1), proof.B
|
||||
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()
|
||||
Pairing.addition(vk_x, proof.a), proof.b,
|
||||
Pairing.negate(proof.h), vk.z,
|
||||
Pairing.negate(proof.c), Pairing.P2()
|
||||
)) return 5;
|
||||
return 0;
|
||||
}
|
||||
event Verified(string s);
|
||||
function verifyTx(
|
||||
Proof memory proof,
|
||||
uint[<%vk_input_length%>] memory input
|
||||
) public returns (bool r) {
|
||||
uint[] memory inputValues = new uint[](input.length);
|
||||
for(uint i = 0; i < input.length; i++){
|
||||
inputValues[i] = input[i];
|
||||
}
|
||||
if (verify(inputValues, proof) == 0) {
|
||||
emit Verified("Transaction successfully verified.");
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
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 k;
|
||||
Pairing.G1Point h;
|
||||
}
|
||||
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 returns (uint) {
|
||||
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++)
|
||||
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;
|
||||
}
|
||||
|
@ -255,14 +343,14 @@ const CONTRACT_TEMPLATE: &str = r#"contract Verifier {
|
|||
uint[<%vk_input_length%>] memory input
|
||||
) public 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]);
|
||||
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[](input.length);
|
||||
for(uint i = 0; i < input.length; i++){
|
||||
inputValues[i] = input[i];
|
||||
|
|
|
@ -394,13 +394,161 @@ 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.5.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
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(
|
||||
[11559732032986387107991004021392285783925812861821192530917403151452391805634,
|
||||
10857046999023057135944570762232829481370756359578518086990519993285655852781],
|
||||
[4082367875863433681332203403145435568316851327593401208105741076214120093531,
|
||||
8495653923123431417604973247489272438418190587263600148770280649306958101930]
|
||||
);
|
||||
}
|
||||
/// @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 the sum of two points of G1
|
||||
function addition(G1Point memory p1, G1Point memory p2) internal 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 := call(sub(gas, 2000), 6, 0, input, 0xc0, r, 0x60)
|
||||
// Use "invalid" to make gas estimation work
|
||||
switch success case 0 { invalid() }
|
||||
}
|
||||
require(success);
|
||||
}
|
||||
/// @return the sum of two points of G2
|
||||
function addition(G2Point memory p1, G2Point memory p2) internal returns (G2Point memory r) {
|
||||
(r.X[1], r.X[0], r.Y[1], r.Y[0]) = BN256G2.ECTwistAdd(p1.X[1],p1.X[0],p1.Y[1],p1.Y[0],p2.X[1],p2.X[0],p2.Y[1],p2.Y[0]);
|
||||
}
|
||||
/// @return 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 returns (G1Point memory r) {
|
||||
uint[3] memory input;
|
||||
input[0] = p.X;
|
||||
input[1] = p.Y;
|
||||
input[2] = s;
|
||||
bool success;
|
||||
assembly {
|
||||
success := call(sub(gas, 2000), 7, 0, 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 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[0];
|
||||
input[i * 6 + 3] = p2[i].X[1];
|
||||
input[i * 6 + 4] = p2[i].Y[0];
|
||||
input[i * 6 + 5] = p2[i].Y[1];
|
||||
}
|
||||
uint[1] memory out;
|
||||
bool success;
|
||||
assembly {
|
||||
success := call(sub(gas, 2000), 8, 0, 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 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 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 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.
|
||||
//
|
||||
// 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.5.0;
|
||||
library Pairing {
|
||||
struct G1Point {
|
||||
|
|
|
@ -23,5 +23,5 @@ pub trait ProofSystem {
|
|||
proof_path: &str,
|
||||
) -> bool;
|
||||
|
||||
fn export_solidity_verifier(&self, reader: BufReader<File>) -> String;
|
||||
fn export_solidity_verifier(&self, reader: BufReader<File>, is_abiv2: bool) -> String;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue