3 releases (breaking)
0.9.0-alpha | Mar 3, 2025 |
---|---|
0.8.0-alpha | Jan 21, 2025 |
0.7.3-alpha | Jan 15, 2025 |
#1370 in Filesystem
142 downloads per month
4MB
296 lines
Contains (ELF exe/lib, 4MB) binary/valida.gz, (ELF exe/lib, 215KB) test_data/program1, (ELF exe/lib, 215KB) test_data/program2
Valida VM API
Lita’s Valida zk-VM stack sets a new standard in zero-knowledge proving, leading in speed, efficiency, modularity and development productivity.
This crate is a wrapper around valida
executable and facilitates
usage of valida
by providing Rust API for
running, proving and verification of Valida programs.
lib.rs
:
Valida VM API
Lita’s Valida zk-VM stack sets a new standard in zero-knowledge proving, leading in speed, efficiency, modularity and development productivity.
This crate is a wrapper around valida
executable and facilitates
usage of valida
by providing Rust API for
running, proving and verification of Valida programs.
Two examples are attached.
The first one proves program1
. The other one proves program2
.
Their sources and prebuilt versions are included in the crate
in the folder test_data
.
program1
reads an integer from stdin, increments it and prints result to stdout.
program2
just unconditionally terminates execution with __valida_builtin_fail()
.
Example 1
#[cfg(target_arch = "x86_64")]
use valida_vm_api_linux_x86::*;
#[cfg(target_arch = "arm")]
use valida_vm_api_linux_arm::*;
use tempfile::NamedTempFile;
use tmpfile_helper::*;
use std::fs;
use std::path::Path;
use std::default;
fn main() {
let program = Path::new("test_data").join("program1");
let valida = create_valida().unwrap();
// stdin is an ASCII representation of character 'a'
let stdin = bytes_to_temp_file("a".as_bytes()).unwrap();
let stdout = NamedTempFile::new().unwrap();
let run_status = valida.run(
&program,
stdout.as_ref(),
stdin.as_ref(),
Default::default());
// Check that program terminated with success, i.e. STOP opcode
assert_eq!(run_status, RunStatus::TerminatedWithStop);
let stdout_content = fs::read_to_string(stdout.as_ref()).unwrap();
// Check that stdout contains ('a' + 1)
assert_eq!(stdout_content, "b");
let proof = NamedTempFile::new().unwrap();
let prove_status = valida.prove(
&program, proof.as_ref(),
stdin.as_ref(),
Default::default(),
Default::default());
// Proving of a program that terminates with STOP opcode must succeed
assert_eq!(prove_status, ProveStatus::Success);
let verify_status_correct_statement = valida.verify(
&program,
proof.as_ref(),
stdout.as_ref(),
Default::default(),
Default::default());
// Verification of a program that terminates with STOP opcode and outputs 'b' must succeed
assert_eq!(verify_status_correct_statement, VerifyStatus::Success);
let incorrect_stdout = bytes_to_temp_file("c".as_bytes()).unwrap();
let verify_status_incorrect_statement = valida.verify(
&program,
proof.as_ref(),
incorrect_stdout.as_ref(),
Default::default(),
Default::default());
// Verification of a program that terminates with STOP opcode
// and outputs something else than `b` must fail
assert_eq!(verify_status_incorrect_statement, VerifyStatus::Failure);
}
Example 2
#[cfg(target_arch = "x86_64")]
use valida_vm_api_linux_x86::*;
#[cfg(target_arch = "arm")]
use valida_vm_api_linux_arm::*;
use tempfile::NamedTempFile;
use tmpfile_helper::*;
use std::fs;
use std::path::Path;
use std::default;
fn main() {
let program = Path::new("test_data").join("program2");
let valida = create_valida().unwrap();
// stdin and stdout do not matter for this program
let stdin = bytes_to_temp_file("".as_bytes()).unwrap();
let stdout = NamedTempFile::new().unwrap();
let run_status = valida.run(
&program,
stdout.as_ref(),
stdin.as_ref(),
Default::default());
// Check that program terminated with failure, i.e. FAIL opcode
assert_eq!(run_status, RunStatus::TerminatedWithFail);
let proof = NamedTempFile::new().unwrap();
let prove_status = valida.prove(
&program,
proof.as_ref(),
stdin.as_ref(),
Default::default(),
Default::default());
// Proving of a program that terminates with STOP opcode may succeed
assert!(prove_status == ProveStatus::Success || prove_status == ProveStatus::Failure);
let verify_status = valida.verify(
&program,
proof.as_ref(),
stdout.as_ref(),
Default::default(),
Default::default());
// Verification of a program that terminates with FAIL opcode must fail
assert_eq!(verify_status, VerifyStatus::Failure);
}
Dependencies
~4–13MB
~171K SLoC