|
1 |
| -# SHA-256 |
| 1 | +# SHA-2 |
2 | 2 |
|
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. |
| 3 | +The OpenVM SHA-2 extension provides tools for using the SHA-256, SHA-512, and SHA-384 hash functions. Refer [here](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf) for more details on the SHA-2 family of hash functions. |
| 4 | +The functional part is provided by the `openvm-sha2-guest` crate, which is a guest library that can be used in any OpenVM program. |
5 | 5 |
|
6 | 6 | ## Functions for guest code
|
7 | 7 |
|
8 |
| -The OpenVM SHA-256Guest extension provides two functions for using in your guest code: |
| 8 | +The OpenVM SHA-2 Guest extension provides three pairs of functions for using in your guest code: |
9 | 9 |
|
10 | 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. |
| 11 | +- `set_sha256(input: &[u8], output: &mut [u8; 32])`: Sets the provided output buffer to the SHA-256 hash of the input data. |
| 12 | +- `sha512(input: &[u8]) -> [u8; 64]`: Computes the SHA-512 hash of the input data and returns it as an array of 64 bytes. |
| 13 | +- `set_sha512(input: &[u8], output: &mut [u8; 64])`: Sets the provided output buffer to the SHA-512 hash of the input data. |
| 14 | +- `sha384(input: &[u8]) -> [u8; 48]`: Computes the SHA-384 hash of the input data and returns it as an array of 48 bytes. |
| 15 | +- `set_sha384(input: &[u8], output: &mut [u8; 64])`: Sets the first 48 bytes of the provided output buffer to the SHA-384 hash of the input data and sets the rest of the buffer to zero. |
12 | 16 |
|
13 |
| -See the full example [here](https://github.com/openvm-org/openvm/blob/main/examples/sha256/src/main.rs). |
| 17 | +See the full example [here](https://github.com/openvm-org/openvm/blob/main/examples/sha2/src/main.rs). |
14 | 18 |
|
15 | 19 | ### Example
|
16 | 20 |
|
17 | 21 | ```rust,no_run,noplayground
|
18 |
| -{{ #include ../../../examples/sha256/src/main.rs:imports }} |
19 |
| -{{ #include ../../../examples/sha256/src/main.rs:main }} |
| 22 | +{{ #include ../../../examples/sha2/src/main.rs:imports }} |
| 23 | +{{ #include ../../../examples/sha2/src/main.rs:main }} |
20 | 24 | ```
|
21 | 25 |
|
22 |
| -To be able to import the `sha256` function, add the following to your `Cargo.toml` file: |
| 26 | +To be able to import the `shaXXX` functions, add the following to your `Cargo.toml` file: |
23 | 27 |
|
24 | 28 | ```toml
|
25 |
| -openvm-sha256-guest = { git = "https://github.com/openvm-org/openvm.git" } |
| 29 | +openvm-sha2-guest = { git = "https://github.com/openvm-org/openvm.git" } |
26 | 30 | hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
|
27 | 31 | ```
|
28 | 32 |
|
29 | 33 | ## External Linking
|
30 | 34 |
|
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`. |
| 35 | +The SHA-2 guest extension also provides another way to use the intrinsic SHA-2 implementations. It provides functions that are meant to be linked to other external libraries. The external libraries can use these functions as hooks for the SHA-2 intrinsics. This is enabled only when the target is `zkvm`. |
32 | 36 |
|
33 |
| -- `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. |
| 37 | +- `zkvm_shaXXX_impl(input: *const u8, len: usize, output: *mut u8)`: where `XXX` is `256`, `512`, or `384`. These functions have `C` ABI. They take in a pointer to the input, the length of the input, and a pointer to the output buffer. |
34 | 38 |
|
35 | 39 | In the external library, you can do the following:
|
36 | 40 |
|
@@ -59,5 +63,5 @@ fn sha256(input: &[u8]) -> [u8; 32] {
|
59 | 63 | For the guest program to build successfully add the following to your `.toml` file:
|
60 | 64 |
|
61 | 65 | ```toml
|
62 |
| -[app_vm_config.sha256] |
| 66 | +[app_vm_config.sha2] |
63 | 67 | ```
|
0 commit comments