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

fix(install): Add workspaces when install script executed #36

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ bincode = "1.3.3"
rpassword = "7.3.1"
env_logger = "0.11.3"
log = "0.4.22"
# std::env::dir is deprecated on windows
dirs = "5.0.0"
27 changes: 22 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
install: install_sp1 install_risc0
SHELL := $(shell echo $$SHELL)

# Find the profile directory based on the shell
ifeq ($(findstring bash,$(SHELL)),bash)
PROFILE := ~/.bashrc
else ifeq ($(findstring zsh,$(SHELL)),zsh)
PROFILE := ~/.zshenv
else ifeq ($(findstring fish,$(SHELL)),fish)
PROFILE := ~/.config/fish/config.fish
else ifeq ($(findstring sh,$(SHELL)),sh)
PROFILE := ~/.profile
else
echo "zkrust: could not detect shell, manually add ${ZKRUST_BIN_DIR} to your PATH."
exit 1
endif

install: install_risc0 install_sp1

install_sp1:
@curl -L https://sp1.succinct.xyz | bash
@source $(PROFILE)
@sp1up
@cargo prove --version

Expand Down Expand Up @@ -52,14 +69,14 @@ prove_sp1_ecdsa:
prove_sp1_json:
@RUST_LOG=info cargo run --release -- prove-sp1 examples/json

prove_risc0_regex:
prove_sp1_regex:
@RUST_LOG=info cargo run --release -- prove-risc0 examples/regex

prove_risc0_sha:
prove_sp1_sha:
@RUST_LOG=info cargo run --release -- prove-risc0 examples/sha

prove_risc0_tendermint:
prove_sp1_tendermint:
@RUST_LOG=info cargo run --release -- prove-risc0 examples/tendermint

prove_risc0_zkquiz:
prove_sp1_zkquiz:
@RUST_LOG=info cargo run --release -- prove-risc0 examples/zkquiz
15 changes: 13 additions & 2 deletions install_zkrust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ BASE_DIR=$HOME
ZKRUST_DIR="${ZKRUST_DIR-"$BASE_DIR/.zkRust"}"
ZKRUST_BIN_DIR="$ZKRUST_DIR/bin"
ZKRUST_BIN_PATH="$ZKRUST_BIN_DIR/zkRust"
ZKRUST_GIT_REPO_URL="https://github.com/yetanotherco/zkRust.git"
CURRENT_TAG=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/yetanotherco/zkRust/releases/latest \
| grep '"tag_name":' | awk -F'"' '{print $4}')
RELEASE_URL="https://github.com/yetanotherco/zkRust/releases/download/$CURRENT_TAG/"

ARCH=$(uname -m)

if [ "$ARCH" == "x86_64" ]; then
Expand Down Expand Up @@ -86,4 +86,15 @@ sp1up
cargo prove --version
echo "Sp1 Toolchain Installed"

echo "Run 'source $PROFILE' or start a new terminal session to use aligned."
# Clone the specific directory structure from the Git repository

echo "Cloning Workspaces..."
git clone "$ZKRUST_GIT_REPO_URL" "$ZKRUST_DIR/zkRust"

# Copy the directory structure from the cloned repository to the .zkRust folder
cp -r "$ZKRUST_DIR/zkRust/workspaces" "$ZKRUST_DIR/."

# Clean up the cloned repository
rm -rf "$ZKRUST_DIR/zkRust"

echo "Run 'source $PROFILE' or start a new terminal session to use zkRust!"
96 changes: 61 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use log::error;
use log::info;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use tokio::io;
use zkRust::{risc0, sp1, submit_proof_to_aligned, utils, ProofArgs};

