frost-dkg 0.6.0

An implementation of the FROST Distributed Key Generation protocol
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
//! Curve25519 wrappers with a scalar type that implements `PrimeFieldBits`.

use core::{
    fmt::{self, Debug, Formatter},
    iter::{Product, Sum},
    ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};

use curve25519_dalek::{
    edwards::SubgroupPoint as DalekSubgroupPoint, ristretto::RistrettoPoint as DalekRistrettoPoint,
    scalar::Scalar as DalekScalar,
};
use elliptic_curve::{
    ff::{Field, FieldBits, PrimeField, PrimeFieldBits},
    group::{Group, GroupEncoding, prime::PrimeGroup},
    rand_core::TryRng,
    subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
};

/// Scalar field for Curve25519 groups with `PrimeFieldBits` support.
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub struct Scalar(DalekScalar);

impl Scalar {
    /// Convert into the inner dalek scalar.
    pub fn into_inner(self) -> DalekScalar {
        self.0
    }
}

impl Debug for Scalar {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Scalar").field(&self.0).finish()
    }
}

impl From<DalekScalar> for Scalar {
    fn from(value: DalekScalar) -> Self {
        Self(value)
    }
}

impl From<Scalar> for DalekScalar {
    fn from(value: Scalar) -> Self {
        value.0
    }
}

impl From<u64> for Scalar {
    fn from(value: u64) -> Self {
        Self(DalekScalar::from(value))
    }
}

impl ConstantTimeEq for Scalar {
    fn ct_eq(&self, other: &Self) -> Choice {
        self.0.ct_eq(&other.0)
    }
}

impl ConditionallySelectable for Scalar {
    fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
        Self(DalekScalar::conditional_select(&a.0, &b.0, choice))
    }
}

macro_rules! impl_scalar_binop {
    ($trait:ident, $method:ident, $op:tt) => {
        impl $trait for Scalar {
            type Output = Self;

            fn $method(self, rhs: Self) -> Self::Output {
                Self(self.0 $op rhs.0)
            }
        }

        impl $trait<&Scalar> for Scalar {
            type Output = Self;

            fn $method(self, rhs: &Scalar) -> Self::Output {
                Self(self.0 $op rhs.0)
            }
        }
    };
}

macro_rules! impl_scalar_assignop {
    ($trait:ident, $method:ident, $op:tt) => {
        impl $trait for Scalar {
            fn $method(&mut self, rhs: Self) {
                self.0 = self.0 $op rhs.0;
            }
        }

        impl $trait<&Scalar> for Scalar {
            fn $method(&mut self, rhs: &Scalar) {
                self.0 = self.0 $op rhs.0;
            }
        }
    };
}

impl_scalar_binop!(Add, add, +);
impl_scalar_binop!(Sub, sub, -);
impl_scalar_binop!(Mul, mul, *);
impl_scalar_assignop!(AddAssign, add_assign, +);
impl_scalar_assignop!(SubAssign, sub_assign, -);
impl_scalar_assignop!(MulAssign, mul_assign, *);

impl Neg for Scalar {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self(-self.0)
    }
}

impl Sum for Scalar {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::ZERO, |acc, item| acc + item)
    }
}

impl<'a> Sum<&'a Self> for Scalar {
    fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
        iter.fold(Self::ZERO, |acc, item| acc + item)
    }
}

impl Product for Scalar {
    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::ONE, |acc, item| acc * item)
    }
}

impl<'a> Product<&'a Self> for Scalar {
    fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
        iter.fold(Self::ONE, |acc, item| acc * item)
    }
}

impl Field for Scalar {
    const ZERO: Self = Self(DalekScalar::ZERO);
    const ONE: Self = Self(DalekScalar::ONE);

    fn try_random<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
        DalekScalar::try_random(rng).map(Self)
    }

    fn square(&self) -> Self {
        Self(self.0.square())
    }

    fn double(&self) -> Self {
        Self(self.0.double())
    }

    fn invert(&self) -> CtOption<Self> {
        <DalekScalar as Field>::invert(&self.0).map(Self)
    }

    fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self) {
        let (choice, value) = DalekScalar::sqrt_ratio(&num.0, &div.0);
        (choice, Self(value))
    }

    fn sqrt(&self) -> CtOption<Self> {
        <DalekScalar as Field>::sqrt(&self.0).map(Self)
    }
}

impl PrimeField for Scalar {
    type Repr = <DalekScalar as PrimeField>::Repr;

    fn from_repr(repr: Self::Repr) -> CtOption<Self> {
        DalekScalar::from_repr(repr).map(Self)
    }

    fn from_repr_vartime(repr: Self::Repr) -> Option<Self> {
        DalekScalar::from_repr_vartime(repr).map(Self)
    }

    fn to_repr(&self) -> Self::Repr {
        self.0.to_repr()
    }

    fn is_odd(&self) -> Choice {
        self.0.is_odd()
    }

    const MODULUS: &'static str = <DalekScalar as PrimeField>::MODULUS;
    const NUM_BITS: u32 = <DalekScalar as PrimeField>::NUM_BITS;
    const CAPACITY: u32 = <DalekScalar as PrimeField>::CAPACITY;
    const TWO_INV: Self = Self(<DalekScalar as PrimeField>::TWO_INV);
    const MULTIPLICATIVE_GENERATOR: Self =
        Self(<DalekScalar as PrimeField>::MULTIPLICATIVE_GENERATOR);
    const S: u32 = <DalekScalar as PrimeField>::S;
    const ROOT_OF_UNITY: Self = Self(<DalekScalar as PrimeField>::ROOT_OF_UNITY);
    const ROOT_OF_UNITY_INV: Self = Self(<DalekScalar as PrimeField>::ROOT_OF_UNITY_INV);
    const DELTA: Self = Self(<DalekScalar as PrimeField>::DELTA);
}

