use crate::{ generic_public_key::{GenericPublicKey, TPublicKey}, Error, }; use std::fmt::{self, Debug}; use std::marker::PhantomData; /// Implemented on some struct from a BLS library so it may be used internally in this crate. pub trait TAggregatePublicKey: Sized + Clone { fn to_public_key(&self) -> GenericPublicKey; // NOTE: this API *could* take a `&[&Pub]` as that's what the underlying library needs, // but it seems that this type would rarely occur due to our use of wrapper structs fn aggregate(pubkeys: &[GenericPublicKey]) -> Result; } /// A BLS aggregate public key that is generic across some BLS point (`AggPub`). /// /// Provides generic functionality whilst deferring all serious cryptographic operations to `AggPub`. #[derive(Clone)] pub struct GenericAggregatePublicKey { /// The underlying point which performs *actual* cryptographic operations. point: AggPub, _phantom: PhantomData, } impl GenericAggregatePublicKey where AggPub: TAggregatePublicKey, { pub fn to_public_key(&self) -> GenericPublicKey { self.point.to_public_key() } pub fn aggregate(pubkeys: &[GenericPublicKey]) -> Result { Ok(Self { point: AggPub::aggregate(pubkeys)?, _phantom: PhantomData, }) } } impl Debug for GenericAggregatePublicKey where AggPub: TAggregatePublicKey, Pub: TPublicKey, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self.to_public_key()) } }