tape-core 0.4.0

A decentralized object store using Solana.
Documentation
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#![allow(unexpected_cfgs)]

use core::{fmt, str::from_utf8_unchecked};

use tape_crypto::bls12254::errors::BLSError;
use tape_crypto::bls12254::min_sig::*;
use bytemuck::{Pod, Zeroable};
use serde::{Deserialize, Serialize};
#[cfg(feature = "wincode")]
use wincode_derive::{SchemaRead, SchemaWrite};

// Note we're using G2 for public keys and G1 for signatures, which is the
// opposite of the more common choice (min_pk). This is because Solana does 
// not have syscalls needed to do verification on G2 signatures.

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct BlsPrivateKey(pub PrivKey);

#[cfg(not(target_os = "solana"))]
impl BlsPrivateKey {
    /// Generate a new random private key.
    pub fn from_random() -> BlsPrivateKey {
        BlsPrivateKey(PrivKey::from_random())
    }

    /// Derive the public key in G2 from this private key.
    pub fn public_key(&self) -> Result<BlsPubkey, BLSError> {
        let pk = G2Point::try_from(&self.0)?;
        Ok(BlsPubkey(pk))
    }

    /// Sign a PoP message derived from the canonical (compressed) public key.
    pub fn proof_of_possession(&self) -> Result<BlsSignature, BLSError> {
        let pop = self.0.proof_of_possession()?;
        let pop_compressed = G1CompressedPoint::try_from(pop)?;
        Ok(BlsSignature(pop_compressed))
    }

    /// Sign an arbitrary message, returning the signature.
    pub fn sign<T: AsRef<[u8]>>(&self, message: T) -> Result<BlsSignature, BLSError> {
        let sig = self.0.sign(message)?;
        let sig_compressed = G1CompressedPoint::try_from(sig)?; 
        Ok(BlsSignature(sig_compressed))
    }
}

#[repr(C)]
#[derive(Clone, Copy, PartialEq, Pod, Zeroable)]
#[cfg_attr(feature = "wincode", derive(SchemaRead, SchemaWrite))]
pub struct BlsSignature(pub G1CompressedPoint);
impl Eq for BlsSignature {}

impl Serialize for BlsSignature {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0 .0.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for BlsSignature {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let bytes = <[u8; 32]>::deserialize(deserializer)?;
        Ok(Self(G1CompressedPoint(bytes)))
    }
}

impl BlsSignature {

    /// Aggregate many compressed BLS signatures into one compressed signature
    pub fn aggregate(partials: &[BlsSignature]) -> Result<BlsSignature, BLSError> {
        if partials.is_empty() {
            return Err(BLSError::SerializationError);
        }

        // Decompress → aggregate → recompress
        let decompressed: Result<Vec<G1Point>, _> = partials
            .iter()
            .map(|s| G1Point::try_from(&s.0))
            .collect();

        let decompressed = decompressed?;
        #[cfg(not(target_os = "solana"))]
        let agg = tape_crypto::bls12254::min_sig::native::aggregate_partials(&decompressed)?;
        #[cfg(target_os = "solana")]
        let agg = aggregate_partials(&decompressed)?;
        let compressed = G1CompressedPoint::try_from(agg)?;

        Ok(BlsSignature(compressed))
    }

    /// Verify an aggregated signature against exact list of signers
    /// Verifies against a quorum key summed once and reused across spools.
    #[cfg(not(target_os = "solana"))]
    pub fn verify_quorum<M: AsRef<[u8]>>(
        &self,
        message: M,
        quorum: &BlsQuorumKey,
    ) -> Result<(), BLSError> {
        let decompressed_sig = G1Point::try_from(&self.0)?;
        tape_crypto::bls12254::min_sig::native::verify_summed(message, &quorum.0, &decompressed_sig)
    }

    pub fn verify_aggregate<M: AsRef<[u8]>>(
        &self,
        message: M,
        signer_pubkeys: &[BlsPubkey],
    ) -> Result<(), BLSError> {
        if signer_pubkeys.is_empty() {
            return Err(BLSError::SerializationError);
        }

        let decompressed_sig = G1Point::try_from(&self.0)?;
        let g2_points: Vec<G2Point> = signer_pubkeys
            .iter()
            .map(|pk| Ok(pk.0))
            .collect::<Result<Vec<_>, _>>()?;

        #[cfg(not(target_os = "solana"))]
        return tape_crypto::bls12254::min_sig::native::verify_signers(
            message,
            &g2_points,
            &decompressed_sig,
        );
        #[cfg(target_os = "solana")]
        verify_aggregate(message, &g2_points, &decompressed_sig)
    }

