From 03686254531c3e678344a91e055bd82946110dc8 Mon Sep 17 00:00:00 2001 From: steebchen Date: Thu, 18 Jul 2024 17:18:54 +0200 Subject: [PATCH] revamp --- .dockerignore | 3 ++ .github/workflows/ci.yml | 20 ++++++++ .tool-versions | 1 + README.md | 108 +++++++++++++++++++-------------------- scripts/0-venv.sh | 13 +++-- scripts/1-compile.sh | 12 +++-- scripts/2-merge.sh | 2 + scripts/combine_json.py | 6 +-- 8 files changed, 99 insertions(+), 66 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/ci.yml create mode 100644 .tool-versions diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c6eb944 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.idea/ +.git/ +target/ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..810caee --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,20 @@ +name: ci +on: push +env: + CARGO_TERM_COLOR: always +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build + run: cargo build --verbose + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: asdf-vm/actions/install@v3 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --workspace --verbose diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..20e21fa --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +python 3.9.18 diff --git a/README.md b/README.md index a49467b..c67ad74 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ The Prover SDK is a Rust library for interacting with the Prover service. It pro Before using the prover key has to be authorized by the prover operator. To generate the key use: ```bash - cargo run --bin keygen +cargo run --bin keygen ``` It will output 2 keys. @@ -20,26 +20,24 @@ It will output 2 keys. First parse a private key corresponding to an authorized public key. ```rust - ProverAccessKey::from_hex_string( - "0xf91350db1ca372b54376b519be8bf73a7bbbbefc4ffe169797bc3f5ea2dec740", - ) - .unwrap() - +ProverAccessKey::from_hex_string( + "0xf91350db1ca372b54376b519be8bf73a7bbbbefc4ffe169797bc3f5ea2dec740", +) +.unwrap() ``` Then construct an instance with ```rust - let prover_url = Url::parse("http://localhost:3000").unwrap(); - let sdk = ProverSDK::new(key, prover_url).await?; - +let prover_url = Url::parse("http://localhost:3000").unwrap(); +let sdk = ProverSDK::new(key, prover_url).await?; ``` Then you can use below to prove an execution ```rust - let data = load_cairo1(PathBuf::from("../prover/resources/input_cairo1.json")).await?; - let proof = sdk.prove_cairo1(data).await; +let data = load_cairo1(PathBuf::from("../prover/resources/input_cairo1.json")).await?; +let proof = sdk.prove_cairo1(data).await; ``` # Operating a prover @@ -49,7 +47,7 @@ To run the sdk first make sure prover/authorized_keys.json contains your public_ Run the following command in your terminal: ```bash - cargo run -p prover -- --jwt-secret-key --message-expiration-time --session-expiration-time --authorized-keys , +cargo run -p prover -- --jwt-secret-key --message-expiration-time --session-expiration-time --authorized-keys , ``` Alternatively use the flag `--authorized-keys-path authorized_keys.json` instead of `--authorized-keys` to load keys from a json file. It needs to have the format below @@ -75,59 +73,59 @@ To use the SDK, follow these steps: Authenticate with the Prover service using your private key and the authentication URL: -``` - #[tokio::main] - async fn main() -> Result<(), ProverSdkErrors> { - let private_key_hex : String= env::var("PRIVATE_KEY")?; - let url_auth = "http://localhost:3000/auth"; - let url_prover = "http://localhost:3000/prove/cairo1"; - - let result = ProverSDK::new(url_auth, url_prover) - .auth(&private_key_hex) - .await?; - - // Handle authentication result - Ok(()) - } +```rust +#[tokio::main] +async fn main() -> Result<(), ProverSdkErrors> { + let private_key_hex : String= env::var("PRIVATE_KEY")?; + let url_auth = "http://localhost:3000/auth"; + let url_prover = "http://localhost:3000/prove/cairo1"; + + let result = ProverSDK::new(url_auth, url_prover) + .auth(&private_key_hex) + .await?; + + // Handle authentication result + Ok(()) +} ``` Use the SDK to prove data: -``` - #[tokio::main] - async fn main() -> Result<(), ProverSdkErrors> { - // Authentication code goes here... +```rust +#[tokio::main] +async fn main() -> Result<(), ProverSdkErrors> { + // Authentication code goes here... - let sdk = result.build()?; - let data = read_json_file("resources/input.json").await?; - let proof = sdk.prove(data).await?; + let sdk = result.build()?; + let data = read_json_file("resources/input.json").await?; + let proof = sdk.prove(data).await?; - // Handle proof result - Ok(()) - } + // Handle proof result + Ok(()) +} ``` Handle errors using the provided error types: -``` - #[tokio::main] - async fn main() -> Result<(), ProverSdkErrors> { - // Authentication code goes here... - - let result = ProverSDK::new(url_auth, url_prover) - .auth(&private_key_hex) - .await; - - match result { - Ok(sdk) => { - // Continue with SDK usage... - } - Err(err) => { - // Handle authentication error - println!("Authentication failed: {}", err); - } - } +```rust +#[tokio::main] +async fn main() -> Result<(), ProverSdkErrors> { + // Authentication code goes here... - Ok(()) + let result = ProverSDK::new(url_auth, url_prover) + .auth(&private_key_hex) + .await; + + match result { + Ok(sdk) => { + // Continue with SDK usage... + } + Err(err) => { + // Handle authentication error + println!("Authentication failed: {}", err); + } } + + Ok(()) +} ``` diff --git a/scripts/0-venv.sh b/scripts/0-venv.sh index 415da29..542feb3 100644 --- a/scripts/0-venv.sh +++ b/scripts/0-venv.sh @@ -1,25 +1,28 @@ #!/usr/bin/env bash +set -eux + # Define the name of the virtual environment VENV_NAME=".venv" # Check if the virtual environment already exists if [ ! -d "$VENV_NAME" ]; then # If it doesn't exist, create the virtual environment - python -m venv $VENV_NAME && \ + python -m venv $VENV_NAME # Activate the virtual environment - source $VENV_NAME/bin/activate && \ + source $VENV_NAME/bin/activate # Upgrade pip to the latest version - pip install --upgrade pip && \ + pip install --upgrade pip # Install the required packages from the requirements.txt file - pip install cairo-lang==0.13.1 && \ + pip install sympy==1.12.1 + pip install cairo-lang==0.13.1 # Deactivate the virtual environment deactivate else # If it does exist, print a message indicating that it already exists echo "Virtual environment $VENV_NAME already exists." -fi \ No newline at end of file +fi diff --git a/scripts/1-compile.sh b/scripts/1-compile.sh index 835a5b7..58aba76 100644 --- a/scripts/1-compile.sh +++ b/scripts/1-compile.sh @@ -1,8 +1,14 @@ #!/usr/bin/env bash -source .venv/bin/activate && \ +set -eux + +source .venv/bin/activate + +mkdir -p resources + cairo-compile \ examples/CairoZero/fibonacci.cairo \ --output resources/fibonacci_compiled.json \ - --proof_mode && \ -deactivate \ No newline at end of file + --proof_mode + +deactivate diff --git a/scripts/2-merge.sh b/scripts/2-merge.sh index 07e6a08..88115a7 100644 --- a/scripts/2-merge.sh +++ b/scripts/2-merge.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -eux + # This script assumes Python is installed and accessible from the command line as 'python' # Variables for JSON files and output diff --git a/scripts/combine_json.py b/scripts/combine_json.py index d91982f..ecabbd6 100644 --- a/scripts/combine_json.py +++ b/scripts/combine_json.py @@ -3,16 +3,16 @@ def combine_json_files(file1, file2, layout, output_file): with open(file1, 'r') as f: data1 = json.load(f) - + with open(file2, 'r') as f: data2 = json.load(f) - + combined_data = { "program_input": data2, "layout": layout, "program": data1 } - + with open(output_file, 'w') as f: json.dump(combined_data, f, indent=4)