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

Add CLI interface for verifier contract generation #74

Merged
merged 51 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
d08aa19
add: solidity-verifier workspace member
CPerezz Feb 5, 2024
77b9538
chore: Update toolchain to 1.74
CPerezz Feb 5, 2024
6a0d023
feat: Add basic clap cli interface for solidity verifier
CPerezz Feb 5, 2024
f4c229a
chore: Expose SoldityVerifier template struct
CPerezz Feb 6, 2024
ba6f100
feat: Finish first working version
CPerezz Feb 6, 2024
21ebf13
change: Modify some settings
CPerezz Feb 6, 2024
5abe618
fix: Fix rebase conflicts
CPerezz Feb 6, 2024
e7acfe5
chore: Leave resolver 2 for workspace
CPerezz Feb 19, 2024
a8931b1
chore: Rename KZG+G16 template
CPerezz Feb 20, 2024
8466dc2
fixup
CPerezz Feb 20, 2024
418f20a
chore: Rename to NovaCyclefoldDecider the template
CPerezz Feb 20, 2024
f2c5161
chore: Change constructors to `new` instead of `from`
CPerezz Feb 20, 2024
6defe48
add: ProtocolData trait helper
CPerezz Feb 21, 2024
c66cc9b
chore: Create `From` impls formally
CPerezz Feb 21, 2024
324543e
add: Support G16, KZG and Nova+Cyclefold in cli
CPerezz Feb 21, 2024
351dfcb
chore: Adapt `main.rs` to new cli changes
CPerezz Feb 21, 2024
6e44ac8
chore: Make solidity helper fns `cfg(test)`
CPerezz Feb 22, 2024
ba44b62
chore: Rework folding-schemes-solidity structure
CPerezz Feb 22, 2024
5f6dc33
chore: Remove g1_crs_batch_points_len from KZGData
CPerezz Feb 22, 2024
ff83064
add: Serde tests for all template targets
CPerezz Feb 22, 2024
ebfe6ad
tmp: Add NovaCyclefold testing
CPerezz Feb 22, 2024
f4b0503
add: HeaderInclusion template
CPerezz Feb 23, 2024
92d8ce1
chore: Add tests for NovaCyclefold contract
CPerezz Feb 23, 2024
b401dce
chore: Update g16 to use HeaderInclusion template rendering
CPerezz Feb 23, 2024
3a965ef
add: Small builder-pattern to construct HeaderInclusion Templates
CPerezz Feb 23, 2024
4cd87ad
remove: sdpx & pragma from KZG template
CPerezz Feb 23, 2024
c2b861f
chore: Update templates to use HeaderInclusion builder
CPerezz Feb 23, 2024
effffb9
chore: Update tests to use HeaderInclusion builderPattern
CPerezz Feb 23, 2024
085b506
remove: fixed pragma version in novacyclefold template
CPerezz Feb 23, 2024
b3deb7b
chore: Accept Into<Template> in builder
CPerezz Feb 23, 2024
198f5bb
tmp: Only KZG return passes. Fix Groth
CPerezz Feb 23, 2024
3840819
fix: Prevent `revert` from paniking for negative tests
CPerezz Feb 26, 2024
18fd256
feat: Merge G16 and KZG contract results in NovaCyclefold
CPerezz Feb 26, 2024
0e2225c
chore: Add assets for quicker/easier testing
CPerezz Feb 26, 2024
4b189ec
fix: Address clippy & warnings
CPerezz Feb 26, 2024
7245ecc
fix: Spelling to prevent PR farmers LOL
CPerezz Feb 26, 2024
a3a1cd3
chore: Add about and long_about to CLI tool
CPerezz Feb 26, 2024
74b2743
add: README.md
CPerezz Feb 26, 2024
9690df6
chore: Revert asset-based testing approach
CPerezz Mar 1, 2024
c84fbc6
remove: Assets folder
CPerezz Mar 1, 2024
18fe615
fix: Rebase issues
CPerezz Mar 1, 2024
561ec40
fix: use &mut for Reader
CPerezz Mar 1, 2024
c77acc0
fix: rebase error with Contract name
CPerezz Mar 1, 2024
b219f2a
chore: Reduce tests LOC with setup fn
CPerezz Mar 5, 2024
d3336d0
chore: Set MIT license indentifier for CLI & KZG
CPerezz Mar 5, 2024
8146370
chore: Add extra usage example
CPerezz Mar 5, 2024
5a91372
chore: Update novacyclefold contract comments on soundess
CPerezz Mar 6, 2024
1a7e402
chore: Typo
CPerezz Mar 6, 2024
20e1516
chore: Allow type complexity clippy for setup fn
CPerezz Mar 6, 2024
1de9dac
chore: Address Pierre's comments
CPerezz Mar 11, 2024
cab541e
chore: Rename workspace members
CPerezz Mar 11, 2024
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Cargo.lock
/src/frontend/circom/test_folder/test_circuit_js/

