@@ -20,33 +20,33 @@ impl std::error::Error for QM31Error {}
20
20
impl fmt:: Display for QM31Error {
21
21
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
22
22
match self {
23
- QM31Error :: UnreducedFelt ( felt) => writeln ! (
23
+ QM31Error :: UnreducedFelt ( felt) => write ! (
24
24
f,
25
25
"Number is not a packing of a QM31 in reduced form: {felt})"
26
26
) ,
27
- QM31Error :: FeltTooBig ( felt) => writeln ! (
27
+ QM31Error :: FeltTooBig ( felt) => write ! (
28
28
f,
29
29
"Number used as QM31 since it's more than 144 bits long: {felt}"
30
30
) ,
31
- QM31Error :: InvalidInversion => writeln ! ( f, "Attempt to invert a qm31 equal to zero" ) ,
31
+ QM31Error :: InvalidInversion => write ! ( f, "Attempt to invert a qm31 equal to zero" ) ,
32
32
}
33
33
}
34
34
}
35
35
36
36
#[ repr( transparent) ]
37
37
#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
38
- pub struct QM31Felt ( [ u64 ; 4 ] ) ;
38
+ pub struct QM31 ( [ u64 ; 4 ] ) ;
39
39
40
- impl QM31Felt {
41
- /// [QM31Felt ] constant that's equal to 0.
40
+ impl QM31 {
41
+ /// [QM31 ] constant that's equal to 0.
42
42
pub const ZERO : Self = Self ( [ 0 , 0 , 0 , 0 ] ) ;
43
43
44
44
pub fn as_raw ( & self ) -> [ u64 ; 4 ] {
45
45
self . 0
46
46
}
47
47
48
- /// Create a [QM31Felt ] from the raw internal representation. Reduces four u64 coordinates so that they fit in 144 bits.
49
- pub fn from_raw ( coordinates : [ u64 ; 4 ] ) -> QM31Felt {
48
+ /// Create a [QM31 ] from the raw internal representation. Reduces four u64 coordinates so that they fit in 144 bits.
49
+ pub fn from_raw ( coordinates : [ u64 ; 4 ] ) -> QM31 {
50
50
Self ( [
51
51
coordinates[ 0 ] % STWO_PRIME ,
52
52
coordinates[ 1 ] % STWO_PRIME ,
@@ -55,8 +55,8 @@ impl QM31Felt {
55
55
] )
56
56
}
57
57
58
- /// Packs the [QM31Felt ] coordinates into a Felt.
59
- pub fn pack_into_felt ( & self ) -> Felt {
58
+ /// Packs the [QM31 ] coordinates into a Felt.
59
+ fn pack_into_felt ( & self ) -> Felt {
60
60
let coordinates = self . 0 ;
61
61
62
62
let bytes_part1 = ( coordinates[ 0 ] as u128 + ( ( coordinates[ 1 ] as u128 ) << 36 ) ) . to_le_bytes ( ) ;
@@ -68,8 +68,8 @@ impl QM31Felt {
68
68
Felt :: from_bytes_le ( & result_bytes)
69
69
}
70
70
71
- /// Computes the addition of two [QM31Felt ] elements in reduced form.
72
- pub fn add ( & self , rhs : & QM31Felt ) -> QM31Felt {
71
+ /// Computes the addition of two [QM31 ] elements in reduced form.
72
+ pub fn add ( & self , rhs : & QM31 ) -> QM31 {
73
73
let coordinates1 = self . as_raw ( ) ;
74
74
let coordinates2 = rhs. as_raw ( ) ;
75
75
let result_unreduced_coordinates = [
@@ -81,8 +81,8 @@ impl QM31Felt {
81
81
Self :: from_raw ( result_unreduced_coordinates)
82
82
}
83
83
84
- /// Computes the negative of a [QM31Felt ] element in reduced form.
85
- pub fn neg ( & self ) -> QM31Felt {
84
+ /// Computes the negative of a [QM31 ] element in reduced form.
85
+ pub fn neg ( & self ) -> QM31 {
86
86
let coordinates = self . as_raw ( ) ;
87
87
Self :: from_raw ( [
88
88
STWO_PRIME - coordinates[ 0 ] ,
@@ -92,8 +92,8 @@ impl QM31Felt {
92
92
] )
93
93
}
94
94
95
- /// Computes the subtraction of two [QM31Felt ] elements in reduced form.
96
- pub fn sub ( & self , rhs : & QM31Felt ) -> QM31Felt {
95
+ /// Computes the subtraction of two [QM31 ] elements in reduced form.
96
+ pub fn sub ( & self , rhs : & QM31 ) -> QM31 {
97
97
let coordinates1 = self . as_raw ( ) ;
98
98
let coordinates2 = rhs. as_raw ( ) ;
99
99
let result_unreduced_coordinates = [
@@ -105,8 +105,8 @@ impl QM31Felt {
105
105
Self :: from_raw ( result_unreduced_coordinates)
106
106
}
107
107
108
- /// Computes the multiplication of two [QM31Felt ] elements in reduced form.
109
- pub fn mul ( & self , rhs : & QM31Felt ) -> QM31Felt {
108
+ /// Computes the multiplication of two [QM31 ] elements in reduced form.
109
+ pub fn mul ( & self , rhs : & QM31 ) -> QM31 {
110
110
let coordinates1_u64 = self . as_raw ( ) ;
111
111
let coordinates2_u64 = rhs. as_raw ( ) ;
112
112
let coordinates1 = coordinates1_u64. map ( u128:: from) ;
@@ -160,9 +160,9 @@ impl QM31Felt {
160
160
u
161
161
}
162
162
163
- /// Computes the inverse of a [QM31Felt ] element in reduced form.
163
+ /// Computes the inverse of a [QM31 ] element in reduced form.
164
164
/// Returns an error if the operand is equal to zero.
165
- pub fn inverse ( & self ) -> Result < QM31Felt , QM31Error > {
165
+ pub fn inverse ( & self ) -> Result < QM31 , QM31Error > {
166
166
if * self == Self :: ZERO {
167
167
return Err ( QM31Error :: InvalidInversion ) ;
168
168
}
@@ -201,9 +201,9 @@ impl QM31Felt {
201
201
] ) )
202
202
}
203
203
204
- /// Computes the division of two [QM31Felt ] elements in reduced form. Returns an error
204
+ /// Computes the division of two [QM31 ] elements in reduced form. Returns an error
205
205
/// if the rhs value is equal to zero.
206
- pub fn div ( & self , rhs : & QM31Felt ) -> Result < QM31Felt , QM31Error > {
206
+ pub fn div ( & self , rhs : & QM31 ) -> Result < QM31 , QM31Error > {
207
207
let rhs_inv = rhs. inverse ( ) ?;
208
208
Ok ( self . mul ( & rhs_inv) )
209
209
}
@@ -213,26 +213,26 @@ impl QM31Felt {
213
213
}
214
214
}
215
215
216
- impl From < & QM31Felt > for Felt {
217
- fn from ( value : & QM31Felt ) -> Self {
216
+ impl From < & QM31 > for Felt {
217
+ fn from ( value : & QM31 ) -> Self {
218
218
value. pack_into_felt ( )
219
219
}
220
220
}
221
221
222
- impl From < QM31Felt > for Felt {
223
- fn from ( value : QM31Felt ) -> Self {
222
+ impl From < QM31 > for Felt {
223
+ fn from ( value : QM31 ) -> Self {
224
224
value. pack_into_felt ( )
225
225
}
226
226
}
227
227
228
- impl TryFrom < Felt > for QM31Felt {
228
+ impl TryFrom < Felt > for QM31 {
229
229
type Error = QM31Error ;
230
230
231
231
fn try_from ( value : Felt ) -> Result < Self , Self :: Error > {
232
232
let limbs = value. to_le_digits ( ) ;
233
233
234
234
// Check value fits in 144 bits. This check is only done here
235
- // because we are trying to convert a Felt into a QM31Felt . This
235
+ // because we are trying to convert a Felt into a QM31 . This
236
236
// Felt should represent a packed QM31 which is at most 144 bits long.
237
237
if limbs[ 3 ] != 0 || limbs[ 2 ] >= 1 << 16 {
238
238
return Err ( QM31Error :: FeltTooBig ( value) ) ;
@@ -256,14 +256,14 @@ impl TryFrom<Felt> for QM31Felt {
256
256
}
257
257
}
258
258
259
- impl TryFrom < & Felt > for QM31Felt {
259
+ impl TryFrom < & Felt > for QM31 {
260
260
type Error = QM31Error ;
261
261
262
262
fn try_from ( value : & Felt ) -> Result < Self , Self :: Error > {
263
263
let limbs = value. to_le_digits ( ) ;
264
264
265
265
// Check value fits in 144 bits. This check is only done here
266
- // because we are trying to convert a Felt into a QM31Felt . This
266
+ // because we are trying to convert a Felt into a QM31 . This
267
267
// Felt should represent a packed QM31 which is at most 144 bits long.
268
268
if limbs[ 3 ] != 0 || limbs[ 2 ] >= 1 << 16 {
269
269
return Err ( QM31Error :: FeltTooBig ( * value) ) ;
@@ -298,38 +298,38 @@ mod test {
298
298
} ;
299
299
300
300
use crate :: felt:: {
301
- qm31:: { QM31Error , QM31Felt , STWO_PRIME } ,
301
+ qm31:: { QM31Error , QM31 , STWO_PRIME } ,
302
302
Felt ,
303
303
} ;
304
304
305
305
#[ test]
306
306
fn qm31_to_felt ( ) {
307
- let coordinates = QM31Felt :: from_raw ( [ 1 , 2 , 3 , 4 ] ) ;
307
+ let coordinates = QM31 :: from_raw ( [ 1 , 2 , 3 , 4 ] ) ;
308
308
let packed_coordinates = Felt :: from ( coordinates) ;
309
- let unpacked_coordinates = QM31Felt :: try_from ( packed_coordinates) . unwrap ( ) ;
309
+ let unpacked_coordinates = QM31 :: try_from ( packed_coordinates) . unwrap ( ) ;
310
310
assert_eq ! ( coordinates, unpacked_coordinates) ;
311
311
312
- let qm31 = QM31Felt :: from_raw ( [ u64:: MAX , 0 , 0 , 0 ] ) ;
312
+ let qm31 = QM31 :: from_raw ( [ u64:: MAX , 0 , 0 , 0 ] ) ;
313
313
let felt: Felt = qm31. try_into ( ) . unwrap ( ) ;
314
- let felt_to_qm31 = QM31Felt :: try_from ( felt) . unwrap ( ) ;
314
+ let felt_to_qm31 = QM31 :: try_from ( felt) . unwrap ( ) ;
315
315
316
316
assert_eq ! ( felt_to_qm31, qm31) ;
317
317
318
- let qm31 = QM31Felt :: from_raw ( [ u64:: MAX , u64:: MAX , 0 , 0 ] ) ;
318
+ let qm31 = QM31 :: from_raw ( [ u64:: MAX , u64:: MAX , 0 , 0 ] ) ;
319
319
let felt: Felt = qm31. try_into ( ) . unwrap ( ) ;
320
- let felt_to_qm31 = QM31Felt :: try_from ( felt) . unwrap ( ) ;
320
+ let felt_to_qm31 = QM31 :: try_from ( felt) . unwrap ( ) ;
321
321
322
322
assert_eq ! ( felt_to_qm31, qm31) ;
323
323
324
- let qm31 = QM31Felt :: from_raw ( [ u64:: MAX , u64:: MAX , u64:: MAX , 0 ] ) ;
324
+ let qm31 = QM31 :: from_raw ( [ u64:: MAX , u64:: MAX , u64:: MAX , 0 ] ) ;
325
325
let felt: Felt = qm31. try_into ( ) . unwrap ( ) ;
326
- let felt_to_qm31 = QM31Felt :: try_from ( felt) . unwrap ( ) ;
326
+ let felt_to_qm31 = QM31 :: try_from ( felt) . unwrap ( ) ;
327
327
328
328
assert_eq ! ( felt_to_qm31, qm31) ;
329
329
330
- let qm31 = QM31Felt :: from_raw ( [ u64:: MAX , u64:: MAX , u64:: MAX , u64:: MAX ] ) ;
330
+ let qm31 = QM31 :: from_raw ( [ u64:: MAX , u64:: MAX , u64:: MAX , u64:: MAX ] ) ;
331
331
let felt: Felt = qm31. try_into ( ) . unwrap ( ) ;
332
- let felt_to_qm31 = QM31Felt :: try_from ( felt) . unwrap ( ) ;
332
+ let felt_to_qm31 = QM31 :: try_from ( felt) . unwrap ( ) ;
333
333
334
334
assert_eq ! ( felt_to_qm31, qm31) ;
335
335
}
@@ -339,7 +339,7 @@ mod test {
339
339
let mut felt_bytes = [ 0u8 ; 32 ] ;
340
340
felt_bytes[ 18 ] = 1 ;
341
341
let felt = Felt :: from_bytes_le ( & felt_bytes) ;
342
- let qm31: Result < QM31Felt , QM31Error > = felt. try_into ( ) ;
342
+ let qm31: Result < QM31 , QM31Error > = felt. try_into ( ) ;
343
343
assert ! ( matches!(
344
344
qm31,
345
345
Err ( QM31Error :: FeltTooBig ( bx) ) if bx == felt
@@ -350,8 +350,8 @@ mod test {
350
350
fn test_qm31_packed_reduced_add ( ) {
351
351
let x_coordinates = [ 1414213562 , 1732050807 , 1618033988 , 1234567890 ] ;
352
352
let y_coordinates = [ 1234567890 , 1414213562 , 1732050807 , 1618033988 ] ;
353
- let x = QM31Felt :: from_raw ( x_coordinates) ;
354
- let y = QM31Felt :: from_raw ( y_coordinates) ;
353
+ let x = QM31 :: from_raw ( x_coordinates) ;
354
+ let y = QM31 :: from_raw ( y_coordinates) ;
355
355
let res = x. add ( & y) ;
356
356
let res_coordinates = res. as_raw ( ) ;
357
357
assert_eq ! (
@@ -368,7 +368,7 @@ mod test {
368
368
#[ test]
369
369
fn test_qm31_packed_reduced_neg ( ) {
370
370
let x_coordinates = [ 1749652895 , 834624081 , 1930174752 , 2063872165 ] ;
371
- let x = QM31Felt :: from_raw ( x_coordinates) ;
371
+ let x = QM31 :: from_raw ( x_coordinates) ;
372
372
let res = x. neg ( ) ;
373
373
let res_coordinates = res. as_raw ( ) ;
374
374
assert_eq ! (
@@ -391,8 +391,8 @@ mod test {
391
391
( 1234567890 + 1618033988 ) % STWO_PRIME ,
392
392
] ;
393
393
let y_coordinates = [ 1414213562 , 1732050807 , 1618033988 , 1234567890 ] ;
394
- let x = QM31Felt :: from_raw ( x_coordinates) ;
395
- let y = QM31Felt :: from_raw ( y_coordinates) ;
394
+ let x = QM31 :: from_raw ( x_coordinates) ;
395
+ let y = QM31 :: from_raw ( y_coordinates) ;
396
396
let res = x. sub ( & y) ;
397
397
let res_coordinates = res. as_raw ( ) ;
398
398
assert_eq ! (
@@ -405,8 +405,8 @@ mod test {
405
405
fn test_qm31_packed_reduced_mul ( ) {
406
406
let x_coordinates = [ 1414213562 , 1732050807 , 1618033988 , 1234567890 ] ;
407
407
let y_coordinates = [ 1259921049 , 1442249570 , 1847759065 , 2094551481 ] ;
408
- let x = QM31Felt :: from_raw ( x_coordinates) ;
409
- let y = QM31Felt :: from_raw ( y_coordinates) ;
408
+ let x = QM31 :: from_raw ( x_coordinates) ;
409
+ let y = QM31 :: from_raw ( y_coordinates) ;
410
410
let res = x. mul ( & y) ;
411
411
let res_coordinates = res. as_raw ( ) ;
412
412
assert_eq ! (
@@ -418,19 +418,19 @@ mod test {
418
418
#[ test]
419
419
fn test_qm31_packed_reduced_inv ( ) {
420
420
let x_coordinates = [ 1259921049 , 1442249570 , 1847759065 , 2094551481 ] ;
421
- let x = QM31Felt :: from_raw ( x_coordinates) ;
421
+ let x = QM31 :: from_raw ( x_coordinates) ;
422
422
let res = x. inverse ( ) . unwrap ( ) ;
423
423
let expected = Felt :: from ( 1 ) . try_into ( ) . unwrap ( ) ;
424
424
assert_eq ! ( x. mul( & res) , expected) ;
425
425
426
426
let x_coordinates = [ 1 , 2 , 3 , 4 ] ;
427
- let x = QM31Felt :: from_raw ( x_coordinates) ;
427
+ let x = QM31 :: from_raw ( x_coordinates) ;
428
428
let res = x. inverse ( ) . unwrap ( ) ;
429
429
let expected = Felt :: from ( 1 ) . try_into ( ) . unwrap ( ) ;
430
430
assert_eq ! ( x. mul( & res) , expected) ;
431
431
432
432
let x_coordinates = [ 1749652895 , 834624081 , 1930174752 , 2063872165 ] ;
433
- let x = QM31Felt :: from_raw ( x_coordinates) ;
433
+ let x = QM31 :: from_raw ( x_coordinates) ;
434
434
let res = x. inverse ( ) . unwrap ( ) ;
435
435
let expected = Felt :: from ( 1 ) . try_into ( ) . unwrap ( ) ;
436
436
assert_eq ! ( x. mul( & res) , expected) ;
@@ -447,10 +447,10 @@ mod test {
447
447
. prop_filter( "All configs cant be 0" ,
448
448
|arr| !arr. iter( ) . all( |x| * x == 0 ) )
449
449
) {
450
- let x = QM31Felt :: from_raw( x_coordinates) ;
450
+ let x = QM31 :: from_raw( x_coordinates) ;
451
451
let res = x. inverse( ) . unwrap( ) ;
452
452
// Expect 1_felt252
453
- let expected = QM31Felt :: from_raw( [ 1 , 0 , 0 , 0 ] ) ;
453
+ let expected = QM31 :: from_raw( [ 1 , 0 , 0 , 0 ] ) ;
454
454
assert_eq!( x. mul( & res) , expected) ;
455
455
}
456
456
@@ -461,10 +461,10 @@ mod test {
461
461
|arr| !arr. iter( ) . all( |x| * x == 0 ) )
462
462
. no_shrink( )
463
463
) {
464
- let x = QM31Felt :: from_raw( x_coordinates) ;
464
+ let x = QM31 :: from_raw( x_coordinates) ;
465
465
let res = x. inverse( ) . unwrap( ) ;
466
466
// Expect 1_felt252
467
- let expected = QM31Felt :: from_raw( [ 1 , 0 , 0 , 0 ] ) ;
467
+ let expected = QM31 :: from_raw( [ 1 , 0 , 0 , 0 ] ) ;
468
468
assert_eq!( x. mul( & res) , expected) ;
469
469
}
470
470
}
@@ -474,9 +474,9 @@ mod test {
474
474
let x_coordinates = [ 1259921049 , 1442249570 , 1847759065 , 2094551481 ] ;
475
475
let y_coordinates = [ 1414213562 , 1732050807 , 1618033988 , 1234567890 ] ;
476
476
let xy_coordinates = [ 947980980 , 1510986506 , 623360030 , 1260310989 ] ;
477
- let x = QM31Felt :: from_raw ( x_coordinates) ;
478
- let y = QM31Felt :: from_raw ( y_coordinates) ;
479
- let xy = QM31Felt :: from_raw ( xy_coordinates) ;
477
+ let x = QM31 :: from_raw ( x_coordinates) ;
478
+ let y = QM31 :: from_raw ( y_coordinates) ;
479
+ let xy = QM31 :: from_raw ( xy_coordinates) ;
480
480
481
481
let res = xy. div ( & y) . unwrap ( ) ;
482
482
assert_eq ! ( res, x) ;
0 commit comments