@@ -63,7 +63,7 @@ impl QM31 {
63
63
///
64
64
/// A QM31 is composed of 4 coordinates each of which can be represented with 36 bits, meaning
65
65
/// it can be store in a Felt252. This method packs a given QM31 and stores it in the first 144 bits of a Felt252.
66
- fn pack_into_felt ( & self ) -> Felt {
66
+ pub fn pack_into_felt ( & self ) -> Felt {
67
67
let coordinates = self . 0 ;
68
68
69
69
let bytes_part1 = ( coordinates[ 0 ] as u128 + ( ( coordinates[ 1 ] as u128 ) << 36 ) ) . to_le_bytes ( ) ;
@@ -76,7 +76,7 @@ impl QM31 {
76
76
}
77
77
78
78
/// Unpacks the reduced coordinates stored in [Felt] and creates a [QM31].
79
- fn unpack_from_felt ( felt : & Felt ) -> Result < QM31 , QM31Error > {
79
+ pub fn unpack_from_felt ( felt : & Felt ) -> Result < QM31 , QM31Error > {
80
80
let limbs = felt. to_le_digits ( ) ;
81
81
82
82
// Check value fits in 144 bits. This check is only done here
@@ -251,34 +251,6 @@ impl QM31 {
251
251
}
252
252
}
253
253
254
- impl From < & QM31 > for Felt {
255
- fn from ( value : & QM31 ) -> Self {
256
- value. pack_into_felt ( )
257
- }
258
- }
259
-
260
- impl From < QM31 > for Felt {
261
- fn from ( value : QM31 ) -> Self {
262
- value. pack_into_felt ( )
263
- }
264
- }
265
-
266
- impl TryFrom < Felt > for QM31 {
267
- type Error = QM31Error ;
268
-
269
- fn try_from ( value : Felt ) -> Result < Self , Self :: Error > {
270
- Self :: unpack_from_felt ( & value)
271
- }
272
- }
273
-
274
- impl TryFrom < & Felt > for QM31 {
275
- type Error = QM31Error ;
276
-
277
- fn try_from ( value : & Felt ) -> Result < Self , Self :: Error > {
278
- Self :: unpack_from_felt ( value)
279
- }
280
- }
281
-
282
254
#[ cfg( test) ]
283
255
mod test {
284
256
use core:: u64;
@@ -297,31 +269,31 @@ mod test {
297
269
#[ test]
298
270
fn qm31_to_felt ( ) {
299
271
let coordinates = QM31 :: from_coordinates ( [ 1 , 2 , 3 , 4 ] ) ;
300
- let packed_coordinates = Felt :: from ( coordinates) ;
301
- let unpacked_coordinates = QM31 :: try_from ( packed_coordinates) . unwrap ( ) ;
272
+ let packed_coordinates = coordinates. pack_into_felt ( ) ;
273
+ let unpacked_coordinates = QM31 :: unpack_from_felt ( & packed_coordinates) . unwrap ( ) ;
302
274
assert_eq ! ( coordinates, unpacked_coordinates) ;
303
275
304
276
let qm31 = QM31 :: from_coordinates ( [ u64:: MAX , 0 , 0 , 0 ] ) ;
305
- let felt: Felt = qm31. try_into ( ) . unwrap ( ) ;
306
- let felt_to_qm31 = QM31 :: try_from ( felt) . unwrap ( ) ;
277
+ let felt: Felt = qm31. pack_into_felt ( ) ;
278
+ let felt_to_qm31 = QM31 :: unpack_from_felt ( & felt) . unwrap ( ) ;
307
279
308
280
assert_eq ! ( felt_to_qm31, qm31) ;
309
281
310
282
let qm31 = QM31 :: from_coordinates ( [ u64:: MAX , u64:: MAX , 0 , 0 ] ) ;
311
- let felt: Felt = qm31. try_into ( ) . unwrap ( ) ;
312
- let felt_to_qm31 = QM31 :: try_from ( felt) . unwrap ( ) ;
283
+ let felt: Felt = qm31. pack_into_felt ( ) ;
284
+ let felt_to_qm31 = QM31 :: unpack_from_felt ( & felt) . unwrap ( ) ;
313
285
314
286
assert_eq ! ( felt_to_qm31, qm31) ;
315
287
316
288
let qm31 = QM31 :: from_coordinates ( [ u64:: MAX , u64:: MAX , u64:: MAX , 0 ] ) ;
317
- let felt: Felt = qm31. try_into ( ) . unwrap ( ) ;
318
- let felt_to_qm31 = QM31 :: try_from ( felt) . unwrap ( ) ;
289
+ let felt: Felt = qm31. pack_into_felt ( ) ;
290
+ let felt_to_qm31 = QM31 :: unpack_from_felt ( & felt) . unwrap ( ) ;
319
291
320
292
assert_eq ! ( felt_to_qm31, qm31) ;
321
293
322
294
let qm31 = QM31 :: from_coordinates ( [ u64:: MAX , u64:: MAX , u64:: MAX , u64:: MAX ] ) ;
323
- let felt: Felt = qm31. try_into ( ) . unwrap ( ) ;
324
- let felt_to_qm31 = QM31 :: try_from ( felt) . unwrap ( ) ;
295
+ let felt: Felt = qm31. pack_into_felt ( ) ;
296
+ let felt_to_qm31 = QM31 :: unpack_from_felt ( & felt) . unwrap ( ) ;
325
297
326
298
assert_eq ! ( felt_to_qm31, qm31) ;
327
299
}
@@ -331,7 +303,7 @@ mod test {
331
303
let mut felt_bytes = [ 0u8 ; 32 ] ;
332
304
felt_bytes[ 18 ] = 1 ;
333
305
let felt = Felt :: from_bytes_le ( & felt_bytes) ;
334
- let qm31: Result < QM31 , QM31Error > = felt . try_into ( ) ;
306
+ let qm31: Result < QM31 , QM31Error > = QM31 :: unpack_from_felt ( & felt ) ;
335
307
assert ! ( matches!(
336
308
qm31,
337
309
Err ( QM31Error :: FeltTooBig ( bx) ) if bx == felt
@@ -412,19 +384,19 @@ mod test {
412
384
let x_coordinates = [ 1259921049 , 1442249570 , 1847759065 , 2094551481 ] ;
413
385
let x = QM31 :: from_coordinates ( x_coordinates) ;
414
386
let res = x. inverse ( ) . unwrap ( ) ;
415
- let expected = Felt :: from ( 1 ) . try_into ( ) . unwrap ( ) ;
387
+ let expected = QM31 :: unpack_from_felt ( & Felt :: from ( 1 ) ) . unwrap ( ) ;
416
388
assert_eq ! ( x. mul( & res) , expected) ;
417
389
418
390
let x_coordinates = [ 1 , 2 , 3 , 4 ] ;
419
391
let x = QM31 :: from_coordinates ( x_coordinates) ;
420
392
let res = x. inverse ( ) . unwrap ( ) ;
421
- let expected = Felt :: from ( 1 ) . try_into ( ) . unwrap ( ) ;
393
+ let expected = QM31 :: unpack_from_felt ( & Felt :: from ( 1 ) ) . unwrap ( ) ;
422
394
assert_eq ! ( x. mul( & res) , expected) ;
423
395
424
396
let x_coordinates = [ 1749652895 , 834624081 , 1930174752 , 2063872165 ] ;
425
397
let x = QM31 :: from_coordinates ( x_coordinates) ;
426
398
let res = x. inverse ( ) . unwrap ( ) ;
427
- let expected = Felt :: from ( 1 ) . try_into ( ) . unwrap ( ) ;
399
+ let expected = QM31 :: unpack_from_felt ( & Felt :: from ( 1 ) ) . unwrap ( ) ;
428
400
assert_eq ! ( x. mul( & res) , expected) ;
429
401
}
430
402
0 commit comments