impl PrimeFieldBits for Scalar {
    type ReprBits = [u8; 32];

    fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
        FieldBits::new(self.0.to_bytes())
    }

    fn char_le_bits() -> FieldBits<Self::ReprBits> {
        FieldBits::new([
            0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9,
            0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x10,
        ])
    }
}

macro_rules! define_point_wrapper {
    ($name:ident, $inner:ty, $generator:expr) => {
        /// Prime-order Curve25519 group point.
        #[derive(Clone, Copy, Default, Eq, PartialEq)]
        pub struct $name($inner);

        impl $name {
            /// Convert into the inner dalek point.
            pub fn into_inner(self) -> $inner {
                self.0
            }
        }

        impl Debug for $name {
            fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
                f.debug_tuple(stringify!($name)).field(&self.0).finish()
            }
        }

        impl From<$inner> for $name {
            fn from(value: $inner) -> Self {
                Self(value)
            }
        }

        impl From<$name> for $inner {
            fn from(value: $name) -> Self {
                value.0
            }
        }

        impl ConstantTimeEq for $name {
            fn ct_eq(&self, other: &Self) -> Choice {
                self.0.ct_eq(&other.0)
            }
        }

        impl ConditionallySelectable for $name {
            fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
                Self(<$inner>::conditional_select(&a.0, &b.0, choice))
            }
        }

        impl Neg for $name {
            type Output = Self;

            fn neg(self) -> Self::Output {
                Self(-self.0)
            }
        }

        impl Add for $name {
            type Output = Self;

            fn add(self, rhs: Self) -> Self::Output {
                Self(self.0 + rhs.0)
            }
        }

        impl Add<&$name> for $name {
            type Output = Self;

            fn add(self, rhs: &$name) -> Self::Output {
                Self(self.0 + rhs.0)
            }
        }

        impl Sub for $name {
            type Output = Self;

            fn sub(self, rhs: Self) -> Self::Output {
                Self(self.0 - rhs.0)
            }
        }

        impl Sub<&$name> for $name {
            type Output = Self;

            fn sub(self, rhs: &$name) -> Self::Output {
                Self(self.0 - rhs.0)
            }
        }

        impl AddAssign for $name {
            fn add_assign(&mut self, rhs: Self) {
                self.0 += rhs.0;
            }
        }

        impl AddAssign<&$name> for $name {
            fn add_assign(&mut self, rhs: &$name) {
                self.0 += rhs.0;
            }
        }

        impl SubAssign for $name {
            fn sub_assign(&mut self, rhs: Self) {
                self.0 -= rhs.0;
            }
        }

        impl SubAssign<&$name> for $name {
            fn sub_assign(&mut self, rhs: &$name) {
                self.0 -= rhs.0;
            }
        }

        impl Mul<Scalar> for $name {
            type Output = Self;

            fn mul(self, rhs: Scalar) -> Self::Output {
                Self(self.0 * rhs.0)
            }
        }

        impl Mul<&Scalar> for $name {
            type Output = Self;

            fn mul(self, rhs: &Scalar) -> Self::Output {
                Self(self.0 * rhs.0)
            }
        }

        impl MulAssign<Scalar> for $name {
            fn mul_assign(&mut self, rhs: Scalar) {
                self.0 *= rhs.0;
            }
        }

        impl MulAssign<&Scalar> for $name {
            fn mul_assign(&mut self, rhs: &Scalar) {
                self.0 *= rhs.0;
            }
        }

        impl Sum for $name {
            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
                iter.fold(Self::identity(), |acc, item| acc + item)
            }
        }

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

        impl Group for $name {
            type Scalar = Scalar;

            fn try_random<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
                <$inner as Group>::try_random(rng).map(Self)
            }

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

            fn generator() -> Self {
                Self($generator)
            }

            fn is_identity(&self) -> Choice {
                self.0.is_identity()
            }

            fn double(&self) -> Self {
                Self(self.0.double())
            }

            fn mul_by_generator(scalar: &Self::Scalar) -> Self {
                Self(<$inner as Group>::mul_by_generator(&scalar.0))
            }
        }

        impl GroupEncoding for $name {
            type Repr = <$inner as GroupEncoding>::Repr;

            fn from_bytes(bytes: &Self::Repr) -> CtOption<Self> {
                <$inner as GroupEncoding>::from_bytes(bytes).map(Self)
            }

            fn from_bytes_unchecked(bytes: &Self::Repr) -> CtOption<Self> {
                <$inner as GroupEncoding>::from_bytes_unchecked(bytes).map(Self)
            }

            fn to_bytes(&self) -> Self::Repr {
                self.0.to_bytes()
            }
        }

        impl PrimeGroup for $name {}
    };
}

define_point_wrapper!(
    RistrettoPoint,
    DalekRistrettoPoint,
    curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT
);
define_point_wrapper!(
    EdwardsPoint,
    DalekSubgroupPoint,
    DalekSubgroupPoint::generator()
);