Patched Crates

We maintain forks of commonly used libraries in blockchain infrastructure to significantly accelerate the execution of certain operations. Under the hood, we use precompiles to acheive tremendous performance improvements in proof generation time.

If you know of a library or library version that you think should be patched, please open an issue or a pull request!

Supported Libraries

Crate NameRepositoryNotes
ed25519-consensussp1-patches/ed25519-consensused25519 verify
curve25519-dalek-ngsp1-patches/curve25519-dalek-nged25519 verify
curve25519-daleksp1-patches/curve25519-daleked25519 verify
revm-precompilesp1-patches/revmecrecover precompile
reth-primitivessp1-patches/rethecrecover transactions

Using Patched Crates

To use the patched libraries, you can use corresponding patch entries in your program's Cargo.toml such as:

sha2-v0-9-8 = { git = "", package = "sha2", branch = "patch-v0.9.8" }
sha2-v0-10-6 = { git = "", package = "sha2", branch = "patch-v0.10.6" }
sha2-v0-10-8 = { git = "", package = "sha2", branch = "patch-v0.10.8" }
curve25519-dalek = { git = "", branch = "patch-v4.1.1" }
curve25519-dalek-ng = { git = "", branch = "patch-v4.1.1" }
ed25519-consensus = { git = "", branch = "patch-v2.1.0" }
tiny-keccak = { git = "", branch = "patch-v2.0.2" }
revm = { git = "", branch = "patch-v5.0.0" }
reth-primitives = { git = "", default-features = false, branch = "sp1-reth" }

You may also need to update your Cargo.lock file. For example:

cargo update -p ed25519-consensus

If you encounter issues relating to cargo / git, you can try setting CARGO_NET_GIT_FETCH_WITH_CLI:

CARGO_NET_GIT_FETCH_WITH_CLI=true cargo update -p ed25519-consensus

You can permanently set this value in ~/.cargo/config:

git-fetch-with-cli = true

Sanity Checks

You must make sure your patch is in the workspace root, otherwise it will not be applied.

You can check if the patch was applied by running a command like the following:

cargo tree -p sha2
cargo tree -p sha2@0.9.8

Next to the package name, it should have a link to the Github repository that you patched with.

Checking whether a precompile is used

To check if a precompile is used by your program, when running the script to generate a proof, make sure to use the RUST_LOG=info environment variable and set up utils::setup_logger() in your script. Then, when you run the script, you should see a log message like the following:

2024-03-02T19:10:39.570244Z  INFO ... 
2024-03-02T19:10:39.570244Z  INFO ... 
2024-03-02T19:10:40.003907Z  INFO runtime.prove(...): Sharding the execution record.
2024-03-02T19:10:40.003916Z  INFO runtime.prove(...): Generating trace for each chip.
2024-03-02T19:10:40.003918Z  INFO runtime.prove(...): Record stats before generate_trace (incomplete): ShardStats {
    nb_cpu_events: 7476561,
    nb_add_events: 2126546,
    nb_mul_events: 11116,
    nb_sub_events: 54075,
    nb_bitwise_events: 646940,
    nb_shift_left_events: 142595,
    nb_shift_right_events: 274016,
    nb_divrem_events: 0,
    nb_lt_events: 81862,
    nb_field_events: 0,
    nb_sha_extend_events: 0,
    nb_sha_compress_events: 0,
    nb_keccak_permute_events: 2916,
    nb_ed_add_events: 0,
    nb_ed_decompress_events: 0,
    nb_weierstrass_add_events: 0,
    nb_weierstrass_double_events: 0,
    nb_k256_decompress_events: 0,

The ShardStats struct contains the number of events for each "table" from the execution of the program, including precompile tables. In the example above, the nb_keccak_permute_events field is 2916, indicating that the precompile for the Keccak permutation was used.