#![no_main] use arbitrary::Arbitrary; use commonware_codec::{Decode, DecodeExt, Encode, Read}; use commonware_consensus::simplex::{ scheme::{ bls12381_multisig, bls12381_threshold::{self}, ed25519, Scheme, }, types::{Certificate, Vote}, }; use commonware_cryptography::{ bls12381::primitives::variant::{MinPk, MinSig}, ed25519::PublicKey, sha256, }; use libfuzzer_sys::fuzz_target; type Ed25519Scheme = ed25519::Scheme; type Bls12381MultisigMinPk = bls12381_multisig::Scheme; type Bls12381MultisigMinSig = bls12381_multisig::Scheme; type ThresholdSchemeMinPk = bls12381_threshold::Scheme; type ThresholdSchemeMinSig = bls12381_threshold::Scheme; #[derive(Arbitrary, Debug)] enum FuzzInput { // Ed25519 Ed25519Vote(Vec), Ed25519Certificate { participants: u8, data: Vec }, // BLS12-381 Multisig MinPk MultisigMinPkVote(Vec), MultisigMinPkCertificate { participants: u8, data: Vec }, // BLS12-381 Multisig MinSig MultisigMinSigVote(Vec), MultisigMinSigCertificate { participants: u8, data: Vec }, // BLS12-381 Threshold MinPk ThresholdMinPkVote(Vec), ThresholdMinPkCertificate(Vec), // BLS12-381 Threshold MinSig ThresholdMinSigVote(Vec), ThresholdMinSigCertificate(Vec), } fn roundtrip_vote>(data: &[u8]) { if let Ok(vote) = Vote::::decode(data) { let encoded = vote.encode(); assert_eq!(data, encoded.as_ref()); } } fn roundtrip_certificate>( data: &[u8], cfg: &::Cfg, ) where S::Certificate: Read, { if let Ok(cert) = Certificate::::decode_cfg(data, cfg) { let encoded = cert.encode(); assert_eq!(data, encoded.as_ref()); } } fn participant_cfg(participants: u8) -> usize { participants.clamp(4, 255) as usize } fn fuzz(input: FuzzInput) { match input { FuzzInput::Ed25519Vote(data) => roundtrip_vote::(&data), FuzzInput::Ed25519Certificate { participants, data } => { roundtrip_certificate::(&data, &participant_cfg(participants)) } FuzzInput::MultisigMinPkVote(data) => roundtrip_vote::(&data), FuzzInput::MultisigMinPkCertificate { participants, data } => { roundtrip_certificate::(&data, &participant_cfg(participants)) } FuzzInput::MultisigMinSigVote(data) => roundtrip_vote::(&data), FuzzInput::MultisigMinSigCertificate { participants, data } => { roundtrip_certificate::(&data, &participant_cfg(participants)) } FuzzInput::ThresholdMinPkVote(data) => roundtrip_vote::(&data), FuzzInput::ThresholdMinPkCertificate(data) => { roundtrip_certificate::(&data, &()) } FuzzInput::ThresholdMinSigVote(data) => roundtrip_vote::(&data), FuzzInput::ThresholdMinSigCertificate(data) => { roundtrip_certificate::(&data, &()) } } } fuzz_target!(|input: FuzzInput| { fuzz(input); });