-
Notifications
You must be signed in to change notification settings - Fork 0
Add c-hash-sig #14
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
Add c-hash-sig #14
Changes from 1 commit
896357d
f5aad4c
071d9e9
cfa80a9
3e5c0dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "c_hash_sig_crust" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| name = "c_hash_sig_crust" | ||
| crate-type = ["cdylib", "staticlib"] | ||
|
|
||
| [dependencies] | ||
| hashsig = { git = "https://github.com/b-wagn/hash-sig" } | ||
| rand = "0.9" | ||
| bincode = { version = "2.0.1", features = ["serde"] } | ||
|
|
||
| [build-dependencies] | ||
| build-helper = { path = "../build-helper" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| .PHONY: all build build-release test clean example run-example | ||
|
|
||
| # Build library in debug mode | ||
| build: | ||
| cargo build | ||
|
|
||
| # Build library in release mode | ||
| build-release: | ||
| cargo build --release | ||
|
|
||
| # Run tests | ||
| test: | ||
| cargo test | ||
|
|
||
| # Compile C example | ||
| example: build-release | ||
| gcc -o example example.c \ | ||
| -I. \ | ||
| -L./target/release \ | ||
| -lpq_bindings_c_rust \ | ||
| -lpthread -ldl -lm \ | ||
| -Wl,-rpath,./target/release | ||
|
|
||
| # Run example | ||
| run-example: example | ||
| ./example | ||
|
|
||
| # Clean | ||
| clean: | ||
| cargo clean | ||
| rm -f example | ||
| rm -rf include | ||
|
|
||
| # Build everything (library + example) | ||
| all: build-release example | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,278 @@ | ||||||||||
| # PQ Bindings C Rust | ||||||||||
|
|
||||||||||
| C bindings for the post-quantum cryptography library [hash-sig](https://github.com/b-wagn/hash-sig/). | ||||||||||
|
|
||||||||||
| ## Description | ||||||||||
|
|
||||||||||
| This project provides a C API for working with post-quantum hash-based signature schemes. It implements bindings for the XMSS (eXtended Merkle Signature Scheme) using SHA-3 and Winternitz encoding. | ||||||||||
|
|
||||||||||
| ## Features | ||||||||||
|
|
||||||||||
| ### Data Structures | ||||||||||
|
|
||||||||||
| - **PQSignatureSchemeSecretKey** - wrapper for secret key | ||||||||||
| - **PQSignatureSchemePublicKey** - wrapper for public key | ||||||||||
| - **PQSignature** - wrapper for signature | ||||||||||
| - **PQRange** - epoch range representation | ||||||||||
| - **PQSigningError** - error codes | ||||||||||
|
|
||||||||||
| ### Core Functions | ||||||||||
|
|
||||||||||
| #### Key Management | ||||||||||
|
|
||||||||||
| ```c | ||||||||||
| // Generate key pair | ||||||||||
| enum PQSigningError pq_key_gen( | ||||||||||
| uintptr_t activation_epoch, | ||||||||||
| uintptr_t num_active_epochs, | ||||||||||
| struct PQSignatureSchemePublicKey **pk_out, | ||||||||||
| struct PQSignatureSchemeSecretKey **sk_out | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| // Free memory | ||||||||||
| void pq_secret_key_free(struct PQSignatureSchemeSecretKey *key); | ||||||||||
| void pq_public_key_free(struct PQSignatureSchemePublicKey *key); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| #### Signing and Verification | ||||||||||
|
|
||||||||||
| ```c | ||||||||||
| // Sign message | ||||||||||
| enum PQSigningError pq_sign( | ||||||||||
| const struct PQSignatureSchemeSecretKey *sk, | ||||||||||
| uint32_t epoch, | ||||||||||
| const uint8_t *message, | ||||||||||
| uintptr_t message_len, | ||||||||||
| struct PQSignature **signature_out | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| // Verify signature | ||||||||||
| int pq_verify( | ||||||||||
| const struct PQSignatureSchemePublicKey *pk, | ||||||||||
| uint32_t epoch, | ||||||||||
| const uint8_t *message, | ||||||||||
| uintptr_t message_len, | ||||||||||
| const struct PQSignature *signature | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| // Free memory | ||||||||||
| void pq_signature_free(struct PQSignature *signature); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| #### State Management | ||||||||||
|
|
||||||||||
| ```c | ||||||||||
| // Get key activation interval | ||||||||||
| struct PQRange pq_get_activation_interval(const struct PQSignatureSchemeSecretKey *key); | ||||||||||
|
|
||||||||||
| // Get prepared interval | ||||||||||
| struct PQRange pq_get_prepared_interval(const struct PQSignatureSchemeSecretKey *key); | ||||||||||
|
|
||||||||||
| // Advance preparation to next interval | ||||||||||
| void pq_advance_preparation(struct PQSignatureSchemeSecretKey *key); | ||||||||||
|
|
||||||||||
| // Get maximum scheme lifetime | ||||||||||
| uint64_t pq_get_lifetime(void); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| #### Serialization | ||||||||||
|
|
||||||||||
| ```c | ||||||||||
| // Serialize/deserialize keys and signatures | ||||||||||
| enum PQSigningError pq_secret_key_serialize(...); | ||||||||||
| enum PQSigningError pq_secret_key_deserialize(...); | ||||||||||
| enum PQSigningError pq_public_key_serialize(...); | ||||||||||
| enum PQSigningError pq_public_key_deserialize(...); | ||||||||||
| enum PQSigningError pq_signature_serialize(...); | ||||||||||
| enum PQSigningError pq_signature_deserialize(...); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| #### Error Handling | ||||||||||
|
|
||||||||||
| ```c | ||||||||||
| // Get error description | ||||||||||
| char *pq_error_description(enum PQSigningError error); | ||||||||||
|
|
||||||||||
| // Free string | ||||||||||
| void pq_string_free(char *s); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ## Building | ||||||||||
|
|
||||||||||
| ### Requirements | ||||||||||
|
|
||||||||||
| - Rust 1.87 or higher | ||||||||||
| - Cargo | ||||||||||
|
|
||||||||||
| ### Build Library | ||||||||||
|
|
||||||||||
| ```bash | ||||||||||
| # Debug build | ||||||||||
| cargo build | ||||||||||
|
|
||||||||||
| # Release build (recommended for production) | ||||||||||
| cargo build --release | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| After building: | ||||||||||
| - Library: `target/release/libpq_bindings_c_rust.so` (Linux) or `target/release/libpq_bindings_c_rust.dylib` (macOS) or `target/release/pq_bindings_c_rust.dll` (Windows) | ||||||||||
| - Static library: `target/release/libpq_bindings_c_rust.a` | ||||||||||
|
||||||||||
| - Library: `target/release/libpq_bindings_c_rust.so` (Linux) or `target/release/libpq_bindings_c_rust.dylib` (macOS) or `target/release/pq_bindings_c_rust.dll` (Windows) | |
| - Static library: `target/release/libpq_bindings_c_rust.a` | |
| - Library: `target/release/libc_hash_sig_crust.so` (Linux) or `target/release/libc_hash_sig_crust.dylib` (macOS) or `target/release/c_hash_sig_crust.dll` (Windows) | |
| - Static library: `target/release/libc_hash_sig_crust.a` |
Outdated
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The header file name pq-bindings-c-rust.h is inconsistent with the library name c_hash_sig_crust defined in Cargo.toml. The generated header file will be based on the actual crate name, not this reference.
| - Header file: `include/pq-bindings-c-rust.h` (automatically generated) | |
| - Header file: `include/c_hash_sig_crust.h` (automatically generated) |
Outdated
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The header file name pq-bindings-c-rust.h is inconsistent with the library name c_hash_sig_crust defined in Cargo.toml. The generated header file will be based on the actual crate name, not this reference.
Outdated
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The gcc command references -lpq_bindings_c_rust, but the actual library name in Cargo.toml is c_hash_sig_crust. This should be -lc_hash_sig_crust to match the library name, otherwise linking will fail.
| gcc -o example example.c -I. -L./target/release -lpq_bindings_c_rust -lpthread -ldl -lm | |
| gcc -o example example.c -I. -L./target/release -lc_hash_sig_crust -lpthread -ldl -lm |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
|
|
||
| fn main() { | ||
| build_helper::run_cbindgen(); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Makefile references
-lpq_bindings_c_rust, but the actual library name in Cargo.toml isc_hash_sig_crust. This should be-lc_hash_sig_crustto match the library name, otherwise the example compilation will fail.