Crate valida_vm_api_linux_x86

Source
Expand description

§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.

See Valida documentation

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.

§Examples

program1 reads an integer from stdin, increments it and prints result to stdout. program1 was obtained by compiling test_data/program.c: /valida-toolchain/bin/clang -O3 -target valida program1.c -o program1

use valida_vm_api::*;
use tempfile::NamedTempFile;
use std::fs;
use std::path::Path;

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());

// 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());

// 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());

// 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());

// 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);

program2 just terminates execution with __valida_builtin_fail(). program2 was obtained by compiling test_data/program2.c: /valida-toolchain/bin/clang -O3 -target valida program2.c -o program2

use valida_vm_api::*;
use tempfile::NamedTempFile;
use std::fs;
use std::path::Path;

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());

// 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());

// 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());

// Verification of a program that terminates with FAIL opcode must fail
assert_eq!(verify_status, VerifyStatus::Failure);

Structs§

Enums§

Functions§

  • Convenience function that creates a temporary file from bytes. Used by the example.
  • This function creates a wrapper for valida executable.
  • Convenience function that reads file contents as bytes. Used by the example.