Skip to content

Commit cf7b099

Browse files
authored
curve: rename FieldElement*::as_bytes => ::to_bytes (#767)
* curve: rename `FieldElement*::as_bytes` => `::to_bytes` Methods named `as_*` should perform a zero-cost borrowing conversion: https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv Methods named `to_*` can perform an expensive owned conversion. Since the `FieldElement*` types are technically part of the public API (but feature gated), this also preserves the old names with a deprecation. We can remove them in the next breaking release. The same change was also made to the backend `Scalar*` types, however these types are not a part of the public API.
1 parent ad4a37d commit cf7b099

File tree

12 files changed

+50
-29
lines changed

12 files changed

+50
-29
lines changed

curve25519-dalek-derive/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,7 @@ fn process_mod(
203203
};
204204

205205
let feature = feature.value();
206-
if !spec_features
207-
.iter()
208-
.any(|enabled_feature| feature == *enabled_feature)
209-
{
206+
if !spec_features.contains(&feature) {
210207
*item = syn::Item::Verbatim(Default::default());
211208
continue 'next_item;
212209
}

curve25519-dalek/src/backend/serial/fiat_u32/field.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,15 @@ impl FieldElement2625 {
239239
FieldElement2625(output)
240240
}
241241

242+
/// Renamed to `to_bytes`.
243+
#[deprecated(since = "4.1.4", note = "use `to_bytes` instead")]
244+
pub fn as_bytes(&self) -> [u8; 32] {
245+
self.to_bytes()
246+
}
247+
242248
/// Serialize this `FieldElement51` to a 32-byte array. The
243249
/// encoding is canonical.
244-
pub fn as_bytes(&self) -> [u8; 32] {
250+
pub fn to_bytes(self) -> [u8; 32] {
245251
let mut bytes = [0u8; 32];
246252
fiat_25519_to_bytes(&mut bytes, &self.0);
247253
bytes

curve25519-dalek/src/backend/serial/fiat_u64/field.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,15 @@ impl FieldElement51 {
216216
FieldElement51(output)
217217
}
218218

219+
/// Renamed to `to_bytes`.
220+
#[deprecated(since = "4.1.4", note = "use `to_bytes` instead")]
221+
pub fn as_bytes(&self) -> [u8; 32] {
222+
self.to_bytes()
223+
}
224+
219225
/// Serialize this `FieldElement51` to a 32-byte array. The
220226
/// encoding is canonical.
221-
pub fn as_bytes(&self) -> [u8; 32] {
227+
pub fn to_bytes(self) -> [u8; 32] {
222228
let mut bytes = [0u8; 32];
223229
fiat_25519_to_bytes(&mut bytes, &self.0);
224230
bytes

curve25519-dalek/src/backend/serial/u32/field.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,16 @@ impl FieldElement2625 {
428428
FieldElement2625::reduce(h)
429429
}
430430

431+
/// Renamed to `to_bytes`.
432+
#[deprecated(since = "4.1.4", note = "use `to_bytes` instead")]
433+
pub fn as_bytes(&self) -> [u8; 32] {
434+
self.to_bytes()
435+
}
436+
431437
/// Serialize this `FieldElement51` to a 32-byte array. The
432438
/// encoding is canonical.
433439
#[allow(clippy::identity_op)]
434-
pub fn as_bytes(&self) -> [u8; 32] {
440+
pub fn to_bytes(self) -> [u8; 32] {
435441
let inp = &self.0;
436442
// Reduce the value represented by `in` to the range [0,2*p)
437443
let mut h: [u32; 10] = FieldElement2625::reduce([

curve25519-dalek/src/backend/serial/u32/scalar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Scalar29 {
129129
/// Pack the limbs of this `Scalar29` into 32 bytes.
130130
#[rustfmt::skip] // keep alignment of s[*] calculations
131131
#[allow(clippy::identity_op)]
132-
pub fn as_bytes(&self) -> [u8; 32] {
132+
pub fn to_bytes(self) -> [u8; 32] {
133133
let mut s = [0u8; 32];
134134

135135
s[ 0] = (self.0[0] >> 0) as u8;

curve25519-dalek/src/backend/serial/u64/field.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,16 @@ impl FieldElement51 {
362362
])
363363
}
364364

365+
/// Renamed to `to_bytes`.
366+
#[deprecated(since = "4.1.4", note = "use `to_bytes` instead")]
367+
pub fn as_bytes(&self) -> [u8; 32] {
368+
self.to_bytes()
369+
}
370+
365371
/// Serialize this `FieldElement51` to a 32-byte array. The
366372
/// encoding is canonical.
367373
#[rustfmt::skip] // keep alignment of s[*] calculations
368-
pub fn as_bytes(&self) -> [u8; 32] {
374+
pub fn to_bytes(self) -> [u8; 32] {
369375
// Let h = limbs[0] + limbs[1]*2^51 + ... + limbs[4]*2^204.
370376
//
371377
// Write h = pq + r with 0 <= r < p.

curve25519-dalek/src/backend/serial/u64/scalar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Scalar52 {
118118
/// Pack the limbs of this `Scalar52` into 32 bytes
119119
#[rustfmt::skip] // keep alignment of s[*] calculations
120120
#[allow(clippy::identity_op)]
121-
pub fn as_bytes(&self) -> [u8; 32] {
121+
pub fn to_bytes(self) -> [u8; 32] {
122122
let mut s = [0u8; 32];
123123

124124
s[ 0] = (self.0[ 0] >> 0) as u8;

curve25519-dalek/src/edwards.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl EdwardsPoint {
560560
let U = &self.Z + &self.Y;
561561
let W = &self.Z - &self.Y;
562562
let u = &U * &W.invert();
563-
MontgomeryPoint(u.as_bytes())
563+
MontgomeryPoint(u.to_bytes())
564564
}
565565

566566
/// Converts a large batch of points to Edwards at once. This has the same
@@ -579,7 +579,7 @@ impl EdwardsPoint {
579579
let mut ret = Vec::with_capacity(eds.len());
580580
for (ed, d) in eds.iter().zip(denominators.iter()) {
581581
let u = &(&ed.Z + &ed.Y) * d;
582-
ret.push(MontgomeryPoint(u.as_bytes()));
582+
ret.push(MontgomeryPoint(u.to_bytes()));
583583
}
584584

585585
ret
@@ -614,7 +614,7 @@ impl EdwardsPoint {
614614
/// Compress affine Edwards coordinates into `CompressedEdwardsY` format.
615615
#[inline]
616616
fn compress_affine(x: FieldElement, y: FieldElement) -> CompressedEdwardsY {
617-
let mut s = y.as_bytes();
617+
let mut s = y.to_bytes();
618618
s[31] ^= x.is_negative().unwrap_u8() << 7;
619619
CompressedEdwardsY(s)
620620
}

curve25519-dalek/src/field.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl ConstantTimeEq for FieldElement {
8686
/// internal representation is not canonical, the field elements
8787
/// are normalized to wire format before comparison.
8888
fn ct_eq(&self, other: &FieldElement) -> Choice {
89-
self.as_bytes().ct_eq(&other.as_bytes())
89+
self.to_bytes().ct_eq(&other.to_bytes())
9090
}
9191
}
9292

@@ -99,7 +99,7 @@ impl FieldElement {
9999
///
100100
/// If negative, return `Choice(1)`. Otherwise, return `Choice(0)`.
101101
pub(crate) fn is_negative(&self) -> Choice {
102-
let bytes = self.as_bytes();
102+
let bytes = self.to_bytes();
103103
(bytes[0] & 1).into()
104104
}
105105

@@ -110,7 +110,7 @@ impl FieldElement {
110110
/// If zero, return `Choice(1)`. Otherwise, return `Choice(0)`.
111111
pub(crate) fn is_zero(&self) -> Choice {
112112
let zero = [0u8; 32];
113-
let bytes = self.as_bytes();
113+
let bytes = self.to_bytes();
114114

115115
bytes.ct_eq(&zero)
116116
}
@@ -480,7 +480,7 @@ mod test {
480480
// Decode to a field element
481481
let one = FieldElement::from_bytes(&one_encoded_wrongly_bytes);
482482
// .. then check that the encoding is correct
483-
let one_bytes = one.as_bytes();
483+
let one_bytes = one.to_bytes();
484484
assert_eq!(one_bytes[0], 1);
485485
for byte in &one_bytes[1..] {
486486
assert_eq!(*byte, 0);

curve25519-dalek/src/montgomery.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl Hash for MontgomeryPoint {
104104
fn hash<H: Hasher>(&self, state: &mut H) {
105105
// Do a round trip through a `FieldElement`. `as_bytes` is guaranteed to give a canonical
106106
// 32-byte encoding
107-
let canonical_bytes = FieldElement::from_bytes(&self.0).as_bytes();
107+
let canonical_bytes = FieldElement::from_bytes(&self.0).to_bytes();
108108
canonical_bytes.hash(state);
109109
}
110110
}
@@ -245,7 +245,7 @@ impl MontgomeryPoint {
245245

246246
let y = &(&u - &one) * &(&u + &one).invert();
247247

248-
let mut y_bytes = y.as_bytes();
248+
let mut y_bytes = y.to_bytes();
249249
y_bytes[31] ^= sign << 7;
250250

251251
CompressedEdwardsY(y_bytes).decompress()
@@ -278,7 +278,7 @@ pub(crate) fn elligator_encode(r_0: &FieldElement) -> MontgomeryPoint {
278278
let mut u = &d + &Atemp; /* d, or d+A if nonsquare */
279279
u.conditional_negate(!eps_is_sq); /* d, or -d-A if nonsquare */
280280

281-
MontgomeryPoint(u.as_bytes())
281+
MontgomeryPoint(u.to_bytes())
282282
}
283283

284284
/// A `ProjectivePoint` holds a point on the projective line
@@ -327,7 +327,7 @@ impl ProjectivePoint {
327327
/// * \\( 0 \\) if \\( W \eq 0 \\);
328328
pub fn as_affine(&self) -> MontgomeryPoint {
329329
let u = &self.U * &self.W.invert();
330-
MontgomeryPoint(u.as_bytes())
330+
MontgomeryPoint(u.to_bytes())
331331
}
332332
}
333333

@@ -498,14 +498,14 @@ mod test {
498498
let one = FieldElement::ONE;
499499

500500
// u = 2 corresponds to a point on the twist.
501-
let two = MontgomeryPoint((&one + &one).as_bytes());
501+
let two = MontgomeryPoint((&one + &one).to_bytes());
502502

503503
assert!(two.to_edwards(0).is_none());
504504

505505
// u = -1 corresponds to a point on the twist, but should be
506506
// checked explicitly because it's an exceptional point for the
507507
// birational map. For instance, libsignal will accept it.
508-
let minus_one = MontgomeryPoint((-&one).as_bytes());
508+
let minus_one = MontgomeryPoint((-&one).to_bytes());
509509

510510
assert!(minus_one.to_edwards(0).is_none());
511511
}

0 commit comments

Comments
 (0)