-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from Chia-Network/chia-keys
add new crate chia-bls
- Loading branch information
Showing
19 changed files
with
1,471 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "chia-bls" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tiny-bip39 = "=1.0.0" | ||
anyhow = "=1.0.65" | ||
# the newer sha2 crate doesn't implement the digest traits required by hkdf | ||
sha2 = "=0.9.9" | ||
bls12_381_plus = "=0.7.0" | ||
num-bigint = "=0.4.3" | ||
hkdf = "=0.11.0" | ||
group = "=0.12.0" | ||
|
||
[dev-dependencies] | ||
hex = "^0.4.3" | ||
rand = "^0.8.5" | ||
criterion = "^0.4" | ||
|
||
[[bench]] | ||
name = "derive_key" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "sign" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "verify" | ||
harness = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
Library providing building blocks for a Chia wallet. | ||
|
||
BIP39 mnemonic handling: | ||
|
||
``` | ||
fn entropy_to_mnemonic(entropy: &[u8; 32]) -> String | ||
fn mnemonic_to_entropy(mnemonic: &str) -> Result<[u8; 32], Error> | ||
fn entropy_to_seed(entropy: &[u8; 32]) -> [u8; 64] | ||
``` | ||
|
||
SecretKey | ||
|
||
``` | ||
impl SecretKey { | ||
pub fn from_seed(seed: &[u8; 64]) -> SecretKey | ||
pub fn from_bytes(bytes: &[u8; 32]) -> Option<SecretKey> | ||
pub fn to_bytes(&self) -> [u8; 32] | ||
pub fn public_key(&self) -> PublicKey | ||
pub fn derive_unhardened(&self, idx: u32) -> SecretKey | ||
pub fn derive_hardened(&self, idx: u32) -> SecretKey | ||
} | ||
``` | ||
|
||
PublicKey | ||
|
||
``` | ||
impl PublicKey { | ||
pub fn from_bytes(bytes: &[u8; 48]) -> Option<PublicKey> | ||
pub fn to_bytes(&self) -> [u8; 48] | ||
pub fn derive_unhardened(&self, idx: u32) -> PublicKey | ||
} | ||
``` | ||
|
||
Unhardened Key derivation (`Key` can be both a secret- or public key) | ||
|
||
``` | ||
fn master_to_wallet_unhardened_intermediate<Key: DerivableKey>(key: &Key) -> Key | ||
fn master_to_wallet_unhardened<Key: DerivableKey>(key: &Key, idx: u32) -> Key | ||
``` | ||
|
||
Hardened key derivation (only SecretKey) | ||
|
||
``` | ||
fn master_to_wallet_hardened_intermediate(key: &SecretKey) -> SecretKey | ||
fn master_to_wallet_hardened(key: &SecretKey, idx: u32) -> SecretKey | ||
fn master_to_pool_singleton(key: &SecretKey, pool_wallet_idx: u32) -> SecretKey | ||
fn master_to_pool_authentication(key: &SecretKey, pool_wallet_idx: u32, idx: u32) -> SecretKey | ||
``` | ||
|
||
Signature | ||
|
||
``` | ||
impl Signature { | ||
pub fn from_bytes(buf: &[u8; 96]) -> Option<Signature> | ||
pub fn to_bytes(&self) -> [u8; 96] | ||
pub fn aggregate(&mut self, sig: &Signature) | ||
} | ||
impl Default for Signature { | ||
fn default() -> Self | ||
} | ||
``` | ||
|
||
sign and verify (using the Augmented scheme) | ||
|
||
``` | ||
pub fn sign<Msg: AsRef<[u8]>>(sk: &SecretKey, msg: Msg) -> Signature | ||
pub fn aggregate<Sig: Borrow<Signature>, I>(sigs: I) -> Signature | ||
where I: IntoIterator<Item = Sig> | ||
pub fn verify<Msg: AsRef<[u8]>>(sig: &Signature, key: &PublicKey, msg: Msg) -> bool | ||
pub fn aggregate_verify<Pk: Borrow<PublicKey>, Msg: Borrow<[u8]>, I>(sig: &Signature, data: I) -> bool | ||
where I: IntoIterator<Item = (Pk, Msg)> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use chia_bls::derivable_key::DerivableKey; | ||
use chia_bls::secret_key::SecretKey; | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use rand::rngs::StdRng; | ||
use rand::{Rng, SeedableRng}; | ||
use std::time::Instant; | ||
|
||
fn key_derivation_benchmark(c: &mut Criterion) { | ||
let mut rng = StdRng::seed_from_u64(1337); | ||
let mut data = [0u8; 32]; | ||
rng.fill(data.as_mut_slice()); | ||
|
||
let sk = SecretKey::from_seed(&data); | ||
let pk = sk.public_key(); | ||
|
||
c.bench_function("secret key, unhardened", |b| { | ||
b.iter_custom(|iters| { | ||
let start = Instant::now(); | ||
for i in 0..iters { | ||
black_box(sk.derive_unhardened(i as u32)); | ||
} | ||
start.elapsed() | ||
}) | ||
}); | ||
c.bench_function("secret key, hardened", |b| { | ||
b.iter_custom(|iters| { | ||
let start = Instant::now(); | ||
for i in 0..iters { | ||
black_box(sk.derive_hardened(i as u32)); | ||
} | ||
start.elapsed() | ||
}) | ||
}); | ||
c.bench_function("public key, unhardened", |b| { | ||
b.iter_custom(|iters| { | ||
let start = Instant::now(); | ||
for i in 0..iters { | ||
black_box(pk.derive_unhardened(i as u32)); | ||
} | ||
start.elapsed() | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(key_derivation, key_derivation_benchmark); | ||
criterion_main!(key_derivation); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use chia_bls::secret_key::SecretKey; | ||
use chia_bls::signature::sign; | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use rand::rngs::StdRng; | ||
use rand::{Rng, SeedableRng}; | ||
|
||
fn sign_benchmark(c: &mut Criterion) { | ||
let mut rng = StdRng::seed_from_u64(1337); | ||
let mut data = [0u8; 32]; | ||
rng.fill(data.as_mut_slice()); | ||
|
||
let sk = SecretKey::from_seed(&data); | ||
let small_msg = b"The quick brown fox jumps over the lazy dog"; | ||
let large_msg = [42_u8; 4096]; | ||
|
||
c.bench_function("sign, small msg", |b| { | ||
b.iter(|| { | ||
sign(&sk, black_box(&small_msg)); | ||
}); | ||
}); | ||
c.bench_function("sign, 4kiB msg", |b| { | ||
b.iter(|| { | ||
sign(&sk, black_box(&large_msg)); | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(signing, sign_benchmark); | ||
criterion_main!(signing); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use chia_bls::secret_key::SecretKey; | ||
use chia_bls::signature; | ||
use chia_bls::signature::sign; | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use rand::rngs::StdRng; | ||
use rand::{Rng, SeedableRng}; | ||
|
||
fn verify_benchmark(c: &mut Criterion) { | ||
let mut rng = StdRng::seed_from_u64(1337); | ||
let mut data = [0u8; 32]; | ||
rng.fill(data.as_mut_slice()); | ||
|
||
let sk = SecretKey::from_seed(&data); | ||
let pk = sk.public_key(); | ||
let msg_small = b"The quick brown fox jumps over the lazy dog"; | ||
let msg_large = [42_u8; 4096]; | ||
let sig_small = sign(&sk, &msg_small); | ||
let sig_large = sign(&sk, &msg_large); | ||
|
||
c.bench_function("verify, small msg", |b| { | ||
b.iter(|| { | ||
signature::verify(&sig_small, &pk, black_box(&msg_small)); | ||
}); | ||
}); | ||
c.bench_function("verify, 4kiB msg", |b| { | ||
b.iter(|| { | ||
signature::verify(&sig_large, &pk, black_box(&msg_large)); | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(verify, verify_benchmark); | ||
criterion_main!(verify); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target | ||
corpus | ||
artifacts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[package] | ||
name = "chia-bls-fuzz" | ||
version = "0.0.0" | ||
authors = ["Automatically generated"] | ||
publish = false | ||
edition = "2018" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
libfuzzer-sys = "0.4" | ||
pyo3 = { version = ">=0.17.2", features = ["auto-initialize"]} | ||
|
||
[dependencies.chia-bls] | ||
path = ".." | ||
|
||
# Prevent this from interfering with workspaces | ||
[workspace] | ||
members = ["."] | ||
|
||
[[bin]] | ||
name = "derive" | ||
path = "fuzz_targets/derive.rs" | ||
test = false | ||
doc = false | ||
|
||
[[bin]] | ||
name = "blspy-fidelity" | ||
path = "fuzz_targets/blspy-fidelity.rs" | ||
test = false | ||
doc = false |
Oops, something went wrong.