Skip to content

Commit 84a28b7

Browse files
authored
CHIA-193: Rust bindings to chiapos (#439)
Adds a Rust crate with a memory safe (at least, according to fuzz tests and unit tests I've written) binding to `validate_proof`. This is part of an effort to port block prevalidation to Rust.
2 parents e5eeb6e + e81f6f5 commit 84a28b7

File tree

18 files changed

+1034
-4
lines changed

18 files changed

+1034
-4
lines changed

.github/workflows/rust.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Rust bindings
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
release:
8+
types: [published]
9+
pull_request:
10+
branches:
11+
- "**"
12+
13+
permissions:
14+
id-token: write
15+
contents: read
16+
17+
jobs:
18+
fuzz_targets:
19+
name: Run fuzzers
20+
runs-on: ubuntu-latest
21+
env:
22+
CARGO_PROFILE_RELEASE_LTO: false
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: dtolnay/rust-toolchain@nightly
26+
27+
- name: Install cargo-fuzz
28+
run: cargo +nightly install cargo-fuzz
29+
30+
- name: Cargo fuzz
31+
run: |
32+
cd rust-bindings
33+
cargo fuzz list | xargs -I "%" sh -c "cargo +nightly fuzz run % -- -max_total_time=600 || exit 255"
34+
35+
build_crate:
36+
name: Build crate
37+
runs-on: ubuntu-latest
38+
strategy:
39+
fail-fast: false
40+
41+
steps:
42+
- uses: actions/checkout@v4
43+
with:
44+
fetch-depth: 0
45+
46+
- name: Set up Rust
47+
uses: dtolnay/rust-toolchain@stable
48+
with:
49+
components: rustfmt, clippy
50+
51+
- name: Rustfmt
52+
run: cargo fmt -- --files-with-diff --check
53+
54+
- name: Clippy
55+
run: cargo clippy
56+
57+
- name: Tests
58+
run: cargo test && cargo test --release
59+
60+
- name: Build
61+
run: cargo build --release
62+
63+
- name: Prepare for publish
64+
run: |
65+
mkdir rust-bindings/cpp
66+
cp -r src lib tests uint128_t python-bindings c-bindings CMakeLists.txt rust-bindings/cpp
67+
68+
- name: Publish to crates.io (dry run)
69+
# We use `--allow-dirty` because the `cpp` folder is copied into the working directory.
70+
# This is necessary because the `cpp` folder is not part of the crate otherwise.
71+
run: cargo publish --dry-run -p chiapos --allow-dirty
72+
73+
- name: Upload crate artifacts
74+
uses: actions/upload-artifact@v4
75+
with:
76+
name: crate
77+
path: ./target/package/*-*.crate
78+
79+
- name: Set Env
80+
uses: Chia-Network/actions/setjobenv@main
81+
env:
82+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
83+
84+
- name: Publish to crates.io
85+
if: env.RELEASE == 'true'
86+
env:
87+
CARGO_REGISTRY_TOKEN: ${{ secrets.cargo_registry_token }}
88+
# See comment above for why `--allow-dirty` is used.
89+
run: cargo publish -p chiapos --allow-dirty

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ build-*
3131
cmake-build*
3232
*.zip
3333
*.tar.gz
34-
libs/
34+
libs/
35+
target/

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ include_directories(
156156
option(BUILD_PROOF_OF_SPACE_STATICALLY "Build ProofOfSpace target statically" OFF)
157157
IF (BUILD_PROOF_OF_SPACE_STATICALLY)
158158
message("Statically build ProofOfSpace")
159-
target_link_libraries(ProofOfSpace -static -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)
159+
target_link_libraries(ProofOfSpace PUBLIC -static -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)
160+
ENDIF()
161+
162+
option(BUILD_STATIC_CHIAPOS_LIBRARY "Build chiapos static library (verify-only)" OFF)
163+
IF (BUILD_STATIC_CHIAPOS_LIBRARY)
164+
message("Build chiapos static library (verify-only)")
165+
add_library(chiapos_static STATIC src/chacha8.c c-bindings/wrapper.cpp)
166+
target_link_libraries(chiapos_static PUBLIC blake3)
167+
target_include_directories(chiapos_static PUBLIC lib/include)
160168
ENDIF()
161169

162170
FetchContent_Declare(

0 commit comments

Comments
 (0)