    /// Checks many single signer signatures under one final exponentiation.
    ///
    /// Names no member on failure, so a caller that needs one rechecks singly.
    #[cfg(not(target_os = "solana"))]
    pub fn verify_batch(items: &[(&[u8], BlsPubkey, BlsSignature)]) -> Result<(), BLSError> {
        use tape_crypto::bls12254::min_sig::native;

        if items.is_empty() {
            return Err(BLSError::SerializationError);
        }
        let signers = items
            .iter()
            .map(|(_, pubkey, _)| native::g2_from_bytes(&pubkey.0))
            .collect::<Result<Vec<_>, _>>()?;
        let signatures = items
            .iter()
            .map(|(_, _, signature)| G1Point::try_from(&signature.0))
            .collect::<Result<Vec<_>, _>>()?;
        let batch: Vec<native::BatchItem<'_>> = items
            .iter()
            .zip(signers.iter())
            .zip(signatures.iter())
            .map(|(((message, _, _), signer), signature)| native::BatchItem {
                message,
                signer,
                signature,
            })
            .collect();
        native::verify_batch(&batch)
    }

    /// Size of the compressed signature in bytes
    pub const fn size() -> usize {
        32 // G1 compressed
    }
}


#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, Pod, Zeroable, Serialize, Deserialize)]
#[cfg_attr(feature = "wincode", derive(SchemaRead, SchemaWrite))]
pub struct BlsPubkey(pub G2Point); // using the uncompressed form to reduce CU

/// A quorum's keys summed once, reused across every spool the set certifies.
#[cfg(not(target_os = "solana"))]
#[derive(Clone, Copy)]
pub struct BlsQuorumKey(G2Point);

#[cfg(not(target_os = "solana"))]
impl BlsQuorumKey {
    /// Sums distinct, non-zero keys; the caller guarantees the set matches the
    /// certificate's signers.
    pub fn sum(keys: &[BlsPubkey]) -> Result<Self, BLSError> {
        if keys.is_empty() {
            return Err(BLSError::SerializationError);
        }
        for (i, key) in keys.iter().enumerate() {
            if key.0.0 == [0u8; 128] || keys[..i].iter().any(|held| held.0.0 == key.0.0) {
                return Err(BLSError::SerializationError);
            }
        }
        let points: Vec<G2Point> = keys.iter().map(|key| key.0).collect();
        Ok(Self(sum_pubkeys(&points)?))
    }
}

impl BlsPubkey {
    #[cfg(not(target_os = "solana"))]
    pub fn new_unique() -> Self {
        BlsPubkey(G2Point::new_unique())
    }

    /// Verify a proof of possession (PoP) against this public key.
    pub fn is_valid(&self, pop: BlsSignature) -> bool {
        let pubkey = G2Point::try_from(self.0);
        let signature = G1Point::try_from(&pop.0);

        if pubkey.is_err() || signature.is_err() {
            return false;
        }

        let pubkey = pubkey.unwrap();
        let signature = signature.unwrap();

        verify_proof_of_possession(&pubkey, &signature).is_ok()
    }
}

fn write_sig_base58(f: &mut fmt::Formatter<'_>, sig: &BlsSignature) -> fmt::Result {
    const SIG_BYTES: usize = 32; // G1 compressed

    let sig_bytes = sig.0.0;
    if sig_bytes.len() != 32 {
        return f.write_str("<invalid bls signature>");
    }
    let mut in32 = [0u8; SIG_BYTES];
    in32.copy_from_slice(&sig_bytes);

    let mut out = [0u8; tape_base58::MAX_ENCODED_32];
    let len = tape_base58::encode_32(&in32, &mut out);
    let s = unsafe { from_utf8_unchecked(&out[..len]) };
    f.write_str(s)
}

fn write_pubkey_base58(f: &mut fmt::Formatter<'_>, pk: &BlsPubkey) -> fmt::Result {
    const PK_BYTES: usize = 64;  // G2 compressed

    match G2CompressedPoint::try_from(&pk.0) {
        Ok(comp) => {
            let pk_bytes = comp.0;
            if pk_bytes.len() != PK_BYTES {
                return f.write_str("<invalid bls pubkey>");
            }
            let mut in64 = [0u8; PK_BYTES];
            in64.copy_from_slice(&pk_bytes);

            let mut out = [0u8; tape_base58::MAX_ENCODED_64];
            let len = tape_base58::encode_64(&in64, &mut out);
            let s = unsafe { from_utf8_unchecked(&out[..len]) };
            f.write_str(s)
        }
        Err(_) => f.write_str("<invalid bls pubkey>"),
    }
}

impl fmt::Debug for BlsPubkey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_pubkey_base58(f, self)
    }
}

impl fmt::Display for BlsPubkey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_pubkey_base58(f, self)
    }
}

impl fmt::Debug for BlsSignature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_sig_base58(f, self)
    }
}

