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
use core::iter::{Product, Sum};
use core::ops::{
    Add, AddAssign, Deref, DerefMut, Mul, MulAssign, Neg, ShlAssign, ShrAssign, Sub, SubAssign,
};

use primeorder::elliptic_curve::ops::Invert;
use subtle::{ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, CtOption};

use crate::bigint::{Encoding, Uint, U256};
use crate::elliptic_curve::{
    self,
    scalar::{FromUintUnchecked, IsHigh, ScalarPrimitive},
    Field, PrimeField,
};
use crate::generic_array::GenericArray;
use crate::typenum;

/// Wraps scalar or field element
///
/// Field element and scalar, derived by [ff], initially are not compatible with [elliptic-curve] crate
/// and cannot be used together. This struct wraps scalar or field element, and implements required traits.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct W<F>(F);

impl<F> W<F> {
    /// Wraps `n`
    pub const fn new(n: F) -> Self {
        Self(n)
    }
}

impl<F> Deref for W<F> {
    type Target = F;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<F> DerefMut for W<F> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<F: PrimeField> W<F> {
    /// Converts integer to byte array in big-endian
    pub fn to_be_bytes(&self) -> F::Repr {
        self.0.to_repr()
    }

    /// Converts integer to byte array in little-endian
    pub fn to_le_bytes(&self) -> F::Repr {
        let mut bytes = self.to_be_bytes();
        bytes.as_mut().reverse();
        bytes
    }

    /// Constructs integer from its bytes representation in big-endian
    ///
    /// Returns `None` if it overflows maximum allowed value
    pub fn from_be_bytes(bytes: F::Repr) -> CtOption<Self> {
        F::from_repr(bytes).map(Self)
    }

    /// Constructs integer from its bytes representation in little-endian
    ///
    /// Returns `None` if it overflows maximum allowed value
    pub fn from_le_bytes(mut bytes: F::Repr) -> CtOption<Self> {
        bytes.as_mut().reverse();
        Self::from_be_bytes(bytes)
    }

    /// Constructs integer from bytes in big-endian
    ///
    /// Integer is reduced modulo max allowed value ($p$ if it's field element, $n$ if it's a scalar)
    pub fn from_be_bytes_mod_order(bytes: &[u8]) -> Self {
        Self(
            bytes
                .iter()
                .fold(F::ZERO, |s, b| s * F::from(256) + F::from(u64::from(*b))),
        )
    }

    /// Constructs integer from bytes in little-endian
    ///
    /// Integer is reduced modulo max allowed value ($p$ if it's field element, $n$ if it's a scalar)
    pub fn from_le_bytes_mod_order(bytes: &[u8]) -> Self {
        Self(
            bytes
                .iter()
                .rev()
                .fold(F::ZERO, |s, b| s * F::from(256) + F::from(u64::from(*b))),
        )
    }

    /// Constructs integer from [U256]
    ///
    /// Integer is reduced modulo max allowed value ($p$ if it's field element, $n$ if it's a scalar)
    pub fn from_uint_mod_order(uint: &U256) -> Self {
        Self::from_be_bytes_mod_order(&uint.to_be_bytes())
    }
}

impl<F: PrimeField> W<F>
where
    [u8; 32]: From<F::Repr>,
{
    /// Converts integer to [U256]
    pub fn to_uint(&self) -> U256 {
        U256::from_be_bytes(self.to_be_bytes().into())
    }
}

impl<F> AsRef<W<F>> for W<F> {
    fn as_ref(&self) -> &W<F> {
        self
    }
}

impl<F: From<u64>> From<u64> for W<F> {
    fn from(n: u64) -> Self {
        Self(F::from(n))
    }
}

impl<F: PrimeField, const LIMBS: usize> From<W<F>> for Uint<LIMBS>
where
    Uint<LIMBS>: Encoding,
    <Uint<LIMBS> as Encoding>::Repr: From<F::Repr>,
{
    fn from(s: W<F>) -> Self {
        Uint::from_be_bytes(s.to_be_bytes().into())
    }
}

impl<F: PrimeField> FromUintUnchecked for W<F> {
    type Uint = U256;

    fn from_uint_unchecked(uint: Self::Uint) -> Self {
        let bytes_be = uint.to_be_bytes();
        Self::from_be_bytes_mod_order(&bytes_be)
    }
}

impl<F: ConditionallySelectable> ConditionallySelectable for W<F> {
    fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
        Self(F::conditional_select(&a.0, &b.0, choice))
    }
}

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

impl<F: Sum> Sum for W<F> {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        Self(iter.map(|f| f.0).sum())
    }
}

impl<'f, F: Sum<&'f F>> Sum<&'f W<F>> for W<F> {
    fn sum<I: Iterator<Item = &'f W<F>>>(iter: I) -> Self {
        Self(iter.map(|f| &f.0).sum())
    }
}

impl<F: Product> Product for W<F> {
    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
        Self(iter.map(|f| f.0).product())
    }
}

impl<'f, F: Product<&'f F>> Product<&'f W<F>> for W<F> {
    fn product<I: Iterator<Item = &'f W<F>>>(iter: I) -> Self {
        Self(iter.map(|f| &f.0).product())
    }
}

impl<F: Add<Output = F>> Add for W<F> {
    type Output = Self;

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

impl<F: Sub<Output = F>> Sub for W<F> {
    type Output = W<F>;

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

impl<F: Mul<Output = F>> Mul for W<F> {
    type Output = W<F>;

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

impl<F: Neg<Output = F>> Neg for W<F> {
    type Output = W<F>;

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

impl<'r, F: Add<&'r F, Output = F>> Add<&'r W<F>> for W<F> {
    type Output = W<F>;

