|
| 1 | +# Binius Prover Client |
| 2 | + |
| 3 | +A Rust crate providing a clean API for Binius ZK proof generation. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This is a standard Rust library that provides a type-safe interface for generating Binius proofs. It uses FFI to communicate with the prover via serialized data, enabling the prover to be distributed as a closed-source binary while keeping the interface open-source. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +``` |
| 12 | +┌─────────────────────┐ ┌─────────────────────┐ ┌──────────────────────┐ |
| 13 | +│ Your Rust App │────│ Prover Client │────│ Binius Prover │ |
| 14 | +│ │ │ (This Crate) │ │ (C Library) │ |
| 15 | +│ - ConstraintSystem │ │ - ProverClient │ │ - binius_prove() │ |
| 16 | +│ - ValuesData │ │ - Serialization │ │ - Proof generation │ |
| 17 | +│ - Proof │ │ - Error handling │ │ │ |
| 18 | +└─────────────────────┘ └─────────────────────┘ └──────────────────────┘ |
| 19 | +``` |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Add this crate to your `Cargo.toml`: |
| 24 | + |
| 25 | +```toml |
| 26 | +[dependencies] |
| 27 | +binius-prover-client = { path = "../prover-client" } |
| 28 | +``` |
| 29 | + |
| 30 | +## Requirements |
| 31 | + |
| 32 | +This crate requires the Binius prover to be available as a C library: |
| 33 | + |
| 34 | +```bash |
| 35 | +export BINIUS_PROVER_LIB_PATH=/path/to/prover/library |
| 36 | +``` |
| 37 | + |
| 38 | +Without this, the crate will compile but return runtime errors when attempting to generate proofs. |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +```rust |
| 43 | +use binius_prover_client::ProverClient; |
| 44 | +use binius_core::constraint_system::{ConstraintSystem, ValuesData}; |
| 45 | + |
| 46 | +// Create a prover client instance |
| 47 | +let prover = ProverClient::new(1); // log_inv_rate = 1 |
| 48 | + |
| 49 | +// Generate proof from constraint system and witness data |
| 50 | +let proof = prover.prove(&constraint_system, &public_witness, &private_witness)?; |
| 51 | +``` |
| 52 | + |
| 53 | +The crate provides three API methods: |
| 54 | +- `prove()` - Takes Rust types, handles serialization internally |
| 55 | +- `prove_serialized()` - Takes pre-serialized bytes, returns deserialized `Proof` |
| 56 | +- `prove_serialized_raw()` - Takes and returns raw bytes for maximum efficiency |
| 57 | + |
| 58 | +## FFI Interface Details |
| 59 | + |
| 60 | +The FFI boundary uses a single C function with serialized inputs/outputs: |
| 61 | + |
| 62 | +```c |
| 63 | +// Returns proof size on success, negative error code on failure |
| 64 | +int32_t binius_prove( |
| 65 | + const uint8_t* cs_bytes, // Serialized ConstraintSystem |
| 66 | + size_t cs_len, |
| 67 | + const uint8_t* pub_witness_bytes, // Serialized public ValuesData |
| 68 | + size_t pub_witness_len, |
| 69 | + const uint8_t* priv_witness_bytes,// Serialized private ValuesData |
| 70 | + size_t priv_witness_len, |
| 71 | + uint32_t log_inv_rate, // Proof generation parameter |
| 72 | + uint8_t* proof_out, // Output buffer for serialized Proof |
| 73 | + size_t proof_capacity // Size of output buffer |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +### Error Codes |
| 78 | + |
| 79 | +- **Positive number**: Size of the proof written to `proof_out` (success) |
| 80 | +- **-1**: Null pointer error |
| 81 | +- **-2**: Invalid input data |
| 82 | +- **-3**: Proving error |
| 83 | +- **-4**: Serialization error |
| 84 | +- **-5**: Output buffer too small |
| 85 | + |
| 86 | +## Testing and Development |
| 87 | + |
| 88 | +### Running the Test Suite |
| 89 | + |
| 90 | +The crate includes a focused test suite with automatic FFI library management: |
| 91 | + |
| 92 | +```bash |
| 93 | +# Quick test - builds FFI library and runs all tests |
| 94 | +./test_prover_client.sh |
| 95 | +``` |
| 96 | + |
| 97 | +This script will: |
| 98 | +1. Build the FFI library with the current implementation |
| 99 | +2. Set up library paths automatically |
| 100 | +3. Run integration tests for all API variants |
| 101 | +4. Verify FFI boundary crossing works correctly |
| 102 | + |
| 103 | +### Manual Testing |
| 104 | + |
| 105 | +```bash |
| 106 | +# Build the FFI library |
| 107 | +cargo build --release --features ffi-impl |
| 108 | + |
| 109 | +# Set library path and run tests |
| 110 | +export BINIUS_PROVER_LIB_PATH=$(pwd)/target/release |
| 111 | +cargo test |
| 112 | +``` |
| 113 | + |
| 114 | +### Test Coverage |
| 115 | + |
| 116 | +The test suite focuses on interface correctness: |
| 117 | +- **API methods**: All three variants (`prove`, `prove_serialized`, `prove_serialized_raw`) |
| 118 | +- **FFI boundary**: Verifies data crosses the FFI boundary correctly |
| 119 | +- **Serialization**: Ensures proper serialization/deserialization |
| 120 | +- **Trait implementation**: Tests Default trait and accessor methods |
| 121 | + |
| 122 | +## Implementation Notes |
| 123 | + |
| 124 | +### Library Detection |
| 125 | + |
| 126 | +The crate's build script automatically detects the external prover library: |
| 127 | + |
| 128 | +- Checks `BINIUS_PROVER_LIB_PATH` environment variable |
| 129 | +- Sets up linking when library is found |
| 130 | +- Provides graceful fallback when library is unavailable |
| 131 | + |
| 132 | +### FFI Implementation |
| 133 | + |
| 134 | +The file `src/ffi_impl.rs` contains the Binius prover wrapped in a C-compatible FFI interface. This is used to test the FFI boundary. In a closed-source deployment, this code would be compiled as a proprietary C library. |
| 135 | + |
| 136 | +## Advanced Usage |
| 137 | + |
| 138 | +### Error Handling |
| 139 | + |
| 140 | +The interface provides detailed error information: |
| 141 | + |
| 142 | +```rust |
| 143 | +use binius_prover_client::{ProverClient, ProverError}; |
| 144 | + |
| 145 | +match prover.prove(&cs, &pub_witness, &priv_witness) { |
| 146 | + Ok(proof) => println!("Proof generated: {} bytes", proof.data().len()), |
| 147 | + Err(ProverError::LibraryNotAvailable(msg)) => { |
| 148 | + eprintln!("FFI library not found: {}", msg); |
| 149 | + // Handle library not available case |
| 150 | + } |
| 151 | + Err(ProverError::FfiError(code)) => { |
| 152 | + eprintln!("FFI error code: {}", code); |
| 153 | + // Handle specific FFI error codes |
| 154 | + } |
| 155 | + Err(e) => eprintln!("Other error: {}", e), |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +### Performance Considerations |
| 160 | + |
| 161 | +- **Pre-serialized data**: Use `prove_serialized_raw()` when you already have serialized inputs |
| 162 | +- **Library linking**: Dynamic linking adds minimal overhead compared to proof generation time |
| 163 | +- **Memory management**: The FFI boundary uses copying; consider this for very large constraint systems |
| 164 | + |
| 165 | +### Integration with Existing Code |
| 166 | + |
| 167 | +The interface is designed to integrate easily with existing Binius workflows: |
| 168 | + |
| 169 | +```rust |
| 170 | +// Works with existing constraint system construction |
| 171 | +let cs = constraint_system_builder.build(); |
| 172 | +let witness = witness_builder.build(); |
| 173 | + |
| 174 | +// Drop-in replacement for direct prover usage |
| 175 | +let prover = ProverClient::new(log_inv_rate); |
| 176 | +let proof = prover.prove(&cs.constraint_system, &witness.public, &witness.private)?; |
| 177 | + |
| 178 | +// Use proof with existing verification code |
| 179 | +verify_proof(&proof, &public_inputs)?; |
| 180 | +``` |
0 commit comments