impl fmt::Display for BlsSignature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_sig_base58(f, self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn base58_encoding_vectors_are_stable() {
        let mut encoded_32 = [0u8; tape_base58::MAX_ENCODED_32];
        let len_32 = tape_base58::encode_32(&[0u8; 32], &mut encoded_32);
        assert_eq!(&encoded_32[..len_32], b"11111111111111111111111111111111");
        let len_32 = tape_base58::encode_32(&[0xff; 32], &mut encoded_32);
        assert_eq!(
            &encoded_32[..len_32],
            b"JEKNVnkbo3jma5nREBBJCDoXFVeKkD56V3xKrvRmWxFG"
        );

        let mut encoded_64 = [0u8; tape_base58::MAX_ENCODED_64];
        let len_64 = tape_base58::encode_64(&[0u8; 64], &mut encoded_64);
        assert_eq!(
            &encoded_64[..len_64],
            b"1111111111111111111111111111111111111111111111111111111111111111"
        );
        let len_64 = tape_base58::encode_64(&[0xff; 64], &mut encoded_64);
        assert_eq!(
            &encoded_64[..len_64],
            b"67rpwLCuS5DGA8KGZXKsVQ7dnPb9goRLoKfgGbLfQg9WoLUgNY77E2jT11fem3coV9nAkguBACzrU1iyZM4B8roQ"
        );
    }

    #[test]
    fn pop_roundtrip_valid() {
        let sk = BlsPrivateKey::from_random();
        let pk = sk.public_key().expect("derive pubkey");
        let pop = sk.proof_of_possession().expect("make pop");

        assert!(pk.is_valid(pop), "PoP should verify against its public key");
    }

    #[test]
    fn pop_invalid_with_different_pubkey() {
        let sk1 = BlsPrivateKey::from_random();
        let sk2 = BlsPrivateKey::from_random();

        let pk1 = sk1.public_key().expect("pk1");
        let pk2 = sk2.public_key().expect("pk2");

        let pop1 = sk1.proof_of_possession().expect("pop1");

        assert!(pk1.is_valid(pop1), "PoP should verify on matching pk");
        assert!(!pk2.is_valid(pop1), "PoP must not verify on a different pk");
    }

    #[test]
    fn sign_and_verify_message() {
        let sk = BlsPrivateKey::from_random();
        let pk = sk.public_key().expect("derive pubkey");
        let message = b"test message";

        let sig = sk.sign(message).expect("sign message");
        let sig_point = G1Point::try_from(&sig.0).expect("decompress sig");

        assert!(pk.0.verify(&sig_point, message).is_ok(), "signature should verify");
    }

    #[test]
    fn aggregate_full_roundtrip_2_of_3_committee() {
        let message = b"consensus finality goes brrr";

        // Committee setup: 3 members
        let sk1 = BlsPrivateKey::from_random();
        let sk2 = BlsPrivateKey::from_random();
        let sk3 = BlsPrivateKey::from_random();

        let pk1 = sk1.public_key().unwrap();
        let pk2 = sk2.public_key().unwrap();
        let pk3 = sk3.public_key().unwrap();

        // Only 2 sign (say 1 and 3)
        let sig1 = sk1.sign(message).unwrap();
        let sig3 = sk3.sign(message).unwrap();

        // Off-chain aggregator combines them
        let aggregated = BlsSignature::aggregate(&[sig1, sig3]).unwrap();

        // On-chain: verify using exact list of who signed
        let signers = [pk1, pk3]; // order doesn't matter
        aggregated.verify_aggregate(message, &signers).unwrap();

        // Negative: wrong message fails
        let wrong_msg = b"hacked!";
        assert_eq!(
            aggregated.verify_aggregate(wrong_msg, &signers).unwrap_err(),
            BLSError::BLSVerificationError
        );

        // Negative: missing signer fails
        let only_one = [pk1];
        assert_eq!(
            aggregated.verify_aggregate(message, &only_one).unwrap_err(),
            BLSError::BLSVerificationError
        );

        // Negative: extra signer fails
        let all_three = [pk1, pk2, pk3];
        assert_eq!(
            aggregated.verify_aggregate(message, &all_three).unwrap_err(),
            BLSError::BLSVerificationError
        );

        // Negative: duplicate pubkey rejected
        let dup = [pk1, pk1];
        assert_eq!(
            aggregated.verify_aggregate(message, &dup).unwrap_err(),
            BLSError::SerializationError
        );
    }

    #[test]
    fn aggregate_10_signers_stress_test() {
        let message = b"10x speed, 1x size";

        let keys: Vec<_> = (0..10)
            .map(|_| BlsPrivateKey::from_random())
            .collect();

        let pubkeys: Vec<_> = keys.iter()
            .map(|sk| sk.public_key().unwrap())
            .collect();

        // All 10 sign
        let partials: Vec<_> = keys.iter()
            .map(|sk| sk.sign(message).unwrap())
            .collect();

        let aggregated = BlsSignature::aggregate(&partials).unwrap();

        // Verify all 10
        aggregated.verify_aggregate(message, &pubkeys).unwrap();

        // Tamper one bit → fails
        let mut tampered = aggregated;
        tampered.0 .0[0] ^= 0x01;
        assert!(
            tampered.verify_aggregate(message, &pubkeys).is_err(),
        );
    }
}