iris-crypto 0.1.3

Cryptographic primitives for Iris Nockchain Wallet
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use ibig::UBig;
use iris_ztd::{
    crypto::cheetah::{
        ch_add, ch_neg, ch_scal_big, trunc_g_order, CheetahPoint, F6lt, A_GEN, G_ORDER,
    },
    tip5::hash::hash_varlen,
    Belt, Digest, Hashable, Noun, NounDecode, NounEncode,
};
use iris_ztd_derive::{NounDecode, NounEncode};
extern crate alloc;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, NounEncode, NounDecode)]
pub struct PublicKey(pub CheetahPoint);

impl PublicKey {
    pub fn verify(&self, m: &Digest, sig: &Signature) -> bool {
        if sig.c == UBig::from(0u64)
            || sig.c >= *G_ORDER
            || sig.s == UBig::from(0u64)
            || sig.s >= *G_ORDER
        {
            return false;
        }

        // Compute scalar = s*G - c*pubkey
        // This is equivalent to: scalar = s*G + (-c)*pubkey
        let sg = match ch_scal_big(&sig.s, &A_GEN) {
            Ok(pt) => pt,
            Err(_) => return false,
        };
        let c_pk = match ch_scal_big(&sig.c, &self.0) {
            Ok(pt) => pt,
            Err(_) => return false,
        };
        let scalar = match ch_add(&sg, &ch_neg(&c_pk)) {
            Ok(pt) => pt,
            Err(_) => return false,
        };
        let chal = {
            let mut transcript: Vec<Belt> = Vec::new();
            transcript.extend_from_slice(&scalar.x.0);
            transcript.extend_from_slice(&scalar.y.0);
            transcript.extend_from_slice(&self.0.x.0);
            transcript.extend_from_slice(&self.0.y.0);
            transcript.extend_from_slice(&m.0);
            trunc_g_order(&hash_varlen(&mut transcript))
        };

        chal == sig.c
    }

    pub fn to_be_bytes(&self) -> [u8; 97] {
        let mut data = [0u8; 97];
        data[0] = 0x01; // prefix byte
        let mut offset = 1;
        // y-coordinate: 6 belts × 8 bytes = 48 bytes
        for belt in self.0.y.0.iter().rev() {
            data[offset..offset + 8].copy_from_slice(&belt.0.to_be_bytes());
            offset += 8;
        }
        // x-coordinate: 6 belts × 8 bytes = 48 bytes
        for belt in self.0.x.0.iter().rev() {
            data[offset..offset + 8].copy_from_slice(&belt.0.to_be_bytes());
            offset += 8;
        }
        data
    }

    pub fn from_be_bytes(bytes: &[u8]) -> PublicKey {
        let mut x = [Belt(0); 6];
        let mut y = [Belt(0); 6];

        // y-coordinate: bytes 1-48
        for i in 0..6 {
            let offset = 1 + i * 8;
            let mut buf = [0u8; 8];
            buf.copy_from_slice(&bytes[offset..offset + 8]);
            y[5 - i] = Belt(u64::from_be_bytes(buf));
        }

        // x-coordinate: bytes 49-96
        for i in 0..6 {
            let offset = 49 + i * 8;
            let mut buf = [0u8; 8];
            buf.copy_from_slice(&bytes[offset..offset + 8]);
            x[5 - i] = Belt(u64::from_be_bytes(buf));
        }

        PublicKey(CheetahPoint {
            x: F6lt(x),
            y: F6lt(y),
            inf: false,
        })
    }

    /// SLIP-10 compatible serialization (legacy 65-byte format for compatibility)
    pub(crate) fn to_slip10_bytes(&self) -> Vec<u8> {
        let mut data = Vec::new();
        for belt in self.0.y.0.iter().rev().chain(self.0.x.0.iter().rev()) {
            data.extend_from_slice(&belt.0.to_be_bytes());
        }
        data
    }
}

impl core::ops::Add for &PublicKey {
    type Output = PublicKey;

    fn add(self, other: &PublicKey) -> PublicKey {
        PublicKey(ch_add(&self.0, &other.0).unwrap())
    }
}

impl core::ops::Add for PublicKey {
    type Output = PublicKey;

