Skip to content

Commit 0a7e105

Browse files
rename to QM31, make pack_to_felt private, don't add a new line on error
1 parent c63dfab commit 0a7e105

File tree

1 file changed

+59
-59
lines changed
  • crates/starknet-types-core/src/felt

1 file changed

+59
-59
lines changed

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

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ impl std::error::Error for QM31Error {}
2020
impl fmt::Display for QM31Error {
2121
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2222
match self {
23-
QM31Error::UnreducedFelt(felt) => writeln!(
23+
QM31Error::UnreducedFelt(felt) => write!(
2424
f,
2525
"Number is not a packing of a QM31 in reduced form: {felt})"
2626
),
27-
QM31Error::FeltTooBig(felt) => writeln!(
27+
QM31Error::FeltTooBig(felt) => write!(
2828
f,
2929
"Number used as QM31 since it's more than 144 bits long: {felt}"
3030
),
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"),
3232
}
3333
}
3434
}
3535

3636
#[repr(transparent)]
3737
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
38-
pub struct QM31Felt([u64; 4]);
38+
pub struct QM31([u64; 4]);
3939

40-
impl QM31Felt {
41-
/// [QM31Felt] constant that's equal to 0.
40+
impl QM31 {
41+
/// [QM31] constant that's equal to 0.
4242
pub const ZERO: Self = Self([0, 0, 0, 0]);
4343

4444
pub fn as_raw(&self) -> [u64; 4] {
4545
self.0
4646
}
4747

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 {
5050
Self([
5151
coordinates[0] % STWO_PRIME,
5252
coordinates[1] % STWO_PRIME,
@@ -55,8 +55,8 @@ impl QM31Felt {
5555
])
5656
}
5757

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 {
6060
let coordinates = self.0;
6161

6262
let bytes_part1 = (coordinates[0] as u128 + ((coordinates[1] as u128) << 36)).to_le_bytes();
@@ -68,8 +68,8 @@ impl QM31Felt {
6868
Felt::from_bytes_le(&result_bytes)
6969
}
7070

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 {
7373
let coordinates1 = self.as_raw();
7474
let coordinates2 = rhs.as_raw();
7575
let result_unreduced_coordinates = [
@@ -81,8 +81,8 @@ impl QM31Felt {
8181
Self::from_raw(result_unreduced_coordinates)
8282
}
8383

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 {
8686
let coordinates = self.as_raw();
8787
Self::from_raw([
8888
STWO_PRIME - coordinates[0],
@@ -92,8 +92,8 @@ impl QM31Felt {
9292
])
9393
}
9494

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 {
9797
let coordinates1 = self.as_raw();
9898
let coordinates2 = rhs.as_raw();
9999
let result_unreduced_coordinates = [
@@ -105,8 +105,8 @@ impl QM31Felt {
105105
Self::from_raw(result_unreduced_coordinates)
106106
}
107107

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 {
110110
let coordinates1_u64 = self.as_raw();
111111
let coordinates2_u64 = rhs.as_raw();
112112
let coordinates1 = coordinates1_u64.map(u128::from);
@@ -160,9 +160,9 @@ impl QM31Felt {
160160
u
161161
}
162162

163-
/// Computes the inverse of a [QM31Felt] element in reduced form.
163+
/// Computes the inverse of a [QM31] element in reduced form.
164164
/// 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> {
166166
if *self == Self::ZERO {
167167
return Err(QM31Error::InvalidInversion);
168168
}
@@ -201,9 +201,9 @@ impl QM31Felt {
201201
]))
202202
}
203203

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
205205
/// 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> {
207207
let rhs_inv = rhs.inverse()?;
208208
Ok(self.mul(&rhs_inv))
209209
}
@@ -213,26 +213,26 @@ impl QM31Felt {
213213
}
214214
}
215215

216-
impl From<&QM31Felt> for Felt {
217-
fn from(value: &QM31Felt) -> Self {
216+
impl From<&QM31> for Felt {
217+
fn from(value: &QM31) -> Self {
218218
value.pack_into_felt()
219219
}
220220
}
221221

222-
impl From<QM31Felt> for Felt {
223-
fn from(value: QM31Felt) -> Self {
222+
impl From<QM31> for Felt {
223+
fn from(value: QM31) -> Self {
224224
value.pack_into_felt()
225225
}
226226
}
227227

228-
impl TryFrom<Felt> for QM31Felt {
228+
impl TryFrom<Felt> for QM31 {
229229
type Error = QM31Error;
230230

231231
fn try_from(value: Felt) -> Result<Self, Self::Error> {
232232
let limbs = value.to_le_digits();
233233

234234
// 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
236236
// Felt should represent a packed QM31 which is at most 144 bits long.
237237
if limbs[3] != 0 || limbs[2] >= 1 << 16 {
238238
return Err(QM31Error::FeltTooBig(value));
@@ -256,14 +256,14 @@ impl TryFrom<Felt> for QM31Felt {
256256
}
257257
}
258258

259-
impl TryFrom<&Felt> for QM31Felt {
259+
impl TryFrom<&Felt> for QM31 {
260260
type Error = QM31Error;
261261

262262
fn try_from(value: &Felt) -> Result<Self, Self::Error> {
263263
let limbs = value.to_le_digits();
264264

265265
// 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
267267
// Felt should represent a packed QM31 which is at most 144 bits long.
268268
if limbs[3] != 0 || limbs[2] >= 1 << 16 {
269269
return Err(QM31Error::FeltTooBig(*value));
@@ -298,38 +298,38 @@ mod test {
298298
};
299299

300300
use crate::felt::{
301-
qm31::{QM31Error, QM31Felt, STWO_PRIME},
301+
qm31::{QM31Error, QM31, STWO_PRIME},
302302
Felt,
303303
};
304304

305305
#[test]
306306
fn qm31_to_felt() {
307-
let coordinates = QM31Felt::from_raw([1, 2, 3, 4]);
307+
let coordinates = QM31::from_raw([1, 2, 3, 4]);
308308
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();
310310
assert_eq!(coordinates, unpacked_coordinates);
311311

312-
let qm31 = QM31Felt::from_raw([u64::MAX, 0, 0, 0]);
312+
let qm31 = QM31::from_raw([u64::MAX, 0, 0, 0]);
313313
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();
315315

316316
assert_eq!(felt_to_qm31, qm31);
317317

318-
let qm31 = QM31Felt::from_raw([u64::MAX, u64::MAX, 0, 0]);
318+
let qm31 = QM31::from_raw([u64::MAX, u64::MAX, 0, 0]);
319319
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();
321321

322322
assert_eq!(felt_to_qm31, qm31);
323323

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]);
325325
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();
327327

328328
assert_eq!(felt_to_qm31, qm31);
329329

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]);
331331
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();
333333

334334
assert_eq!(felt_to_qm31, qm31);
335335
}
@@ -339,7 +339,7 @@ mod test {
339339
let mut felt_bytes = [0u8; 32];
340340
felt_bytes[18] = 1;
341341
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();
343343
assert!(matches!(
344344
qm31,
345345
Err(QM31Error::FeltTooBig(bx)) if bx == felt
@@ -350,8 +350,8 @@ mod test {
350350
fn test_qm31_packed_reduced_add() {
351351
let x_coordinates = [1414213562, 1732050807, 1618033988, 1234567890];
352352
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);
355355
let res = x.add(&y);
356356
let res_coordinates = res.as_raw();
357357
assert_eq!(
@@ -368,7 +368,7 @@ mod test {
368368
#[test]
369369
fn test_qm31_packed_reduced_neg() {
370370
let x_coordinates = [1749652895, 834624081, 1930174752, 2063872165];
371-
let x = QM31Felt::from_raw(x_coordinates);
371+
let x = QM31::from_raw(x_coordinates);
372372
let res = x.neg();
373373
let res_coordinates = res.as_raw();
374374
assert_eq!(
@@ -391,8 +391,8 @@ mod test {
391391
(1234567890 + 1618033988) % STWO_PRIME,
392392
];
393393
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);
396396
let res = x.sub(&y);
397397
let res_coordinates = res.as_raw();
398398
assert_eq!(
@@ -405,8 +405,8 @@ mod test {
405405
fn test_qm31_packed_reduced_mul() {
406406
let x_coordinates = [1414213562, 1732050807, 1618033988, 1234567890];
407407
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);
410410
let res = x.mul(&y);
411411
let res_coordinates = res.as_raw();
412412
assert_eq!(
@@ -418,19 +418,19 @@ mod test {
418418
#[test]
419419
fn test_qm31_packed_reduced_inv() {
420420
let x_coordinates = [1259921049, 1442249570, 1847759065, 2094551481];
421-
let x = QM31Felt::from_raw(x_coordinates);
421+
let x = QM31::from_raw(x_coordinates);
422422
let res = x.inverse().unwrap();
423423
let expected = Felt::from(1).try_into().unwrap();
424424
assert_eq!(x.mul(&res), expected);
425425

426426
let x_coordinates = [1, 2, 3, 4];
427-
let x = QM31Felt::from_raw(x_coordinates);
427+
let x = QM31::from_raw(x_coordinates);
428428
let res = x.inverse().unwrap();
429429
let expected = Felt::from(1).try_into().unwrap();
430430
assert_eq!(x.mul(&res), expected);
431431

432432
let x_coordinates = [1749652895, 834624081, 1930174752, 2063872165];
433-
let x = QM31Felt::from_raw(x_coordinates);
433+
let x = QM31::from_raw(x_coordinates);
434434
let res = x.inverse().unwrap();
435435
let expected = Felt::from(1).try_into().unwrap();
436436
assert_eq!(x.mul(&res), expected);
@@ -447,10 +447,10 @@ mod test {
447447
.prop_filter("All configs cant be 0",
448448
|arr| !arr.iter().all(|x| *x == 0))
449449
) {
450-
let x = QM31Felt::from_raw(x_coordinates);
450+
let x = QM31::from_raw(x_coordinates);
451451
let res = x.inverse().unwrap();
452452
// Expect 1_felt252
453-
let expected = QM31Felt::from_raw([1,0,0,0]);
453+
let expected = QM31::from_raw([1,0,0,0]);
454454
assert_eq!(x.mul(&res), expected);
455455
}
456456

@@ -461,10 +461,10 @@ mod test {
461461
|arr| !arr.iter().all(|x| *x == 0))
462462
.no_shrink()
463463
) {
464-
let x = QM31Felt::from_raw(x_coordinates);
464+
let x = QM31::from_raw(x_coordinates);
465465
let res = x.inverse().unwrap();
466466
// Expect 1_felt252
467-
let expected = QM31Felt::from_raw([1,0,0,0]);
467+
let expected = QM31::from_raw([1,0,0,0]);
468468
assert_eq!(x.mul(&res), expected);
469469
}
470470
}
@@ -474,9 +474,9 @@ mod test {
474474
let x_coordinates = [1259921049, 1442249570, 1847759065, 2094551481];
475475
let y_coordinates = [1414213562, 1732050807, 1618033988, 1234567890];
476476
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);
480480

481481
let res = xy.div(&y).unwrap();
482482
assert_eq!(res, x);

0 commit comments

Comments
 (0)