osst 0.1.1

one-step schnorr threshold identification with proactive resharing
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Curve abstraction for OSST
//!
//! Defines traits for curve operations, allowing OSST to work with different
//! elliptic curve backends:
//! - ristretto255 (Polkadot/sr25519 compatible)
//! - Pallas (Zcash Orchard compatible)
//! - Jubjub (Zcash Sapling compatible) - future

use core::fmt::Debug;

/// Scalar field element trait
pub trait OsstScalar: Clone + Debug + Sized + PartialEq + Send + Sync {
    /// The zero element
    fn zero() -> Self;

    /// The one element
    fn one() -> Self;

    /// Create from u32
    fn from_u32(v: u32) -> Self;

    /// Addition
    fn add(&self, other: &Self) -> Self;

    /// Subtraction
    fn sub(&self, other: &Self) -> Self;

    /// Multiplication
    fn mul(&self, other: &Self) -> Self;

    /// Negation
    fn neg(&self) -> Self;

    /// Compute multiplicative inverse
    fn invert(&self) -> Self;

    /// Generate random scalar
    fn random<R: rand_core::RngCore + rand_core::CryptoRng>(rng: &mut R) -> Self;

    /// Create from 64-byte wide hash output (reduction mod order)
    fn from_bytes_wide(bytes: &[u8; 64]) -> Self;

    /// Serialize to bytes
    fn to_bytes(&self) -> [u8; 32];

    /// Deserialize from canonical bytes
    fn from_canonical_bytes(bytes: &[u8; 32]) -> Option<Self>;
}

/// Curve point trait
pub trait OsstPoint: Clone + Debug + Sized + PartialEq + Send + Sync {
    type Scalar: OsstScalar;

    /// Compressed point size in bytes (32 for pallas/ristretto, 33 for secp256k1)
    const COMPRESSED_SIZE: usize;

    /// The identity element
    fn identity() -> Self;

    /// The generator point
    fn generator() -> Self;

    /// Scalar multiplication
    fn mul_scalar(&self, scalar: &Self::Scalar) -> Self;

    /// Point addition
    fn add(&self, other: &Self) -> Self;

    /// Multiscalar multiplication (optimized)
    fn multiscalar_mul(scalars: &[Self::Scalar], points: &[Self]) -> Self;

    /// Compress to bytes (32 bytes for most curves)
    fn compress(&self) -> [u8; 32];

    /// Decompress from bytes (32 bytes for most curves)
    fn decompress(bytes: &[u8; 32]) -> Option<Self>;

    /// Compress to variable-size bytes (for secp256k1 compatibility)
    fn compress_vec(&self) -> alloc::vec::Vec<u8> {
        self.compress().to_vec()
    }

    /// Decompress from variable-size bytes (for secp256k1 compatibility)
    fn decompress_slice(bytes: &[u8]) -> Option<Self> {
        if bytes.len() == 32 {
            let arr: [u8; 32] = bytes.try_into().ok()?;
            Self::decompress(&arr)
        } else {
            None
        }
    }
}

extern crate alloc;

/// Complete curve backend
pub trait OsstCurve: Clone + Debug + Default {
    type Scalar: OsstScalar;
    type Point: OsstPoint<Scalar = Self::Scalar>;
}

// ============================================================================
// Ristretto255 implementation
// ============================================================================

#[cfg(feature = "ristretto255")]
pub mod ristretto {
    use super::*;
    use curve25519_dalek::{
        constants::RISTRETTO_BASEPOINT_POINT,
        ristretto::{CompressedRistretto, RistrettoPoint},
        scalar::Scalar,
        traits::MultiscalarMul,
    };

    impl OsstScalar for Scalar {
        fn zero() -> Self {
            Scalar::ZERO
        }

        fn one() -> Self {
            Scalar::ONE
        }

        fn from_u32(v: u32) -> Self {
            Scalar::from(v)
        }

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

        fn sub(&self, other: &Self) -> Self {
            self - other
        }

