Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
duyuefeng0708
GitHub Repository: duyuefeng0708/Cryptography-From-First-Principle
Path: blob/main/frontier/07-pairings/rust/src/lib.rs
483 views
unlisted
1
//! # Module 07: Bilinear Pairings — Exercises
2
//!
3
//! Pairings are hard to implement from scratch in toy code.
4
//! These exercises focus on using pairing-like abstractions for BLS signatures.
5
//!
6
//! ## Progression
7
//! 1. `bls_sign` — signature + doc
8
//! 2. `bls_verify` — signature + doc
9
//! 3. `bls_aggregate_sigs` — signature + doc
10
//! 4. `bls_aggregate_verify` — signature only
11
12
/// Placeholder type for a group element (in a real implementation, this
13
/// would be a point on a pairing-friendly curve).
14
pub type GroupElement = u64;
15
16
/// Placeholder pairing function type: e(G1, G2) -> GT.
17
pub type PairingFn = fn(GroupElement, GroupElement) -> GroupElement;
18
19
/// Sign a message point with a secret key: σ = sk * H(m).
20
///
21
/// In BLS, the signature is a scalar multiplication of the
22
/// hash-to-curve output by the secret key.
23
pub fn bls_sign(sk: u64, message_point: GroupElement) -> GroupElement {
24
todo!("BLS sign: sk * H(m)")
25
}
26
27
/// Verify a BLS signature.
28
///
29
/// Check that e(σ, g2) == e(H(m), pk) where:
30
/// - σ is the signature
31
/// - g2 is the generator of G2
32
/// - H(m) is the message hashed to G1
33
/// - pk is the public key in G2
34
pub fn bls_verify(
35
sig: GroupElement,
36
g2: GroupElement,
37
message_point: GroupElement,
38
pk: GroupElement,
39
pairing: PairingFn,
40
) -> bool {
41
todo!("BLS verify: check pairing equation")
42
}
43
44
/// Aggregate multiple BLS signatures into one.
45
///
46
/// Aggregation is simply the sum (or product, depending on group notation)
47
/// of all individual signatures.
48
pub fn bls_aggregate_sigs(sigs: &[GroupElement]) -> GroupElement {
49
todo!("Sum/combine all signatures into one")
50
}
51
52
/// Verify an aggregated BLS signature against multiple public keys and messages.
53
///
54
/// For distinct-message aggregation:
55
/// e(σ_agg, g2) == ∏ e(H(m_i), pk_i)
56
pub fn bls_aggregate_verify(
57
agg_sig: GroupElement,
58
g2: GroupElement,
59
message_points: &[GroupElement],
60
pks: &[GroupElement],
61
pairing: PairingFn,
62
) -> bool {
63
todo!("Verify aggregated BLS signature")
64
}
65
66
#[cfg(test)]
67
mod tests {
68
use super::*;
69
70
// Toy "pairing": simple multiplication (NOT cryptographically meaningful).
71
fn toy_pairing(a: GroupElement, b: GroupElement) -> GroupElement {
72
a.wrapping_mul(b)
73
}
74
75
#[test]
76
#[ignore]
77
fn test_bls_sign() {
78
let sig = bls_sign(42, 7);
79
assert_ne!(sig, 0);
80
}
81
82
#[test]
83
#[ignore]
84
fn test_bls_aggregate_sigs() {
85
let sigs = vec![10, 20, 30];
86
let agg = bls_aggregate_sigs(&sigs);
87
assert_ne!(agg, 0);
88
}
89
}
90
91