This tutorial guides you through creating, building, and deploying a zero-knowledge program using Bonsol on Solana. By the end, you'll understand how to create ZK proofs that can be verified on-chain.
Let's examine the simple ZK program provided in the repo at bonsol/images/simple/src/main.rs.
# bonsol/images/simple/src/main.rs
use gjson::Kind;
use risc0_zkvm::{guest::{env, sha::Impl},sha::{Digest, Sha256}};
fn main() {
let mut public1 = Vec::new();
env::read_slice(&mut public1);
let publici1 = String::from_utf8(public1).unwrap();
let mut private2 = Vec::new();
env::read_slice(&mut private2);
let privatei2 = String::from_utf8(private2).unwrap();
let valid = gjson::valid(&publici1);
let mut res = 0;
if valid {
let val = gjson::get(&publici1, "attestation");
if val.kind() == Kind::String && val.str() == privatei2 {
res = 1;
}
}
let digest = Impl::hash_bytes(
&[
publici1.as_bytes(),
privatei2.as_bytes(),
].concat(),
);
env::commit_slice(digest.as_bytes());
env::commit_slice(&[res]);
}
This simple program demonstrates private input validation, where only the prover knows the private input, but anyone can verify the result. Here's how the program works:
Reads two inputs
public1: A JSON string with an "attestation" field
private2: A private string to compare against the attestation
Validates if
The public input is valid JSON
The "attestation" field in the JSON matches the private input
Outputs
A cryptographic digest of both inputs
A result (1 for match, 0 for no match)
Building the ZK program
Now that we understand the program, let's build it:
bonsol build --zk-program-path ./images/simple
This compiles the Rust code into a format compatible with the RISC Zero VM and generates a manifest.json file containing:
Next, deploy the program to an S3 bucket and register it on-chain:
$ bonsol deploy s3 \
--bucket bonsol \
--region us-east-1 \
--access-key $AWS_ACCESS_KEY_ID \
--secret-key $AWS_SECRET_ACCESS_KEY \
--manifest-path ./images/simple/manifest.json
Uploaded to S3 url https://bonsol.s3.us-east-1.amazonaws.com/simple2-ec93e0a9592a2f00c177a7fce6ff191019740ff83f589e334153126c02f5772e
Deploying to Solana, which will cost real money. Are you sure you want to continue? (y/n)
y
ec93e0a9592a2f00c177a7fce6ff191019740ff83f589e334153126c02f5772e deployed
💡 Note: When deploying to mainnet, this operation costs SOL to register your program on-chain.
Creating and submitting an execution request
Edit the execution request
Locate the sample execution request template at bonsol/charts/input_files/simple_execution_request.json. Update the template with your specific imageId from the manifest.jsonfile and remove the callback configuration (we'll cover callbacks in a separate tutorial):