Skip to content

Commit 065835b

Browse files
HrikBjonathanpwang
andauthored
docs: guest libs (#1805)
Separates the functions exposed by guest libs into their own section in the book. Closes INT-4294 --------- Co-authored-by: Jonathan Wang <[email protected]>
1 parent 196c768 commit 065835b

File tree

14 files changed

+314
-246
lines changed

14 files changed

+314
-246
lines changed

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/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: 1 addition & 49 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

@@ -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: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
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-sha256-guest`](./sha256.md) - SHA2-256 hash function.
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-sha256-guest`](./sha256.md) - SHA-256 hash function. 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-sha256-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, three 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.
@@ -57,12 +55,14 @@ supported_moduli = ["<modulus_1>", "<modulus_2>", ...]
5755
supported_curves = ["Bls12_381", "Bn254"]
5856

5957
[[app_vm_config.ecc.supported_curves]]
58+
struct_name = "<curve_name_1>"
6059
modulus = "<modulus_1>"
6160
scalar = "<scalar_1>"
6261
a = "<a_1>"
6362
b = "<b_1>"
6463

6564
[[app_vm_config.ecc.supported_curves]]
65+
struct_name = "<curve_name_2>"
6666
modulus = "<modulus_2>"
6767
scalar = "<scalar_2>"
6868
a = "<a_2>"

book/src/custom-extensions/pairing.md

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -19,99 +19,3 @@ 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-
## Guest program setup
24-
25-
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.
26-
27-
In the guest program, we will import the `PairingCheck` and `IntMod` traits, along with the BLS12-381 curve structs (**IMPORTANT:** this requires the `bls12_381` feature enabled in Cargo.toml for the `openvm-pairing-guest` dependency), and a few other values that we will need:
28-
29-
```rust,no_run,noplayground title="guest program"
30-
{{ #include ../../../examples/pairing/src/main.rs:imports }}
31-
```
32-
33-
Additionally, we'll need to initialize our moduli and `Fp2` struct via the following macros. For a more in-depth description of these macros, please see the [OpenVM Algebra](./algebra.md) section.
34-
35-
```rust,no_run,noplayground
36-
{{ #include ../../../examples/pairing/src/main.rs:init }}
37-
```
38-
39-
## Input values
40-
41-
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.
42-
43-
We can create a new struct to hold these `AffinePoint`s for the purpose of this guide. You may instead put them into a custom struct to serve your use case.
44-
45-
```rust
46-
#[derive(Clone, serde::Serialize, serde::Deserialize)]
47-
pub struct PairingCheckInput {
48-
p0: AffinePoint<Fp>,
49-
p1: AffinePoint<Fp2>,
50-
q0: AffinePoint<Fp>,
51-
q1: AffinePoint<Fp2>,
52-
}
53-
```
54-
55-
## Pairing check
56-
57-
Most users that use the pairing extension will want to assert that a pairing is valid (the final exponentiation equals one). With the `PairingCheck` trait imported from the previous section, we have access to the `pairing_check` function on the `Bls12_381` struct. After reading in the input struct, we can use its values in the `pairing_check`:
58-
59-
```rust,no_run,noplayground
60-
{{ #include ../../../examples/pairing/src/main.rs:pairing_check }}
61-
```
62-
63-
## Additional functionality
64-
65-
We also have access to each of the specific functions that the pairing check utilizes for either the BN254 or BLS12-381 elliptic curves.
66-
67-
### Multi-Miller loop
68-
69-
The multi-Miller loop requires the MultiMillerLoop trait can also be run separately via:
70-
71-
```rust
72-
let f = Bls12_381::multi_miller_loop(
73-
&[p0, p1],
74-
&[q0, q1],
75-
);
76-
```
77-
78-
## Running via CLI
79-
80-
### Config parameters
81-
82-
For the guest program to build successfully, we'll need to create an `openvm.toml` configuration file somewhere. It contains all of the necessary configuration information for enabling the OpenVM components that are used in the pairing check.
83-
84-
```toml
85-
# openvm.toml
86-
[app_vm_config.pairing]
87-
supported_curves = ["Bls12_381"]
88-
89-
[app_vm_config.modular]
90-
supported_moduli = [
91-
"4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787",
92-
]
93-
94-
[app_vm_config.fp2]
95-
supported_moduli = [
96-
["Bls12_381Fp2", "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787"],
97-
]
98-
```
99-
100-
Also note that since this is a complicated computation, the `keygen` step requires quite a lot of memory. Run it with `RUST_MIN_STACK` set to a large value, e.g.
101-
102-
```bash
103-
RUST_MIN_STACK=8388608 cargo openvm keygen
104-
```
105-
106-
### Full example program
107-
108-
This [example code](https://github.com/openvm-org/openvm/blob/main/examples/pairing/src/main.rs) contains hardcoded values and no inputs as an example that can be run via the CLI.
109-
110-
```rust,no_run,noplayground
111-
{{ #include ../../../examples/pairing/src/main.rs:pre }}
112-
{{ #include ../../../examples/pairing/src/main.rs:imports }}
113-
114-
{{ #include ../../../examples/pairing/src/main.rs:init }}
115-
116-
{{ #include ../../../examples/pairing/src/main.rs:main }}
117-
```

book/src/custom-extensions/sha256.md

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

3-
The OpenVM SHA-256 extension provides tools for using the SHA-256 hash function. Refer [here](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf) for more details on SHA-256.
4-
The functional part is provided by the `openvm-sha256-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 SHA-256Guest extension provides two functions for using in your guest code:
9-
10-
- `sha256(input: &[u8]) -> [u8; 32]`: Computes the SHA-256 hash of the input data and returns it as an array of 32 bytes.
11-
- `set_sha256(input: &[u8], output: &mut [u8; 32])`: Sets the output to the SHA-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/sha256/src/main.rs).
14-
15-
### Example
16-
17-
```rust,no_run,noplayground
18-
{{ #include ../../../examples/sha256/src/main.rs:imports }}
19-
{{ #include ../../../examples/sha256/src/main.rs:main }}
20-
```
21-
22-
To be able to import the `sha256` function, add the following to your `Cargo.toml` file:
23-
24-
```toml
25-
openvm-sha256-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 SHA-256 guest extension also provides another way to use the intrinsic SHA-256 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 SHA-256 intrinsic. This is enabled only when the target is `zkvm`.
3+
The SHA-256 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 SHA-256 intrinsic. This is enabled only when the target is `zkvm`.
324

335
- `zkvm_sha256_impl(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

0 commit comments

Comments
 (0)