lambdaworks_math/field/test_fields/
u32_test_field.rs1use crate::{
2 errors::CreationError,
3 field::errors::FieldError,
4 field::traits::{IsFFTField, IsField, IsPrimeField},
5};
6
7use crate::traits::ByteConversion;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10
11pub struct U32Field<const MODULUS: u32>;
12
13impl ByteConversion for u32 {
14 #[cfg(feature = "alloc")]
15 fn to_bytes_be(&self) -> alloc::vec::Vec<u8> {
16 unimplemented!()
17 }
18
19 #[cfg(feature = "alloc")]
20 fn to_bytes_le(&self) -> alloc::vec::Vec<u8> {
21 unimplemented!()
22 }
23
24 fn from_bytes_be(_bytes: &[u8]) -> Result<Self, crate::errors::ByteConversionError>
25 where
26 Self: Sized,
27 {
28 unimplemented!()
29 }
30
31 fn from_bytes_le(_bytes: &[u8]) -> Result<Self, crate::errors::ByteConversionError>
32 where
33 Self: Sized,
34 {
35 unimplemented!()
36 }
37}
38
39impl<const MODULUS: u32> IsField for U32Field<MODULUS> {
40 type BaseType = u32;
41
42 fn add(a: &u32, b: &u32) -> u32 {
43 ((*a as u128 + *b as u128) % MODULUS as u128) as u32
44 }
45
46 fn sub(a: &u32, b: &u32) -> u32 {
47 (((*a as u128 + MODULUS as u128) - *b as u128) % MODULUS as u128) as u32
48 }
49
50 fn neg(a: &u32) -> u32 {
51 MODULUS - a
52 }
53
54 fn mul(a: &u32, b: &u32) -> u32 {
55 ((*a as u128 * *b as u128) % MODULUS as u128) as u32
56 }
57
58 fn div(a: &u32, b: &u32) -> Result<u32, FieldError> {
59 let b_inv = &Self::inv(b)?;
60 Ok(Self::mul(a, b_inv))
61 }
62
63 fn inv(a: &u32) -> Result<u32, FieldError> {
64 if *a == 0 {
65 return Err(FieldError::InvZeroError);
66 }
67 Ok(Self::pow(a, MODULUS - 2))
68 }
69
70 fn eq(a: &u32, b: &u32) -> bool {
71 Self::from_base_type(*a) == Self::from_base_type(*b)
72 }
73
74 fn zero() -> u32 {
75 0
76 }
77
78 fn one() -> u32 {
79 1
80 }
81
82 fn from_u64(x: u64) -> u32 {
83 (x % MODULUS as u64) as u32
84 }
85
86 fn from_base_type(x: u32) -> u32 {
87 x % MODULUS
88 }
89}
90
91impl<const MODULUS: u32> IsPrimeField for U32Field<MODULUS> {
92 type RepresentativeType = u32;
93
94 fn representative(a: &Self::BaseType) -> u32 {
95 *a
96 }
97
98 fn field_bit_size() -> usize {
101 ((MODULUS - 1).ilog2() + 1) as usize
102 }
103
104 fn from_hex(hex_string: &str) -> Result<Self::BaseType, crate::errors::CreationError> {
106 let mut hex_string = hex_string;
107 let mut char_iterator = hex_string.chars();
109 if hex_string.len() > 2
110 && char_iterator.next().unwrap() == '0'
111 && char_iterator.next().unwrap() == 'x'
112 {
113 hex_string = &hex_string[2..];
114 }
115
116 u32::from_str_radix(hex_string, 16).map_err(|_| CreationError::InvalidHexString)
117 }
118
119 #[cfg(feature = "std")]
120 fn to_hex(x: &u32) -> String {
121 format!("{x:X}")
122 }
123}
124
125pub type U32TestField = U32Field<2013265921>;
127
128impl IsFFTField for U32TestField {
130 const TWO_ADICITY: u64 = 27;
131 const TWO_ADIC_PRIMITVE_ROOT_OF_UNITY: u32 = 440532289;
132}
133
134#[cfg(test)]
135mod tests_u32_test_field {
136 use crate::field::{test_fields::u32_test_field::U32TestField, traits::IsPrimeField};
137
138 #[test]
139 fn from_hex_for_b_is_11() {
140 assert_eq!(U32TestField::from_hex("B").unwrap(), 11);
141 }
142
143 #[cfg(feature = "std")]
144 #[test]
145 fn to_hex_test() {
146 let num = U32TestField::from_hex("B").unwrap();
147 assert_eq!(U32TestField::to_hex(&num), "B");
148 }
149
150 #[test]
151 fn bit_size_of_test_field_is_31() {
152 assert_eq!(
153 <U32TestField as crate::field::traits::IsPrimeField>::field_bit_size(),
154 31
155 );
156 }
157}