# generated contracts at test time
folding-schemes-solidity/generated
solidity-verifiers/generated
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[workspace]
resolver = "1"
members = [
"folding-schemes",
"folding-schemes-solidity"
"solidity-verifiers",
"cli"
]
resolver = "2"

# The following patch is to use a version of ark-r1cs-std compatible with
# v0.4.0 but that includes a cherry-picked commit from after v0.4.0 which fixes
Expand Down
38 changes: 38 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "solidity-verifiers-cli"
version = "0.1.0"
edition = "2021"

[dependencies]
ark-ec = "0.4"
ark-ff = "0.4"
ark-poly = "0.4"
ark-std = "0.4"
ark-groth16 = "0.4"
askama = { version = "0.12.0", features = ["config"], default-features = false }
ark-bn254 = "0.4.0"
ark-poly-commit = "0.4.0"
solidity-verifiers = { path = "../solidity-verifiers" }
itertools = "0.12.1"
ark-serialize = "0.4.1"
clap = { version = "4.4", features = ["derive", "string"] }
clap-verbosity-flag = "2.1"
log = "0.4"
env_logger = "0.10"

[dev-dependencies]
revm = "3.5.0"
tracing = { version = "0.1", default-features = false, features = [ "attributes" ] }
tracing-subscriber = { version = "0.2" }

[features]
default = ["parallel"]

parallel = [
"ark-std/parallel",
"ark-ff/parallel",
"ark-poly/parallel",
]



