Skip to content

Commit 9c822f2

Browse files
remove From and TryFrom implementations
1 parent 3ec9501 commit 9c822f2

File tree

1 file changed

+16
-44
lines changed
  • crates/starknet-types-core/src/qm31

1 file changed

+16
-44
lines changed

crates/starknet-types-core/src/qm31/mod.rs

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl QM31 {
6363
///
6464
/// A QM31 is composed of 4 coordinates each of which can be represented with 36 bits, meaning
6565
/// 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 {
6767
let coordinates = self.0;
6868

6969
let bytes_part1 = (coordinates[0] as u128 + ((coordinates[1] as u128) << 36)).to_le_bytes();
@@ -76,7 +76,7 @@ impl QM31 {
7676
}
7777

7878
/// 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> {
8080
let limbs = felt.to_le_digits();
8181

8282
// Check value fits in 144 bits. This check is only done here
@@ -251,34 +251,6 @@ impl QM31 {
251251
}
252252
}
253253

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-
282254
#[cfg(test)]
283255
mod test {
284256
use core::u64;
@@ -297,31 +269,31 @@ mod test {
297269
#[test]
298270
fn qm31_to_felt() {
299271
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();
302274
assert_eq!(coordinates, unpacked_coordinates);
303275

304276
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();
307279

308280
assert_eq!(felt_to_qm31, qm31);
309281

310282
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();
313285

314286
assert_eq!(felt_to_qm31, qm31);
315287

316288
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();
319291

320292
assert_eq!(felt_to_qm31, qm31);
321293

322294
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();
325297

326298
assert_eq!(felt_to_qm31, qm31);
327299
}
@@ -331,7 +303,7 @@ mod test {
331303
let mut felt_bytes = [0u8; 32];
332304
felt_bytes[18] = 1;
333305
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);
335307
assert!(matches!(
336308
qm31,
337309
Err(QM31Error::FeltTooBig(bx)) if bx == felt
@@ -412,19 +384,19 @@ mod test {
412384
let x_coordinates = [1259921049, 1442249570, 1847759065, 2094551481];
413385
let x = QM31::from_coordinates(x_coordinates);
414386
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();
416388
assert_eq!(x.mul(&res), expected);
417389

418390
let x_coordinates = [1, 2, 3, 4];
419391
let x = QM31::from_coordinates(x_coordinates);
420392
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();
422394
assert_eq!(x.mul(&res), expected);
423395

424396
let x_coordinates = [1749652895, 834624081, 1930174752, 2063872165];
425397
let x = QM31::from_coordinates(x_coordinates);
426398
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();
428400
assert_eq!(x.mul(&res), expected);
429401
}
430402

0 commit comments

Comments
 (0)