Path: blob/main/frontier/07-pairings/rust/src/lib.rs
483 views
unlisted
//! # Module 07: Bilinear Pairings — Exercises1//!2//! Pairings are hard to implement from scratch in toy code.3//! These exercises focus on using pairing-like abstractions for BLS signatures.4//!5//! ## Progression6//! 1. `bls_sign` — signature + doc7//! 2. `bls_verify` — signature + doc8//! 3. `bls_aggregate_sigs` — signature + doc9//! 4. `bls_aggregate_verify` — signature only1011/// Placeholder type for a group element (in a real implementation, this12/// would be a point on a pairing-friendly curve).13pub type GroupElement = u64;1415/// Placeholder pairing function type: e(G1, G2) -> GT.16pub type PairingFn = fn(GroupElement, GroupElement) -> GroupElement;1718/// Sign a message point with a secret key: σ = sk * H(m).19///20/// In BLS, the signature is a scalar multiplication of the21/// hash-to-curve output by the secret key.22pub fn bls_sign(sk: u64, message_point: GroupElement) -> GroupElement {23todo!("BLS sign: sk * H(m)")24}2526/// Verify a BLS signature.27///28/// Check that e(σ, g2) == e(H(m), pk) where:29/// - σ is the signature30/// - g2 is the generator of G231/// - H(m) is the message hashed to G132/// - pk is the public key in G233pub fn bls_verify(34sig: GroupElement,35g2: GroupElement,36message_point: GroupElement,37pk: GroupElement,38pairing: PairingFn,39) -> bool {40todo!("BLS verify: check pairing equation")41}4243/// Aggregate multiple BLS signatures into one.44///45/// Aggregation is simply the sum (or product, depending on group notation)46/// of all individual signatures.47pub fn bls_aggregate_sigs(sigs: &[GroupElement]) -> GroupElement {48todo!("Sum/combine all signatures into one")49}5051/// Verify an aggregated BLS signature against multiple public keys and messages.52///53/// For distinct-message aggregation:54/// e(σ_agg, g2) == ∏ e(H(m_i), pk_i)55pub fn bls_aggregate_verify(56agg_sig: GroupElement,57g2: GroupElement,58message_points: &[GroupElement],59pks: &[GroupElement],60pairing: PairingFn,61) -> bool {62todo!("Verify aggregated BLS signature")63}6465#[cfg(test)]66mod tests {67use super::*;6869// Toy "pairing": simple multiplication (NOT cryptographically meaningful).70fn toy_pairing(a: GroupElement, b: GroupElement) -> GroupElement {71a.wrapping_mul(b)72}7374#[test]75#[ignore]76fn test_bls_sign() {77let sig = bls_sign(42, 7);78assert_ne!(sig, 0);79}8081#[test]82#[ignore]83fn test_bls_aggregate_sigs() {84let sigs = vec![10, 20, 30];85let agg = bls_aggregate_sigs(&sigs);86assert_ne!(agg, 0);87}88}899091