You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/docs/writing-programs/compiling.mdx
+24-12Lines changed: 24 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,21 @@ Once you have written an SP1 program, you must compile it to an ELF file that ca
6
6
7
7
## Development Builds
8
8
9
-
> WARNING: This may not generate a reproducible ELF which is necessary for verifying that your binary corresponds to given source code.
9
+
> WARNING: Running `cargo prove build` may not generate a reproducible ELF which is necessary for verifying that your binary corresponds to given source code.
10
10
>
11
-
> Use the[reproducible build system](#production-builds) for production builds.
11
+
> Use SP1's[reproducible build system](#production-builds) for production builds.
12
12
13
13
To build a program while developing, simply run the following command in the crate that contains your SP1 program:
14
14
15
15
```bash
16
+
# Enter the directory containing your SP1 program.
17
+
cd path/to/your/program
18
+
19
+
# Build the program.
16
20
cargo prove build
17
21
```
18
22
19
-
This will compile the ELF that can be executed in the zkVM. The output from the command will look something like this:
23
+
This will compile the ELF that can be executed in the zkVM. The output from the command will look similar to this:
20
24
21
25
```bash
22
26
[sp1] Compiling version_check v0.9.4
@@ -34,17 +38,27 @@ Under the hood, this CLI command calls `cargo build` with the `riscv32im-succinc
34
38
35
39
### Advanced Build Options
36
40
37
-
You can pass additional arguments to the `cargo prove build` command to customize the build process, like configuring what features are enabled, customizing the output directory and more. To see all available options, run `cargo prove build --help`. Many of these options mirror the options available in the `cargo build` command.
41
+
The `cargo prove build` command supports several configuration options to customize the build process for your program:
42
+
43
+
-`--features`: Enable specific features
44
+
-`--output-directory`: Specify a custom output location for the ELF
45
+
-`--elf-name`: Set a custom name for the output ELF file
46
+
-`--no-default-features`: Disable default features
47
+
-`--locked`: Ensure Cargo.lock remains unchanged
48
+
-`--packages`: Build only specified packages
49
+
-`--binaries`: Build only specified binaries
50
+
51
+
Run `cargo prove build --help` to see the complete list of options. Some options mirror those available in the standard `cargo build` command.
38
52
39
53
## Production Builds
40
54
41
-
For production builds of programs, you can build your program inside a Docker container which will generate a **reproducible ELF**on all platforms. To do so, just use the `--docker` flag and optionally the `--tag` flag with the release version you want to use (defaults to `latest`). For example:
55
+
For production builds, use Docker to generate a **reproducible ELF**that will be identical across all platforms. Simply add the `--docker` flag to your build command. You can also specify a release version using `--tag`, otherwise the tag defaults to the latest release. For example:
42
56
43
57
```bash
44
-
cargo prove build --docker --tag v1.0.1
58
+
cargo prove build --docker --tag v4.0.0
45
59
```
46
60
47
-
To verify that your build is reproducible, you can compute the SHA-512 hash of the ELF on different platforms and systems with:
61
+
To verify that your build is truly reproducible across different platforms and systems, compute the SHA-512 hash of the generated ELF file. The hash should be identical regardless of where you build it:
48
62
49
63
```bash
50
64
$ shasum -a 512 elf/riscv32im-succinct-zkvm-elf
@@ -82,19 +96,17 @@ The above output was generated by running `RUST_LOG=info cargo run --release -vv
82
96
83
97
To configure the build process when using the `sp1-build` crate, you can pass a [`BuildArgs`](https://docs.rs/sp1-build/latest/sp1_build/struct.BuildArgs.html) struct to to the [`build_program_with_args`](https://docs.rs/sp1-build/latest/sp1_build/fn.build_program_with_args.html) function. The build arguments are the same as the ones available from the `cargo prove build` command.
84
98
85
-
As an example, you could use the following code to build the Fibonacci example with the `docker` flag set to `true` and a custom output directory for the generated ELF:
99
+
As an example, you could use the following code to build the Fibonacci example with the `docker` flag set to `true` and a custom name for the generated ELF. This will generate a reproducible ELF file (with Docker) with the name `fibonacci-elf`:
**Note:** If you want reproducible builds with the `build.rs` approach, you should use the `docker` flag and the `build_program_with_args` function, as shown in the example above.
Copy file name to clipboardExpand all lines: book/docs/writing-programs/cycle-tracking.mdx
+53-37Lines changed: 53 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,59 +4,75 @@ import Example from "@site/static/examples_cycle-tracking_program_bin_normal.rs.
4
4
5
5
When writing a program, it is useful to know how many RISC-V cycles a portion of the program takes to identify potential performance bottlenecks. SP1 provides a way to track the number of cycles spent in a portion of the program.
6
6
7
-
## Tracking Cycles with Annotations
7
+
## Tracking Cycles
8
8
9
-
To track the number of cycles spent in a portion of the program, you can either put `println!("cycle-tracker-start: block name")` + `println!("cycle-tracker-end: block name")` statements (block name must be same between start and end) around the portion of your program you want to profile or use the `#[sp1_derive::cycle_tracker]` macro on a function. An example is shown below:
9
+
### Using Print Annotations
10
+
For simple debugging, use these annotations to log cycle counts to stdout:
10
11
11
-
<Example />
12
+
```rust
13
+
#![no_main]
14
+
sp1_zkvm::entrypoint!(main);
12
15
13
-
Note that to use the macro, you must add the `sp1-derive` crate to your dependencies for your program.
16
+
fnmain() {
17
+
letmutnums=vec![1, 1];
14
18
15
-
```toml
16
-
[dependencies]
17
-
sp1-derive = "4.0.0"
18
-
```
19
+
// Compute the sum of the numbers.
20
+
println!("cycle-tracker-start: compute");
21
+
letsum:u64=nums.iter().sum();
22
+
println!("cycle-tracker-end: compute");
23
+
}
24
+
```
19
25
20
-
In the script for proof generation, setup the logger with `utils::setup_logger()` and run the script with `RUST_LOG=info cargo run --release`. You should see the following output:
26
+
With this code, you will see output like the following in your logs:
21
27
22
28
```
23
-
$ RUST_LOG=info cargo run --release
24
-
Finished release [optimized] target(s) in 0.21s
25
-
Running `target/release/cycle-tracking-script`
26
-
2024-03-13T02:03:40.567500Z INFO execute: loading memory image
27
-
2024-03-13T02:03:40.567751Z INFO execute: starting execution
28
-
2024-03-13T02:03:40.567760Z INFO execute: clk = 0 pc = 0x2013b8
29
-
2024-03-13T02:03:40.567822Z INFO execute: ┌╴setup
30
-
2024-03-13T02:03:40.568095Z INFO execute: └╴4,398 cycles
31
-
2024-03-13T02:03:40.568122Z INFO execute: ┌╴main-body
32
-
2024-03-13T02:03:40.568149Z INFO execute: │ ┌╴expensive_function
33
-
2024-03-13T02:03:40.568250Z INFO execute: │ └╴1,368 cycles
34
-
stdout: result: 5561
35
-
2024-03-13T02:03:40.568373Z INFO execute: │ ┌╴expensive_function
36
-
2024-03-13T02:03:40.568470Z INFO execute: │ └╴1,368 cycles
37
-
stdout: result: 2940
38
-
2024-03-13T02:03:40.568556Z INFO execute: └╴5,766 cycles
39
-
2024-03-13T02:03:40.568566Z INFO execute: finished execution clk = 11127 pc = 0x0
40
-
2024-03-13T02:03:40.569251Z INFO execute: close time.busy=1.78ms time.idle=21.1µs
29
+
[INFO] compute: 1234 cycles
41
30
```
42
31
43
-
Note that we elegantly handle nested cycle tracking, as you can see above.
44
-
45
-
### Get Tracked Cycle Counts
46
-
47
-
To include tracked cycle counts in the `ExecutionReport` when using `ProverClient::execute`, use the following annotations:
32
+
### Using Report Annotations
33
+
To store cycle counts across multiple invocations in the `ExecutionReport`, use the report annotations:
Profiling a zkVM program produces a useful visualization ([example profile](https://share.firefox.dev/3Om1pzz)) which makes it easy to examine program performance and see exactly where VM cycles are being spent without needing to modify the program at all.
An example of using patched crates is available in our [Tendermint Example](https://github.com/succinctlabs/sp1/blob/main/examples/tendermint/program/Cargo.toml#L22-L25).
63
+
An example of using patched crates is available in [SP1 Blobstream](https://github.com/succinctlabs/sp1-blobstream/blob/89e058052c0b691898c5b56a62a6fa0270b31627/Cargo.toml#L40-L43).
54
64
55
65
## Ed25519 Acceleration
56
66
57
67
To accelerate Ed25519 operations, you'll need to patch crates depending on if you're using the `ed25519-consensus` or `ed25519-dalek` library in your program or dependencies.
58
68
59
-
Generally, `ed25519-consensus` has better performance than `ed25519-dalek` by a factor of 2.
69
+
Generally, `ed25519-consensus` has better performance for Ed25519 operations than `ed25519-dalek` by a factor of 2.
60
70
61
71
### Patches
62
72
63
73
Apply the following patches based on what crates are in your dependencies.
64
74
65
75
-`ed25519-consensus`
66
76
77
+
If using `ed25519-consensus`, you should patch `curve25519-dalek-ng` to accelerate ed25519 operations:
78
+
67
79
```toml
68
80
curve25519-dalek-ng = { git = "https://github.com/sp1-patches/curve25519-dalek-ng", tag = "patch-4.1.1-sp1-4.0.0" }
69
81
```
70
82
71
83
-`ed25519-dalek`
72
84
73
-
If using `ed25519-dalek` version `2.1`, you can patch it with the following:
85
+
If using `ed25519-dalek` version `2.1`, you should patch `curve25519-dalek` to accelerate ed25519 operations:
74
86
75
87
```toml
76
-
curve25519-dalek = { git = "https://github.com/sp1-patches/curve25519-dalek", tag = "curve25519_dalek-v4.1.3-patch-v1" }
88
+
curve25519-dalek = { git = "https://github.com/sp1-patches/curve25519-dalek", tag = "patch-4.1.3-sp1-4.0.0" }
77
89
```
78
90
79
91
## Secp256k1 Acceleration
@@ -89,16 +101,16 @@ Apply the following patches based on what crates are in your dependencies.
Note: The curve operations for `k256` are inside of the `ecdsa-core` crate, so you don't need to patch `k256` itself, and just patching `ecdsa-core` is enough.
96
108
97
109
-`secp256k1`
98
110
99
111
```toml
100
-
secp256k1 = { git = "https://github.com/sp1-patches/rust-secp256k1", tag = "secp256k1-v0.29.0-patch-v1" }
### Performance Benchmarks for Patched `substrate-bn` in `revm`
@@ -128,15 +140,15 @@ Note: The operations `run-add`, `run-mul`, and `run-pair` are from the `revm` cr
128
140
To accelerate [revm](https://github.com/bluealloy/revm) in SP1 using the BN254 patched crate, replace the `substrate-bn` crate with the patched crate by adding the following to `crates/precompile/Cargo.toml`:
To accelerate BLS12-381 operations, you'll need to patch the `bls12_381` crate. Apply the following patch by adding the following to your list of dependencies:
137
149
138
150
```toml
139
-
bls12_381 = { git = "https://github.com/sp1-patches/bls12_381", tag = "bls12_381-v0.8.0-patch-v1" }
151
+
bls12_381 = { git = "https://github.com/sp1-patches/bls12_381", tag = "patch-0.8.0-sp1-4.0.0" }
140
152
```
141
153
142
154
This patch significantly improves the performance of BLS12-381 operations, making it essential for applications that rely heavily on these cryptographic primitives.
@@ -158,15 +170,15 @@ This patch significantly improves the performance of BLS12-381 operations, makin
158
170
You can check if the patch was applied by using cargo's tree command to print the dependencies of the crate you patched.
159
171
160
172
```bash
161
-
cargo tree -p sha2@0.9.8
173
+
cargo tree -p sha2@0.10.8
162
174
```
163
175
164
176
Next to the package name, it should have a link to the Github repository that you patched with.
0 commit comments