Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plonk: Add solidity verifier #1249

Open
wants to merge 25 commits into
base: add-bellman-plonk-backend
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9de7966
Add matter-labs Plonk solidity verifier
georgwiese Nov 23, 2022
7c27642
Write basic code to return static verifier
georgwiese Nov 23, 2022
b95f98e
Update solidity version, format, add dummy verifyTx()
georgwiese Nov 23, 2022
d7adaf7
Get dummy proof working
georgwiese Nov 23, 2022
f351605
Move inputs outside of proof struct, run actual verification
georgwiese Nov 23, 2022
98d432b
Pass correct proof
georgwiese Nov 23, 2022
037ec0a
Replace plonk verifier template with one from solidity_plonk_verifier…
georgwiese Nov 25, 2022
ff999c5
Update solidity version
georgwiese Nov 25, 2022
d8df138
Rename verifier
georgwiese Nov 25, 2022
f44ff73
Plonk solidity verifier: Move inputs outside prove, add verifyTx() fu…
georgwiese Nov 25, 2022
c06a21c
Add solidity renderer
georgwiese Nov 25, 2022
ff4736d
Beginn moving plonk proving scheme
georgwiese Nov 25, 2022
35fcbe7
Implement Plonk proving scheme in zokrates_bellamn, fix verifier temp…
georgwiese Nov 30, 2022
77afc6d
Enable more tests
georgwiese Nov 30, 2022
8517c29
Clean up integration test
georgwiese Nov 30, 2022
944429e
Clean up
georgwiese Nov 30, 2022
28c0029
Clean up
georgwiese Nov 30, 2022
fdf2132
Clean up
georgwiese Nov 30, 2022
9dfd51c
Refactor solidity renderer to not depend on bellman
georgwiese Nov 30, 2022
195f9af
Move Plonk proving system to zokrates_proof_systems
georgwiese Nov 30, 2022
58e248a
Add zokrates_proof_system files
georgwiese Nov 30, 2022
254dbf6
Remove unused imports
georgwiese Nov 30, 2022
d09d5db
Cleanup
georgwiese Nov 30, 2022
092b4e3
Review feedback
georgwiese Dec 21, 2022
759a4a0
Add universal setup for marlin
georgwiese Dec 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion zokrates_ark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ mod parse {
use super::*;
use ark_ff::ToBytes;
use zokrates_field::G2Type;
use zokrates_proof_systems::{Fq2, Fr, G1Affine, G2Affine, G2AffineFq, G2AffineFq2, GAffine};
use zokrates_proof_systems::{Fq2, Fr, G1Affine, G2Affine, G2AffineFq, GAffine};

pub fn parse_g1<T: Field + ArkFieldExtensions>(
e: &<T::ArkEngine as PairingEngine>::G1Affine,
Expand Down
5 changes: 1 addition & 4 deletions zokrates_bellman/src/groth16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ use bellman::pairing::{ff::to_hex, CurveAffine, Engine};
use zokrates_field::BellmanFieldExtensions;
use zokrates_field::Field;
use zokrates_proof_systems::{
Backend, G1Affine, G2Affine, MpcBackend, NonUniversalBackend, Proof, SetupKeypair,
Backend, G1Affine, G2Affine, NonUniversalBackend, Proof, SetupKeypair,
};

use crate::Bellman;
use crate::Computation;
use crate::{parse_g1, parse_g2, serialization};
use phase2::MPCParameters;
use rand_0_4::Rng;
use std::io::{Read, Write};
use zokrates_ast::ir::{ProgIterator, Statement, Witness};
use zokrates_proof_systems::groth16::{ProofPoints, VerificationKey, G16};
use zokrates_proof_systems::Scheme;
Expand Down
7 changes: 7 additions & 0 deletions zokrates_bellman/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ fn deserialize_vk<T: Field + BellmanFieldExtensions>(
fn serialize_vk<T: Field + BellmanFieldExtensions>(
vk: BellmanVerificationKey<T::BellmanEngine, PlonkCsWidth4WithNextStepParams>,
) -> <Plonk as Scheme<T>>::VerificationKey {
let domain = bellman::plonk::domains::Domain::<
<T::BellmanEngine as bellman::pairing::ff::ScalarEngine>::Fr,
>::new_for_size(vk.n.next_power_of_two() as u64)
.unwrap();
let omega = parse_fr::<T>(&domain.generator);

VerificationKey {
n: vk.n as u32,
num_inputs: vk.num_inputs as u32,
Expand All @@ -195,6 +201,7 @@ fn serialize_vk<T: Field + BellmanFieldExtensions>(
.try_into()
.map_err(|_| ())
.unwrap(),
omega: omega,
}
}

Expand Down
3 changes: 3 additions & 0 deletions zokrates_cli/src/ops/export_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> {
(CurveParameter::Bn128, SchemeParameter::MARLIN) => {
cli_export_verifier::<Bn128Field, Marlin>(sub_matches, vk)
}
(CurveParameter::Bn128, SchemeParameter::PLONK) => {
cli_export_verifier::<Bn128Field, Plonk>(sub_matches, vk)
}
(curve_parameter, scheme_parameter) => Err(format!("Could not export verifier with given parameters (curve: {}, scheme: {}): not supported", curve_parameter, scheme_parameter))
}
}
Expand Down
5 changes: 2 additions & 3 deletions zokrates_cli/src/ops/mpc/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use clap::{App, Arg, ArgMatches, SubCommand};
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::Path;
use zokrates_bellman::Bellman;
use zokrates_common::constants::{BLS12_381, BN128};
use zokrates_field::{BellmanFieldExtensions, Bls12_381Field, Bn128Field, Field};
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
use zokrates_field::{BellmanFieldExtensions, Field};
use zokrates_proof_systems::{MpcBackend, MpcScheme};

pub fn subcommand() -> App<'static, 'static> {
SubCommand::with_name("beacon")
Expand Down
5 changes: 2 additions & 3 deletions zokrates_cli/src/ops/mpc/contribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use clap::{App, Arg, ArgMatches, SubCommand};
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::Path;
use zokrates_bellman::Bellman;
use zokrates_common::constants::{BLS12_381, BN128};
use zokrates_field::{BellmanFieldExtensions, Bls12_381Field, Bn128Field, Field};
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
use zokrates_field::{BellmanFieldExtensions, Field};
use zokrates_proof_systems::{MpcBackend, MpcScheme};

pub fn subcommand() -> App<'static, 'static> {
SubCommand::with_name("contribute")
Expand Down
5 changes: 2 additions & 3 deletions zokrates_cli/src/ops/mpc/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use clap::{App, Arg, ArgMatches, SubCommand};
use std::fs::File;
use std::io::{BufReader, Write};
use std::path::Path;
use zokrates_bellman::Bellman;
use zokrates_common::constants::{BLS12_381, BN128};
use zokrates_field::{BellmanFieldExtensions, Bls12_381Field, Bn128Field, Field};
use zokrates_proof_systems::{MpcBackend, MpcScheme, TaggedVerificationKey, G16};
use zokrates_field::{BellmanFieldExtensions, Field};
use zokrates_proof_systems::{MpcBackend, MpcScheme, TaggedVerificationKey};

pub fn subcommand() -> App<'static, 'static> {
SubCommand::with_name("export")
Expand Down
3 changes: 1 addition & 2 deletions zokrates_cli/src/ops/mpc/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::Path;
use zokrates_ast::ir::{self, ProgEnum};
use zokrates_bellman::Bellman;
use zokrates_field::{BellmanFieldExtensions, Field};
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
use zokrates_proof_systems::{MpcBackend, MpcScheme};

pub fn subcommand() -> App<'static, 'static> {
SubCommand::with_name("init")
Expand Down
3 changes: 1 addition & 2 deletions zokrates_cli/src/ops/mpc/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use zokrates_ast::ir::{self, ProgEnum};
use zokrates_bellman::Bellman;
use zokrates_field::{BellmanFieldExtensions, Field};
use zokrates_proof_systems::{MpcBackend, MpcScheme, G16};
use zokrates_proof_systems::{MpcBackend, MpcScheme};

pub fn subcommand() -> App<'static, 'static> {
SubCommand::with_name("verify")
Expand Down
50 changes: 40 additions & 10 deletions zokrates_cli/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod integration {
use zokrates_ast::typed::abi::Abi;
use zokrates_field::Bn128Field;
use zokrates_proof_systems::{
to_token::ToToken, Marlin, Proof, SolidityCompatibleScheme, G16, GM17,
to_token::ToToken, Marlin, Plonk, Proof, SolidityCompatibleScheme, G16, GM17,
SOLIDITY_G2_ADDITION_LIB,
};

Expand All @@ -43,7 +43,6 @@ mod integration {
fn test_compile_and_witness_dir() {
let global_dir = TempDir::new("global").unwrap();
let global_base = global_dir.path();
let universal_setup_path = global_base.join("universal_setup.dat");

// GENERATE A UNIVERSAL SETUP
assert_cli::Assert::main_binary()
Expand All @@ -54,7 +53,27 @@ mod integration {
"--proving-scheme",
"marlin",
georgwiese marked this conversation as resolved.
Show resolved Hide resolved
"--universal-setup-path",
universal_setup_path.to_str().unwrap(),
global_base
.join("universal_setup_marlin.dat")
.to_str()
.unwrap(),
])
.succeeds()
.unwrap();
assert_cli::Assert::main_binary()
.with_args(&[
"universal-setup",
"--backend",
"bellman",
"--size",
"10",
"--proving-scheme",
"plonk",
"--universal-setup-path",
global_base
.join("universal_setup_plonk.dat")
.to_str()
.unwrap(),
])
.succeeds()
.unwrap();
Expand Down Expand Up @@ -89,6 +108,8 @@ mod integration {
expected_witness_path: &Path,
global_path: &Path,
) {
println!("Running test for program: {:?}", program_name);

let tmp_dir = TempDir::new(program_name).unwrap();
let tmp_base = tmp_dir.path();
let test_case_path = tmp_base.join(program_name);
Expand All @@ -97,7 +118,6 @@ mod integration {
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 universal_setup_path = global_path.join("universal_setup.dat");
let verification_key_path = tmp_base
.join(program_name)
.join("verification")
Expand Down Expand Up @@ -235,14 +255,16 @@ mod integration {
}

let backends = map! {
"bellman" => vec!["g16"],
"bellman" => vec!["g16", "plonk"],
"ark" => vec!["g16", "gm17", "marlin"]
};

for (backend, schemes) in backends {
for scheme in &schemes {
println!("test with {}, {}", backend, scheme);
// SETUP
let universal_setup_path =
global_path.join(format!("universal_setup_{}.dat", scheme));
let setup = assert_cli::Assert::main_binary()
.with_args(&[
"setup",
Expand All @@ -264,6 +286,10 @@ mod integration {
.doesnt_contain("This program is too small to generate a setup with Marlin")
.execute();

if let Err(e) = &setup {
eprint!("{}", e);
}

if setup.is_ok() {
// GENERATE-PROOF
assert_cli::Assert::main_binary()
Expand Down Expand Up @@ -318,16 +344,22 @@ mod integration {
.unwrap();
match *scheme {
"marlin" => {
// Get the proof
let proof: Proof<Bn128Field, Marlin> = serde_json::from_reader(
File::open(proof_path.to_str().unwrap()).unwrap(),
)
.unwrap();

test_solidity_verifier(contract_str, proof);
}
"plonk" => {
let proof: Proof<Bn128Field, Plonk> = serde_json::from_reader(
File::open(proof_path.to_str().unwrap()).unwrap(),
)
.unwrap();

test_solidity_verifier(contract_str, proof);
}
"g16" => {
// Get the proof
let proof: Proof<Bn128Field, G16> = serde_json::from_reader(
File::open(proof_path.to_str().unwrap()).unwrap(),
)
Expand All @@ -336,7 +368,6 @@ mod integration {
test_solidity_verifier(contract_str, proof);
}
"gm17" => {
// Get the proof
let proof: Proof<Bn128Field, GM17> = serde_json::from_reader(
File::open(proof_path.to_str().unwrap()).unwrap(),
)
Expand Down Expand Up @@ -411,7 +442,6 @@ mod integration {
})
.collect::<Vec<_>>(),
);

let inputs = [proof_token, input_token.clone()];

// Call verify function on contract
Expand Down Expand Up @@ -445,7 +475,7 @@ mod integration {
)
.unwrap();

assert_eq!(result.op_out, Return::InvalidOpcode);
assert!(result.op_out == Return::InvalidOpcode || result.op_out == Return::Revert);
}

fn test_compile_and_smtlib2(
Expand Down
6 changes: 5 additions & 1 deletion zokrates_proof_systems/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ cfg-if = "0.1"
ethabi = "17.0.0"
primitive-types = { version = "0.11", features = ["rlp"] }
rand_0_4 = { version = "0.4", package = "rand" }
getrandom = { version = "0.2", features = ["js"] }
getrandom = { version = "0.2", features = ["js"] }

# Used by solidity renderer
handlebars = "3.*"
serde_json = "1.*"
Loading