stark_curve/core/
mod.rs

1//! Core functionality
2//!
3//! Contains [field_element] and [scalar] derived by [ff] crate, and a wrapper struct [`W`] that makes field element
4//! and scalar compatible with [elliptic-curve] crate.
5
6mod wrapper;
7
8pub use wrapper::W;
9
10/// Field element, derived by [ff] crate
11#[allow(missing_docs)]
12pub mod field_element {
13    use ff::PrimeField;
14
15    use crate::generic_array::{typenum, GenericArray};
16
17    #[derive(PrimeField)]
18    #[PrimeFieldModulus = "3618502788666131213697322783095070105623107215331596699973092056135872020481"]
19    #[PrimeFieldGenerator = "3"]
20    #[PrimeFieldReprEndianness = "big"]
21    pub struct FieldElementCore([u64; 4]);
22
23    impl FieldElementCore {
24        pub(crate) const fn from_internal_repr(repr: [u64; 4]) -> Self {
25            Self(repr)
26        }
27
28        #[cfg(test)]
29        pub(crate) const fn internal_repr(&self) -> &[u64; 4] {
30            &self.0
31        }
32    }
33
34    impl From<[u8; 32]> for FieldElementCoreRepr {
35        fn from(bytes: [u8; 32]) -> Self {
36            Self(bytes)
37        }
38    }
39
40    impl From<FieldElementCoreRepr> for [u8; 32] {
41        fn from(s: FieldElementCoreRepr) -> Self {
42            s.0
43        }
44    }
45
46    impl From<GenericArray<u8, typenum::U32>> for FieldElementCoreRepr {
47        fn from(bytes: GenericArray<u8, typenum::U32>) -> Self {
48            Self(bytes.into())
49        }
50    }
51
52    impl From<FieldElementCoreRepr> for GenericArray<u8, typenum::U32> {
53        fn from(s: FieldElementCoreRepr) -> Self {
54            s.0.into()
55        }
56    }
57}
58
59/// Scalar, derived by [ff] crate
60#[allow(missing_docs)]
61pub mod scalar {
62    use ff::PrimeField;
63
64    use crate::generic_array::{typenum, GenericArray};
65
66    #[derive(PrimeField)]
67    #[PrimeFieldModulus = "3618502788666131213697322783095070105526743751716087489154079457884512865583"]
68    #[PrimeFieldGenerator = "3"]
69    #[PrimeFieldReprEndianness = "big"]
70    pub struct ScalarCore([u64; 4]);
71
72    impl ScalarCore {
73        #[allow(dead_code)]
74        pub(crate) const fn from_internal_repr(repr: [u64; 4]) -> Self {
75            Self(repr)
76        }
77
78        #[allow(dead_code)]
79        pub(crate) const fn internal_repr(&self) -> &[u64; 4] {
80            &self.0
81        }
82    }
83
84    impl From<[u8; 32]> for ScalarCoreRepr {
85        fn from(bytes: [u8; 32]) -> Self {
86            Self(bytes)
87        }
88    }
89
90    impl From<ScalarCoreRepr> for [u8; 32] {
91        fn from(s: ScalarCoreRepr) -> Self {
92            s.0
93        }
94    }
95
96    impl From<GenericArray<u8, typenum::U32>> for ScalarCoreRepr {
97        fn from(bytes: GenericArray<u8, typenum::U32>) -> Self {
98            Self(bytes.into())
99        }
100    }
101
102    impl From<ScalarCoreRepr> for GenericArray<u8, typenum::U32> {
103        fn from(s: ScalarCoreRepr) -> Self {
104            s.0.into()
105        }
106    }
107}
108
109use crate::ff::Field;
110
111impl W<field_element::FieldElementCore> {
112    /// Field element $x = 0$
113    pub const ZERO: Self = Self::new(field_element::FieldElementCore::ZERO);
114    /// Field element $x = 1$
115    pub const ONE: Self = Self::new(field_element::FieldElementCore::ONE);
116}
117
118impl W<scalar::ScalarCore> {
119    /// Scalar $x = 0$
120    pub const ZERO: Self = Self::new(scalar::ScalarCore::ZERO);
121    /// Scalar $x = 1$
122    pub const ONE: Self = Self::new(scalar::ScalarCore::ONE);
123}