Skip to content

Commit

Permalink
feat: Incorporate downloading Sphinx parameters using AWS cli (#63)
Browse files Browse the repository at this point in the history
* feat: Incorporate downloading using AWS cli

* chore: SP1 -> Sphinx renaming in Solidity assets

* ci: Add S3 credentials (#64)

---------

Co-authored-by: Samuel Burnham <45365069+samuelburnham@users.noreply.github.com>
  • Loading branch information
2 people authored and wwared committed Jun 24, 2024
1 parent 010f2af commit b848fc2
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

env:
AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_KEY }}

jobs:
test:
name: CI Test Suite
Expand Down
4 changes: 2 additions & 2 deletions prover/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::utils::{babybear_bytes_to_bn254, babybears_to_bn254, words_to_bytes};
use crate::{OuterSC, SphinxProver};

/// Tries to install the PLONK artifacts if they are not already installed.
pub fn try_install_plonk_bn254_artifacts() -> PathBuf {
pub fn try_install_plonk_bn254_artifacts(use_aws_cli: bool) -> PathBuf {
let build_dir = plonk_bn254_artifacts_dir();

if build_dir.exists() {
Expand All @@ -31,7 +31,7 @@ pub fn try_install_plonk_bn254_artifacts() -> PathBuf {
PLONK_BN254_ARTIFACTS_COMMIT,
build_dir.display()
);
install_plonk_bn254_artifacts(&build_dir);
install_plonk_bn254_artifacts(&build_dir, use_aws_cli);
}
build_dir
}
Expand Down
42 changes: 31 additions & 11 deletions prover/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use reqwest::Client;
use crate::utils::block_on;

/// The base URL for the S3 bucket containing the plonk bn254 artifacts.
pub const PLONK_BN254_ARTIFACTS_URL_BASE: &str = "https://sp1-circuits.s3-us-east-2.amazonaws.com";
pub const PLONK_BN254_ARTIFACTS_URL_BASE: &str = "s3://sphinx-plonk-params";

/// The current version of the plonk bn254 artifacts.
pub const PLONK_BN254_ARTIFACTS_COMMIT: &str = "4a525e9f";
Expand All @@ -21,7 +21,7 @@ pub const PLONK_BN254_ARTIFACTS_COMMIT: &str = "4a525e9f";
///
/// This function will download the latest plonk bn254 artifacts from the S3 bucket and extract them to
/// the directory specified by [plonk_bn254_artifacts_dir()].
pub fn install_plonk_bn254_artifacts(build_dir: &Path) {
pub fn install_plonk_bn254_artifacts(build_dir: &Path, use_aws_cli: bool) {
// Create the build directory.
std::fs::create_dir_all(build_dir).expect("failed to create build directory");

Expand All @@ -32,15 +32,21 @@ pub fn install_plonk_bn254_artifacts(build_dir: &Path) {
);
let mut artifacts_tar_gz_file =
tempfile::NamedTempFile::new().expect("failed to create tempfile");
let client = Client::builder()
.build()
.expect("failed to create reqwest client");
block_on(download_file(
&client,
&download_url,
&mut artifacts_tar_gz_file,
))
.expect("failed to download file");

if use_aws_cli {
block_on(download_file_aws(&download_url, &mut artifacts_tar_gz_file))
.expect("failed to download file [aws]");
} else {
let client = Client::builder()
.build()
.expect("failed to create reqwest client");
block_on(download_file(
&client,
&download_url,
&mut artifacts_tar_gz_file,
))
.expect("failed to download file");
}

// Extract the tarball to the build directory.
let mut res = Command::new("tar")
Expand Down Expand Up @@ -106,3 +112,17 @@ pub async fn download_file(

Ok(())
}

/// Download the file using the AWS cli
pub async fn download_file_aws(
url: &str,
file: &mut tempfile::NamedTempFile,
) -> Result<(), String> {
let mut res = Command::new("aws")
.args(["s3", "cp", url, file.path().to_str().unwrap()])
.spawn()
.expect("couldn't run `aws` command. Probably it is not installed / configured");
res.wait().unwrap();

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

/// @title SP1 Verifier Interface
/// @author Succinct Labs
/// @notice This contract is the interface for the SP1 Verifier.
interface ISP1Verifier {
/// @notice Returns the version of the SP1 Verifier.
/// @title Sphinx Verifier Interface
/// @notice This contract is the interface for the Sphinx Verifier.
interface ISphinxVerifier {
/// @notice Returns the version of the Sphinx Verifier.
function VERSION() external pure returns (string memory);

/// @notice Verifies a proof with given public values and vkey.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import {ISP1Verifier} from "./ISP1Verifier.sol";
import {ISphinxVerifier} from "./ISphinxVerifier.sol";

/// @title SP1 Mock Verifier
/// @author Succinct Labs
/// @notice This contracts implements a Mock solidity verifier for SP1.
contract SP1MockVerifier is ISP1Verifier {
/// @title Sphinx Mock Verifier
/// @notice This contracts implements a Mock solidity verifier for Sphinx.
contract SphinxMockVerifier is ISphinxVerifier {
function VERSION() external pure returns (string memory) {
return "TODO";
}

/// @notice Verifies a mock proof with given public values and vkey.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
/// @param proofBytes The proof of the program execution the Sphinx zkVM encoded as bytes.
function verifyProof(
bytes32,
bytes memory,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import {ISP1Verifier} from "./ISP1Verifier.sol";
import {ISphinxVerifier} from "./ISphinxVerifier.sol";
import {PlonkVerifier} from "./PlonkVerifier.sol";

/// @title SP1 Verifier
/// @author Succinct Labs
/// @notice This contracts implements a solidity verifier for SP1.
contract SP1Verifier is PlonkVerifier {
/// @title Sphinx Verifier
/// @notice This contracts implements a solidity verifier for Sphinx.
contract SphinxVerifier is PlonkVerifier {
function VERSION() external pure returns (string memory) {
return "TODO";
}
Expand All @@ -23,7 +22,7 @@ contract SP1Verifier is PlonkVerifier {
/// @notice Verifies a proof with given public values and vkey.
/// @param vkey The verification key for the RISC-V program.
/// @param publicValues The public values encoded as bytes.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
/// @param proofBytes The proof of the program execution the Sphinx zkVM encoded as bytes.
function verifyProof(
bytes32 vkey,
bytes memory publicValues,
Expand Down
12 changes: 6 additions & 6 deletions recursion/gnark-ffi/src/plonk_bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ impl PlonkBn254Prover {
build_plonk_bn254(build_dir.to_str().unwrap());

// Write the corresponding asset files to the build dir.
let sphinx_mock_verifier_path = build_dir.join("SP1MockVerifier.sol");
let sphinx_mock_verifier_str = include_str!("../assets/SP1MockVerifier.txt");
let sphinx_mock_verifier_path = build_dir.join("SphinxMockVerifier.sol");
let sphinx_mock_verifier_str = include_str!("../assets/SphinxMockVerifier.txt");
let mut mock_verifier_file = File::create(sphinx_mock_verifier_path).unwrap();
mock_verifier_file
.write_all(sphinx_mock_verifier_str.as_bytes())
.unwrap();

let sphinx_verifier_path = build_dir.join("SP1Verifier.sol");
let sphinx_verifier_str = include_str!("../assets/SP1Verifier.txt");
let sphinx_verifier_path = build_dir.join("SphinxVerifier.sol");
let sphinx_verifier_str = include_str!("../assets/SphinxVerifier.txt");
let mut sphinx_verifier_file = File::create(sphinx_verifier_path).unwrap();
sphinx_verifier_file
.write_all(sphinx_verifier_str.as_bytes())
.unwrap();

let interface_sphinx_verifier_path = build_dir.join("ISP1Verifier.sol");
let interface_sphinx_verifier_str = include_str!("../assets/ISP1Verifier.txt");
let interface_sphinx_verifier_path = build_dir.join("ISphinxVerifier.sol");
let interface_sphinx_verifier_str = include_str!("../assets/ISphinxVerifier.txt");
let mut interface_sphinx_verifier_file =
File::create(interface_sphinx_verifier_path).unwrap();
interface_sphinx_verifier_file
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub fn export_solidity_plonk_bn254_verifier(output_dir: impl Into<PathBuf>) -> R
let artifacts_dir = if sphinx_prover::build::sphinx_dev_mode() {
sphinx_prover::build::plonk_bn254_artifacts_dev_dir()
} else {
try_install_plonk_bn254_artifacts()
try_install_plonk_bn254_artifacts(true)
};
let verifier_path = artifacts_dir.join("SP1Verifier.sol");
let verifier_path = artifacts_dir.join("SphinxVerifier.sol");

if !verifier_path.exists() {
return Err(anyhow::anyhow!(
Expand All @@ -29,7 +29,7 @@ pub fn export_solidity_plonk_bn254_verifier(output_dir: impl Into<PathBuf>) -> R
}

std::fs::create_dir_all(&output_dir).context("Failed to create output directory.")?;
let output_path = output_dir.join("SP1Verifier.sol");
let output_path = output_dir.join("SphinxVerifier.sol");
std::fs::copy(&verifier_path, &output_path).context("Failed to copy verifier file.")?;
tracing::info!(
"exported verifier from {} to {}",
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/provers/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Prover for LocalProver {
&outer_proof.proof,
)
} else {
sphinx_prover::build::try_install_plonk_bn254_artifacts()
sphinx_prover::build::try_install_plonk_bn254_artifacts(true)
};
let proof = self.prover.wrap_plonk_bn254(outer_proof, &plonk_bn254_aritfacts);
Ok(SphinxProofWithPublicValues {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/provers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub trait Prover: Send + Sync {
let plonk_bn254_aritfacts = if sphinx_prover::build::sphinx_dev_mode() {
sphinx_prover::build::plonk_bn254_artifacts_dev_dir()
} else {
sphinx_prover::build::try_install_plonk_bn254_artifacts()
sphinx_prover::build::try_install_plonk_bn254_artifacts(true)
};
sphinx_prover.verify_plonk_bn254(
&proof.proof,
Expand Down

0 comments on commit b848fc2

Please sign in to comment.