Skip to content

Commit d5973f1

Browse files
nyc432claude
andcommitted
feat: Python-friendly Receipt API improvements (v0.4.0)
Major API enhancements for better Python ergonomics and security: Receipt API improvements: - Added journal_bytes, journal_hex, journal_text, journal_len properties - Added ExitStatus with structured exit information (kind, user_code, ok) - Added ExitKind enum (HALTED, PAUSED, SYSTEM_SPLIT, SESSION_LIMIT) - Added claimed_image_id_hex/bytes properties (clearly marked as untrusted) - Added verify_hex() and verify_bytes() methods with 0x prefix support - Added verify_integrity() for safe failure inspection - Added Receipt serialization with to_bytes()/from_bytes() - Added compute_image_id_hex() module function - Improved __repr__() with journal preview and image ID Security improvements: - Clear separation between claimed (from receipt) and trusted (from verifier) image IDs - Verification methods require explicit trusted image ID - Better error messages and validation Removed: - decode_journal() - not ready for RISC Zero 1.2 (requires 2.x+ APIs) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5a61333 commit d5973f1

File tree

9 files changed

+675
-29
lines changed

9 files changed

+675
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "pyr0"
5-
version = "0.3.0"
5+
version = "0.4.0"
66
edition = "2021"
77

88
[lib]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# PyR0 - Python Interface for RISC Zero zkVM
22

3-
[![Version](https://img.shields.io/badge/version-0.3.0-orange)](https://github.com/garyrob/PyR0/releases)
3+
[![Version](https://img.shields.io/badge/version-0.4.0-orange)](https://github.com/garyrob/PyR0/releases)
44
**⚠️ Experimental Alpha - Apple Silicon Only**
55

66
Python bindings for [RISC Zero](https://www.risczero.com/) zkVM, enabling zero-knowledge proof generation and verification from Python.
77

8-
> **Note**: This is an experimental alpha release (v0.3.0) currently targeting Apple Silicon (M1/M2/M3) Macs only.
8+
> **Note**: This is an experimental alpha release (v0.4.0) currently targeting Apple Silicon (M1/M2/M3) Macs only.
99
1010
## Overview
1111

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "maturin"
44

55
[project]
66
name = "PyR0"
7-
version = "0.3.0"
7+
version = "0.4.0"
88
authors = [
99
{ name = "Weikeng Chen", email = "weikeng.chen@l2iterative.com" }
1010
]

run_all_tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ run_test "Real Verification Test" "uv run test_real_verification.py"
4141
run_test "Security Verification Test" "uv run test_security_verification.py"
4242
run_test "Verify API Test" "uv run test_verify_api.py"
4343

44+
# API tests
45+
echo -e "\n=== API Tests ==="
46+
run_test "Receipt API Test" "uv run test_receipt_api.py"
47+
4448
# Merkle tree tests
4549
echo -e "\n=== Merkle Tree Tests ==="
4650
run_test "Merkle ZKP Test" "uv run test/test_merkle_zkp.py"

src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod receipt;
33
mod session;
44

55
use crate::image::Image;
6-
use crate::receipt::Receipt;
6+
use crate::receipt::{Receipt, ExitStatus, ExitKind};
77
use crate::session::{ExitCode, SessionInfo};
88
use pyo3::prelude::*;
99
use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts};
@@ -87,6 +87,22 @@ fn prove_with_opts(_py: Python<'_>, image: &Image, input_bytes: &Bound<'_, PyAny
8787
// Advanced functions removed - segments are no longer exposed
8888
// If needed in future, these could work with Receipt types instead
8989

90+
/// Compute the expected image ID from an ELF file as hex string
91+
///
92+
/// Args:
93+
/// elf_bytes: The ELF binary to compute ID from
94+
///
95+
/// Returns:
96+
/// 64-character hex string of the image ID
97+
#[pyfunction]
98+
fn compute_image_id_hex(elf_bytes: Vec<u8>) -> PyResult<String> {
99+
let image_id = risc0_binfmt::compute_image_id(&elf_bytes)
100+
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(
101+
format!("Failed to compute image ID: {}", e)
102+
))?;
103+
Ok(hex::encode(image_id))
104+
}
105+
90106

91107

92108

@@ -96,11 +112,14 @@ fn _rust(m: &Bound<'_, PyModule>) -> PyResult<()> {
96112
m.add_class::<ExitCode>()?;
97113
m.add_class::<SessionInfo>()?;
98114
m.add_class::<Receipt>()?;
115+
m.add_class::<ExitStatus>()?;
116+
m.add_class::<ExitKind>()?;
99117

100118
// Core API functions
101119
m.add_function(wrap_pyfunction!(load_image, m)?)?;
102120
m.add_function(wrap_pyfunction!(prove, m)?)?;
103121
m.add_function(wrap_pyfunction!(prove_with_opts, m)?)?;
122+
m.add_function(wrap_pyfunction!(compute_image_id_hex, m)?)?;
104123

105124
// Optional debugging function
106125
m.add_function(wrap_pyfunction!(dry_run, m)?)?;

src/pyr0/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"load_image",
77
"prove",
88
"prove_with_opts",
9+
"compute_image_id_hex",
910

1011
# Debugging functions
1112
"dry_run",
@@ -16,4 +17,6 @@
1617
"Receipt",
1718
"ExitCode",
1819
"SessionInfo",
20+
"ExitStatus",
21+
"ExitKind",
1922
]

0 commit comments

Comments
 (0)