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

feat: Use TOML for config #67

Merged
merged 15 commits into from
Feb 1, 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
4 changes: 2 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Prepare CI for iOS
run: ./scripts/prepare_ci.sh
- name: Build for iOS
run: ./scripts/build_ios.sh x86_64 debug
run: ./scripts/build_ios.sh config-example.toml
- name: Run iOS tests
run: |
cd mopro-ios/MoproKit/Example
Expand All @@ -51,7 +51,7 @@ jobs:
- name: Prepare CI for Core and FFI
run: ./scripts/prepare_ci.sh
- name: Build for Android
run: ./scripts/build_android.sh arm64 debug
run: ./scripts/build_android.sh config-example-android.toml
- name: Run core tests
run: cd mopro-core && cargo test -- --nocapture
- name: Run FFI tests
Expand Down
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ Zooming in a bit:

(Note that we require `uniffi-bindgen` to be `0.25`, if you have an older version you might need to remove this to re-install the latest).

### Configure settings

By creating a `toml` configuration file you can specify what build settings you want to use. Example is provided in `config-example.toml`:

```
# config-example.toml

[build]
# For iOS device_type can be x86_64, simulator, device
# For Android device_type can be x86_64, arm, arm64
device_type = "simulator" # Options: x86_64, simulator, device, arm, arm64

# debug is for Rust library to be in debug mode and release for release mode
# We recommend release mode by default for performance
build_mode = "release" # Options: debug, release

[circuit]
dir = "examples/circom/keccak256" # Directory of the circuit
name = "keccak256_256_test" # Name of the circuit

[dylib]
use_dylib = false # Options: true, false
name = "keccak256.dylib" # Name of the dylib file, only used if use_dylib is true
```

### iOS

#### Prepare
Expand All @@ -39,20 +64,17 @@ Zooming in a bit:

#### Build Bindings

To build bindings for iOS simulator debug mode, run
To build bindings for iOS, adjust settings in your config file (we recommend starting with `simulator` and `release`) and run:

```sh
./scripts/build_ios.sh simulator debug
./scripts/build_ios.sh config-example.toml
```

Open the `mopro-ios/MoproKit/Example/MoproKit.xcworkspace` in Xcode.

#### Update Bindings

To update bindings, run `./scripts/update_bindings.sh simulator|device debug|release`.

- `simulator` is for building library to run on iOS simulator, `device` is for running on a real device
- `debug` is for Rust library to be in debug mode and `release` for release mode
To update bindings, run `./scripts/update_bindings.sh config-example`.

### Android

Expand Down Expand Up @@ -80,7 +102,7 @@ To update bindings, run `./scripts/update_bindings.sh simulator|device debug|rel
To build bindings for android simulator debug mode, run

```sh
./scripts/build_android.sh arm64 debug
./scripts/build_android.sh config-example.toml
```

- **Device types:** `x86_64`, `x86`, `arm`, `arm64`
Expand Down
22 changes: 22 additions & 0 deletions config-example-android.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# config-example-android.toml

# Android specific configuration

[build]
# For iOS device_type can be x86_64, simulator, device
# For Android device_type can be x86_64, arm, arm64
device_type = "arm64" # Options: x86_64, simulator, device, arm, arm64

# debug is for Rust library to be in debug mode and release for release mode
# We recommend release mode by default for performance
build_mode = "debug" # Options: debug, release

[circuit]
dir = "examples/circom/keccak256" # Directory of the circuit
name = "keccak256_256_test" # Name of the circuit

[dylib]
# NOTE: Dylib support is experimental and requires some fiddling in iOS
# See https://github.com/oskarth/mopro/pull/37 and https://github.com/oskarth/mopro/pull/38
use_dylib = false # Options: true, false
name = "keccak256.dylib" # Name of the dylib file, only used if use_dylib is true
20 changes: 20 additions & 0 deletions config-example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# config-example.toml

[build]
# For iOS device_type can be x86_64, simulator, device
# For Android device_type can be x86_64, arm, arm64
device_type = "simulator" # Options: x86_64, simulator, device, arm, arm64

# debug is for Rust library to be in debug mode and release for release mode
# We recommend release mode by default for performance
build_mode = "release" # Options: debug, release

[circuit]
dir = "examples/circom/keccak256" # Directory of the circuit
name = "keccak256_256_test" # Name of the circuit

[dylib]
# NOTE: Dylib support is experimental and requires some fiddling in iOS
# See https://github.com/oskarth/mopro/pull/37 and https://github.com/oskarth/mopro/pull/38
use_dylib = false # Options: true, false
name = "keccak256.dylib" # Name of the dylib file, only used if use_dylib is true
11 changes: 7 additions & 4 deletions mopro-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ edition = "2021"
wasmer = { git = "https://github.com/oskarth/wasmer.git", rev = "09c7070" }

[features]
default = []
dylib = ["wasmer/dylib"]
default = ["wasmer/dylib"]
dylib = [] # NOTE: can probably remove this if we use env config instead
gpu-benchmarks = ["jemalloc-ctl", "jemallocator"]

[dependencies]
Expand Down Expand Up @@ -47,13 +47,16 @@ color-eyre = "=0.6.2"
criterion = "=0.3.6"

# GPU benchmarks
jemalloc-ctl = { version = "0.5.4", optional = true}
jemallocator = { version = "0.5.4", optional = true}
jemalloc-ctl = { version = "0.5.4", optional = true }
jemallocator = { version = "0.5.4", optional = true }

[build-dependencies]
color-eyre = "0.6"
enumset = "1.0.8"
wasmer = { git = "https://github.com/oskarth/wasmer.git", rev = "09c7070" }
toml = "0.8"
serde = "1.0"
serde_derive = "1.0"

[[bin]]
name = "generate_benchmark_report"
Expand Down
137 changes: 94 additions & 43 deletions mopro-core/build.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
use color_eyre::eyre::Result;
use std::env;
use serde::Deserialize;
use std::path::PathBuf;
use std::{env, fs};
use toml;

fn prepare_env(zkey_path: String, wasm_path: String, arkzkey_path: String) -> Result<()> {
let project_dir = env::var("CARGO_MANIFEST_DIR")?;
let zkey_file = PathBuf::from(&project_dir).join(zkey_path);
let wasm_file = PathBuf::from(&project_dir).join(wasm_path);
let arkzkey_file = PathBuf::from(&project_dir).join(arkzkey_path);

// TODO: Right now emitting as warnings for visibility, figure out better way to do this?
println!("cargo:warning=zkey_file: {}", zkey_file.display());
println!("cargo:warning=wasm_file: {}", wasm_file.display());
println!("cargo:warning=arkzkey_file: {}", arkzkey_file.display());

// Set BUILD_RS_ZKEY_FILE and BUILD_RS_WASM_FILE env var
println!("cargo:rustc-env=BUILD_RS_ZKEY_FILE={}", zkey_file.display());
println!("cargo:rustc-env=BUILD_RS_WASM_FILE={}", wasm_file.display());
println!(
"cargo:rustc-env=BUILD_RS_ARKZKEY_FILE={}",
arkzkey_file.display()
);
#[derive(Deserialize)]
struct Config {
circuit: CircuitConfig,
dylib: Option<DylibConfig>,
}

Ok(())
#[derive(Deserialize)]
struct CircuitConfig {
dir: String,
name: String,
}

#[derive(Deserialize)]
struct DylibConfig {
use_dylib: bool,
name: String,
}

#[cfg(feature = "dylib")]
fn build_dylib(wasm_path: String, dylib_name: String) -> Result<()> {
use std::path::Path;
use std::{fs, str::FromStr};
use std::str::FromStr;

use color_eyre::eyre::eyre;
use enumset::enum_set;
Expand Down Expand Up @@ -76,29 +73,83 @@ fn build_dylib(wasm_path: String, dylib_name: String) -> Result<()> {

Ok(())
}
fn build_circuit(config: &Config) -> Result<()> {
let project_dir = env::var("CARGO_MANIFEST_DIR")?;
let circuit_dir = &config.circuit.dir;
let circuit_name = &config.circuit.name;

let zkey_path = PathBuf::from(&project_dir).join(format!(
"{}/target/{}_final.zkey",
circuit_dir, circuit_name
));
let wasm_path = PathBuf::from(&project_dir).join(format!(
"{}/target/{}_js/{}.wasm",
circuit_dir, circuit_name, circuit_name
));
let arkzkey_path = PathBuf::from(&project_dir).join(format!(
"{}/target/{}_final.arkzkey",
circuit_dir, circuit_name
));

println!("cargo:warning=zkey_file: {}", zkey_path.display());
println!("cargo:warning=wasm_file: {}", wasm_path.display());
println!("cargo:warning=arkzkey_file: {}", arkzkey_path.display());

// Set BUILD_RS_* environment variables
println!("cargo:rustc-env=BUILD_RS_ZKEY_FILE={}", zkey_path.display());
println!("cargo:rustc-env=BUILD_RS_WASM_FILE={}", wasm_path.display());
println!(
"cargo:rustc-env=BUILD_RS_ARKZKEY_FILE={}",
arkzkey_path.display()
);

fn main() -> Result<()> {
// TODO: build_circuit function to builds all related artifacts, instead of doing this externally
let dir = "examples/circom/keccak256";
let circuit = "keccak256_256_test";

// XXX: Use RSA
// let dir = "examples/circom/rsa";
// let circuit = "main";

let zkey_path = format!("{}/target/{}_final.zkey", dir, circuit);
let wasm_path = format!("{}/target/{}_js/{}.wasm", dir, circuit, circuit);
// TODO: Need to modify script for this
let arkzkey_path = format!("{}/target/{}_final.arkzkey", dir, circuit);

prepare_env(zkey_path, wasm_path.clone(), arkzkey_path)?;
Ok(())
}

#[cfg(feature = "dylib")]
{
// TODO: This should depends on the circuit name
//let dylib_name = "keccak256.dylib";
let dylib_name = "rsa.dylib";
build_dylib(wasm_path.clone(), dylib_name.to_string())?;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config_str = match env::var("BUILD_CONFIG_PATH") {
Ok(config_path) => {
println!("cargo:warning=config: {}", config_path);
fs::read_to_string(config_path)?
}
Err(_) => {
println!("cargo:warning=BUILD_CONFIG_PATH not set. Using default configuration.");
// Default configuration
let default_config = r#"
[circuit]
dir = "examples/circom/keccak256"
name = "keccak256_256_test"

#[dylib]
use_dlib = false
#name = "keccak256.dylib"
"#;
default_config.to_string()
}
};

let config: Config = toml::from_str(&config_str)?;

// Build circuit
build_circuit(&config)?;

// Build dylib if enabled
if let Some(dylib_config) = &config.dylib {
if dylib_config.use_dylib {
println!("cargo:warning=Building dylib: {}", dylib_config.name);
build_dylib(
config.circuit.dir.clone()
+ "/target/"
+ &config.circuit.name
+ "_js/"
+ &config.circuit.name
+ ".wasm",
dylib_config.name.clone(),
)?;
} else {
println!("cargo:warning=Dylib usage is disabled in the config.");
}
}

Ok(())
}
37 changes: 37 additions & 0 deletions scripts/_prelude.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# _prelude.sh
# Commonly used shell utilities and functions, shared across scripts.

# Deal with errors
set -euo pipefail

# Color definitions
DEFAULT='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
GREY='\033[0;90m'

# NOTE: This is quite noisy so turning off by default
# Coloring the -x output (commands)
#DEBUG_COLOR="${DEFAULT}"
#trap 'echo -e ${DEBUG_COLOR}${BASH_COMMAND}${DEFAULT}' DEBUG

# Function to handle exit
handle_exit() {
# $? is a special variable that holds the exit code of the last command executed
if [ $? -ne 0 ]; then
echo -e "\n${RED}Script did not finish successfully!${DEFAULT}"
fi
}

# Set the trap
trap handle_exit EXIT

print_action() {
printf "\n${GREEN}$1${DEFAULT}\n"
}

print_warning() {
printf "\n${YELLOW}$1${DEFAULT}\n"
}
Loading
Loading