|
| 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 | +}); |
0 commit comments