dalek-cryptography / curve25519-dalek

A pure-Rust implementation of group operations on Ristretto and Curve25519
Other
886 stars 449 forks source link

Implement Default for MontgomeryPoint #207

Closed DebugSteven closed 5 years ago

DebugSteven commented 5 years ago

MontegomeryPoint should have an implementation of Default so values that have types associated with it can zero their values with clear instead of transmute on drop. This is related to https://github.com/dalek-cryptography/x25519-dalek/issues/9

hdevalence commented 5 years ago

This would be great! One decision to make is what the default value should be. For the other point types we define default() to return the identity point, but for MontgomeryPoints this isn't possible because the identity point is not representable in affine coordinates.

myrrlyn commented 5 years ago

The conversion source is:

/// Convert this `EdwardsPoint` on the Edwards model to the
/// corresponding `MontgomeryPoint` on the Montgomery model.
///
/// Note that this is a one-way conversion, since the Montgomery
/// model does not retain sign information.
pub fn to_montgomery(&self) -> MontgomeryPoint {
    // We have u = (1+y)/(1-y) = (Z+Y)/(Z-Y).
    //
    // The denominator is zero only when y=1, the identity point of
    // the Edwards curve.  Since 0.invert() = 0, in this case we
    // compute u = 0, the identity point of the Montgomery line.
    let U = &self.Z + &self.Y;
    let W = &self.Z - &self.Y;
    let u = &U * &W.invert();
    MontgomeryPoint(u.to_bytes())
}

I presume that the zero point ([0u8; 32]) is suitable both as a default value and, coincidentally enough, also the value most beloved for the Clear trait to use as the clobber.

hdevalence commented 5 years ago

Sorry for the delay, the comment I wrote in the quoted code isn't correct so I wanted to double-check the reasoning.

Backing up a little bit: the Montgomery u-coordinate 0 corresponds to the point (u,v) = (0,0), which is a 2-torsion point (= adding it to itself gives the identity), but isn't the identity point. The identity point on Montgomery curves isn't representable in affine coordinates (u,v) (it's "at infinity", so it requires projective coordinates). This is why the Edwards identity point (x,y) = (0,1) is exceptional for the conversion u = (1+y)/(1-y), where it gives denominator 0. The identity point of the Montgomery u-line (in projective coordinates) is (U : W) = (1 : 0), which is converted to the MontgomeryPoint with u = 0, which is a standard part of X25519 (see section 5.4 of https://arxiv.org/pdf/1703.01863v1.pdf , page 16, for more details).

So:

I can fix the comment and docstr for the to_montgomery function separately from this issue (or someone else can, if they want, but I'm happy to do it).