Skip to content

Commit

Permalink
Merge e293a2b into a5f1610
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwen01 authored Nov 12, 2024
2 parents a5f1610 + e293a2b commit 9a601f1
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 48 deletions.
26 changes: 21 additions & 5 deletions .github/workflows/docker-gnark.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# This workflow generates and tests the docker image for groth16 and plonk proving.

name: Docker Gnark

on:
push:
branches: [main]
branches: [main, dev]
pull_request:
branches:
- "**"
paths:
- "recursion/gnark-ffi/**"
- "recursion/gnark-cli/**"
- "!recursion/gnark-ffi/assets/**"
- "crates/**"
- "Cargo.toml"
- ".github/workflows/**"
merge_group:

jobs:
Expand Down Expand Up @@ -39,7 +41,21 @@ jobs:
uses: actions-rs/cargo@v1
env:
SP1_GNARK_IMAGE: sp1-gnark
RUST_LOG: info
RUSTFLAGS: -Copt-level=3 -Cdebug-assertions -Coverflow-checks=y -Cdebuginfo=0 -C target-cpu=native
RUST_BACKTRACE: 1
with:
command: test
toolchain: 1.81.0
args: --release -p sp1-prover -- --exact tests::test_e2e
args: --release -p sp1-prover -- --exact tests::test_e2e --nocapture

- name: Make sure the contracts were modified
run: |
if grep -q "pragma solidity ^0.8.0" ~/.sp1/circuits/dev/Groth16Verifier.sol; then
echo "Error: Groth16Verifier.sol still contains the old pragma version"
exit 1
fi
if grep -q "pragma solidity ^0.8.0" ~/.sp1/circuits/dev/PlonkVerifier.sol; then
echo "Error: PlonkVerifier.sol still contains the old pragma version"
exit 1
fi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This workflow publishes the docker image for the CLI to ghcr.io.
# Source: https://raw.githubusercontent.com/foundry-rs/foundry/master/.github/workflows/docker-publish.yml
name: docker
name: Docker Publish CLI

on:
push:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/docker-publish-gnark.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: docker-gnark
# This workflow publishes the docker image for groth16 and plonk proving to ghcr.io.
name: Docker Publish Gnark

on:
push:
Expand Down
50 changes: 50 additions & 0 deletions crates/recursion/gnark-ffi/go/sp1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,50 @@ import (
"github.com/succinctlabs/sp1-recursion-gnark/sp1/trusted_setup"
)

// Modify the PlonkVerifier so that it works with the SP1Verifier.
func modifyPlonkVerifier(file *os.File) {
// Read the entire file
content, err := os.ReadFile(file.Name())
if err != nil {
panic(err)
}

// Replace the pragma version
modifiedContent := strings.Replace(
string(content),
"pragma solidity ^0.8.0;",
"pragma solidity ^0.8.20;",
1,
)

// Write the modified content back to the file
err = os.WriteFile(file.Name(), []byte(modifiedContent), 0644)
if err != nil {
panic(err)
}
}

// Modify the Groth16Verifier so that it works with the SP1Verifier.
func modifyGroth16Verifier(file *os.File) {
// Read the entire file
content, err := os.ReadFile(file.Name())
if err != nil {
panic(err)
}

// Perform all replacements
modifiedContent := string(content)
modifiedContent = strings.Replace(modifiedContent, "pragma solidity ^0.8.0;", "pragma solidity ^0.8.20;", 1)
modifiedContent = strings.Replace(modifiedContent, "contract Verifier {", "contract Groth16Verifier {", 1)
modifiedContent = strings.Replace(modifiedContent, "function verifyProof(", "function Verify(", 1)

// Write the modified content back to the file
err = os.WriteFile(file.Name(), []byte(modifiedContent), 0644)
if err != nil {
panic(err)
}
}

