Built-in Functions
StoffelVM provides a set of built-in functions that are available to all StoffelLang programs. These functions handle common operations like I/O, array manipulation, and MPC-specific functionality.Console Output
- Output is displayed during VM execution
- Useful for debugging and progress tracking
- Does not reveal secret values (they remain masked)
Array Operations
array_push
Adds an element to the end of an array. Signature:- Uses 0-based indexing (updated in recent versions)
- Modifies the array in place
- Works with both clear and secret element types
array_len
Returns the length of an array. Signature:ClientStore Operations
TheClientStore is a built-in singleton that provides access to client inputs in MPC computations. It acts as the bridge between external client data and the secure computation.
ClientStore.get_number_clients
Returns the total number of clients participating in the MPC computation. Signature:int64 value representing the client count.
Example:
- Returns a clear (public) value, not a secret
- Can be assigned to regular variables
- Useful for dynamic iteration over client inputs
ClientStore.take_share
Retrieves a secret share from a specific client at a given index. Signature:client_id: The ID of the client (0-indexed)share_index: The index of the share from that client (0-indexed)
int64 value.
Example:
ClientStore.take_share() must be assigned to secret variables. The compiler enforces this requirement.
Valid usage patterns:
MPC-Specific Builtins
These functions are available when running in MPC mode and interact with the underlying protocol.Share Operations
When values are declared assecret, the VM automatically handles:
- Secret sharing: Splitting values into shares across parties
- Share arithmetic: Computing on shares using MPC protocols
- Reconstruction: Combining shares to produce final outputs
Protocol Integration
StoffelVM integrates with the HoneyBadger MPC protocol through internal hooks:| Operation | Internal Function | Description |
|---|---|---|
| Addition | mpc_add | Adds two secret values |
| Subtraction | mpc_sub | Subtracts two secret values |
| Multiplication | mpc_mul | Multiplies using Beaver triples |
| Comparison | mpc_cmp | Compares two secret values |
Type Conversion Builtins
int_to_string (Planned)
Convert an integer to its string representation.string_to_int (Planned)
Parse a string as an integer.Memory and Lifecycle
Automatic Memory Management
StoffelVM manages memory automatically:- Values are allocated on the stack or heap as needed
- Arrays grow dynamically with
array_push - Memory is reclaimed when values go out of scope
Register Architecture
The VM uses a dual-register architecture:- Clear registers: For public/non-secret values
- Secret registers: For MPC-protected values
FFI Integration
StoffelVM supports calling external functions through Foreign Function Interface (FFI):Summary Table
| Function | Input Types | Return Type | Description |
|---|---|---|---|
print | string | nil | Console output |
array_push | [T], T | nil | Add element to array |
array_len | [T] | int64 | Get array length |
ClientStore.get_number_clients | - | int64 (clear) | Get client count |
ClientStore.take_share | int64, int64 | secret int64 | Get client share |