3 releases
new 0.1.5 | Mar 17, 2025 |
---|---|
0.1.3 | Mar 14, 2025 |
0.1.1 | Mar 14, 2025 |
0.1.0 |
|
#24 in Magic Beans
346 downloads per month
Used in spark-rust
130KB
2.5K
SLoC
Spark Cryptography
Welcome to Spark Cryptography, a Rust library providing cryptographic primitives, signature schemes, secret sharing, and key manipulation utilities for the Spark ecosystem.
Note: This crate is under active development and may not be production-ready.
Table of Contents
Overview
Spark Cryptography offers a variety of cryptographic operations aimed at supporting more advanced features such as threshold signing (FROST), adaptor signatures, Shamir secret sharing, and more. Highlights include:
- Key Arithmetic (e.g., adding and tweaking secp256k1 keys)
- Adaptor Signatures (for partial revelation in transactions)
- Secret Sharing (both a custom approach and Feldman/FROST-based schemes)
- FROST support for threshold Schnorr signatures on secp256k1
The crate is primarily designed to integrate with the broader Spark Rust SDK, but can also be used standalone.
Features
Below are some noteworthy features and their corresponding modules:
-
Key Management
- File:
src/key.rs
- Provides utilities for seed phrase handling, hierarchical deterministic (HD) key derivation, and more.
- File:
-
Key Arithmetic (optional, enabled via
self-signing
feature)- File:
src/key_arithmetic.rs
- Functions to perform elliptic-curve arithmetic on keys (e.g., add public keys, tweak keys, etc.).
- File:
-
Adaptor Signatures
- File:
src/adaptor_signature.rs
- Implementation for creating and applying adaptor signatures, verifying them, and bridging partial reveals in cryptographic protocols.
- File:
-
Secret Sharing (optional, enabled via
self-signing
feature)- Files:
src/secret_sharing/
- Offers Shamir-based verifiable secret sharing (VSS) and Feldman/FROST variants.
- Files:
-
FROST (requires the
spark-protos
dependency andproto-conversion
feature)- File:
src/signing.rs
- Provides utilities for multi-party Schnorr-style threshold signing under secp256k1 (FROST).
- File:
-
Utilities
- File:
src/utils.rs
- Various utility functions for future expansions or specialized conversions.
- File:
Installation
Add spark-cryptography to your Cargo.toml
:
[dependencies]
spark-cryptography = { git = "https://github.com/polarity/spark-rust-sdk.git", package = "spark-cryptography" }
(Adjust the Git URL as necessary, or specify a version if/when published to crates.io.)
To enable optional features, do something like:
[dependencies]
spark-cryptography = { git = "https://github.com/polarity/spark-rust-sdk.git", package = "spark-cryptography", features = ["self-signing"] }
Usage
use spark_cryptography::key::seed_from_phrase;
use spark_cryptography::key_arithmetic::add_public_keys;
fn main() {
// Example seed from a mnemonic phrase
let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
let seed = seed_from_phrase(phrase).expect("Failed to generate seed");
println!("Generated seed: {:?}", seed);
// Example addition of two compressed public keys
let pk1 = [/* 33-byte compressed secp256k1 pubkey */];
let pk2 = [/* 33-byte compressed secp256k1 pubkey */];
#[cfg(feature = "self-signing")]
{
let combined = add_public_keys(&pk1, &pk2).expect("Failed to add pubkeys");
println!("Combined compressed public key: {:?}", combined);
}
}
Key Management
seed_from_phrase
: convert a mnemonic phrase into a 64-byte seed.master_xpriv_from_seed
: derive a root extended private key.derive_new_leaf_key_from_xpriv
: derive child keys for hierarchical structures.
Adaptor Signatures
generate_adaptor_from_signature
: produce an adaptor signature that partially hides information until a certain condition is revealed.generate_signature_from_existing_adaptor
: finalize an adaptor signature once the secret is known.
Secret Sharing
split_secret_with_proofs
: create verifiable shares of a given secret (works withVerifiableSecretShare
).recover_secret
: rebuild the secret from a sufficient subset of shares.validate_share
: ensure a given share is valid via polynomial commitments.
FROST (Flexible Round-Optimized Schnorr Threshold)
- Provided under
src/signing.rs
. - Integrates with
frost_secp256k1_tr
. - Allows multiple signers to produce a single Schnorr signature on secp256k1.
Examples
See the test files for full usage examples. Notable test modules include:
Running tests locally:
cd crates/spark-cryptography
cargo test --features self-signing
Testing
Each module contains corresponding unit tests. You can run:
# From the project root
cargo test --workspace --features self-signing
Most core functionalities (secret sharing, key arithmetic, adaptor signatures) are behind the "self-signing"
feature. If you need advanced cryptographic features, ensure that feature is enabled.
License
Licensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Spark Cryptography by you shall be dual-licensed as above, without any additional terms or conditions.
Enjoy building secure and private applications with Spark Cryptography! If you have questions, feedback, or issues, please open an issue in the Spark Rust SDK repository.
@spark-cryptography ❤️
Dependencies
~15–26MB
~355K SLoC