|
1 |
| -# Using Existing Extensions |
| 1 | +# Acceleration Using Pre-Built Extensions |
2 | 2 |
|
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. |
4 | 4 |
|
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. |
10 | 8 | - [`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. |
13 | 11 |
|
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 |
15 | 13 |
|
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: |
17 | 15 |
|
18 | 16 | 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.
|
19 | 17 | 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. |
21 | 18 |
|
22 | 19 | 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.
|
23 | 20 |
|
24 | 21 | 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).
|
25 | 22 |
|
| 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 | + |
26 | 30 | ## Configuration
|
27 | 31 |
|
28 | 32 | 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>", ...]
|
55 | 59 | supported_curves = ["Bls12_381", "Bn254"]
|
56 | 60 |
|
57 | 61 | [[app_vm_config.ecc.supported_curves]]
|
| 62 | +struct_name = "<curve_name_1>" |
58 | 63 | modulus = "<modulus_1>"
|
59 | 64 | scalar = "<scalar_1>"
|
60 | 65 | a = "<a_1>"
|
61 | 66 | b = "<b_1>"
|
62 | 67 |
|
63 | 68 | [[app_vm_config.ecc.supported_curves]]
|
| 69 | +struct_name = "<curve_name_2>" |
64 | 70 | modulus = "<modulus_2>"
|
65 | 71 | scalar = "<scalar_2>"
|
66 | 72 | a = "<a_2>"
|
|
0 commit comments