Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHIA-193: Rust bindings to chiapos #439

Merged
merged 25 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ IF (BUILD_PROOF_OF_SPACE_STATICALLY)
target_link_libraries(ProofOfSpace PUBLIC -static -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)
ENDIF()

option(BUILD_STATIC_CHIAPOS_LIBRARY "Build chiapos library statically" OFF)
option(BUILD_STATIC_CHIAPOS_LIBRARY "Build chiapos static library (verify-only)" OFF)
IF (BUILD_STATIC_CHIAPOS_LIBRARY)
message("Statically build chiapos library")
add_library(chiapos_static STATIC src/chacha8.c src/verifier.hpp c-bindings/wrapper.cpp)
message("Build chiapos static library (verify-only)")
add_library(chiapos_static STATIC src/chacha8.c c-bindings/wrapper.cpp)
target_link_libraries(chiapos_static PUBLIC blake3)
target_include_directories(chiapos_static PUBLIC lib/include)
ENDIF()
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ Attacks that can provide significant space savings for the final file.
./HellmanAttacks -f "plot.dat" check <iterations>
```

## Python
## Python binding

Finally, python bindings are provided in the python-bindings directory.
Python bindings are provided in the python-bindings directory.

### Install

Expand All @@ -85,6 +85,10 @@ Testings uses pytest. Linting uses flake8 and mypy.
py.test ./tests -s -v
```

# Rust binding

Finally, Rust bindings are provided, but only validation of proofs of space is supported, and it cannot be used to make plots or create proofs for plots.

## ci Building
The primary build process for this repository is to use GitHub Actions to
build binary wheels for MacOS, Linux (x64 and aarch64), and Windows and publish
Expand Down
13 changes: 4 additions & 9 deletions c-bindings/wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#include "wrapper.h"
#include "verifier.hpp"

extern "C" {
ByteArray validate_proof(const uint8_t* seed, uint8_t k, const uint8_t* challenge, const uint8_t* proof, uint16_t proof_len) {
bool validate_proof(uint8_t* seed, uint8_t k, uint8_t* challenge, uint8_t* proof, uint16_t proof_len, uint8_t* quality_buf) {
arvidn marked this conversation as resolved.
Show resolved Hide resolved
Verifier v;
auto quality = v.ValidateProof(seed, k, challenge, proof, proof_len);
if (quality.GetSize() == 0) {
Rigidity marked this conversation as resolved.
Show resolved Hide resolved
return ByteArray { nullptr, 0 };
return false;
}
uint8_t *quality_buf = new uint8_t[32];
quality.ToBytes(quality_buf);
delete[] quality_buf;
return ByteArray { quality_buf, 32 };
}

void delete_byte_array(ByteArray array) {
delete[] array.data;
return true;
}
}
10 changes: 1 addition & 9 deletions c-bindings/wrapper.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#include "picosha2.hpp"
#include "../src/verifier.hpp"

extern "C" {
typedef struct {
uint8_t* data;
size_t length;
} ByteArray;

ByteArray validate_proof(const uint8_t* seed, uint8_t k, const uint8_t* challenge, const uint8_t* proof, uint16_t proof_len);

void delete_byte_array(ByteArray array);
bool validate_proof(uint8_t* seed, uint8_t k, uint8_t* challenge, uint8_t* proof, uint16_t proof_len, uint8_t* quality_buf);
arvidn marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 0 additions & 2 deletions rust-bindings/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ fn main() {
.clang_arg(format!("-I{}", blake3_include_path.to_str().unwrap()))
.clang_arg("-std=c++14")
.allowlist_function("validate_proof")
.allowlist_function("delete_byte_array")
.allowlist_type("ByteArray")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
Expand Down
20 changes: 6 additions & 14 deletions rust-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,14 @@ pub fn validate_proof(
};

unsafe {
let array = bindings::validate_proof(
seed.as_ptr(),
bindings::validate_proof(
seed.as_ptr() as *mut u8,
k,
challenge.as_ptr(),
proof.as_ptr(),
challenge.as_ptr() as *mut u8,
proof.as_ptr() as *mut u8,
arvidn marked this conversation as resolved.
Show resolved Hide resolved
proof_len,
);

if array.data.is_null() {
false
} else {
let data = std::slice::from_raw_parts(array.data, array.length);
quality.copy_from_slice(data);
bindings::delete_byte_array(array);
true
}
quality.as_mut_ptr(),
)
}
}

Expand Down
Loading