    fn add(self, other: PublicKey) -> PublicKey {
        &self + &other
    }
}

impl core::ops::AddAssign for PublicKey {
    fn add_assign(&mut self, other: PublicKey) {
        *self = &*self + &other;
    }
}

impl core::ops::Sub for &PublicKey {
    type Output = PublicKey;

    fn sub(self, other: &PublicKey) -> PublicKey {
        PublicKey(ch_add(&self.0, &ch_neg(&other.0)).unwrap())
    }
}

impl core::ops::SubAssign for PublicKey {
    fn sub_assign(&mut self, other: PublicKey) {
        *self = &*self - &other;
    }
}

impl core::iter::Sum<PublicKey> for PublicKey {
    fn sum<I: Iterator<Item = PublicKey>>(iter: I) -> Self {
        iter.fold(PublicKey(CheetahPoint::identity()), |acc, x| &acc + &x)
    }
}

impl<'a> core::iter::Sum<&'a PublicKey> for PublicKey {
    fn sum<I: Iterator<Item = &'a PublicKey>>(iter: I) -> Self {
        iter.fold(PublicKey(CheetahPoint::identity()), |acc, x| &acc + x)
    }
}

impl Hashable for PublicKey {
    fn hash(&self) -> Digest {
        self.to_noun().hash()
    }
}

#[derive(Debug, Clone)]
pub struct Signature {
    pub c: UBig, // challenge
    pub s: UBig, // signature scalar
}

// Aggregate signature of the same challenge
impl core::iter::Sum<Signature> for Option<Signature> {
    fn sum<I: Iterator<Item = Signature>>(mut iter: I) -> Self {
        let mut c = None;
        let s = iter.try_fold(UBig::from(0u64), |acc, x| {
            if c.is_some() && c.as_ref() != Some(&x.c) {
                return None;
            }
            c = Some(x.c);
            Some((acc + x.s) % &*G_ORDER)
        });
        Some(Signature { c: c?, s: s? })
    }
}

impl NounEncode for Signature {
    fn to_noun(&self) -> Noun {
        (
            Belt::from_bytes(&self.c.to_le_bytes()).as_slice(),
            Belt::from_bytes(&self.s.to_le_bytes()).as_slice(),
        )
            .to_noun()
    }
}

impl NounDecode for Signature {
    fn from_noun(noun: &Noun) -> Option<Self> {
        let (c, s): ([Belt; 8], [Belt; 8]) = NounDecode::from_noun(noun)?;

        let c = Belt::to_bytes(&c);
        let s = Belt::to_bytes(&s);

        Some(Signature {
            c: UBig::from_le_bytes(&c),
            s: UBig::from_le_bytes(&s),
        })
    }
}

impl Hashable for Signature {
    fn hash(&self) -> Digest {
        self.to_noun().hash()
    }
}

#[derive(Debug, Clone)]
pub struct PrivateKey(pub UBig);

impl PrivateKey {
    pub fn public_key(&self) -> PublicKey {
        PublicKey(ch_scal_big(&self.0, &A_GEN).unwrap())
    }

    pub fn sign(&self, m: &Digest) -> Signature {
        self.sign_multi(m, &self.nonce_for(m), &self.public_key())
    }

    pub fn nonce_for(&self, m: &Digest) -> UBig {
        let pubkey = self.public_key().0;
        let nonce = {
            let mut transcript = Vec::new();
            transcript.extend_from_slice(&pubkey.x.0);
            transcript.extend_from_slice(&pubkey.y.0);
            transcript.extend_from_slice(&m.0);
            self.0.to_le_bytes().chunks(4).for_each(|chunk| {
                let mut buf = [0u8; 4];
                buf[..chunk.len()].copy_from_slice(chunk);
                transcript.push(Belt(u32::from_le_bytes(buf) as u64));
            });
            trunc_g_order(&hash_varlen(&mut transcript))
        };
        nonce
    }

    pub fn combine_nonces(nonces: &[UBig]) -> UBig {
        nonces.iter().fold(UBig::from(0u64), |acc, x| &acc + x) % &*G_ORDER
    }

