Skip to content

Commit 2e86a9e

Browse files
Merge branch 'feat/new-execution' into feat/sha-512-new-execution
2 parents 2138780 + a7d1125 commit 2e86a9e

File tree

30 files changed

+528
-310
lines changed

30 files changed

+528
-310
lines changed

.github/workflows/cli.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: OpenVM CLI Tests
33
on:
44
push:
55
branches: ["main"]
6+
tags: ["v*"]
67
pull_request:
78
branches: ["**"]
89
paths:
@@ -14,6 +15,13 @@ on:
1415
- "examples/**"
1516
- "Cargo.toml"
1617
- ".github/workflows/cli.yml"
18+
workflow_dispatch:
19+
inputs:
20+
use_local_openvm:
21+
description: "Test cargo openvm init using local patch"
22+
required: true
23+
type: boolean
24+
default: false
1725

1826
concurrency:
1927
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
@@ -76,6 +84,14 @@ jobs:
7684
export RUST_BACKTRACE=1
7785
cargo openvm keygen --config ./example/app_config.toml --output-dir .
7886
87+
- name: Set USE_LOCAL_OPENVM environment variable
88+
run: |
89+
if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == refs/tags/* ]] || [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.use_local_openvm }}" == "false" ]]; then
90+
echo "USE_LOCAL_OPENVM=0" >> $GITHUB_ENV
91+
else
92+
echo "USE_LOCAL_OPENVM=1" >> $GITHUB_ENV
93+
fi
94+
7995
- name: Run CLI tests
8096
working-directory: crates/cli
8197
run: |

book/src/SUMMARY.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- [Verifying Proofs](./writing-apps/verify.md)
1818
- [Solidity SDK](./writing-apps/solidity.md)
1919

20-
# Using Extensions
20+
# Acceleration Using Extensions
2121

2222
- [Overview](./custom-extensions/overview.md)
2323
- [Keccak](./custom-extensions/keccak.md)
@@ -27,12 +27,20 @@
2727
- [Elliptic Curve Cryptography](./custom-extensions/ecc.md)
2828
- [Elliptic Curve Pairing](./custom-extensions/pairing.md)
2929

30+
# Guest Libraries
31+
32+
- [Keccak256](./guest-libs/keccak256.md)
33+
- [SHA2](./guest-libs/sha2.md)
34+
- [Ruint](./guest-libs/ruint.md)
35+
- [K256](./guest-libs/k256.md)
36+
- [P256](./guest-libs/p256.md)
37+
- [Pairing](./guest-libs/pairing.md)
38+
- [Verify STARK](./guest-libs/verify-stark.md)
39+
3040
# Advanced Usage
3141

3242
- [SDK](./advanced-usage/sdk.md)
3343
- [Creating a New Extension](./advanced-usage/new-extension.md)
3444
- [Recursive Verification](./advanced-usage/recursion.md)
3545

36-
# Guest Libraries
3746

38-
- [Verify STARK](./guest-libs/verify-stark.md)

book/src/custom-extensions/algebra.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The functional part is provided by the `openvm-algebra-guest` crate, which is a
2222

2323
## Modular arithmetic
2424

25-
To [leverage](./overview.md) compile-time known moduli for performance, you declare, initialize, and then set up the arithmetic structures:
25+
To [leverage](./overview.md) compile-time known moduli for performance, you declare and initialize the arithmetic structures:
2626

2727
1. **Declare**: Use the `moduli_declare!` macro to define a modular arithmetic struct. This can be done multiple times in various crates or modules:
2828

@@ -37,10 +37,10 @@ This creates `Bls12_381Fp` and `Bn254Fp` structs, each implementing the `IntMod`
3737
Since both moduli are prime, both structs also implement the `Field` and `Sqrt` traits.
3838
The modulus parameter must be a string literal in decimal or hexadecimal format.
3939

40-
2. **Init**: Use the `init!` macro exactly once in the final binary:
40+
2. **Init**: Use the [`openvm::init!` macro](./overview.md#automating-the-init-step) exactly once in the final binary:
4141

4242
```rust
43-
init!();
43+
openvm::init!();
4444
/* This expands to
4545
moduli_init! {
4646
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab",
@@ -70,10 +70,10 @@ complex_declare! {
7070

7171
This creates a `Bn254Fp2` struct, representing a complex extension field. The `mod_type` must implement `IntMod`.
7272

73-
2. **Init**: After calling `complex_declare!`, the `init!` macro will now expand to the appropriate call to `complex_init!`.
73+
2. **Init**: After calling `complex_declare!`, the [`openvm::init!` macro](./overview.md#automating-the-init-step) will now expand to the appropriate call to `complex_init!`.
7474

7575
```rust
76-
init!();
76+
openvm::init!();
7777
/* This expands to:
7878
moduli_init! {
7979
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab",
@@ -115,9 +115,7 @@ To have the correct imports for the above example, add the following to the `Car
115115
```toml
116116
[dependencies]
117117
openvm = { git = "https://github.com/openvm-org/openvm.git" }
118-
openvm-platform = { git = "https://github.com/openvm-org/openvm.git" }
119118
openvm-algebra-guest = { git = "https://github.com/openvm-org/openvm.git" }
120-
openvm-algebra-complex-macros = { git = "https://github.com/openvm-org/openvm.git" }
121119
serde = { version = "1.0.216", default-features = false }
122120
```
123121

book/src/custom-extensions/bigint.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,6 @@ All of the operations can be used in 6 different ways:
2929

3030
When using the `U256` struct with `target_os = "zkvm"`, the struct utilizes efficient implementations of comparison operators as well as the `clone` method.
3131

32-
### Example matrix multiplication using `U256`
33-
34-
See the full example [here](https://github.com/openvm-org/openvm/blob/main/examples/u256/src/main.rs).
35-
36-
```rust,no_run,noplayground
37-
{{ #include ../../../examples/u256/src/main.rs }}
38-
```
39-
40-
To be able to import the `U256` struct, add the following to your `Cargo.toml` file:
41-
42-
```toml
43-
openvm-bigint-guest = { git = "https://github.com/openvm-org/openvm.git" }
44-
```
45-
4632
## `I256`
4733

4834
The `I256` struct is a 256-bit signed integer type. The `I256` struct is very similar to the `U256` struct.
@@ -70,19 +56,7 @@ The `I256` struct implements the following constructors: `from_i8`, `from_i32`,
7056

7157
When using the `I256` struct with `target_os = "zkvm"`, the struct utilizes efficient implementations of comparison operators as well as the `clone` method.
7258

73-
### Example matrix multiplication using `I256`
74-
75-
See the full example [here](https://github.com/openvm-org/openvm/blob/main/examples/i256/src/main.rs).
76-
77-
```rust,no_run,noplayground
78-
{{ #include ../../../examples/i256/src/main.rs }}
79-
```
8059

81-
To be able to import the `I256` struct, add the following to your `Cargo.toml` file:
82-
83-
```toml
84-
openvm-bigint-guest = { git = "https://github.com/openvm-org/openvm.git" }
85-
```
8660

8761
## External Linking
8862

book/src/custom-extensions/ecc.md

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The OpenVM Elliptic Curve Cryptography Extension provides support for elliptic curve operations through the `openvm-ecc-guest` crate.
44

5-
The secp256k1 and secp256r1 curves are supported out of the box, and developers can enable arbitrary Weierstrass curves by configuring this extension with the modulus for the coordinate field and the coefficients in the curve equation.
5+
Developers can enable arbitrary Weierstrass curves by configuring this extension with the modulus for the coordinate field and the coefficients in the curve equation. Preset configurations for the secp256k1 and secp256r1 curves are provided through the [K256](../guest-libs/k256.md) and [P256](../guest-libs/p256.md) guest libraries.
66

77
## Available traits and methods
88

@@ -43,10 +43,10 @@ sw_declare! {
4343
Each declared curve must specify the `mod_type` (implementing `IntMod`) and a constant `b` for the Weierstrass curve equation \\(y^2 = x^3 + ax + b\\). `a` is optional and defaults to 0 for short Weierstrass curves.
4444
This creates `Bls12_381G1Affine` and `P256Affine` structs which implement the `Group` and `WeierstrassPoint` traits. The underlying memory layout of the structs uses the memory layout of the `Bls12_381Fp` and `P256Coord` structs, respectively.
4545

46-
2. **Init**: Called once, the `init!` macro produces a call to `sw_init!` that enumerates these curves and allows the compiler to produce optimized instructions:
46+
2. **Init**: Called once, the [`openvm::init!` macro](./overview.md#automating-the-init-step) produces a call to `sw_init!` that enumerates these curves and allows the compiler to produce optimized instructions:
4747

4848
```rust
49-
init!();
49+
openvm::init!();
5050
/* This expands to
5151
sw_init! {
5252
Bls12_381G1Affine, P256Affine,
@@ -67,51 +67,3 @@ For the basic operations provided by the `WeierstrassPoint` trait, the scalar fi
6767

6868
The ECC extension supports ECDSA signature verification on any elliptic curve, and pre-defined implementations are provided for the secp256k1 and secp256r1 curves.
6969
To verify an ECDSA signature, first call the `VerifyingKey::recover_from_prehash_noverify` associated function to recover the verifying key, then call the `VerifyingKey::verify_prehashed` method on the recovered verifying key.
70-
71-
## Example program
72-
73-
See a working example [here](https://github.com/openvm-org/openvm/blob/main/examples/ecc/src/main.rs).
74-
75-
To use the ECC extension, add the following dependencies to `Cargo.toml`:
76-
77-
```toml
78-
openvm-algebra-guest = { git = "https://github.com/openvm-org/openvm.git" }
79-
openvm-ecc-guest = { git = "https://github.com/openvm-org/openvm.git", features = ["k256"] }
80-
```
81-
82-
One can define their own ECC structs but we will use the Secp256k1 struct from `openvm-ecc-guest` and thus the `k256` feature should be enabled.
83-
84-
```rust,no_run,noplayground
85-
{{ #include ../../../examples/ecc/src/main.rs:imports }}
86-
{{ #include ../../../examples/ecc/src/main.rs:init }}
87-
```
88-
89-
`moduli_init!` is called for both the coordinate and scalar field because they were declared in the `k256` module, although we will not be using the scalar field below.
90-
91-
With the above we can start doing elliptic curve operations like adding points:
92-
93-
```rust,no_run,noplayground
94-
{{ #include ../../../examples/ecc/src/main.rs:main }}
95-
```
96-
97-
### Config parameters
98-
99-
For the guest program to build successfully, all used moduli and curves must be declared in the `.toml` config file in the following format:
100-
101-
```toml
102-
[app_vm_config.modular]
103-
supported_moduli = ["115792089237316195423570985008687907853269984665640564039457584007908834671663", "115792089237316195423570985008687907852837564279074904382605163141518161494337"]
104-
105-
[[app_vm_config.ecc.supported_curves]]
106-
struct_name = "Secp256k1Point"
107-
modulus = "115792089237316195423570985008687907853269984665640564039457584007908834671663"
108-
scalar = "115792089237316195423570985008687907852837564279074904382605163141518161494337"
109-
a = "0"
110-
b = "7"
111-
```
112-
113-
The `supported_moduli` parameter is a list of moduli that the guest program will use. As mentioned in the [algebra extension](./algebra.md) chapter, the order of moduli in `[app_vm_config.modular]` must match the order in the `moduli_init!` macro.
114-
115-
The `ecc.supported_curves` parameter is a list of supported curves that the guest program will use. They must be provided in decimal format in the `.toml` file. For multiple curves create multiple `[[app_vm_config.ecc.supported_curves]]` sections. The order of curves in `[[app_vm_config.ecc.supported_curves]]` must match the order in the `sw_init!` macro.
116-
Also, the `struct_name` field must be the name of the elliptic curve struct created by `sw_declare!`.
117-
In this example, the `Secp256k1Point` struct is created in `openvm_ecc_guest::k256`.

book/src/custom-extensions/keccak.md

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,6 @@
11
# Keccak256
22

3-
The OpenVM Keccak256 extension provides tools for using the Keccak-256 hash function.
4-
The functional part is provided by the `openvm-keccak-guest` crate, which is a guest library that can be used in any OpenVM program.
5-
6-
## Functions for guest code
7-
8-
The OpenVM Keccak256 Guest extension provides two functions for use in your guest code:
9-
10-
- `keccak256(input: &[u8]) -> [u8; 32]`: Computes the Keccak-256 hash of the input data and returns it as an array of 32 bytes.
11-
- `set_keccak256(input: &[u8], output: &mut [u8; 32])`: Sets the output to the Keccak-256 hash of the input data into the provided output buffer.
12-
13-
See the full example [here](https://github.com/openvm-org/openvm/blob/main/examples/keccak/src/main.rs).
14-
15-
### Example
16-
17-
```rust,no_run,noplayground
18-
{{ #include ../../../examples/keccak/src/main.rs:imports }}
19-
{{ #include ../../../examples/keccak/src/main.rs:main }}
20-
```
21-
22-
To be able to import the `keccak256` function, add the following to your `Cargo.toml` file:
23-
24-
```toml
25-
openvm-keccak256-guest = { git = "https://github.com/openvm-org/openvm.git" }
26-
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
27-
```
28-
29-
## External Linking
30-
31-
The keccak guest extension also provides another way to use the keccak-256 intrinsic implementation. It provides a function that is meant to be linked to other external libraries. The external libraries can use this function as a hook for the keccak-256 intrinsic. This is enabled only when the target is `zkvm`.
3+
The Keccak256 extension guest provides a function that is meant to be linked to other external libraries. The external libraries can use this function as a hook for the keccak-256 intrinsic. This is enabled only when the target is `zkvm`.
324

335
- `native_keccak256(input: *const u8, len: usize, output: *mut u8)`: This function has `C` ABI. It takes in a pointer to the input, the length of the input, and a pointer to the output buffer.
346

book/src/custom-extensions/overview.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
# Using Existing Extensions
1+
# Acceleration Using Pre-Built Extensions
22

3-
You can seamlessly integrate certain performance-optimized extensions maintained by the OpenVM team to enhance your arithmetic operations and cryptographic computations.
3+
OpenVM ships with a set of pre-built extensions maintained by the OpenVM team. Below, we highlight six of these extensions designed to accelerate common arithmetic and cryptographic operations that are notoriously expensive to execute. Some of these extensions have corresponding guest libraries which provide convenient, high-level interfaces for your guest program to interact with the extension.
44

5-
In this chapter, we will explain how to use the following existing extensions:
6-
7-
- [`openvm-keccak-guest`](./keccak.md) - Keccak256 hash function.
8-
- [`openvm-sha2-guest`](./sha2.md) - SHA-2 hash functions.
9-
- [`openvm-bigint-guest`](./bigint.md) - Big integer arithmetic for 256-bit signed and unsigned integers.
5+
- [`openvm-keccak-guest`](./keccak.md) - Keccak256 hash function. See the [Keccak256 guest library](../guest-libs/keccak256.md) for usage details.
6+
- [`openvm-sha2-guest`](./sha2.md) - SHA-2 family of hash functions. See the [SHA-2 guest library](../guest-libs/sha2.md) for usage details.
7+
- [`openvm-bigint-guest`](./bigint.md) - Big integer arithmetic for 256-bit signed and unsigned integers. See the [ruint guest library](../guest-libs/ruint.md) for using accelerated 256-bit integer ops in rust.
108
- [`openvm-algebra-guest`](./algebra.md) - Modular arithmetic and complex field extensions.
11-
- [`openvm-ecc-guest`](./ecc.md) - Elliptic curve cryptography.
12-
- [`openvm-pairing-guest`](./pairing.md) - Elliptic curve optimal Ate pairings.
9+
- [`openvm-ecc-guest`](./ecc.md) - Elliptic curve cryptography. See the [k256](../guest-libs/k256.md) and [p256](../guest-libs/p256.md) guest libraries for using this extension over the respective curves.
10+
- [`openvm-pairing-guest`](./pairing.md) - Elliptic curve optimal Ate pairings. See the [pairing guest library](../guest-libs/pairing.md) for usage details.
1311

14-
Some extensions such as `openvm-keccak-guest`, `openvm-sha2-guest`, and `openvm-bigint-guest` can be enabled without specifying any additional configuration.
12+
## Optimizing Modular Arithmetic
1513

16-
On the other hand certain arithmetic operations, particularly modular arithmetic, can be optimized significantly when the modulus is known at compile time. This approach requires a framework to inform the compiler about all the moduli and associated arithmetic structures we intend to use. To achieve this, three steps are involved:
14+
Some of these extensions—specifically `algebra`, `ecc`, and `pairing`—perform modular arithmetic, which can be significantly optimized when the modulus is known at compile time. Therefore, these extensions provide a framework to inform the compiler about all the moduli and associated arithmetic structures we intend to use. To achieve this, two steps are involved:
1715

1816
1. **Declare**: Introduce a modular arithmetic or related structure, along with its modulus and functionality. This can be done in any library or binary file.
1917
2. **Init**: Performed exactly once in the final binary. It aggregates all previously declared structures, assigns them stable indices, and sets up linkage so that they can be referenced in generated code.
20-
3. **Setup**: A one-time runtime procedure for security. This ensures that the compiled code matches the virtual machine’s expectations and that each instruction set is tied to the correct modulus or extension.
2118

2219
These steps ensure both performance and security: performance because the modulus is known at compile time, and security because runtime checks confirm that the correct structures have been initialized.
2320

2421
Our design for the configuration procedure above was inspired by the [EVMMAX proposal](https://github.com/jwasinger/EIPs/blob/evmmax-2/EIPS/eip-6601.md).
2522

23+
### Automating the `init!` step
24+
25+
The `openvm` crate provides an `init!` macro to automate the **init** step:
26+
1. Call `openvm::init!()` exactly once in the code of the final program binary.
27+
2. When [compiling the program](../writing-apps/build.md), `cargo openvm build` will read the [configuration file](#configuration) to automatically generate the correct init code and write it to `<INIT_FILE_NAME>`, which defaults to `openvm_init.rs` in the manifest directory.
28+
3. The `openvm::init!()` macro will include the `openvm_init.rs` file into the final binary to complete the init process. You can call `openvm::init!(INIT_FILE_NAME)` to include init code from a different file if needed.
29+
2630
## Configuration
2731

2832
To use these extensions, you must populate an `openvm.toml` in your package root directory (where the `Cargo.toml` file is located).
@@ -55,12 +59,14 @@ supported_moduli = ["<modulus_1>", "<modulus_2>", ...]
5559
supported_curves = ["Bls12_381", "Bn254"]
5660

5761
[[app_vm_config.ecc.supported_curves]]
62+
struct_name = "<curve_name_1>"
5863
modulus = "<modulus_1>"
5964
scalar = "<scalar_1>"
6065
a = "<a_1>"
6166
b = "<b_1>"
6267

6368
[[app_vm_config.ecc.supported_curves]]
69+
struct_name = "<curve_name_2>"
6470
modulus = "<modulus_2>"
6571
scalar = "<scalar_2>"
6672
a = "<a_2>"

0 commit comments

Comments
 (0)