Skip to content

Commit a05cfd9

Browse files
Update book and crate specs
1 parent 0b40031 commit a05cfd9

File tree

7 files changed

+6
-38
lines changed

7 files changed

+6
-38
lines changed

book/src/custom-extensions/algebra.md

-5
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,10 @@ moduli_init! {
4646

4747
This step enumerates the declared moduli (e.g., `0` for the first one, `1` for the second one) and sets up internal linkage so the compiler can generate the appropriate RISC-V instructions associated with each modulus.
4848

49-
3. **Setup**: At runtime, before performing arithmetic, a setup instruction must be sent to ensure security and correctness. For the \\(i\\)-th modulus, you call `setup_<i>()` (e.g., `setup_0()` or `setup_1()`). Alternatively, `setup_all_moduli()` can be used to handle all declared moduli.
50-
5149
**Summary**:
5250

5351
- `moduli_declare!`: Declares modular arithmetic structures and can be done multiple times.
5452
- `init!`: Called once in the final binary to assign and lock in the moduli.
55-
- `setup_<i>()`/`setup_all_moduli()`: Ensures at runtime that the correct modulus is in use, providing a security check and finalizing the environment for safe arithmetic operations.
5653

5754
## Complex field extension
5855

@@ -83,8 +80,6 @@ complex_init! {
8380
*/
8481
```
8582

86-
3. **Setup**: Similar to moduli, call `setup_complex_<i>()` or `setup_all_complex_extensions()` at runtime to secure the environment.
87-
8883
### Config parameters
8984

9085
For the guest program to build successfully, all used moduli must be declared in the `.toml` config file in the following format:

book/src/custom-extensions/ecc.md

-3
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ sw_init! {
5454
*/
5555
```
5656

57-
3. **Setup**: Similar to the moduli and complex extensions, runtime setup instructions ensure that the correct curve parameters are being used, guaranteeing secure operation.
58-
5957
**Summary**:
6058

6159
- `sw_declare!`: Declares elliptic curve structures.
6260
- `init!`: Initializes them once, linking them to the underlying moduli.
63-
- `setup_sw_<i>()`/`setup_all_curves()`: Secures runtime correctness.
6461

6562
To use elliptic curve operations on a struct defined with `sw_declare!`, it is expected that the struct for the curve's coordinate field was defined using `moduli_declare!`. In particular, the coordinate field needs to be initialized and set up as described in the [algebra extension](./algebra.md) chapter.
6663

book/src/custom-extensions/pairing.md

-8
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ Additionally, we'll need to initialize our moduli and `Fp2` struct via the follo
3636
{{ #include ../../../examples/pairing/src/main.rs:init }}
3737
```
3838

39-
And we'll run the required setup functions at the top of the guest program's `main()` function:
40-
41-
```rust,no_run,noplayground
42-
{{ #include ../../../examples/pairing/src/main.rs:setup }}
43-
```
44-
45-
There are two moduli defined internally in the `Bls12_381` feature. The `moduli_init!` macro thus requires both of them to be initialized. However, we do not need the scalar field of BLS12-381 (which is at index 1), and thus we only initialize the modulus from index 0, thus we only use `setup_0()` (as opposed to `setup_all_moduli()`, which will save us some columns when generating the trace).
46-
4739
## Input values
4840

4941
The inputs to the pairing check are `AffinePoint`s in \\(\mathbb{F}\_p\\) and \\(\mathbb{F}\_{p^2}\\). They can be constructed via the `AffinePoint::new` function, with the inner `Fp` and `Fp2` values constructed via various `from_...` functions.

docs/specs/RISCV.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ generates classes `Bls12381` and `Bn254` that represent the elements of the corr
132132

133133
### Field Arithmetic
134134

135-
For each created modular class, one must call a corresponding `setup_*` function once at the beginning of the program. For example, for the structs above this would be `setup_0()` and `setup_1()`. This function generates the `setup` intrinsics which are distinguished by the `rs2` operand that specifies the chip this instruction is passed to..
135+
For each created modular class, one must call a corresponding `setup_*` function before using the intrinsics.
136+
For example, for the structs above this would be `setup_0()` and `setup_1()`. This function generates the `setup` intrinsics which are distinguished by the `rs2` operand that specifies the chip this instruction is passed to..
137+
For convenience, each modulus's `setup_*` function is automatically called on the first use of any of its intrinsics.
136138

137139
We use `config.mod_idx(N)` to denote the index of `N` in this list. In the list below, `idx` denotes `config.mod_idx(N)`.
138140

extensions/algebra/complex-macros/README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ openvm_algebra_complex_macros::complex_init! {
2727
*/
2828

2929
pub fn main() {
30-
setup_all_moduli();
31-
setup_all_complex_extensions();
3230
// ...
3331
}
3432
```
@@ -79,13 +77,9 @@ mod openvm_intrinsics_ffi_complex {
7977
pub fn setup_complex_0() {
8078
// send the setup instructions
8179
}
82-
pub fn setup_all_complex_extensions() {
83-
setup_complex_0();
84-
// call all other setup_complex_* for all the items in the moduli_init! macro
85-
}
8680
```
8781

88-
3. Obviously, `mod_idx` in the `complex_init!` must match the position of the corresponding modulus in the `moduli_init!` macro. The order of the items in `complex_init!` affects what `setup_complex_*` function will correspond to what complex class. Also, it **must match** the order of the moduli in the chip configuration -- more specifically, in the modular extension parameters (the order of numbers in `Fp2Extension::supported_modulus`, which is usually defined with the whole `app_vm_config` in the `openvm.toml` file). However, it again imposes the restriction that we only can invoke `complex_init!` once. Again analogous to the moduli setups, we must call `setup_complex_*` for each used complex extension before doing anything with entities of that class (or one can call `setup_all_complex_extensions` to setup all of them, if all are used).
82+
3. Obviously, `mod_idx` in the `complex_init!` must match the position of the corresponding modulus in the `moduli_init!` macro. The order of the items in `complex_init!` affects what `setup_complex_*` function will correspond to what complex class. Also, it **must match** the order of the moduli in the chip configuration -- more specifically, in the modular extension parameters (the order of numbers in `Fp2Extension::supported_modulus`, which is usually defined with the whole `app_vm_config` in the `openvm.toml` file). However, it again imposes the restriction that we only can invoke `complex_init!` once. Again analogous to the moduli setups, `setup_complex_*` is automatically called on each complex extension on first use of its intrinsics.
8983

9084
4. Note that, due to the nature of function names, the name of the struct used in `complex_init!` must be the same as in `complex_declare!`. To illustrate, the following code will **fail** to compile:
9185

extensions/algebra/moduli-macros/README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,9 @@ pub fn setup_2() {
145145
// send the setup instruction designed for the chip number 2
146146
}
147147
}
148-
pub fn setup_all_moduli() {
149-
setup_0();
150-
setup_1();
151-
setup_2();
152-
// setup functions for all the other moduli provided in the `moduli_init!` function
153-
}
154148
```
155149

156-
The setup operation (e.g., `setup_2`) consists of reading the value `OPENVM_SERIALIZED_MODULUS_2` from memory and constraining that the read value is equal to the modulus the chip has been configured with. For each used modulus, its corresponding setup instruction **must** be called before all other operations -- this currently must be checked by inspecting the program code; it is not enforced by the virtual machine.
150+
The setup operation (e.g., `setup_2`) consists of reading the value `OPENVM_SERIALIZED_MODULUS_2` from memory and constraining that the read value is equal to the modulus the chip has been configured with. For each used modulus, its corresponding setup instruction is automatically called on first use of any of its intrinsics.
157151

158152
5. It follows from the above that the `moduli_declare!` invocations may be in multiple places in various compilation units, but all the `declare!`d moduli must be specified at least once in `moduli_init!` so that there will be no linker errors due to missing function implementations. Correspondingly, the `moduli_init!` macro should only be called once in the entire program (in the guest crate as the topmost compilation unit). Finally, the order of the moduli in `moduli_init!` has nothing to do with the `moduli_declare!` invocations, but it **must match** the order of the moduli in the chip configuration -- more specifically, in the modular extension parameters (the order of numbers in `ModularExtension::supported_modulus`, which is usually defined with the whole `app_vm_config` in the `openvm.toml` file).
159153

extensions/ecc/sw-macros/README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ openvm_ecc_guest::sw_macros::sw_init! {
3333
*/
3434

3535
pub fn main() {
36-
setup_all_moduli();
37-
setup_all_curves();
3836
// ...
3937
}
4038
```
@@ -90,13 +88,9 @@ pub fn setup_sw_Secp256k1Point() {
9088
// ...
9189
}
9290
}
93-
pub fn setup_all_curves() {
94-
setup_sw_Secp256k1Point();
95-
// other setups
96-
}
9791
```
9892

99-
3. Again, the `setup` function for every used curve must be called before any other instructions for that curve. If all curves are used, one can call `setup_all_curves()` to setup all of them.
93+
3. Again, the `setup` function for every curve is automatically called on first use of any of the curve's intrinsics.
10094

10195
4. The order of the items in `sw_init!` **must match** the order of the moduli in the chip configuration -- more specifically, in the modular extension parameters (the order of `CurveConfig`s in `WeierstrassExtension::supported_curves`, which is usually defined with the whole `app_vm_config` in the `openvm.toml` file).
10296

0 commit comments

Comments
 (0)