    /// Perform a multiparty sign
    ///
    /// # Arguments
    /// * `m` - The digest of message to sign
    /// * `shared_nonce` - The challenge nonce. This is after taking `nonce_for(m)` on all private keys, and combining them with [`PrivateKey::combine_nonces`].
    /// * `combined_pubkey` - The combined public key to sign against.
    ///
    /// # Returns
    /// * `Signature` - The partial signature. This will be invalid until combined with other partial signatures.
    ///
    /// # Example
    ///
    /// ```
    /// # use iris_ztd::{Digest, Belt};
    /// # use iris_crypto::cheetah::*;
    /// # use ibig::UBig;
    /// let pk1 = PrivateKey(UBig::from(123u64));
    /// let pk2 = PrivateKey(UBig::from(456u64));
    /// let m = Digest([Belt(8), Belt(9), Belt(10), Belt(11), Belt(12)]);
    /// let nonce1 = pk1.nonce_for(&m);
    /// let nonce2 = pk2.nonce_for(&m);
    /// let combined_nonce = PrivateKey::combine_nonces(&[nonce1, nonce2]);
    /// let combined_pubkey = pk1.public_key() + pk2.public_key();
    /// let sig1 = pk1.sign_multi(&m, &combined_nonce, &combined_pubkey);
    /// let sig2 = pk2.sign_multi(&m, &combined_nonce, &combined_pubkey);
    /// let sig = [sig1, sig2].into_iter().sum::<Option<Signature>>().unwrap();
    /// assert!(combined_pubkey.verify(&m, &sig));
    /// ```
    pub fn sign_multi(
        &self,
        m: &Digest,
        shared_nonce: &UBig,
        combined_pubkey: &PublicKey,
    ) -> Signature {
        let chal = {
            // scalar = nonce * G
            let scalar = ch_scal_big(shared_nonce, &A_GEN).unwrap();
            let mut transcript = Vec::new();
            transcript.extend_from_slice(&scalar.x.0);
            transcript.extend_from_slice(&scalar.y.0);
            transcript.extend_from_slice(&combined_pubkey.0.x.0);
            transcript.extend_from_slice(&combined_pubkey.0.y.0);
            transcript.extend_from_slice(&m.0);
            trunc_g_order(&hash_varlen(&mut transcript))
        };
        let nonce = self.nonce_for(m);
        let sig = (&nonce + &chal * &self.0) % &*G_ORDER;
        Signature { c: chal, s: sig }
    }

    pub fn to_be_bytes(&self) -> [u8; 32] {
        let bytes = self.0.to_be_bytes();
        let mut arr = [0u8; 32];
        arr[32 - bytes.len()..].copy_from_slice(&bytes);
        arr
    }
}

impl core::ops::Add for &PrivateKey {
    type Output = PrivateKey;

    fn add(self, other: &PrivateKey) -> PrivateKey {
        PrivateKey((&self.0 + &other.0) % &*G_ORDER)
    }
}

impl core::ops::Add for PrivateKey {
    type Output = PrivateKey;

    fn add(self, other: PrivateKey) -> PrivateKey {
        PrivateKey((&self.0 + &other.0) % &*G_ORDER)
    }
}

impl core::ops::AddAssign for PrivateKey {
    fn add_assign(&mut self, other: PrivateKey) {
        *self = &*self + &other;
    }
}

impl core::ops::Sub for &PrivateKey {
    type Output = PrivateKey;

    fn sub(self, other: &PrivateKey) -> PrivateKey {
        PrivateKey((&self.0 - &other.0) % &*G_ORDER)
    }
}

impl core::ops::SubAssign for PrivateKey {
    fn sub_assign(&mut self, other: PrivateKey) {
        *self = &*self - &other;
    }
}

impl core::iter::Sum<PrivateKey> for PrivateKey {
    fn sum<I: Iterator<Item = PrivateKey>>(iter: I) -> Self {
        iter.fold(PrivateKey(UBig::from(0u64)), |acc, x| &acc + &x)
    }
}