        fn mul(&self, other: &Self) -> Self {
            self * other
        }

        fn neg(&self) -> Self {
            -self
        }

        fn invert(&self) -> Self {
            Scalar::invert(self)
        }

        fn random<R: rand_core::RngCore + rand_core::CryptoRng>(rng: &mut R) -> Self {
            Scalar::random(rng)
        }

        fn from_bytes_wide(bytes: &[u8; 64]) -> Self {
            Scalar::from_bytes_mod_order_wide(bytes)
        }

        fn to_bytes(&self) -> [u8; 32] {
            Scalar::to_bytes(self)
        }

        fn from_canonical_bytes(bytes: &[u8; 32]) -> Option<Self> {
            Scalar::from_canonical_bytes(*bytes).into_option()
        }
    }

    impl OsstPoint for RistrettoPoint {
        type Scalar = Scalar;

        const COMPRESSED_SIZE: usize = 32;

        fn identity() -> Self {
            curve25519_dalek::traits::Identity::identity()
        }

        fn generator() -> Self {
            RISTRETTO_BASEPOINT_POINT
        }

        fn mul_scalar(&self, scalar: &Self::Scalar) -> Self {
            self * scalar
        }

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

        fn multiscalar_mul(scalars: &[Self::Scalar], points: &[Self]) -> Self {
            <RistrettoPoint as MultiscalarMul>::multiscalar_mul(scalars, points)
        }

        fn compress(&self) -> [u8; 32] {
            RistrettoPoint::compress(self).to_bytes()
        }

        fn decompress(bytes: &[u8; 32]) -> Option<Self> {
            CompressedRistretto::from_slice(bytes).ok()?.decompress()
        }
    }

    /// Ristretto255 curve backend
    #[derive(Clone, Debug, Default)]
    pub struct Ristretto255;

    impl OsstCurve for Ristretto255 {
        type Scalar = Scalar;
        type Point = RistrettoPoint;
    }
}

// ============================================================================
// Pallas implementation (Zcash Orchard)
// ============================================================================

#[cfg(feature = "pallas")]
pub mod pallas {
    use super::*;
    use pasta_curves::{
        group::{
            ff::{Field, FromUniformBytes, PrimeField},
            Group, GroupEncoding,
        },
        pallas::{Point, Scalar},
    };

    impl OsstScalar for Scalar {
        fn zero() -> Self {
            Scalar::ZERO
        }

        fn one() -> Self {
            Scalar::ONE
        }

        fn from_u32(v: u32) -> Self {
            Scalar::from(v as u64)
        }

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

        fn sub(&self, other: &Self) -> Self {
            *self - *other
        }

        fn mul(&self, other: &Self) -> Self {
            *self * *other
        }

        fn neg(&self) -> Self {
            -(*self)
        }

        fn invert(&self) -> Self {
            Field::invert(self).unwrap_or(Scalar::ZERO)
        }

        fn random<R: rand_core::RngCore + rand_core::CryptoRng>(rng: &mut R) -> Self {
            <Scalar as Field>::random(rng)
        }

        fn from_bytes_wide(bytes: &[u8; 64]) -> Self {
            <Scalar as FromUniformBytes<64>>::from_uniform_bytes(bytes)
        }

        fn to_bytes(&self) -> [u8; 32] {
            self.to_repr()
        }

        fn from_canonical_bytes(bytes: &[u8; 32]) -> Option<Self> {
            Scalar::from_repr(*bytes).into_option()
        }
    }

    impl OsstPoint for Point {
        type Scalar = Scalar;

        const COMPRESSED_SIZE: usize = 32;

        fn identity() -> Self {
            <Point as Group>::identity()
        }

        fn generator() -> Self {
            <Point as Group>::generator()
        }

        fn mul_scalar(&self, scalar: &Self::Scalar) -> Self {
            self * scalar
        }

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

