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
//! Core functionality
//!
//! Contains [field_element] and [scalar] derived by [ff] crate, and a wrapper struct [`W`] that makes field element
//! and scalar compatible with [elliptic-curve] crate.

mod wrapper;

pub use wrapper::W;

/// Field element, derived by [ff] crate
#[allow(missing_docs)]
pub mod field_element {
    use ff::PrimeField;

    use crate::generic_array::{typenum, GenericArray};

    #[derive(PrimeField)]
    #[PrimeFieldModulus = "3618502788666131213697322783095070105623107215331596699973092056135872020481"]
    #[PrimeFieldGenerator = "3"]
    #[PrimeFieldReprEndianness = "big"]
    pub struct FieldElementCore([u64; 4]);

    impl FieldElementCore {
        pub(crate) const fn from_internal_repr(repr: [u64; 4]) -> Self {
            Self(repr)
        }

        #[cfg(test)]
        pub(crate) const fn internal_repr(&self) -> &[u64; 4] {
            &self.0
        }
    }

    impl From<[u8; 32]> for FieldElementCoreRepr {
        fn from(bytes: [u8; 32]) -> Self {
            Self(bytes)
        }
    }

    impl From<FieldElementCoreRepr> for [u8; 32] {
        fn from(s: FieldElementCoreRepr) -> Self {
            s.0
        }
    }

    impl From<GenericArray<u8, typenum::U32>> for FieldElementCoreRepr {
        fn from(bytes: GenericArray<u8, typenum::U32>) -> Self {
            Self(bytes.into())
        }
    }

    impl From<FieldElementCoreRepr> for GenericArray<u8, typenum::U32> {
        fn from(s: FieldElementCoreRepr) -> Self {
            s.0.into()
        }
    }
}

/// Scalar, derived by [ff] crate
#[allow(missing_docs)]
pub mod scalar {
    use ff::PrimeField;

    use crate::generic_array::{typenum, GenericArray};

    #[derive(PrimeField)]
    #[PrimeFieldModulus = "3618502788666131213697322783095070105526743751716087489154079457884512865583"]
    #[PrimeFieldGenerator = "3"]
    #[PrimeFieldReprEndianness = "big"]
    pub struct ScalarCore([u64; 4]);

    impl ScalarCore {
        #[allow(dead_code)]
        pub(crate) const fn from_internal_repr(repr: [u64; 4]) -> Self {
            Self(repr)
        }

        #[allow(dead_code)]
        pub(crate) const fn internal_repr(&self) -> &[u64; 4] {
            &self.0
        }
    }

    impl From<[u8; 32]> for ScalarCoreRepr {
        fn from(bytes: [u8; 32]) -> Self {
            Self(bytes)
        }
    }

    impl From<ScalarCoreRepr> for [u8; 32] {
        fn from(s: ScalarCoreRepr) -> Self {
            s.0
        }
    }

    impl From<GenericArray<u8, typenum::U32>> for ScalarCoreRepr {
        fn from(bytes: GenericArray<u8, typenum::U32>) -> Self {
            Self(bytes.into())
        }
    }

    impl From<ScalarCoreRepr> for GenericArray<u8, typenum::U32> {
        fn from(s: ScalarCoreRepr) -> Self {
            s.0.into()
        }
    }
}

use crate::ff::Field;

impl W<field_element::FieldElementCore> {
    /// Field element $x = 0$
    pub const ZERO: Self = Self::new(field_element::FieldElementCore::ZERO);
    /// Field element $x = 1$
    pub const ONE: Self = Self::new(field_element::FieldElementCore::ONE);
}

impl W<scalar::ScalarCore> {
    /// Scalar $x = 0$
    pub const ZERO: Self = Self::new(scalar::ScalarCore::ZERO);
    /// Scalar $x = 1$
    pub const ONE: Self = Self::new(scalar::ScalarCore::ONE);
}