Replace unnecessary moves of Copy types with immutable references

This significantly reduces the size of the stack.
This commit is contained in:
Jack Grigg 2019-04-30 09:23:51 +01:00
parent 8f6d6298d0
commit 085e8ae675
No known key found for this signature in database
GPG Key ID: 9E8255172BBF9898
6 changed files with 39 additions and 33 deletions

View File

@ -207,7 +207,7 @@ impl Fq {
/// Attempts to convert a little-endian byte representation of
/// a field element into an element of `Fq`, failing if the input
/// is not canonical (is not smaller than q).
pub fn from_bytes(bytes: [u8; 32]) -> CtOption<Fq> {
pub fn from_bytes(bytes: &[u8; 32]) -> CtOption<Fq> {
let mut tmp = Fq([0, 0, 0, 0]);
tmp.0[0] = LittleEndian::read_u64(&bytes[0..8]);
@ -251,7 +251,7 @@ impl Fq {
/// Converts a 512-bit little endian integer into
/// an element of Fq by reducing modulo q.
pub fn from_bytes_wide(bytes: [u8; 64]) -> Fq {
pub fn from_bytes_wide(bytes: &[u8; 64]) -> Fq {
Fq::from_u512([
LittleEndian::read_u64(&bytes[0..8]),
LittleEndian::read_u64(&bytes[8..16]),
@ -684,7 +684,7 @@ fn test_into_bytes() {
#[test]
fn test_from_bytes() {
assert_eq!(
Fq::from_bytes([
Fq::from_bytes(&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0
]).unwrap(),
@ -692,7 +692,7 @@ fn test_from_bytes() {
);
assert_eq!(
Fq::from_bytes([
Fq::from_bytes(&[
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0
]).unwrap(),
@ -700,7 +700,7 @@ fn test_from_bytes() {
);
assert_eq!(
Fq::from_bytes([
Fq::from_bytes(&[
254, 255, 255, 255, 1, 0, 0, 0, 2, 72, 3, 0, 250, 183, 132, 88, 245, 79, 188, 236, 239,
79, 140, 153, 111, 5, 197, 172, 89, 177, 36, 24
]).unwrap(),
@ -709,7 +709,7 @@ fn test_from_bytes() {
// -1 should work
assert!(
Fq::from_bytes([
Fq::from_bytes(&[
0, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115
]).is_some()
@ -718,7 +718,7 @@ fn test_from_bytes() {
// modulus is invalid
assert!(
Fq::from_bytes([
Fq::from_bytes(&[
1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115
]).is_none()
@ -727,21 +727,21 @@ fn test_from_bytes() {
// Anything larger than the modulus is invalid
assert!(
Fq::from_bytes([
Fq::from_bytes(&[
2, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115
]).is_none()
.unwrap_u8() == 1
);
assert!(
Fq::from_bytes([
Fq::from_bytes(&[
1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 58, 51, 72, 125, 157, 41, 83, 167, 237, 115
]).is_none()
.unwrap_u8() == 1
);
assert!(
Fq::from_bytes([
Fq::from_bytes(&[
1, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 116
]).is_none()
@ -789,7 +789,7 @@ fn test_from_u512_max() {
fn test_from_bytes_wide_r2() {
assert_eq!(
R2,
Fq::from_bytes_wide([
Fq::from_bytes_wide(&[
254, 255, 255, 255, 1, 0, 0, 0, 2, 72, 3, 0, 250, 183, 132, 88, 245, 79, 188, 236, 239,
79, 140, 153, 111, 5, 197, 172, 89, 177, 36, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -801,7 +801,7 @@ fn test_from_bytes_wide_r2() {
fn test_from_bytes_wide_negative_one() {
assert_eq!(
-&Fq::one(),
Fq::from_bytes_wide([
Fq::from_bytes_wide(&[
0, 0, 0, 0, 255, 255, 255, 255, 254, 91, 254, 255, 2, 164, 189, 83, 5, 216, 161, 9, 8,
216, 57, 51, 72, 125, 157, 41, 83, 167, 237, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -818,7 +818,7 @@ fn test_from_bytes_wide_maximum() {
0xda44ec81daf9a422,
0x5605aa601c162e79
]),
Fq::from_bytes_wide([0xff; 64])
Fq::from_bytes_wide(&[0xff; 64])
);
}

View File

@ -189,7 +189,7 @@ impl Fr {
/// Attempts to convert a little-endian byte representation of
/// a field element into an element of `Fr`, failing if the input
/// is not canonical (is not smaller than r).
pub fn from_bytes(bytes: [u8; 32]) -> CtOption<Fr> {
pub fn from_bytes(bytes: &[u8; 32]) -> CtOption<Fr> {
let mut tmp = Fr([0, 0, 0, 0]);
tmp.0[0] = LittleEndian::read_u64(&bytes[0..8]);
@ -233,7 +233,7 @@ impl Fr {
/// Converts a 512-bit little endian integer into
/// an element of Fr by reducing modulo r.
pub fn from_bytes_wide(bytes: [u8; 64]) -> Fr {
pub fn from_bytes_wide(bytes: &[u8; 64]) -> Fr {
Fr::from_u512([
LittleEndian::read_u64(&bytes[0..8]),
LittleEndian::read_u64(&bytes[8..16]),
@ -642,7 +642,7 @@ fn test_into_bytes() {
#[test]
fn test_from_bytes() {
assert_eq!(
Fr::from_bytes([
Fr::from_bytes(&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0
]).unwrap(),
@ -650,7 +650,7 @@ fn test_from_bytes() {
);
assert_eq!(
Fr::from_bytes([
Fr::from_bytes(&[
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0
]).unwrap(),
@ -658,7 +658,7 @@ fn test_from_bytes() {
);
assert_eq!(
Fr::from_bytes([
Fr::from_bytes(&[
217, 7, 150, 185, 179, 11, 248, 37, 80, 231, 182, 102, 47, 214, 21, 243, 244, 20, 136,
235, 238, 20, 37, 147, 198, 85, 145, 71, 111, 252, 166, 9
]).unwrap(),
@ -667,7 +667,7 @@ fn test_from_bytes() {
// -1 should work
assert!(
Fr::from_bytes([
Fr::from_bytes(&[
182, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
]).is_some()
@ -676,7 +676,7 @@ fn test_from_bytes() {
// modulus is invalid
assert!(
Fr::from_bytes([
Fr::from_bytes(&[
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
]).is_none()
@ -685,7 +685,7 @@ fn test_from_bytes() {
// Anything larger than the modulus is invalid
assert!(
Fr::from_bytes([
Fr::from_bytes(&[
184, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14
]).is_none()
@ -693,7 +693,7 @@ fn test_from_bytes() {
);
assert!(
Fr::from_bytes([
Fr::from_bytes(&[
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 104, 6, 169, 175, 51, 101, 234, 180, 125, 14
]).is_none()
@ -701,7 +701,7 @@ fn test_from_bytes() {
);
assert!(
Fr::from_bytes([
Fr::from_bytes(&[
183, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 15
]).is_none()
@ -749,7 +749,7 @@ fn test_from_u512_max() {
fn test_from_bytes_wide_r2() {
assert_eq!(
R2,
Fr::from_bytes_wide([
Fr::from_bytes_wide(&[
217, 7, 150, 185, 179, 11, 248, 37, 80, 231, 182, 102, 47, 214, 21, 243, 244, 20, 136,
235, 238, 20, 37, 147, 198, 85, 145, 71, 111, 252, 166, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -761,7 +761,7 @@ fn test_from_bytes_wide_r2() {
fn test_from_bytes_wide_negative_one() {
assert_eq!(
-&Fr::one(),
Fr::from_bytes_wide([
Fr::from_bytes_wide(&[
182, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204, 147, 32, 104, 166, 0, 59, 52,
1, 1, 59, 103, 6, 169, 175, 51, 101, 234, 180, 125, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -778,7 +778,7 @@ fn test_from_bytes_wide_maximum() {
0x6440c91261da51b3,
0xa5e07ffb20991cf
]),
Fr::from_bytes_wide([0xff; 64])
Fr::from_bytes_wide(&[0xff; 64])
);
}

View File

@ -176,13 +176,13 @@ impl From<AffinePoint> for ExtendedPoint {
}
}
impl From<ExtendedPoint> for AffinePoint {
impl<'a> From<&'a ExtendedPoint> for AffinePoint {
/// Constructs an affine point from an extended point
/// using the map `(U, V, Z, T1, T2) => (U/Z, V/Z)`
/// as Z is always nonzero. **This requires a field inversion
/// and so it is recommended to perform these in a batch
/// using [`batch_normalize`](crate::batch_normalize) instead.**
fn from(extended: ExtendedPoint) -> AffinePoint {
fn from(extended: &'a ExtendedPoint) -> AffinePoint {
// Z coordinate is always nonzero, so this is
// its inverse.
let zinv = extended.z.invert().unwrap();
@ -194,6 +194,12 @@ impl From<ExtendedPoint> for AffinePoint {
}
}
impl From<ExtendedPoint> for AffinePoint {
fn from(extended: ExtendedPoint) -> AffinePoint {
AffinePoint::from(&extended)
}
}
/// This is a pre-processed version of an affine point `(u, v)`
/// in the form `(v + u, v - u, u * v * 2d)`. This can be added to an
/// [`ExtendedPoint`](crate::ExtendedPoint).
@ -332,7 +338,7 @@ impl AffinePoint {
b[31] &= 0b0111_1111;
// Interpret what remains as the v-coordinate
Fq::from_bytes(b).and_then(|v| {
Fq::from_bytes(&b).and_then(|v| {
// -u^2 + v^2 = 1 + d.u^2.v^2
// -u^2 = 1 + d.u^2.v^2 - v^2 (rearrange)
// -u^2 - d.u^2.v^2 = 1 - v^2 (rearrange)

View File

@ -16,7 +16,7 @@ impl MyRandom for Fq {
fn new_random<T: RngCore>(rng: &mut T) -> Self {
let mut random_bytes = [0u8; 64];
rng.fill_bytes(&mut random_bytes);
Fq::from_bytes_wide(random_bytes)
Fq::from_bytes_wide(&random_bytes)
}
}
@ -24,6 +24,6 @@ impl MyRandom for Fr {
fn new_random<T: RngCore>(rng: &mut T) -> Self {
let mut random_bytes = [0u8; 64];
rng.fill_bytes(&mut random_bytes);
Fr::from_bytes_wide(random_bytes)
Fr::from_bytes_wide(&random_bytes)
}
}

View File

@ -8,7 +8,7 @@ fn test_to_and_from_bytes() {
let mut rng = new_rng();
for _ in 0..NUM_BLACK_BOX_CHECKS {
let a = Fq::new_random(&mut rng);
assert_eq!(a, Fq::from_bytes(Fq::into_bytes(&a)).unwrap());
assert_eq!(a, Fq::from_bytes(&Fq::into_bytes(&a)).unwrap());
}
}

View File

@ -8,7 +8,7 @@ fn test_to_and_from_bytes() {
let mut rng = new_rng();
for _ in 0..NUM_BLACK_BOX_CHECKS {
let a = Fr::new_random(&mut rng);
assert_eq!(a, Fr::from_bytes(Fr::into_bytes(&a)).unwrap());
assert_eq!(a, Fr::from_bytes(&Fr::into_bytes(&a)).unwrap());
}
}