Skip to content

Commit ed4297b

Browse files
Merge pull request #73 from unseddd/fuzz
Fuzz tests for various components
2 parents e5e0b2f + 4aad7da commit ed4297b

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

fuzz/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
target
3+
corpus
4+
artifacts

fuzz/Cargo.toml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
[package]
3+
name = "grin_secp256k1zkp-fuzz"
4+
version = "0.0.0"
5+
authors = ["Automatically generated"]
6+
publish = false
7+
edition = "2018"
8+
9+
[package.metadata]
10+
cargo-fuzz = true
11+
12+
[dependencies]
13+
libfuzzer-sys = "0.3"
14+
15+
[dependencies.grin_secp256k1zkp]
16+
path = ".."
17+
18+
# Prevent this from interfering with workspaces
19+
[workspace]
20+
members = ["."]
21+
22+
[[bin]]
23+
name = "fuzz_sign"
24+
path = "fuzz_targets/fuzz_sign.rs"
25+
26+
[[bin]]
27+
name = "fuzz_ecdh"
28+
path = "fuzz_targets/fuzz_ecdh.rs"
29+
30+
[[bin]]
31+
name = "fuzz_aggsig"
32+
path = "fuzz_targets/fuzz_aggsig.rs"

fuzz/fuzz_targets/fuzz_aggsig.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
4+
extern crate secp256k1zkp;
5+
6+
use secp256k1zkp::{
7+
AggSigPartialSignature,
8+
ContextFlag,
9+
Message,
10+
Secp256k1,
11+
PublicKey,
12+
SecretKey
13+
};
14+
15+
use secp256k1zkp::aggsig::AggSigContext;
16+
use secp256k1zkp::rand::{Rng, thread_rng};
17+
18+
fuzz_target!(|data: &[u8]| {
19+
let numkeys = 3;
20+
if data.len() < (numkeys + 1) * 32 {
21+
return ();
22+
}
23+
24+
let mut rng = thread_rng();
25+
let secp = Secp256k1::with_caps(ContextFlag::Full);
26+
let mut pks: Vec<PublicKey> = Vec::with_capacity(numkeys);
27+
let mut keypairs: Vec<(SecretKey, PublicKey)> = Vec::with_capacity(numkeys);
28+
29+
for i in 0..numkeys {
30+
if let Ok(sk) = SecretKey::from_slice(&secp, &data[i*32..(i+1)*32]) {
31+
let pk = PublicKey::from_secret_key(&secp, &sk).unwrap();
32+
pks.push(pk.clone());
33+
keypairs.push((sk, pk));
34+
} else {
35+
let (sk, pk) = secp.generate_keypair(&mut rng).unwrap();
36+
pks.push(pk.clone());
37+
keypairs.push((sk, pk));
38+
}
39+
}
40+
41+
let aggsig = AggSigContext::new(&secp, &pks);
42+
43+
for i in 0..numkeys {
44+
if aggsig.generate_nonce(i) != true {
45+
panic!("failed to generate aggsig nonce: {}", i);
46+
}
47+
}
48+
49+
let mut msg_in = [0u8; 32];
50+
rng.fill(&mut msg_in);
51+
let msg = Message::from_slice(&msg_in).unwrap();
52+
53+
let mut partial_sigs: Vec<AggSigPartialSignature> = vec![];
54+
55+
for (i, (ss, _)) in keypairs.iter().enumerate() {
56+
match aggsig.partial_sign(msg.clone(), ss.clone(), i) {
57+
Ok(res) => partial_sigs.push(res),
58+
Err(e) => panic!("error creating partial signature: {:?}", e),
59+
}
60+
}
61+
62+
match aggsig.combine_signatures(&partial_sigs) {
63+
Ok(full_sig) => { let _ = aggsig.verify(full_sig, msg.clone(), &pks); () },
64+
Err(e) => panic!("error combining signatures: {:?}", e),
65+
}
66+
});

fuzz/fuzz_targets/fuzz_ecdh.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
4+
extern crate secp256k1zkp;
5+
6+
use secp256k1zkp::{Secp256k1, PublicKey, SecretKey};
7+
use secp256k1zkp::ecdh::SharedSecret;
8+
9+
fuzz_target!(|data: &[u8]| {
10+
if data.len() < 32 {
11+
return ();
12+
}
13+
14+
let s = Secp256k1::new();
15+
16+
if let Ok(sk) = SecretKey::from_slice(&s, &data[..32]) {
17+
match PublicKey::from_secret_key(&s, &sk) {
18+
Ok(pk) => { let _ = SharedSecret::new(&s, &pk, &sk); () },
19+
Err(e) => panic!("cannot create public key from secret: {}", e),
20+
}
21+
}
22+
});
23+

fuzz/fuzz_targets/fuzz_sign.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
4+
extern crate secp256k1zkp;
5+
6+
use secp256k1zkp::{Message, Secp256k1, PublicKey, SecretKey};
7+
8+
fuzz_target!(|data: &[u8]| {
9+
if data.len() < 64 {
10+
return ();
11+
}
12+
13+
let s = Secp256k1::new();
14+
15+
let msg = Message::from_slice(&data[..32]).unwrap();
16+
17+
if let Ok(sk) = SecretKey::from_slice(&s, &data[32..64]) {
18+
match s.sign(&msg, &sk) {
19+
Ok(sig) => {
20+
match PublicKey::from_secret_key(&s, &sk) {
21+
Ok(pk) => s.verify(&msg, &sig, &pk).unwrap(),
22+
Err(e) => panic!("cannot create public key from secret: {}", e),
23+
}
24+
}
25+
Err(e) => panic!("error creating signature: {}", e),
26+
}
27+
}
28+
});

0 commit comments

Comments
 (0)