mirror of
https://github.com/saymrwulf/risc0-curve25519-dalek-source.git
synced 2026-09-10 21:00:41 +00:00
Some rustfmt fixes. I disagreed with all the other ones.
This commit is contained in:
parent
648f95887a
commit
674a00df5b
5 changed files with 64 additions and 59 deletions
34
src/curve.rs
34
src/curve.rs
|
|
@ -317,8 +317,9 @@ impl<'de> Deserialize<'de> for ExtendedPoint {
|
|||
where E: serde::de::Error
|
||||
{
|
||||
if v.len() == 32 {
|
||||
let arr32 = array_ref!(v,0,32); // &[u8;32] from &[u8]
|
||||
CompressedEdwardsY(*arr32).decompress()
|
||||
let arr32 = array_ref!(v, 0, 32); // &[u8;32] from &[u8]
|
||||
CompressedEdwardsY(*arr32)
|
||||
.decompress()
|
||||
.ok_or(serde::de::Error::custom("decompression failed"))
|
||||
} else {
|
||||
Err(serde::de::Error::invalid_length(v.len(), &self))
|
||||
|
|
@ -518,7 +519,7 @@ impl CTAssignable for ExtendedPoint {
|
|||
|
||||
impl CTEq for ExtendedPoint {
|
||||
fn ct_eq(&self, other: &ExtendedPoint) -> u8 {
|
||||
arrays_equal( self.compress_edwards().as_bytes(),
|
||||
arrays_equal(self.compress_edwards().as_bytes(),
|
||||
other.compress_edwards().as_bytes())
|
||||
}
|
||||
}
|
||||
|
|
@ -714,7 +715,7 @@ impl ExtendedPoint {
|
|||
// Addition and Subtraction
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
impl<'a,'b> Add<&'b ProjectiveNielsPoint> for &'a ExtendedPoint {
|
||||
impl<'a, 'b> Add<&'b ProjectiveNielsPoint> for &'a ExtendedPoint {
|
||||
type Output = CompletedPoint;
|
||||
|
||||
fn add(self, other: &'b ProjectiveNielsPoint) -> CompletedPoint {
|
||||
|
|
@ -735,7 +736,7 @@ impl<'a,'b> Add<&'b ProjectiveNielsPoint> for &'a ExtendedPoint {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a,'b> Sub<&'b ProjectiveNielsPoint> for &'a ExtendedPoint {
|
||||
impl<'a, 'b> Sub<&'b ProjectiveNielsPoint> for &'a ExtendedPoint {
|
||||
type Output = CompletedPoint;
|
||||
|
||||
fn sub(self, other: &'b ProjectiveNielsPoint) -> CompletedPoint {
|
||||
|
|
@ -756,7 +757,7 @@ impl<'a,'b> Sub<&'b ProjectiveNielsPoint> for &'a ExtendedPoint {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a,'b> Add<&'b AffineNielsPoint> for &'a ExtendedPoint {
|
||||
impl<'a, 'b> Add<&'b AffineNielsPoint> for &'a ExtendedPoint {
|
||||
type Output = CompletedPoint;
|
||||
|
||||
fn add(self, other: &'b AffineNielsPoint) -> CompletedPoint {
|
||||
|
|
@ -776,7 +777,7 @@ impl<'a,'b> Add<&'b AffineNielsPoint> for &'a ExtendedPoint {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a,'b> Sub<&'b AffineNielsPoint> for &'a ExtendedPoint {
|
||||
impl<'a, 'b> Sub<&'b AffineNielsPoint> for &'a ExtendedPoint {
|
||||
type Output = CompletedPoint;
|
||||
|
||||
fn sub(self, other: &'b AffineNielsPoint) -> CompletedPoint {
|
||||
|
|
@ -796,7 +797,7 @@ impl<'a,'b> Sub<&'b AffineNielsPoint> for &'a ExtendedPoint {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a,'b> Add<&'b ExtendedPoint> for &'a ExtendedPoint {
|
||||
impl<'a, 'b> Add<&'b ExtendedPoint> for &'a ExtendedPoint {
|
||||
type Output = ExtendedPoint;
|
||||
fn add(self, other: &'b ExtendedPoint) -> ExtendedPoint {
|
||||
(self + &other.to_projective_niels()).to_extended()
|
||||
|
|
@ -809,7 +810,7 @@ impl<'b> AddAssign<&'b ExtendedPoint> for ExtendedPoint {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a,'b> Sub<&'b ExtendedPoint> for &'a ExtendedPoint {
|
||||
impl<'a, 'b> Sub<&'b ExtendedPoint> for &'a ExtendedPoint {
|
||||
type Output = ExtendedPoint;
|
||||
fn sub(self, other: &'b ExtendedPoint) -> ExtendedPoint {
|
||||
(self - &other.to_projective_niels()).to_extended()
|
||||
|
|
@ -1193,8 +1194,9 @@ pub mod vartime {
|
|||
///
|
||||
/// A vector of `Scalar`s and a vector of `ExtendedPoints`. It is an
|
||||
/// error to call this function with two vectors of different lengths.
|
||||
pub fn k_fold_scalar_mult<'a,'b,I,J>(scalars: I, points: J) -> ExtendedPoint
|
||||
where I: IntoIterator<Item=&'a Scalar>, J: IntoIterator<Item=&'b ExtendedPoint>
|
||||
pub fn k_fold_scalar_mult<'a, 'b, I, J>(scalars: I, points: J) -> ExtendedPoint
|
||||
where I: IntoIterator<Item = &'a Scalar>,
|
||||
J: IntoIterator<Item = &'b ExtendedPoint>
|
||||
{
|
||||
//assert_eq!(scalars.len(), points.len());
|
||||
|
||||
|
|
@ -1652,7 +1654,7 @@ mod test {
|
|||
// CBOR apparently has two bytes of overhead for a 32-byte string.
|
||||
// Set the low byte of the compressed point to 1 to make it invalid.
|
||||
output[2] = 1;
|
||||
let parsed: Result<ExtendedPoint,_> = serde_cbor::from_slice(&output);
|
||||
let parsed: Result<ExtendedPoint, _> = serde_cbor::from_slice(&output);
|
||||
assert!(parsed.is_err());
|
||||
}
|
||||
}
|
||||
|
|
@ -1667,7 +1669,7 @@ mod bench {
|
|||
use test::Bencher;
|
||||
use constants;
|
||||
use super::*;
|
||||
use super::test::{A_SCALAR};
|
||||
use super::test::A_SCALAR;
|
||||
|
||||
#[bench]
|
||||
fn edwards_decompress(b: &mut Bencher) {
|
||||
|
|
@ -1734,21 +1736,21 @@ mod bench {
|
|||
fn projective_double_output_completed(b: &mut Bencher) {
|
||||
let p1 = constants::ED25519_BASEPOINT.to_projective();
|
||||
|
||||
b.iter(|| p1.double() );
|
||||
b.iter(|| p1.double());
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn extended_double_output_extended(b: &mut Bencher) {
|
||||
let p1 = constants::ED25519_BASEPOINT;
|
||||
|
||||
b.iter(|| p1.double() );
|
||||
b.iter(|| p1.double());
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn mult_by_cofactor(b: &mut Bencher) {
|
||||
let p1 = constants::ED25519_BASEPOINT;
|
||||
|
||||
b.iter(|| p1.mult_by_cofactor() );
|
||||
b.iter(|| p1.mult_by_cofactor());
|
||||
}
|
||||
|
||||
#[cfg(feature="basepoint_table_creation")]
|
||||
|
|
|
|||
25
src/decaf.rs
25
src/decaf.rs
|
|
@ -60,7 +60,7 @@ pub struct CompressedDecaf(pub [u8; 32]);
|
|||
/// The result of compressing a `DecafPoint`.
|
||||
impl CompressedDecaf {
|
||||
/// View this `CompressedDecaf` as an array of bytes.
|
||||
pub fn as_bytes<'a>(&'a self) -> &'a [u8;32] {
|
||||
pub fn as_bytes<'a>(&'a self) -> &'a [u8; 32] {
|
||||
&self.0
|
||||
}
|
||||
|
||||
|
|
@ -121,7 +121,7 @@ impl CompressedDecaf {
|
|||
|
||||
impl Identity for CompressedDecaf {
|
||||
fn identity() -> CompressedDecaf {
|
||||
CompressedDecaf([0u8;32])
|
||||
CompressedDecaf([0u8; 32])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -165,8 +165,9 @@ impl<'de> Deserialize<'de> for DecafPoint {
|
|||
where E: serde::de::Error
|
||||
{
|
||||
if v.len() == 32 {
|
||||
let arr32 = array_ref!(v,0,32); // &[u8;32] from &[u8]
|
||||
CompressedDecaf(*arr32).decompress()
|
||||
let arr32 = array_ref!(v, 0, 32); // &[u8;32] from &[u8]
|
||||
CompressedDecaf(*arr32)
|
||||
.decompress()
|
||||
.ok_or(serde::de::Error::custom("decompression failed"))
|
||||
} else {
|
||||
Err(serde::de::Error::invalid_length(v.len(), &self))
|
||||
|
|
@ -304,7 +305,7 @@ impl DecafPoint {
|
|||
let (tmp_is_nonzero_square, W) = tmp.invsqrt();
|
||||
// tmp should always be a square (why? related to being in the
|
||||
// image of the isogeny?)
|
||||
debug_assert_eq!( tmp_is_nonzero_square | tmp.is_zero(), 1u8 );
|
||||
debug_assert_eq!(tmp_is_nonzero_square | tmp.is_zero(), 1u8);
|
||||
|
||||
let xy = &T.square() * &(&W.square() * &(&TZ * &ZZ_plus_XX));
|
||||
let rotate = 1u8 & !(Y.is_nonzero() & xy.is_nonnegative_decaf());
|
||||
|
|
@ -398,7 +399,7 @@ impl DecafPoint {
|
|||
s *= &c;
|
||||
|
||||
// 6. Compute t <--- -c*N*(r-1)* ((a-2d)*e)^2 -1
|
||||
let a_minus_2d_e_sq = (&(&minus_one-&constants::d2)*&e).square();
|
||||
let a_minus_2d_e_sq = (&(&minus_one - &constants::d2) * &e).square();
|
||||
let c_N_r_minus_1 = &c * &(&N * &(&r + &minus_one));
|
||||
let t = &minus_one - &(&c_N_r_minus_1 * &a_minus_2d_e_sq);
|
||||
|
||||
|
|
@ -467,7 +468,8 @@ impl DecafPoint {
|
|||
/// ```
|
||||
///
|
||||
pub fn hash_from_bytes<D>(input: &[u8]) -> DecafPoint
|
||||
where D: Digest<OutputSize=U32> + Default {
|
||||
where D: Digest<OutputSize = U32> + Default
|
||||
{
|
||||
let mut hash = D::default();
|
||||
hash.input(input);
|
||||
DecafPoint::from_hash(hash)
|
||||
|
|
@ -479,7 +481,8 @@ impl DecafPoint {
|
|||
/// to stream data into the `Digest` than to pass a single byte
|
||||
/// slice.
|
||||
pub fn from_hash<D>(hash: D) -> DecafPoint
|
||||
where D: Digest<OutputSize=U32> + Default {
|
||||
where D: Digest<OutputSize = U32> + Default
|
||||
{
|
||||
// XXX this seems clumsy
|
||||
let mut output = [0u8; 32];
|
||||
output.copy_from_slice(hash.result().as_slice());
|
||||
|
|
@ -674,8 +677,9 @@ pub mod vartime {
|
|||
///
|
||||
/// A vector of `Scalar`s and a vector of `ExtendedPoints`. It is an
|
||||
/// error to call this function with two vectors of different lengths.
|
||||
pub fn k_fold_scalar_mult<'a,'b,I,J>(scalars: I, points: J) -> DecafPoint
|
||||
where I: IntoIterator<Item=&'a Scalar>, J: IntoIterator<Item=&'b DecafPoint>
|
||||
pub fn k_fold_scalar_mult<'a, 'b, I, J>(scalars: I, points: J) -> DecafPoint
|
||||
where I: IntoIterator<Item = &'a Scalar>,
|
||||
J: IntoIterator<Item = &'b DecafPoint>
|
||||
{
|
||||
let extended_points = points.into_iter().map(|P| &P.0);
|
||||
DecafPoint(curve::vartime::k_fold_scalar_mult(scalars, extended_points))
|
||||
|
|
@ -836,4 +840,3 @@ mod bench {
|
|||
b.iter(|| P.compress());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
10
src/field.rs
10
src/field.rs
|
|
@ -116,7 +116,7 @@ impl Index<usize> for FieldElement {
|
|||
|
||||
impl IndexMut<usize> for FieldElement {
|
||||
fn index_mut(&mut self, _index: usize) -> &mut Limb {
|
||||
&mut(self.0[_index])
|
||||
&mut (self.0[_index])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -456,7 +456,7 @@ impl FieldElement {
|
|||
|
||||
#[cfg(not(feature="radix_51"))]
|
||||
fn reduce(mut h: [i64; 10]) -> FieldElement { //FeCombine
|
||||
let mut c = [0i64;10];
|
||||
let mut c = [0i64; 10];
|
||||
|
||||
/*
|
||||
|h[0]| <= (1.1*1.1*2^52*(1+19+19+19+19)+1.1*1.1*2^50*(38+38+38+38+38))
|
||||
|
|
@ -709,7 +709,7 @@ impl FieldElement {
|
|||
// evidently 2^255 h10-2^255 q = 0.
|
||||
// Goal: Output h[0]+...+2^230 h[9].
|
||||
|
||||
let mut s = [0u8;32];
|
||||
let mut s = [0u8; 32];
|
||||
s[0] = (h[0] >> 0) as u8;
|
||||
s[1] = (h[0] >> 8) as u8;
|
||||
s[2] = (h[0] >> 16) as u8;
|
||||
|
|
@ -1263,7 +1263,7 @@ mod test {
|
|||
fn a_mul_a_vs_a_squared_constant() {
|
||||
let a = FieldElement::from_bytes(&A_BYTES);
|
||||
let asq = FieldElement::from_bytes(&ASQ_BYTES);
|
||||
assert_eq!(asq, &a*&a);
|
||||
assert_eq!(asq, &a * &a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -1381,7 +1381,7 @@ mod bench {
|
|||
#[bench]
|
||||
fn fieldelement_a_mul_a(b: &mut Bencher) {
|
||||
let a = FieldElement::from_bytes(&A_BYTES);
|
||||
b.iter(|| &a*&a);
|
||||
b.iter(|| &a * &a);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ impl Debug for Scalar {
|
|||
}
|
||||
}
|
||||
|
||||
impl Eq for Scalar{}
|
||||
impl Eq for Scalar {}
|
||||
impl PartialEq for Scalar {
|
||||
/// Test equality between two `Scalar`s.
|
||||
///
|
||||
|
|
@ -101,7 +101,7 @@ impl Index<usize> for Scalar {
|
|||
|
||||
impl IndexMut<usize> for Scalar {
|
||||
fn index_mut(&mut self, _index: usize) -> &mut u8 {
|
||||
&mut(self.0[_index])
|
||||
&mut (self.0[_index])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -218,7 +218,7 @@ impl<'de> Deserialize<'de> for Scalar {
|
|||
{
|
||||
if v.len() == 32 {
|
||||
// array_ref turns &[u8] into &[u8;32]
|
||||
Ok(Scalar(*array_ref!(v,0,32)))
|
||||
Ok(Scalar(*array_ref!(v, 0, 32)))
|
||||
} else {
|
||||
Err(serde::de::Error::invalid_length(v.len(), &self))
|
||||
}
|
||||
|
|
@ -270,7 +270,8 @@ impl Scalar {
|
|||
/// ```
|
||||
///
|
||||
pub fn hash_from_bytes<D>(input: &[u8]) -> Scalar
|
||||
where D: Digest<OutputSize = U64> + Default {
|
||||
where D: Digest<OutputSize = U64> + Default
|
||||
{
|
||||
let mut hash = D::default();
|
||||
hash.input(input);
|
||||
Scalar::from_hash(hash)
|
||||
|
|
@ -282,9 +283,10 @@ impl Scalar {
|
|||
/// to stream data into the `Digest` than to pass a single byte
|
||||
/// slice.
|
||||
pub fn from_hash<D>(hash: D) -> Scalar
|
||||
where D: Digest<OutputSize=U64> + Default {
|
||||
where D: Digest<OutputSize = U64> + Default
|
||||
{
|
||||
// XXX this seems clumsy
|
||||
let mut output = [0u8;64];
|
||||
let mut output = [0u8; 64];
|
||||
output.copy_from_slice(hash.result().as_slice());
|
||||
Scalar::reduce(&output)
|
||||
}
|
||||
|
|
@ -320,7 +322,7 @@ impl Scalar {
|
|||
}
|
||||
|
||||
/// Get the bits of the scalar.
|
||||
pub fn bits(&self) -> [i8;256] {
|
||||
pub fn bits(&self) -> [i8; 256] {
|
||||
let mut bits = [0i8; 256];
|
||||
for i in 0..256 {
|
||||
// As i runs from 0..256, the bottom 3 bits index the bit,
|
||||
|
|
@ -379,7 +381,7 @@ impl Scalar {
|
|||
|
||||
// Unpack a scalar into 12 21-bit limbs.
|
||||
fn unpack(&self) -> UnpackedScalar {
|
||||
let mask_21bits: i64 = (1 << 21) -1;
|
||||
let mask_21bits: i64 = (1 << 21) - 1;
|
||||
let mut a = UnpackedScalar([0i64; 12]);
|
||||
a[ 0] = mask_21bits & load3(&self.0[ 0..]) ;
|
||||
a[ 1] = mask_21bits & (load4(&self.0[ 2..]) >> 5);
|
||||
|
|
@ -504,7 +506,7 @@ impl Index<usize> for UnpackedScalar {
|
|||
|
||||
impl IndexMut<usize> for UnpackedScalar {
|
||||
fn index_mut(&mut self, _index: usize) -> &mut i64 {
|
||||
&mut(self.0[_index])
|
||||
&mut (self.0[_index])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -640,7 +642,7 @@ impl UnpackedScalar {
|
|||
fn reduce_limbs(mut limbs: &mut [i64; 24]) -> UnpackedScalar {
|
||||
#[inline]
|
||||
#[allow(dead_code)]
|
||||
fn do_reduction(limbs: &mut [i64; 24], i:usize) {
|
||||
fn do_reduction(limbs: &mut [i64; 24], i: usize) {
|
||||
limbs[i - 12] += limbs[i] * 666643;
|
||||
limbs[i - 11] += limbs[i] * 470296;
|
||||
limbs[i - 10] += limbs[i] * 654183;
|
||||
|
|
@ -662,7 +664,7 @@ impl UnpackedScalar {
|
|||
#[allow(dead_code)]
|
||||
/// Carry excess from the `i`-th limb into the `(i+1)`-th limb.
|
||||
/// Postcondition: `-2^20 <= limbs[i] < 2^20`.
|
||||
fn do_carry_centered(limbs: &mut [i64; 24], i:usize) {
|
||||
fn do_carry_centered(limbs: &mut [i64; 24], i: usize) {
|
||||
let carry: i64 = (limbs[i] + (1<<20)) >> 21;
|
||||
limbs[i+1] += carry;
|
||||
limbs[i ] -= carry << 21;
|
||||
|
|
@ -717,7 +719,6 @@ impl UnpackedScalar {
|
|||
|
||||
UnpackedScalar(*array_ref!(limbs, 0, 12))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -901,7 +902,7 @@ mod bench {
|
|||
|
||||
#[bench]
|
||||
fn scalar_multiply_add(b: &mut Bencher) {
|
||||
b.iter(|| Scalar::multiply_add(&X, &Y, &Z) );
|
||||
b.iter(|| Scalar::multiply_add(&X, &Y, &Z));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
|
@ -915,6 +916,6 @@ mod bench {
|
|||
let x = X.unpack();
|
||||
let y = Y.unpack();
|
||||
let z = Z.unpack();
|
||||
b.iter(|| UnpackedScalar::multiply_add(&x, &y, &z) );
|
||||
b.iter(|| UnpackedScalar::multiply_add(&x, &y, &z));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,8 +35,7 @@ pub trait CTEq {
|
|||
///
|
||||
/// Note: it is not necessary to implement this trait, as a generic
|
||||
/// implementation is provided.
|
||||
pub trait CTNegatable
|
||||
{
|
||||
pub trait CTNegatable {
|
||||
/// Conditionally negate an element if `choice == 1u8`.
|
||||
fn conditional_negate(&mut self, choice: u8);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue