Skip to content

Commit

Permalink
feat: inclusion + non-inclusion gnark circuits
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeytimoshin committed Apr 2, 2024
1 parent 2380564 commit 60f42a5
Show file tree
Hide file tree
Showing 51 changed files with 3,127 additions and 1,642 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/prover-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
build-and-test:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
timeout-minutes: 600
timeout-minutes: 2400
steps:
- name: Checkout sources
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion circuit-lib/circuit-lib.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "pnpm test-gnark",
"test-gnark": "pnpm gnark-prover && ts-mocha --resolveJsonModule ./tsconfig.json -t 100000000 tests/gnark.test.ts --exit",
"gnark-prover": "./scripts/prover.sh",
"gnark-prover": "./scripts/prover.sh \"inclusion non-inclusion\"",
"format": "prettier --write \"tests/**/*.{ts,js}\" \"src/**/*.{ts,js}\"",
"lint": "pnpm prettier \"tests/**/*.{ts,js}\" \"src/**/*.{ts,js}\" --check",
"build": "rimraf lib && pnpm tsc"
Expand Down
74 changes: 64 additions & 10 deletions circuit-lib/circuit-lib.js/scripts/prover.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,74 @@
#!/usr/bin/env bash

killall light-prover
kill_light_prover() {
killall light-prover || echo "light-prover process not found"
}

# Get the root directory of the Git repository
root_dir=$(git rev-parse --show-toplevel)
# Get the root directory of the Git repository (robust error handling)
root_dir=$(git rev-parse --show-toplevel 2>/dev/null) || {
echo "Error: Not in a Git repository or 'git' command not found."
exit 1
}

# Change the directory to 'gnark-prover' within the Git root directory
# shellcheck disable=SC2164
cd "$root_dir/gnark-prover"

# If 'gnark-prover' directory does not exist, print error and exit
if [ $? -ne 0 ]; then
echo "Directory gnark-prover does not exist in the Git root directory. Run \`git submodule update --init\` to fetch the submodule."
go build || {
echo "Build failed. Check for errors."
exit 1
}

if [[ $# -ne 1 ]]; then
echo "Error: Please provide a single argument containing light-prover options."
echo "Allowed options: inclusion, non-inclusion, combined (individually or combined)"
exit 1
fi

go build
options=($1)
inclusion=""
non_inclusion=""
combined=""

for option in "${options[@]}"; do
case $option in
inclusion)
inclusion="--inclusion=true"
;;
non-inclusion)
non_inclusion="--non-inclusion=true"
;;
combined)
combined="--combined=true"
;;
*)
echo "Error: Invalid option '$option'. Allowed options: inclusion, non-inclusion, combined"
exit 1
;;
esac
done

kill_light_prover && ./light-prover start $inclusion $non_inclusion $combined &
light_prover_pid=$!

health_check_url="http://localhost:3001/health"
timeout=120
interval=2

start_time=$(date +%s)

while true; do
status_code=$(curl -s -o /dev/null -w "%{http_code}" "$health_check_url")

if [[ "$status_code" -eq 200 ]]; then
echo "light-prover health check successful!"
break
fi

current_time=$(date +%s)
if (( current_time - start_time >= timeout )); then
echo "light-prover failed to start within $timeout seconds."
kill_light_prover
exit 1
fi

./light-prover start &
sleep "$interval"
done
63 changes: 41 additions & 22 deletions circuit-lib/circuitlib-rs/src/gnark/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
process::{Child, Command},
thread,
process::Command,
sync::atomic::{AtomicBool, Ordering},
time::Duration,
};

Expand All @@ -11,46 +11,65 @@ use serde_json::json;

use crate::gnark::constants::{HEALTH_CHECK, SERVER_ADDRESS};

pub fn spawn_gnark_server(path: &str, wait_time: u64) -> Child {
let server_process = Command::new("sh")
.arg("-c")
.arg(path)
.spawn()
.expect("Failed to start server process");

// Wait for the server to launch before proceeding.
thread::sleep(Duration::from_secs(wait_time));
static IS_LOADING: AtomicBool = AtomicBool::new(false);

server_process
pub async fn spawn_gnark_server(
path: &str,
restart: bool,
inclusion: bool,
non_inclusion: bool,
combined: bool,
) {
if restart {
kill_gnark_server();
}
if !health_check(1, 3).await && !IS_LOADING.load(Ordering::Relaxed) {
IS_LOADING.store(true, Ordering::Relaxed);
let mut arg: String = "".to_string();
if inclusion {
arg += "inclusion";
} else if non_inclusion {
arg += "non-inclusion";
} else if combined {
arg += "combined";
}
Command::new("sh")
.arg("-c")
.arg(format!("{} {}", path, arg))
.spawn()
.expect("Failed to start server process");
health_check(20, 5).await;
IS_LOADING.store(false, Ordering::Relaxed);
}
}

pub fn kill_gnark_server(gnark: &mut Child) {
pub fn kill_gnark_server() {
Command::new("sh")
.arg("-c")
.arg("killall light-prover")
.spawn()
.unwrap();
gnark.kill().unwrap();
}

pub async fn health_check() {
const MAX_RETRIES: usize = 20;
const TIMEOUT: usize = 5;

pub async fn health_check(retries: usize, timeout: usize) -> bool {
let client = reqwest::Client::new();

for _ in 0..MAX_RETRIES {
let mut result = false;
for _ in 0..retries {
match client
.get(&format!("{}{}", SERVER_ADDRESS, HEALTH_CHECK))
.send()
.await
{
Ok(_) => break,
Ok(_) => {
result = true;
break;
}
Err(_) => {
tokio::time::sleep(Duration::from_secs(TIMEOUT as u64)).await;
tokio::time::sleep(Duration::from_secs(timeout as u64)).await;
}
}
}
result
}

pub fn create_vec_of_string(number_of_utxos: usize, element: &BigInt) -> Vec<String> {
Expand Down
6 changes: 0 additions & 6 deletions circuit-lib/circuitlib-rs/src/verifying_keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,3 @@ mod ni_26_1;
pub use crate::verifying_keys::ni_26_1::VERIFYINGKEY as VK_ni_26_1;
mod ni_26_2;
pub use crate::verifying_keys::ni_26_2::VERIFYINGKEY as VK_ni_26_2;
mod ni_26_3;
pub use crate::verifying_keys::ni_26_3::VERIFYINGKEY as VK_ni_26_3;
mod ni_26_4;
pub use crate::verifying_keys::ni_26_4::VERIFYINGKEY as VK_ni_26_4;
mod ni_26_8;
pub use crate::verifying_keys::ni_26_8::VERIFYINGKEY as VK_ni_26_8;
87 changes: 0 additions & 87 deletions circuit-lib/circuitlib-rs/src/verifying_keys/ni_26_3/mod.rs

This file was deleted.

99 changes: 0 additions & 99 deletions circuit-lib/circuitlib-rs/src/verifying_keys/ni_26_4/mod.rs

This file was deleted.

Loading

0 comments on commit 60f42a5

Please sign in to comment.