20 releases (6 breaking)

new 0.7.0-rc.1 Mar 1, 2024
0.6.0 Jan 29, 2024
0.6.0-rc.1 Dec 19, 2023
0.5.1 Nov 9, 2023
0.2.0 Jul 11, 2023

#627 in Cryptography

Download history 1305/week @ 2023-11-09 833/week @ 2023-11-16 594/week @ 2023-11-23 542/week @ 2023-11-30 605/week @ 2023-12-07 791/week @ 2023-12-14 534/week @ 2023-12-21 415/week @ 2023-12-28 953/week @ 2024-01-04 756/week @ 2024-01-11 1057/week @ 2024-01-18 964/week @ 2024-01-25 938/week @ 2024-02-01 757/week @ 2024-02-08 1175/week @ 2024-02-15 1522/week @ 2024-02-22

4,591 downloads per month
Used in 3 crates

Apache-2.0

49KB
733 lines

A library to handle HTTP REST requests to the Bonsai-alpha prover interface

Example Usage

use std::time::Duration;

use anyhow::Result;
use bonsai_sdk::alpha as bonsai_sdk;
use methods::{METHOD_ELF, METHOD_ID};
use risc0_zkvm::{compute_image_id, serde::to_vec, Receipt};

fn run_bonsai(input_data: Vec<u8>) -> Result<()> {
    let client = bonsai_sdk::Client::from_env(risc0_zkvm::VERSION)?;

    // Compute the image_id, then upload the ELF with the image_id as its key.
    let image_id = hex::encode(compute_image_id(METHOD_ELF)?);
    client.upload_img(&image_id, METHOD_ELF.to_vec())?;

    // Prepare input data and upload it.
    let input_data = to_vec(&input_data).unwrap();
    let input_data = bytemuck::cast_slice(&input_data).to_vec();
    let input_id = client.upload_input(input_data)?;

    // Add a list of assumptions
    let assumptions: Vec<String> = vec![];

    // Start a session running the prover
    let session = client.create_session(image_id, input_id, assumptions)?;
    loop {
        let res = session.status(&client)?;
        if res.status == "RUNNING" {
            eprintln!(
                "Current status: {} - state: {} - continue polling...",
                res.status,
                res.state.unwrap_or_default()
            );
            std::thread::sleep(Duration::from_secs(15));
            continue;
        }
        if res.status == "SUCCEEDED" {
            // Download the receipt, containing the output
            let receipt_url = res
                .receipt_url
                .expect("API error, missing receipt on completed session");

            let receipt_buf = client.download(&receipt_url)?;
            let receipt: Receipt = bincode::deserialize(&receipt_buf)?;
            receipt
                .verify(METHOD_ID)
                .expect("Receipt verification failed");
        } else {
            panic!(
                "Workflow exited: {} - | err: {}",
                res.status,
                res.error_msg.unwrap_or_default()
            );
        }

        break;
    }

    // Optionally run stark2snark
    // run_stark2snark(session.uuid)?;

    Ok(())
}

STARK to SNARK

After a STARK proof is generated, it is possible to convert the proof to SNARK.

Example

use std::time::Duration;

use anyhow::Result;
use bonsai_sdk::alpha as bonsai_sdk;

fn run_stark2snark(session_id: String) -> Result<()> {
    let client = bonsai_sdk::Client::from_env(risc0_zkvm::VERSION)?;

    let snark_session = client.create_snark(session_id)?;
    eprintln!("Created snark session: {}", snark_session.uuid);
    loop {
        let res = snark_session.status(&client)?;
        match res.status.as_str() {
            "RUNNING" => {
                eprintln!("Current status: {} - continue polling...", res.status,);
                std::thread::sleep(Duration::from_secs(15));
                continue;
            }
            "SUCCEEDED" => {
                let snark_receipt = res.output;
                eprintln!("Snark proof!: {snark_receipt:?}");
                break;
            }
            _ => {
                panic!(
                    "Workflow exited: {} err: {}",
                    res.status,
                    res.error_msg.unwrap_or_default()
                );
            }
        }
    }
    Ok(())
}

Dependencies

~4–18MB
~257K SLoC