func BuildPlonk(dataDir string) {
// Set the environment variable for the constraints file.
//
Expand Down Expand Up @@ -159,6 +203,9 @@ func BuildPlonk(dataDir string) {
panic(err)
}
vk.ExportSolidity(solidityVerifierFile)

// Modify the solidity verifier.
modifyPlonkVerifier(solidityVerifierFile)
defer solidityVerifierFile.Close()

// Write the R1CS.
Expand Down Expand Up @@ -262,6 +309,9 @@ func BuildGroth16(dataDir string) {
panic(err)
}
vk.ExportSolidity(solidityVerifierFile)

// Modify the solidity verifier.
modifyGroth16Verifier(solidityVerifierFile)
defer solidityVerifierFile.Close()

// Write the R1CS.
Expand Down
7 changes: 5 additions & 2 deletions crates/recursion/gnark-ffi/src/ffi/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ fn get_docker_image() -> String {
}

/// Calls `docker run` with the given arguments and bind mounts.
///
/// Note: files created here by `call_docker` are read-only for after the process exits.
/// To fix this, manually set the docker user to the current user by supplying a `-u` flag.
fn call_docker(args: &[&str], mounts: &[(&str, &str)]) -> Result<()> {
log::info!("Running {} in docker", args[0]);
let mut cmd = Command::new("docker");
Expand Down Expand Up @@ -162,9 +165,9 @@ pub fn verify_groth16_bn254(
}

fn test(system: ProofSystem, witness_json: &str, constraints_json: &str) -> Result<()> {
let mounts = [(constraints_json, "/constraints"), (witness_json, "/witness")];
let mounts = [(witness_json, "/witness"), (constraints_json, "/constraints")];
assert_docker();
call_docker(&["test", "--system", system.as_str(), "/constraints", "/witness"], &mounts)
call_docker(&["test", "--system", system.as_str(), "/witness", "/constraints"], &mounts)
}

pub fn test_plonk_bn254(witness_json: &str, constraints_json: &str) {
Expand Down
24 changes: 3 additions & 21 deletions crates/recursion/gnark-ffi/src/groth16_bn254.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::File,
io::{Read, Write},
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -63,11 +63,7 @@ impl Groth16Bn254Prover {
.replace("{SP1_CIRCUIT_VERSION}", SP1_CIRCUIT_VERSION)
.replace("{VERIFIER_HASH}", format!("0x{}", hex::encode(vkey_hash)).as_str())
.replace("{PROOF_SYSTEM}", "Groth16");
let mut sp1_verifier_file = File::create(sp1_verifier_path).unwrap();
sp1_verifier_file.write_all(sp1_verifier_str.as_bytes()).unwrap();

let groth16_verifier_path = build_dir.join("Groth16Verifier.sol");
Self::modify_groth16_verifier(&groth16_verifier_path);
fs::write(sp1_verifier_path, sp1_verifier_str).unwrap();
}

/// Builds the Groth16 circuit locally.
Expand Down Expand Up @@ -129,20 +125,6 @@ impl Groth16Bn254Prover {
)
.expect("failed to verify proof")
}

/// Modify the Groth16Verifier so that it works with the SP1Verifier.
fn modify_groth16_verifier(file_path: &Path) {
let mut content = String::new();
File::open(file_path).unwrap().read_to_string(&mut content).unwrap();

content = content
.replace("pragma solidity ^0.8.0;", "pragma solidity ^0.8.20;")
.replace("contract Verifier {", "contract Groth16Verifier {")
.replace("function verifyProof(", "function Verify(");

let mut file = File::create(file_path).unwrap();
file.write_all(content.as_bytes()).unwrap();
}
}

impl Default for Groth16Bn254Prover {
Expand Down
21 changes: 3 additions & 18 deletions crates/recursion/gnark-ffi/src/plonk_bn254.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::File,
io::{Read, Write},
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -79,11 +79,7 @@ impl PlonkBn254Prover {
.replace("{SP1_CIRCUIT_VERSION}", SP1_CIRCUIT_VERSION)
.replace("{VERIFIER_HASH}", format!("0x{}", hex::encode(vkey_hash)).as_str())
.replace("{PROOF_SYSTEM}", "Plonk");
let mut sp1_verifier_file = File::create(sp1_verifier_path).unwrap();
sp1_verifier_file.write_all(sp1_verifier_str.as_bytes()).unwrap();

let plonk_verifier_path = build_dir.join("PlonkVerifier.sol");
Self::modify_plonk_verifier(&plonk_verifier_path);
fs::write(sp1_verifier_path, sp1_verifier_str).unwrap();
}

/// Generates a PLONK proof given a witness.
Expand Down Expand Up @@ -122,17 +118,6 @@ impl PlonkBn254Prover {
)
.expect("failed to verify proof")
}

/// Modify the PlonkVerifier so that it works with the SP1Verifier.
fn modify_plonk_verifier(file_path: &Path) {
let mut content = String::new();
File::open(file_path).unwrap().read_to_string(&mut content).unwrap();

content = content.replace("pragma solidity ^0.8.19;", "pragma solidity ^0.8.20;");

let mut file = File::create(file_path).unwrap();
file.write_all(content.as_bytes()).unwrap();
}
}

impl Default for PlonkBn254Prover {
Expand Down

0 comments on commit 9a601f1

Please sign in to comment.