Expand Down Expand Up @@ -32,25 +33,36 @@ async fn main() -> io::Result<()> {
match &cli.command {
Commands::ProveSp1(args) => {
info!("Proving with SP1, program in: {}", args.guest_path);

// Perform sanitation checks on directory
if utils::validate_directory_structure(&args.guest_path) {
//Ensure `~/.zkRust/workspaces/exists
let Some(home_dir) = dirs::home_dir() else {
error!("Failed to locate Home Dir");
return Ok(());
};
let Ok(current_dir) = std::env::current_dir() else {
error!("Failed to get Current Directory");
return Ok(());
};
let home_dir = home_dir.join(".zkRust");
utils::prepare_workspace(
&args.guest_path,
sp1::SP1_SRC_DIR,
sp1::SP1_GUEST_CARGO_TOML,
"./workspaces/sp1/script",
"./workspaces/sp1/script/Cargo.toml",
sp1::SP1_BASE_HOST_CARGO_TOML,
sp1::SP1_BASE_GUEST_CARGO_TOML,
&PathBuf::from(&args.guest_path),
&home_dir.join(sp1::SP1_SRC_DIR),
&home_dir.join(sp1::SP1_GUEST_CARGO_TOML),
&home_dir.join("workspaces/sp1/script"),
&home_dir.join("workspaces/sp1/script/Cargo.toml"),
&home_dir.join(sp1::SP1_BASE_HOST_CARGO_TOML),
&home_dir.join(sp1::SP1_BASE_GUEST_CARGO_TOML),
)?;

let Ok(imports) = utils::get_imports(sp1::SP1_GUEST_MAIN) else {
let Ok(imports) = utils::get_imports(&home_dir.join(sp1::SP1_GUEST_MAIN)) else {
error!("Failed to Extract Imports");
return Ok(());
};

let main_path = home_dir.join(&sp1::SP1_GUEST_MAIN);
let Ok(function_bodies) = utils::extract_function_bodies(
sp1::SP1_GUEST_MAIN,
&main_path,
vec![
"fn main()".to_string(),
"fn input()".to_string(),
Expand All @@ -73,22 +85,23 @@ async fn main() -> io::Result<()> {
sp1::SP1_GUEST_PROGRAM_HEADER,
sp1::SP1_IO_READ,
sp1::SP1_IO_COMMIT,
sp1::SP1_GUEST_MAIN,
&home_dir.join(sp1::SP1_GUEST_MAIN),
)?;
sp1::prepare_host(&function_bodies[1], &function_bodies[2], &imports)?;
sp1::prepare_host(&function_bodies[1], &function_bodies[2], &imports, &home_dir.join(sp1::SP1_BASE_HOST), &home_dir.join(sp1::SP1_HOST_MAIN))?;

if args.precompiles {
let mut toml_file = OpenOptions::new()
.append(true) // Open the file in append mode
.open(sp1::SP1_GUEST_CARGO_TOML)?;
.open(&home_dir.join(sp1::SP1_GUEST_CARGO_TOML))?;

writeln!(toml_file, "{}", sp1::SP1_ACCELERATION_IMPORT)?;
}

if sp1::generate_sp1_proof()?.success() {
let script_dir = home_dir.join(sp1::SP1_SCRIPT_DIR);
if sp1::generate_sp1_proof(&script_dir, &current_dir)?.success() {
info!("SP1 proof and ELF generated");

utils::replace(sp1::SP1_GUEST_CARGO_TOML, sp1::SP1_ACCELERATION_IMPORT, "")?;
utils::replace(&home_dir.join(sp1::SP1_GUEST_CARGO_TOML), sp1::SP1_ACCELERATION_IMPORT, "")?;

// Submit to aligned
if args.submit_to_aligned {
Expand All @@ -107,16 +120,16 @@ async fn main() -> io::Result<()> {
info!("SP1 proof submitted and verified on Aligned");
}

std::fs::copy(sp1::SP1_BASE_HOST_FILE, sp1::SP1_HOST_MAIN).map_err(|e| {
std::fs::copy(home_dir.join(sp1::SP1_BASE_HOST_FILE), &home_dir.join(sp1::SP1_HOST_MAIN)).map_err(|e| {
error!("Failed to clear SP1 Host File");
e
})?;

return Ok(());
}
info!("SP1 proof generation failed");

// Clear host
std::fs::copy(sp1::SP1_BASE_HOST_FILE, sp1::SP1_HOST_MAIN)?;
std::fs::copy(&home_dir.join(sp1::SP1_BASE_HOST_FILE), &home_dir.join(sp1::SP1_HOST_MAIN))?;
return Ok(());
} else {
error!("zkRust Directory structure incorrect please consult the README",);
Expand All @@ -128,23 +141,34 @@ async fn main() -> io::Result<()> {
info!("Proving with Risc0, program in: {}", args.guest_path);

// Perform sanitation checks on directory
if utils::validate_directory_structure(&args.guest_path) {
if utils::validate_directory_structure(&args.guest_path) {
//Ensure `~/.zkRust/workspaces/exists
let Some(home_dir) = dirs::home_dir() else {
error!("Failed to Locate Home Dir");
return Ok(());
};
let Ok(current_dir) = std::env::current_dir() else {
error!("Failed to get Current Directory");
return Ok(());
};
let home_dir = home_dir.join(".zkRust");
utils::prepare_workspace(
&args.guest_path,
risc0::RISC0_SRC_DIR,
risc0::RISC0_GUEST_CARGO_TOML,
"./workspaces/risc0/host",
"./workspaces/risc0/host/Cargo.toml",
risc0::RISC0_BASE_HOST_CARGO_TOML,
risc0::RISC0_BASE_GUEST_CARGO_TOML,
&PathBuf::from(&args.guest_path),
&home_dir.join(risc0::RISC0_SRC_DIR),
&home_dir.join(risc0::RISC0_GUEST_CARGO_TOML),
&home_dir.join("workspaces/risc0/host"),
&home_dir.join("workspaces/risc0/host/Cargo.toml"),
&home_dir.join(risc0::RISC0_BASE_HOST_CARGO_TOML),
&home_dir.join(risc0::RISC0_BASE_GUEST_CARGO_TOML),
)?;

let Ok(imports) = utils::get_imports(risc0::RISC0_GUEST_MAIN) else {
let Ok(imports) = utils::get_imports(&home_dir.join(risc0::RISC0_GUEST_MAIN)) else {
error!("Failed to Extract Imports");
return Ok(());
};
let main_path = home_dir.join(&risc0::RISC0_GUEST_MAIN);
let Ok(function_bodies) = utils::extract_function_bodies(
risc0::RISC0_GUEST_MAIN,
&main_path,
vec![
"fn main()".to_string(),
"fn input()".to_string(),
Expand All @@ -168,21 +192,24 @@ async fn main() -> io::Result<()> {
risc0::RISC0_GUEST_PROGRAM_HEADER,
risc0::RISC0_IO_READ,
risc0::RISC0_IO_COMMIT,
risc0::RISC0_GUEST_MAIN,
&home_dir.join(risc0::RISC0_GUEST_MAIN),
)?;
risc0::prepare_host(&function_bodies[1], &function_bodies[2], &imports)?;
risc0::prepare_host(&function_bodies[1], &function_bodies[2], &imports, &home_dir.join(risc0::RISC0_BASE_HOST), &home_dir.join(risc0::RISC0_HOST_MAIN))?;

if args.precompiles {
let mut toml_file = OpenOptions::new()
.append(true)
.open(risc0::RISC0_GUEST_CARGO_TOML)?;
.open(&home_dir.join(risc0::RISC0_GUEST_CARGO_TOML))?;

writeln!(toml_file, "{}", risc0::RISC0_ACCELERATION_IMPORT)?;
}

if risc0::generate_risc0_proof()?.success() {
let workspace_dir = home_dir.join(risc0::RISC0_WORKSPACE_DIR);
if risc0::generate_risc0_proof(&workspace_dir, &current_dir)?.success() {
info!("Risc0 proof and Image ID generated");

utils::replace(&home_dir.join(risc0::RISC0_GUEST_CARGO_TOML), risc0::RISC0_ACCELERATION_IMPORT, "")?;

// Submit to aligned
if args.submit_to_aligned {
submit_proof_to_aligned(
Expand All @@ -202,19 +229,18 @@ async fn main() -> io::Result<()> {
}

// Clear Host file
std::fs::copy(risc0::RISC0_BASE_HOST_FILE, risc0::RISC0_HOST_MAIN).map_err(
std::fs::copy(&home_dir.join(risc0::RISC0_BASE_HOST_FILE), &home_dir.join(risc0::RISC0_HOST_MAIN)).map_err(
|e| {
error!("Failed to Clear Risc0 Host File");
e
},
)?;

return Ok(());
}
info!("Risc0 proof generation failed");

// Clear Host file
std::fs::copy(risc0::RISC0_BASE_HOST_FILE, risc0::RISC0_HOST_MAIN)?;
std::fs::copy(&home_dir.join(risc0::RISC0_BASE_HOST_FILE), &home_dir.join(risc0::RISC0_HOST_MAIN))?;
return Ok(());
} else {
error!("zkRust Directory structure incorrect please consult the README",);
Expand Down
38 changes: 19 additions & 19 deletions src/risc0.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use std::{
fs,
io::{self, Write},
process::{Command, ExitStatus},
fs, io::{self, Write}, path::PathBuf, process::{Command, ExitStatus}
};

use crate::utils;

/// RISC0 workspace directories
pub const RISC0_WORKSPACE_DIR: &str = "./workspaces/risc0/";
pub const RISC0_SRC_DIR: &str = "./workspaces/risc0/methods/guest";
pub const RISC0_GUEST_MAIN: &str = "./workspaces/risc0/methods/guest/src/main.rs";
pub const RISC0_HOST_MAIN: &str = "./workspaces/risc0/host/src/main.rs";
pub const RISC0_BASE_HOST_CARGO_TOML: &str = "./workspaces/base_files/risc0/cargo_host";
pub const RISC0_BASE_GUEST_CARGO_TOML: &str = "./workspaces/base_files/risc0/cargo_guest";
pub const RISC0_BASE_HOST: &str = include_str!("../workspaces/base_files/risc0/host");
pub const RISC0_BASE_HOST_FILE: &str = "./workspaces/base_files/risc0/host";
pub const RISC0_GUEST_CARGO_TOML: &str = "./workspaces/risc0/methods/guest/Cargo.toml";
pub const RISC0_WORKSPACE_DIR: &str = "workspaces/risc0/";
pub const RISC0_SRC_DIR: &str = "workspaces/risc0/methods/guest";
pub const RISC0_GUEST_MAIN: &str = "workspaces/risc0/methods/guest/src/main.rs";
pub const RISC0_HOST_MAIN: &str = "workspaces/risc0/host/src/main.rs";
pub const RISC0_BASE_HOST_CARGO_TOML: &str = "workspaces/base_files/risc0/cargo_host";
pub const RISC0_BASE_GUEST_CARGO_TOML: &str = "workspaces/base_files/risc0/cargo_guest";
pub const RISC0_BASE_HOST: &str = "workspaces/base_files/risc0/host";
pub const RISC0_BASE_HOST_FILE: &str = "workspaces/base_files/risc0/host";
pub const RISC0_GUEST_CARGO_TOML: &str = "workspaces/risc0/methods/guest/Cargo.toml";

// Proof data generation paths
pub const PROOF_FILE_PATH: &str = "./proof_data/risc0/risc0.proof";
Expand All @@ -41,9 +39,10 @@ pub const RISC0_IO_WRITE: &str = "risc0_zkvm::guest::env::write";
pub const RISC0_IO_COMMIT: &str = "risc0_zkvm::guest::env::commit";
pub const RISC0_IO_OUT: &str = "receipt.journal.decode().unwrap();";

pub fn prepare_host(input: &str, output: &str, imports: &str) -> io::Result<()> {
pub fn prepare_host(input: &str, output: &str, imports: &str, host_dir: &PathBuf, host_main: &PathBuf) -> io::Result<()> {
let mut host_program = imports.to_string();
host_program.push_str(RISC0_BASE_HOST);
let contents = fs::read_to_string(host_dir)?;
host_program.push_str(&contents);

// Insert input body
let host_program = host_program.replace(utils::HOST_INPUT, input);
Expand All @@ -52,7 +51,7 @@ pub fn prepare_host(input: &str, output: &str, imports: &str) -> io::Result<()>

// Extract Variable names from host and add them to the ExecutorEnv::builder()
let values = utils::extract_regex(
RISC0_HOST_MAIN,
&host_main,
&format!("{}[(](.*?)[)]", regex::escape(utils::IO_WRITE)),
)?;

Expand All @@ -72,20 +71,21 @@ pub fn prepare_host(input: &str, output: &str, imports: &str) -> io::Result<()>
// replace zkRust::out()
let host_program = host_program.replace(utils::IO_OUT, RISC0_IO_OUT);

let mut file = fs::File::create(RISC0_HOST_MAIN)?;
let mut file = fs::File::create(host_main)?;
file.write_all(host_program.as_bytes())?;

utils::remove_lines(RISC0_HOST_MAIN, "zk_rust_io::write(")?;
utils::remove_lines(host_main, "zk_rust_io::write(")?;
Ok(())
}

/// Generates RISC0 proof and image ID
pub fn generate_risc0_proof() -> io::Result<ExitStatus> {
let guest_path = fs::canonicalize(RISC0_WORKSPACE_DIR)?;
pub fn generate_risc0_proof(guest_path: &PathBuf, current_dir: &PathBuf) -> io::Result<ExitStatus> {

Command::new("cargo")
.arg("run")
.arg("--release")
.arg("--")
.arg(current_dir)
.current_dir(guest_path)
.status()
}
Loading