| 
 | 1 | +use super::{EdwardsPoint, EdwardsScalar};  | 
 | 2 | +use crate::field::FieldElement;  | 
 | 3 | +use subtle::{Choice, ConditionallyNegatable, ConditionallySelectable, ConstantTimeEq};  | 
 | 4 | + | 
 | 5 | +pub(super) fn scalar_mul(point: &EdwardsPoint, scalar: &EdwardsScalar) -> EdwardsPoint {  | 
 | 6 | +    let mut result = ExtensiblePoint::IDENTITY;  | 
 | 7 | + | 
 | 8 | +    // Recode Scalar  | 
 | 9 | +    let scalar = scalar.to_radix_16();  | 
 | 10 | + | 
 | 11 | +    let lookup = LookupTable::from(point);  | 
 | 12 | + | 
 | 13 | +    for i in (0..113).rev() {  | 
 | 14 | +        result = result.double();  | 
 | 15 | +        result = result.double();  | 
 | 16 | +        result = result.double();  | 
 | 17 | +        result = result.double();  | 
 | 18 | + | 
 | 19 | +        // The mask is the top bit, will be 1 for negative numbers, 0 for positive numbers  | 
 | 20 | +        let mask = scalar[i] >> 7;  | 
 | 21 | +        let sign = mask & 0x1;  | 
 | 22 | +        // Use the mask to get the absolute value of scalar  | 
 | 23 | +        let abs_value = ((scalar[i] + mask) ^ mask) as u32;  | 
 | 24 | + | 
 | 25 | +        let mut neg_P = lookup.select(abs_value);  | 
 | 26 | +        neg_P.conditional_negate(Choice::from((sign) as u8));  | 
 | 27 | + | 
 | 28 | +        result = (EdwardsPoint::from(result) + neg_P).into();  | 
 | 29 | +    }  | 
 | 30 | + | 
 | 31 | +    result.into()  | 
 | 32 | +}  | 
 | 33 | + | 
 | 34 | +struct ExtensiblePoint {  | 
 | 35 | +    X: FieldElement,  | 
 | 36 | +    Y: FieldElement,  | 
 | 37 | +    Z: FieldElement,  | 
 | 38 | +    T1: FieldElement,  | 
 | 39 | +    T2: FieldElement,  | 
 | 40 | +}  | 
 | 41 | + | 
 | 42 | +impl ExtensiblePoint {  | 
 | 43 | +    const IDENTITY: ExtensiblePoint = ExtensiblePoint {  | 
 | 44 | +        X: FieldElement::ZERO,  | 
 | 45 | +        Y: FieldElement::ONE,  | 
 | 46 | +        Z: FieldElement::ONE,  | 
 | 47 | +        T1: FieldElement::ZERO,  | 
 | 48 | +        T2: FieldElement::ONE,  | 
 | 49 | +    };  | 
 | 50 | + | 
 | 51 | +    fn double(&self) -> Self {  | 
 | 52 | +        let A = self.X.square();  | 
 | 53 | +        let B = self.Y.square();  | 
 | 54 | +        let C = self.Z.square().double();  | 
 | 55 | +        let D = A;  | 
 | 56 | +        let E = (self.X + self.Y).square() - A - B;  | 
 | 57 | +        let G = D + B;  | 
 | 58 | +        let F = G - C;  | 
 | 59 | +        let H = D - B;  | 
 | 60 | +        Self {  | 
 | 61 | +            X: E * F,  | 
 | 62 | +            Y: G * H,  | 
 | 63 | +            Z: F * G,  | 
 | 64 | +            T1: E,  | 
 | 65 | +            T2: H,  | 
 | 66 | +        }  | 
 | 67 | +    }  | 
 | 68 | +}  | 
 | 69 | + | 
 | 70 | +impl From<ExtensiblePoint> for EdwardsPoint {  | 
 | 71 | +    fn from(value: ExtensiblePoint) -> Self {  | 
 | 72 | +        Self {  | 
 | 73 | +            X: value.X,  | 
 | 74 | +            Y: value.Y,  | 
 | 75 | +            Z: value.Z,  | 
 | 76 | +            T: value.T1 * value.T2,  | 
 | 77 | +        }  | 
 | 78 | +    }  | 
 | 79 | +}  | 
 | 80 | + | 
 | 81 | +impl From<EdwardsPoint> for ExtensiblePoint {  | 
 | 82 | +    fn from(value: EdwardsPoint) -> Self {  | 
 | 83 | +        Self {  | 
 | 84 | +            X: value.X,  | 
 | 85 | +            Y: value.Y,  | 
 | 86 | +            Z: value.Z,  | 
 | 87 | +            T1: value.T,  | 
 | 88 | +            T2: FieldElement::ONE,  | 
 | 89 | +        }  | 
 | 90 | +    }  | 
 | 91 | +}  | 
 | 92 | + | 
 | 93 | +pub struct LookupTable([EdwardsPoint; 8]);  | 
 | 94 | + | 
 | 95 | +/// Precomputes odd multiples of the point passed in  | 
 | 96 | +impl From<&EdwardsPoint> for LookupTable {  | 
 | 97 | +    fn from(P: &EdwardsPoint) -> LookupTable {  | 
 | 98 | +        let mut table = [*P; 8];  | 
 | 99 | + | 
 | 100 | +        for i in 1..8 {  | 
 | 101 | +            table[i] = P + table[i - 1];  | 
 | 102 | +        }  | 
 | 103 | + | 
 | 104 | +        LookupTable(table)  | 
 | 105 | +    }  | 
 | 106 | +}  | 
 | 107 | + | 
 | 108 | +impl LookupTable {  | 
 | 109 | +    /// Selects a projective niels point from a lookup table in constant time  | 
 | 110 | +    pub fn select(&self, index: u32) -> EdwardsPoint {  | 
 | 111 | +        let mut result = EdwardsPoint::IDENTITY;  | 
 | 112 | + | 
 | 113 | +        for i in 1..9 {  | 
 | 114 | +            let swap = index.ct_eq(&(i as u32));  | 
 | 115 | +            result.conditional_assign(&self.0[i - 1], swap);  | 
 | 116 | +        }  | 
 | 117 | +        result  | 
 | 118 | +    }  | 
 | 119 | +}  | 
0 commit comments