Skip to content

Commit f4fce82

Browse files
docs(book): add docs on init! macro (#1837)
And update on support for both getrandom v0.2 and v0.3. closes INT-4186
1 parent 065835b commit f4fce82

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

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/ecc.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

book/src/custom-extensions/overview.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ OpenVM ships with a set of pre-built extensions maintained by the OpenVM team. B
1111

1212
## Optimizing Modular Arithmetic
1313

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, 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:
1515

1616
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.
1717
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.
18-
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.
1918

2019
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.
2120

2221
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).
2322

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+
2430
## Configuration
2531

2632
To use these extensions, you must populate an `openvm.toml` in your package root directory (where the `Cargo.toml` file is located).

book/src/custom-extensions/pairing.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ This asserts that \\(e(p_0, q_0) e(p_1, q_1) = 1\\).
1919
Naturally, this can be extended to more points by adding more elements to the arrays.
2020

2121
The pairing extension additionally provides field operations in \\(\mathbb{F_{p^{12}}}\\) for both BN254 and BLS12-381 curves where \\(\mathbb{F}\\) is the coordinate field.
22+
23+
See the [pairing guest library](../guest-libs/pairing.md) for usage details.

book/src/guest-libs/pairing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Pairing
1+
# Elliptic Curve Pairing
22

33
We'll be working with an example using the BLS12-381 elliptic curve. This is in addition to the setup that needs to be done in the [Writing a Program](../writing-apps/write-program.md) section.
44

book/src/writing-apps/write-program.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ See the [overview](./overview.md) on how to build and run the program.
126126

127127
## Using crates that depend on `getrandom`
128128

129-
OpenVM is compatible with [getrandom](https://crates.io/crates/getrandom) `v0.3`. The `cargo openvm` CLI will always compile with the [custom](https://docs.rs/getrandom/latest/getrandom/#opt-in-backends) `getrandom` backend.
129+
OpenVM is compatible with [getrandom](https://crates.io/crates/getrandom) `v0.2` and `v0.3`. The `cargo openvm` CLI will always compile with the [custom](https://docs.rs/getrandom/0.3.3/getrandom/#opt-in-backends) `getrandom` backend.
130130

131131
By default the `openvm` crate has a default feature `"getrandom-unsupported"` which exports a `__getrandom_v03_custom` function that always returns `Err(Error::UNSUPPORTED)`. This is enabled by default to allow compilation of guest programs that pull in dependencies which require `getrandom` but where the executed code does not actually use `getrandom` functions.
132132

133-
To override the default behavior and provide a custom implementation, turn off the `"getrandom-unsupported"` feature in the `openvm` crate and supply your own `__getrandom_v03_custom` function as specified in the [getrandom docs](https://docs.rs/getrandom/latest/getrandom/#custom-backend).
133+
To override the default behavior and provide a custom implementation, turn off the `"getrandom-unsupported"` feature in the `openvm` crate and supply your own `__getrandom_v03_custom` function as specified in the [getrandom docs](https://docs.rs/getrandom/0.3.3/getrandom/#custom-backend). Similar customization options are available for `getrandom` `v0.2`.

0 commit comments

Comments
 (0)