        fn multiscalar_mul(scalars: &[Self::Scalar], points: &[Self]) -> Self {
            // Basic implementation - could use more optimized version
            scalars
                .iter()
                .zip(points.iter())
                .fold(<Point as Group>::identity(), |acc, (s, p)| {
                    acc + p.mul_scalar(s)
                })
        }

        fn compress(&self) -> [u8; 32] {
            self.to_bytes()
        }

        fn decompress(bytes: &[u8; 32]) -> Option<Self> {
            Point::from_bytes(bytes).into_option()
        }
    }

    /// Pallas curve backend (Zcash Orchard)
    #[derive(Clone, Debug, Default)]
    pub struct PallasCurve;

    impl OsstCurve for PallasCurve {
        type Scalar = Scalar;
        type Point = Point;
    }
}

// ============================================================================
// secp256k1 implementation (Bitcoin)
// ============================================================================

#[cfg(feature = "secp256k1")]
pub mod secp256k1 {
    use super::*;
    use alloc::vec::Vec;
    use k256::{
        elliptic_curve::{
            ops::Reduce,
            sec1::{FromEncodedPoint, ToEncodedPoint},
            Field, PrimeField,
        },
        ProjectivePoint, Scalar, U256,
    };

    impl OsstScalar for Scalar {
        fn zero() -> Self {
            Scalar::ZERO
        }

        fn one() -> Self {
            Scalar::ONE
        }

        fn from_u32(v: u32) -> Self {
            Scalar::from(v as u64)
        }

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

        fn sub(&self, other: &Self) -> Self {
            *self - *other
        }

        fn mul(&self, other: &Self) -> Self {
            *self * *other
        }

        fn neg(&self) -> Self {
            -(*self)
        }

        fn invert(&self) -> Self {
            <Scalar as Field>::invert(self).unwrap_or(Scalar::ZERO)
        }

        fn random<R: rand_core::RngCore + rand_core::CryptoRng>(rng: &mut R) -> Self {
            <Scalar as Field>::random(rng)
        }

        fn from_bytes_wide(bytes: &[u8; 64]) -> Self {
            // reduce 512-bit value modulo curve order
            let wide = U256::from_be_slice(&bytes[32..64]);
            <Scalar as Reduce<U256>>::reduce(wide)
        }

        fn to_bytes(&self) -> [u8; 32] {
            self.to_bytes().into()
        }

        fn from_canonical_bytes(bytes: &[u8; 32]) -> Option<Self> {
            let arr: &k256::FieldBytes = bytes.into();
            Scalar::from_repr(*arr).into_option()
        }
    }

    impl OsstPoint for ProjectivePoint {
        type Scalar = Scalar;

        // secp256k1 uses 33-byte compressed points
        const COMPRESSED_SIZE: usize = 33;

        fn identity() -> Self {
            Self::IDENTITY
        }

        fn generator() -> Self {
            Self::GENERATOR
        }

        fn mul_scalar(&self, scalar: &Self::Scalar) -> Self {
            self * scalar
        }

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

        fn multiscalar_mul(scalars: &[Self::Scalar], points: &[Self]) -> Self {
            scalars
                .iter()
                .zip(points.iter())
                .fold(Self::IDENTITY, |acc, (s, p)| acc + p.mul_scalar(s))
        }

        // for secp256k1, compress returns first 32 bytes (x-coord)
        // use compress_vec for full 33-byte representation
        fn compress(&self) -> [u8; 32] {
            let affine = self.to_affine();
            let encoded = affine.to_encoded_point(true);
            let bytes = encoded.as_bytes();
            // return x-coordinate (skip the 0x02/0x03 prefix)
            let mut result = [0u8; 32];
            if bytes.len() >= 33 {
                result.copy_from_slice(&bytes[1..33]);
            }
            result
        }

        fn decompress(bytes: &[u8; 32]) -> Option<Self> {
            // try to decompress assuming even y (0x02 prefix)
            let mut compressed = [0u8; 33];
            compressed[0] = 0x02;
            compressed[1..].copy_from_slice(bytes);
            Self::decompress_slice(&compressed)
        }

