1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::{PublicKeyVt, SignatureVt};
use bls12_381_plus::{G1Affine, G2Affine, G2Projective};
use core::ops::{BitOr, Neg, Not};
use group::{Curve, Group};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use subtle::{Choice, CtOption};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AggregateSignatureVt(pub(crate) G2Projective);
impl Default for AggregateSignatureVt {
fn default() -> Self {
Self(G2Projective::identity())
}
}
impl From<&[SignatureVt]> for AggregateSignatureVt {
fn from(sigs: &[SignatureVt]) -> Self {
let mut g = G2Projective::identity();
for s in sigs {
g += s.0;
}
Self(g)
}
}
impl Serialize for AggregateSignatureVt {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(s)
}
}
impl<'de> Deserialize<'de> for AggregateSignatureVt {
fn deserialize<D>(d: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let p = G2Projective::deserialize(d)?;
Ok(Self(p))
}
}
impl AggregateSignatureVt {
pub const BYTES: usize = 96;
pub fn is_valid(&self) -> Choice {
self.0.is_identity().not().bitor(self.0.is_on_curve())
}
pub fn is_invalid(&self) -> Choice {
self.0.is_identity().bitor(self.0.is_on_curve().not())
}
pub fn verify<B: AsRef<[u8]>>(&self, data: &[(PublicKeyVt, B)]) -> Choice {
if self.is_invalid().unwrap_u8() == 1 {
return Choice::from(0u8);
}
#[cfg(not(any(feature = "alloc", feature = "std")))]
fn core_aggregate_verify<B: AsRef<[u8]>>(
sig: &G2Projective,
data: &[(PublicKeyVt, B)],
) -> Choice {
use bls12_381_plus::{pairing, Gt};
let mut res = Gt::identity();
for (key, msg) in data {
if key.is_invalid().unwrap_u8() == 1 {
return Choice::from(0u8);
}
let a = SignatureVt::hash_msg(msg.as_ref());
res += pairing(&key.0.to_affine(), &a.to_affine());
}
res += pairing(&G1Affine::generator().neg(), &sig.to_affine());
res.is_identity()
}
#[cfg(any(feature = "alloc"))]
fn core_aggregate_verify<B: AsRef<[u8]>>(
sig: &G2Projective,
data: &[(PublicKeyVt, B)],
) -> Choice {
use alloc::vec::Vec;
use bls12_381_plus::{multi_miller_loop, G2Prepared};
if data.iter().any(|(k, _)| k.is_invalid().unwrap_u8() == 1) {
return Choice::from(0u8);
}
let mut data = data
.iter()
.map(|(key, m)| {
(
key.0.to_affine(),
G2Prepared::from(SignatureVt::hash_msg(m.as_ref()).to_affine()),
)
})
.collect::<Vec<(G1Affine, G2Prepared)>>();
data.push((
G1Affine::generator().neg(),
G2Prepared::from(sig.to_affine()),
));
let t = data
.iter()
.map(|(p1, p2)| (p1, p2))
.collect::<Vec<(&G1Affine, &G2Prepared)>>();
multi_miller_loop(t.as_slice())
.final_exponentiation()
.is_identity()
}
core_aggregate_verify(&self.0, data)
}
pub fn to_bytes(&self) -> [u8; Self::BYTES] {
self.0.to_affine().to_compressed()
}
pub fn from_bytes(bytes: &[u8; Self::BYTES]) -> CtOption<Self> {
let mut t = [0u8; Self::BYTES];
t.copy_from_slice(bytes);
G2Affine::from_compressed(&t).map(|p| Self(G2Projective::from(&p)))
}
}