72 changes: 72 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Solidity Verifier
_____ ______ ______ ______ ______ ______ ______
| |__| || |__| || |__| || |__| || |__| || |__| || |__| |
| () || () || () || () || () || () || () |
|______||______||______||______||______||______||______|
______ ______
| |__| | ____ _ _ _ _ _ | |__| |
| () | / ___| ___ | (_) __| (_) |_ _ _ | () |
|______| \___ \ / _ \| | |/ _` | | __| | | | |______|
______ ___) | (_) | | | (_| | | |_| |_| | ______
| |__| | |____/ \___/|_|_|\__,_|_|\__|\__, | | |__| |
| () | __ __ _ __ _ |___/ | () |
|______| \ \ / /__ _ __(_)/ _(_) ___ _ __ |______|
______ \ \ / / _ \ '__| | |_| |/ _ \ '__| ______
| |__| | \ V / __/ | | | _| | __/ | | |__| |
| () | \_/ \___|_| |_|_| |_|\___|_| | () |
|______| |______|
______ ______ ______ ______ ______ ______ ______
| |__| || |__| || |__| || |__| || |__| || |__| || |__| |
| () || () || () || () || () || () || () |
|______||______||______||______||______||______||______|

Welcome to Solidity Verifier, a powerful Command-Line Interface (CLI) tool designed to simplify the generation of Solidity smart contracts that verify proofs of Zero Knowledge cryptographic protocols. This tool is developed by the collaborative efforts of the PSE (Privacy & Scaling Explorations) and 0XPARC teams.

As an open-source project, Solidity Verifier is released under the GPL3 license.

## Supported Protocols

Solidity Verifier currently supports the generation of Solidity smart contracts for the verification of proofs in the following Zero Knowledge protocols:

- **Groth16:**
- Efficient and succinct zero-knowledge proof system.
- Template credit: [Jordi Baylina - Groth16 Verifier Template](https://github.com/iden3/snarkjs/blob/master/templates/verifier_groth16.sol.ejs)

- **KZG:**
- Uses the Kate-Zaverucha-Goldberg polynomial commitment scheme.
- Example credit: [weijiekoh - KZG10 Verifier Contract](https://github.com/weijiekoh/libkzg/blob/master/sol/KZGVerifier.sol)

- **Nova + CycleFold Decider:**
- Implements the decider circuit verification for the Nova zero-knowledge proof system in conjunction with the CycleFold protocol optimization.
- Template inspiration and setup credit: [Han - revm/Solidity Contract Testing Functions](https://github.com/privacy-scaling-explorations/halo2-solidity-verifier/tree/main)

## Usage

```bash
solidity-verifier [OPTIONS] -p <PROTOCOL> -pd <PROTOCOL_DATA> -o <OUTPUT_PATH>
```

A real use case (which was used to test the tool itself):
`solidity-verifier -p groth16 -pd ./folding-verifier-solidity/assets/G16_test_vk_data`
This would generate a Groth16 verifier contract for the given G16 data (which consists on the G16_Vkey only) and store this contract in `$pwd`.

### Options:
-v, --verbose: Increase logging verbosity
-q, --quiet: Decrease logging verbosity
-p, --protocol <PROTOCOL>: Selects the protocol for which to generate the Decider circuit Solidity Verifier (possible values: groth16, kzg, nova-cyclefold)
-o, --out <OUT>: Sets the output path for all generated artifacts (default: /home/kr0/Desktop/HDD/ethereum/folding-schemes/verifier.sol)
-d, --protocol-data <PROTOCOL_DATA>: Sets the input path for the file containing all the data required by the chosen protocol for verification contract generation
--pragma <PRAGMA>: Selects the Solidity compiler version to be set in the Solidity Verifier contract artifact
-h, --help: Print help (see a summary with '-h')
-V, --version: Print version

## License
Solidity Verifier is released under the GPL3 license for any of the protocols that include `Groth16`. See the LICENSE file in the project repository for more details.
For the rest of contracts/protocols and the CLI itself, this tooling is released under MIT/Apache license.

## Contributing
Feel free to explore, use, and contribute to Solidity Verifier as we strive to enhance privacy and scalability in the blockchain space!
We welcome contributions to Solidity Verifier! If you encounter any issues, have feature requests, or want to contribute to the codebase, please check out the GitHub repository and follow the guidelines outlined in the contributing documentation.



36 changes: 36 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use ::clap::Parser;
use ark_serialize::Write;
use settings::Cli;
use std::path::Path;
use std::{fs, io};

mod settings;

fn create_or_open_then_write<T: AsRef<[u8]>>(path: &Path, content: &T) -> Result<(), io::Error> {
let mut file = fs::OpenOptions::new().create(true).write(true).open(path)?;
file.write_all(content.as_ref())
}

fn main() {
let cli = Cli::parse();

// generate a subscriber with the desired log level
env_logger::builder()
.format_timestamp_secs()
.filter_level(cli.verbosity.log_level_filter())
.init();

let out_path = cli.out;

// Fetch the exact protocol for which we need to generate the Decider verifier contract.
let protocol = cli.protocol;
// Fetch the protocol data passed by the user from the file.
let protocol_data = std::fs::read(cli.protocol_data).unwrap();

// Generate the Solidity Verifier contract for the selected protocol with the given data.
create_or_open_then_write(
&out_path,
&protocol.render(&protocol_data, cli.pragma).unwrap(),
)
.unwrap();
}
111 changes: 111 additions & 0 deletions cli/src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use ark_serialize::SerializationError;
use clap::{Parser, ValueEnum};
use solidity_verifiers::{Groth16Data, KzgData, NovaCyclefoldData, ProtocolData};
use std::{env, fmt::Display, path::PathBuf};

fn get_default_out_path() -> PathBuf {
let mut path = env::current_dir().unwrap();
path.push("verifier.sol");
path
}

#[derive(Debug, Copy, Clone, ValueEnum)]
pub(crate) enum Protocol {
Groth16,
Kzg,
NovaCyclefold,
}

impl Display for Protocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

// Would be nice to link this to the `Template` or `ProtocolData` traits.
// Sadly, this requires Boxing with `dyn` or similar which would complicate the code more than is actually required.
impl Protocol {
pub(crate) fn render(
&self,
data: &[u8],
pragma: Option<String>,
) -> Result<Vec<u8>, SerializationError> {
match self {
Self::Groth16 => {
Ok(Groth16Data::deserialize_protocol_data(data)?.render_as_template(pragma))
}

Self::Kzg => Ok(KzgData::deserialize_protocol_data(data)?.render_as_template(pragma)),
Self::NovaCyclefold => {
Ok(NovaCyclefoldData::deserialize_protocol_data(data)?.render_as_template(pragma))
}
}
}
}

const ABOUT: &str = "A powerful Command-Line Interface (CLI) tool designed to simplify the generation of Solidity smart contracts that verify proofs of Zero Knowledge cryptographic protocols.
";

const LONG_ABOUT: &str = "
_____ ______ ______ ______ ______ ______ ______
| |__| || |__| || |__| || |__| || |__| || |__| || |__| |
| () || () || () || () || () || () || () |
|______||______||______||______||______||______||______|
______ ______
| |__| | ____ _ _ _ _ _ | |__| |
| () | / ___| ___ | (_) __| (_) |_ _ _ | () |
|______| \\___ \\ / _ \\| | |/ _` | | __| | | | |______|
______ ___) | (_) | | | (_| | | |_| |_| | ______
| |__| | |____/ \\___/|_|_|\\__,_|_|\\__|\\__, | | |__| |
| () | __ __ _ __ _ |___/ | () |
|______| \\ \\ / /__ _ __(_)/ _(_) ___ _ __ |______|
______ \\ \\ / / _ \\ '__| | |_| |/ _ \\ '__| ______
| |__| | \\ V / __/ | | | _| | __/ | | |__| |
| () | \\_/ \\___|_| |_|_| |_|\\___|_| | () |
|______| |______|
______ ______ ______ ______ ______ ______ ______
| |__| || |__| || |__| || |__| || |__| || |__| || |__| |
| () || () || () || () || () || () || () |
|______||______||______||______||______||______||______|

Welcome to Solidity Verifier, a powerful Command-Line Interface (CLI) tool designed to simplify the generation of Solidity smart contracts that verify proofs of Zero Knowledge cryptographic protocols.
for Zero Knowledge protocols. This tool is developed by the collaborative efforts of the PSE (Privacy & Scaling Explorations) and 0XPARC teams.

As an open-source project, Solidity Verifier is released under the GPL3 license.

Solidity Verifier currently supports the generation of Solidity smart contracts for the verification of proofs in the following Zero Knowledge protocols:

Groth16:
Efficient and succinct zero-knowledge proof system.

KZG:
Uses the Kate-Zaverucha-Goldberg polynomial commitment scheme.

Nova + CycleFold Decider:
Implements the decider circuit verification for the Nova zero-knowledge proof system in conjunction with the CycleFold protocol optimization.
";
#[derive(Debug, Parser)]
#[command(author = "0XPARC & PSE", version, about = ABOUT, long_about = Some(LONG_ABOUT))]
#[command(propagate_version = true)]
/// A tool to create Solidity Contracts which act as verifiers for the major Folding Schemes implemented
/// within the `folding-schemes` repo.
pub(crate) struct Cli {
#[command(flatten)]
pub verbosity: clap_verbosity_flag::Verbosity,

/// Selects the protocol for which we want to generate the Solidity Verifier contract.
#[arg(short = 'p', long, value_enum, rename_all = "lower")]
pub protocol: Protocol,

#[arg(short = 'o', long, default_value=get_default_out_path().into_os_string())]
/// Sets the output path for all the artifacts generated by the command.
pub out: PathBuf,

#[arg(short = 'd', long)]
/// Sets the input path for the file containing all the data required by the protocol chosen such that the verification contract can be generated.
pub protocol_data: PathBuf,

/// Selects the Solidity compiler version to be set in the Solidity Verifier contract artifact.
#[arg(long, default_value=None)]
pub pragma: Option<String>,
}
5 changes: 0 additions & 5 deletions folding-schemes-solidity/src/lib.rs

This file was deleted.

1 change: 0 additions & 1 deletion folding-schemes-solidity/src/utils/mod.rs

This file was deleted.

Loading
Loading