#![no_main] use arbitrary::Arbitrary; use commonware_clearing::bajillion::{ Amounts, BalanceProof, Challenge, Close, Deposit, Flow, ForcedWithdrawal, Limits, Lookup, Opening, Payout, PayoutProof, Processed, Receipt, Request, Row, Signatures, State, Summary, Withdrawal, }; use commonware_codec::{Codec, Copying, RangeCfg}; use commonware_cryptography::{Sha256, ed25519::PublicKey, sha256::Digest}; use libfuzzer_sys::fuzz_target; use std::fmt::Debug; /// The most receivers a request or row may pay when decoding bytes. const RECEIVERS: usize = 16; /// The limits closes are decoded from bytes with. const LIMITS: Limits = Limits { rows: 16, receivers: RECEIVERS, withdrawals: 16, }; /// A value of one wire type, or bytes to decode as every wire type. #[allow(clippy::large_enum_variant)] #[derive(Arbitrary, Debug)] enum FuzzInput { Amounts(Amounts), State(State), Signatures(Signatures), Request(Request), Receipt(Receipt), Challenge(Challenge), Withdrawal(Withdrawal), Deposit(Deposit), ForcedWithdrawal(ForcedWithdrawal), Row(Row), Close(Close), Lookup(Lookup), Opening(Opening), Flow(Flow), Processed(Processed), Summary(Summary), Payout(Payout), PayoutProof(PayoutProof), BalanceProof(BalanceProof), Bytes(Vec), } /// Checks that `value` encodes to its stated size and decodes back to itself under `cfg`. fn roundtrip(value: &T, cfg: &T::Cfg) { let encoded = value.encode(); assert_eq!(encoded.len(), value.encode_size()); let decoded = T::decode_cfg(encoded, cfg).expect("an encoded value must decode"); assert_eq!(&decoded, value); } /// Decodes `bytes` as a `T` under `cfg`. A value that decodes must round-trip, and bytes that /// do not decode must be refused without panicking. fn decode(bytes: &[u8], cfg: &T::Cfg) { if let Ok(value) = T::decode_cfg(Copying(bytes), cfg) { roundtrip(&value, cfg); } } fuzz_target!(|input: FuzzInput| { match input { FuzzInput::Amounts(value) => roundtrip(&value, &RangeCfg::new(..=value.len())), FuzzInput::State(value) => roundtrip(&value, &()), FuzzInput::Signatures(value) => roundtrip(&value, &()), FuzzInput::Request(value) => roundtrip(&value, &value.batch.len()), FuzzInput::Receipt(value) => roundtrip(&value, &()), FuzzInput::Challenge(value) => roundtrip(&value, &()), FuzzInput::Withdrawal(value) => roundtrip(&value, &()), FuzzInput::Deposit(value) => roundtrip(&value, &()), FuzzInput::ForcedWithdrawal(value) => roundtrip(&value, &()), FuzzInput::Row(value) => roundtrip(&value, &value.payments.len()), FuzzInput::Close(value) => { let limits = Limits { rows: value.rows().len(), receivers: value .rows() .iter() .map(|row| row.payments.len()) .max() .unwrap_or(0), withdrawals: value.withdrawals().len(), }; roundtrip(&value, &limits) } FuzzInput::Lookup(value) => roundtrip(&value, &()), FuzzInput::Opening(value) => roundtrip(&value, &()), FuzzInput::Flow(value) => roundtrip(&value, &()), FuzzInput::Processed(value) => roundtrip(&value, &()), FuzzInput::Summary(value) => roundtrip(&value, &()), FuzzInput::Payout(value) => roundtrip(&value, &()), FuzzInput::PayoutProof(value) => roundtrip(&value, &()), FuzzInput::BalanceProof(value) => roundtrip(&value, &()), FuzzInput::Bytes(bytes) => { decode::>(&bytes, &RangeCfg::new(..=RECEIVERS)); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &RECEIVERS); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &RECEIVERS); decode::>(&bytes, &LIMITS); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); decode::>(&bytes, &()); } } });