diff --git a/src/scalar.rs b/src/scalar.rs index bb0864d..0878c5a 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -95,6 +95,12 @@ impl Scalar { s } + /// Construct a `Scalar` by reducing a 512-bit little-endian integer + /// modulo the group order \\( \ell \\). + pub fn from_bytes_mod_order_wide(input: &[u8; 64]) -> Scalar { + UnpackedScalar::from_bytes_wide(input).pack() + } + /// Attempt to construct a `Scalar` from a canonical byte representation. /// /// # Return @@ -318,7 +324,7 @@ impl Scalar { pub fn random(rng: &mut T) -> Self { let mut scalar_bytes = [0u8; 64]; rng.fill_bytes(&mut scalar_bytes); - Scalar::reduce_wide(&scalar_bytes) + Scalar::from_bytes_mod_order_wide(&scalar_bytes) } /// Hash a slice of bytes into a scalar. @@ -363,7 +369,7 @@ impl Scalar { // XXX this seems clumsy let mut output = [0u8; 64]; output.copy_from_slice(hash.result().as_slice()); - Scalar::reduce_wide(&output) + Scalar::from_bytes_mod_order_wide(&output) } /// Convert this `Scalar` to its underlying sequence of bytes. @@ -540,11 +546,6 @@ impl Scalar { pub fn is_canonical(&self) -> bool { *self == self.reduce() } - - /// Reduce a 512-bit little endian number mod l - pub fn reduce_wide(input: &[u8; 64]) -> Scalar { - UnpackedScalar::from_bytes_wide(input).pack() - } } impl UnpackedScalar { @@ -721,11 +722,11 @@ mod test { // also_a = (a mod l) tmp[0..32].copy_from_slice(&a_bytes[..]); - let also_a = Scalar::reduce_wide(&tmp); + let also_a = Scalar::from_bytes_mod_order_wide(&tmp); // also_b = (b mod l) tmp[0..32].copy_from_slice(&b_bytes[..]); - let also_b = Scalar::reduce_wide(&tmp); + let also_b = Scalar::from_bytes_mod_order_wide(&tmp); let expected_c = &a * &b; let also_expected_c = &also_a * &also_b; @@ -811,7 +812,7 @@ mod test { } #[test] - fn reduce_wide() { + fn from_bytes_mod_order_wide() { let mut bignum = [0u8; 64]; // set bignum = x + 2^256x for i in 0..32 { @@ -828,7 +829,7 @@ mod test { 28, 82, 31, 197, 100, 165, 192, 8, ], }; - let test_red = Scalar::reduce_wide(&bignum); + let test_red = Scalar::from_bytes_mod_order_wide(&bignum); for i in 0..32 { assert!(test_red[i] == reduced[i]); } @@ -863,7 +864,7 @@ mod test { } #[test] - fn montgomery_reduce_matches_reduce_wide() { + fn montgomery_reduce_matches_from_bytes_mod_order_wide() { let mut bignum = [0u8; 64]; // set bignum = x + 2^256x @@ -881,7 +882,7 @@ mod test { 28, 82, 31, 197, 100, 165, 192, 8 ], }; - let reduced = Scalar::reduce_wide(&bignum); + let reduced = Scalar::from_bytes_mod_order_wide(&bignum); // The reduced scalar should match the expected assert_eq!(reduced.bytes, expected.bytes);