        fn compress_vec(&self) -> Vec<u8> {
            let affine = self.to_affine();
            let encoded = affine.to_encoded_point(true);
            encoded.as_bytes().to_vec()
        }

        fn decompress_slice(bytes: &[u8]) -> Option<Self> {
            use k256::EncodedPoint;
            if bytes.len() == 33 {
                let encoded = EncodedPoint::from_bytes(bytes).ok()?;
                let affine = k256::AffinePoint::from_encoded_point(&encoded);
                if affine.is_some().into() {
                    Some(ProjectivePoint::from(affine.unwrap()))
                } else {
                    None
                }
            } else if bytes.len() == 32 {
                // assume even y
                let mut compressed = [0u8; 33];
                compressed[0] = 0x02;
                compressed[1..].copy_from_slice(bytes);
                Self::decompress_slice(&compressed)
            } else {
                None
            }
        }
    }

    /// secp256k1 curve backend (Bitcoin)
    #[derive(Clone, Debug, Default)]
    pub struct Secp256k1Curve;

    impl OsstCurve for Secp256k1Curve {
        type Scalar = Scalar;
        type Point = ProjectivePoint;
    }
}

// ============================================================================
// decaf377 implementation (Penumbra)
// ============================================================================

#[cfg(feature = "decaf377")]
pub mod decaf377 {
    use super::*;
    use ::decaf377::{Element, Fr};

    impl OsstScalar for Fr {
        fn zero() -> Self {
            Fr::ZERO
        }

        fn one() -> Self {
            Fr::ONE
        }

        fn from_u32(v: u32) -> Self {
            Fr::from(v as u64)
        }

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

        fn sub(&self, other: &Self) -> Self {
            *self - *other
        }

        fn mul(&self, other: &Self) -> Self {
            *self * *other
        }

        fn neg(&self) -> Self {
            -(*self)
        }

        fn invert(&self) -> Self {
            self.inverse().unwrap_or(Fr::ZERO)
        }

        fn random<R: rand_core::RngCore + rand_core::CryptoRng>(rng: &mut R) -> Self {
            let mut bytes = [0u8; 32];
            rng.fill_bytes(&mut bytes);
            Fr::from_le_bytes_mod_order(&bytes)
        }

        fn from_bytes_wide(bytes: &[u8; 64]) -> Self {
            // reduce 512-bit value mod field order
            // decaf377 Fr is ~253 bits, so we use the lower 32 bytes
            let mut arr = [0u8; 32];
            arr.copy_from_slice(&bytes[32..64]);
            Fr::from_le_bytes_mod_order(&arr)
        }

        fn to_bytes(&self) -> [u8; 32] {
            Fr::to_bytes(self)
        }

        fn from_canonical_bytes(bytes: &[u8; 32]) -> Option<Self> {
            Fr::from_bytes_checked(bytes).ok()
        }
    }

    impl OsstPoint for Element {
        type Scalar = Fr;

        const COMPRESSED_SIZE: usize = 32;

        fn identity() -> Self {
            Element::IDENTITY
        }

        fn generator() -> Self {
            Element::GENERATOR
        }

        fn mul_scalar(&self, scalar: &Self::Scalar) -> Self {
            *self * *scalar
        }

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

        fn multiscalar_mul(scalars: &[Self::Scalar], points: &[Self]) -> Self {
            scalars
                .iter()
                .zip(points.iter())
                .fold(Element::IDENTITY, |acc, (s, p)| acc + (*p * *s))
        }

        fn compress(&self) -> [u8; 32] {
            self.vartime_compress().0
        }

        fn decompress(bytes: &[u8; 32]) -> Option<Self> {
            ::decaf377::Encoding(*bytes).vartime_decompress().ok()
        }
    }

    /// decaf377 curve backend (Penumbra)
    #[derive(Clone, Debug, Default)]
    pub struct Decaf377Curve;

    impl OsstCurve for Decaf377Curve {
        type Scalar = Fr;
        type Point = Element;
    }
}