impl<'a> core::iter::Sum<&'a PrivateKey> for PrivateKey {
    fn sum<I: Iterator<Item = &'a PrivateKey>>(iter: I) -> Self {
        iter.fold(PrivateKey(UBig::from(0u64)), |acc, x| &acc + x)
    }
}

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

    #[test]
    fn mupk_test() {
        let privs = [
            UBig::from(123u64),
            UBig::from(124u64),
            &*G_ORDER - &UBig::from(1u64),
        ]
        .map(PrivateKey);
        let pubs = privs.clone().map(|p| p.public_key());
        let pub_key: PublicKey = pubs.iter().sum();
        let priv_key: PrivateKey = privs.iter().sum();
        let pub_key_from_priv = priv_key.public_key();
        assert_eq!(pub_key, pub_key_from_priv);
    }

    #[test]
    fn musig_test() {
        let privs = [
            UBig::from(123u64),
            UBig::from(124u64),
            &*G_ORDER - &UBig::from(1u64),
        ]
        .map(PrivateKey);
        let pubs = privs.clone().map(|p| p.public_key());
        let pub_key: PublicKey = pubs.iter().sum();
        let priv_key: PrivateKey = privs.iter().sum();

        let digest = Digest([Belt(1), Belt(2), Belt(3), Belt(4), Belt(5)]);
        let signature_all = priv_key.sign(&digest);
        // Just testing regular signing
        assert!(pub_key.verify(&digest, &signature_all));

        // Now do split signing
        let nonces = privs
            .iter()
            .map(|p| p.nonce_for(&digest))
            .collect::<Vec<_>>();
        let nonce = PrivateKey::combine_nonces(&nonces);
        let mut sigs = vec![];
        for priv_key in &privs {
            sigs.push(priv_key.sign_multi(&digest, &nonce, &pub_key));
        }
        // Combine all signatures
        let sig = sigs.into_iter().sum::<Option<Signature>>().unwrap();
        // Verify combined signature
        assert!(pub_key.verify(&digest, &sig));
    }

    #[test]
    fn test_sign_and_verify() {
        let priv_key = PrivateKey(UBig::from(123u64));
        let digest = Digest([Belt(1), Belt(2), Belt(3), Belt(4), Belt(5)]);
        let signature = priv_key.sign(&digest);
        let pubkey = priv_key.public_key();
        assert!(
            pubkey.verify(&digest, &signature),
            "Signature verification failed!"
        );

        // Corrupting digest, signature, or pubkey should all cause failure
        let mut wrong_digest = digest;
        wrong_digest.0[0] = Belt(0);
        assert!(
            !pubkey.verify(&wrong_digest, &signature),
            "Should reject wrong digest"
        );
        let mut wrong_sig = signature.clone();
        wrong_sig.s += UBig::from(1u64);
        assert!(
            !pubkey.verify(&digest, &wrong_sig),
            "Should reject wrong signature"
        );
        let mut wrong_pubkey = pubkey.clone();
        wrong_pubkey.0.x.0[0].0 += 1;
        assert!(
            !wrong_pubkey.verify(&digest, &signature),
            "Should reject wrong public key"
        );
    }

    #[test]
    fn test_vector() {
        // from nockchain zkvm-jetpack cheetah_jets.rs test_batch_verify_affine
        let digest = Digest([Belt(8), Belt(9), Belt(10), Belt(11), Belt(12)]);
        let pubkey = PublicKey(CheetahPoint {
            x: F6lt([
                Belt(2754611494552410273),
                Belt(8599518745794843693),
                Belt(10526511002404673680),
                Belt(4830863958577994148),
                Belt(375185138577093320),
                Belt(12938930721685970739),
            ]),
            y: F6lt([
                Belt(3062714866612034253),
                Belt(15671931273416742386),
                Belt(4071440668668521568),
                Belt(7738250649524482367),
                Belt(5259065445844042557),
                Belt(8456011930642078370),
            ]),
            inf: false,
        });
        let c_hex = "6f3cd43cd8709f4368aed04cd84292ab1c380cb645aaa7d010669d70375cbe88";
        let s_hex = "5197ab182e307a350b5cf3606d6e99a6f35b0d382c8330dde6e51fb6ef8ebb8c";
        let signature = Signature {
            c: UBig::from_str_radix(c_hex, 16).unwrap(),
            s: UBig::from_str_radix(s_hex, 16).unwrap(),
        };
        assert!(pubkey.verify(&digest, &signature));
    }
}