    fn add(self, rhs: &'r W<F>) -> Self::Output {
        Self(self.0 + &rhs.0)
    }
}

impl<'r, F: Mul<&'r F, Output = F>> Mul<&'r W<F>> for W<F> {
    type Output = W<F>;

    fn mul(self, rhs: &'r W<F>) -> Self::Output {
        Self(self.0 * &rhs.0)
    }
}

impl<'r, F: Sub<&'r F, Output = F>> Sub<&'r W<F>> for W<F> {
    type Output = W<F>;

    fn sub(self, rhs: &'r W<F>) -> Self::Output {
        Self(self.0 - &rhs.0)
    }
}

impl<F: MulAssign> MulAssign for W<F> {
    fn mul_assign(&mut self, rhs: Self) {
        self.0 *= rhs.0
    }
}

impl<F: AddAssign> AddAssign for W<F> {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0
    }
}

impl<F: SubAssign> SubAssign for W<F> {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0
    }
}

impl<'r, F: MulAssign<&'r F>> MulAssign<&'r W<F>> for W<F> {
    fn mul_assign(&mut self, rhs: &'r W<F>) {
        self.0 *= &rhs.0
    }
}

impl<'r, F: AddAssign<&'r F>> AddAssign<&'r W<F>> for W<F> {
    fn add_assign(&mut self, rhs: &'r W<F>) {
        self.0 += &rhs.0
    }
}

impl<'r, F: SubAssign<&'r F>> SubAssign<&'r W<F>> for W<F> {
    fn sub_assign(&mut self, rhs: &'r W<F>) {
        self.0 -= &rhs.0
    }
}

impl<F: ShlAssign<usize>> ShlAssign<usize> for W<F> {
    fn shl_assign(&mut self, rhs: usize) {
        self.0 <<= rhs
    }
}

impl<F> ShrAssign<usize> for W<F>
where
    [u8; 32]: From<F::Repr>,
    F: PrimeField,
{
    fn shr_assign(&mut self, rhs: usize) {
        let n = self.to_uint();
        *self = Self::from_uint_mod_order(&(n >> rhs))
    }
}

impl<F: Field> Invert for W<F> {
    type Output = CtOption<Self>;

    fn invert(&self) -> Self::Output {
        self.0.invert().map(Self)
    }
}

impl<F> elliptic_curve::ops::Reduce<U256> for W<F>
where
    F: PrimeField,
    W<F>: PrimeField,
{
    type Bytes = <W<F> as PrimeField>::Repr;

    fn reduce(n: U256) -> Self {
        Self::from_be_bytes_mod_order(&n.to_be_bytes())
    }

    fn reduce_bytes(bytes: &Self::Bytes) -> Self {
        Self::from_be_bytes_mod_order(bytes.as_ref())
    }
}

impl<F: Field> Field for W<F> {
    const ZERO: Self = W(F::ZERO);
    const ONE: Self = W(F::ONE);

    fn random(rng: impl crate::rand_core::RngCore) -> Self {
        Self(F::random(rng))
    }

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

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

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

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

    fn sqrt_ratio(num: &Self, div: &Self) -> (subtle::Choice, Self) {
        let (choice, res) = F::sqrt_ratio(num, div);
        (choice, Self(res))
    }
}

impl<F> PrimeField for W<F>
where
    F: PrimeField,
    F::Repr: From<GenericArray<u8, typenum::U32>>,
    GenericArray<u8, typenum::U32>: From<F::Repr>,
{
    type Repr = GenericArray<u8, typenum::U32>;

    const MODULUS: &'static str = F::MODULUS;
    const NUM_BITS: u32 = F::NUM_BITS;
    const CAPACITY: u32 = F::CAPACITY;
    const TWO_INV: Self = Self(F::TWO_INV);
    const MULTIPLICATIVE_GENERATOR: Self = Self(F::MULTIPLICATIVE_GENERATOR);
    const S: u32 = F::S;
    const ROOT_OF_UNITY: Self = Self(F::ROOT_OF_UNITY);
    const ROOT_OF_UNITY_INV: Self = Self(F::ROOT_OF_UNITY_INV);
    const DELTA: Self = Self(F::DELTA);

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

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

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

impl<F: Default + Copy> zeroize::DefaultIsZeroes for W<F> {}

impl<F: PrimeField, C: elliptic_curve::Curve> From<ScalarPrimitive<C>> for W<F> {
    fn from(s: ScalarPrimitive<C>) -> Self {
        let bytes_be = s.as_uint().to_be_bytes();
        Self::from_be_bytes_mod_order(bytes_be.as_ref())
    }
}

impl<F> From<W<F>> for GenericArray<u8, typenum::U32>
where
    W<F>: PrimeField<Repr = GenericArray<u8, typenum::U32>>,
{
    fn from(s: W<F>) -> Self {
        s.to_repr()
    }
}

impl<F> IsHigh for W<F>
where
    F: PrimeField,
    W<F>: Sub<Output = W<F>>,
    U256: From<W<F>>,
{
    fn is_high(&self) -> subtle::Choice {
        let n = Self::ZERO - Self::ONE;
        let n_2 = U256::from(n) >> 1;

        let s = U256::from(*self);

        s.ct_gt(&n_2)
    }
}