1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use openssl;
use openssl::ec::*;
use openssl::bn::*;
use std::ops::Add;
use std::ops::Sub;
use std::ops::Mul;

// currently hardcode curve to P256R1, but in the future probably a good idea
// to generalize the interface, and make it more generics (with generics for all crypto types)
pub const CURVE: openssl::nid::Nid = openssl::nid::X9_62_PRIME256V1;

pub struct Scalar {
    bn: BigNum,
}

pub struct Point {
    point: EcPoint,
}

#[derive(PartialEq)]
pub struct PrivateKey {
    pub scalar: Scalar,
}

#[derive(PartialEq)]
pub struct PublicKey {
    pub point: Point,
}

impl PublicKey {
    pub fn to_bytes(&self) -> Vec<u8> {
        self.point.to_bytes()
    }

    pub fn from_bytes(bytes: &[u8]) -> PublicKey {
        let mut ctx = BigNumContext::new().unwrap();
        PublicKey {
            point: Point {
                point: EcPoint::from_bytes(&get_grp(), bytes, &mut ctx)
                    .expect("Could not create PublicKey from bytes"),
            },
        }
    }
}

impl PrivateKey {
    // to_hex_str?? https://docs.rs/openssl/0.9.14/openssl/bn/struct.BigNum.html#method.to_hex_str
    pub fn to_bytes(&self) -> Vec<u8> {
        self.scalar.bn.to_vec()
    }

    pub fn from_bytes(bytes: &[u8]) -> PrivateKey {
        PrivateKey {
            scalar: Scalar {
                bn: BigNum::from_slice(bytes).expect("Could not create PrivateKey from bytes"),
            },
        }
    }
}

pub fn create_keypair() -> (PublicKey, PrivateKey) {
    let s = Scalar::generate();
    let p = Point::from_scalar(&s);
    return (PublicKey { point: p }, PrivateKey { scalar: s });
}

fn get_grp() -> EcGroup {
    return openssl::ec::EcGroup::from_curve_name(CURVE).unwrap();
}

fn get_order() -> BigNum {
    let mut ctx = BigNumContext::new().unwrap();
    let grp = openssl::ec::EcGroup::from_curve_name(CURVE).unwrap();
    let mut order = BigNum::new().unwrap();
    grp.order(&mut order, &mut ctx).unwrap();
    return order;
}

fn get_point_at_infinity() -> EcPoint {
    let mut ctx = BigNumContext::new().unwrap();
    let grp = openssl::ec::EcGroup::from_curve_name(CURVE).unwrap();
    let mut order = BigNum::new().unwrap();
    grp.order(&mut order, &mut ctx).unwrap();
    let mut p = EcPoint::new(&grp).unwrap();
    p.mul_generator(&grp, &order, &mut ctx).unwrap();
    return p;
}

fn curve_generator() -> EcPoint {
    let mut ctx = BigNumContext::new().unwrap();
    let grp = openssl::ec::EcGroup::from_curve_name(CURVE).unwrap();
    let pow = BigNum::from_u32(1).unwrap();
    let mut p = EcPoint::new(&grp).unwrap();
    p.mul_generator(&grp, &pow, &mut ctx).unwrap();
    return p;
}

impl Scalar {
    pub fn from_u32(v: u32) -> Scalar {
        let r = Scalar { bn: BigNum::from_u32(v).unwrap() };
        return r;
    }
    pub fn generate() -> Scalar {
        let order = get_order();
        let mut r = BigNum::new().unwrap();
        order.rand_range(&mut r).unwrap();
        return Scalar { bn: r };
    }

    pub fn multiplicative_identity() -> Scalar {
        return Self::from_u32(1);
    }

    pub fn hash_points(points: Vec<Point>) -> Scalar {
        let mut data = Vec::new();
        for p in points {
            data.extend_from_slice(p.to_bytes().as_slice());
        }
        let dig = openssl::sha::sha256(data.as_slice());
        let mut ctx = BigNumContext::new().unwrap();
        let order = get_order();
        let b = BigNum::from_slice(&dig).unwrap();
        let mut r = BigNum::new().unwrap();
        r.nnmod(&b, &order, &mut ctx).unwrap();
        return Scalar { bn: r };
    }


    pub fn pow(&self, pow: u32) -> Scalar {
        let mut ctx = BigNumContext::new().unwrap();
        let order = get_order();

        let mut r = BigNum::new().unwrap();
        let bn_pow = BigNum::from_u32(pow).unwrap();
        r.mod_exp(&self.bn, &bn_pow, &order, &mut ctx).unwrap();
        return Scalar { bn: r };
    }

    pub fn inverse(&self) -> Scalar {
        let mut ctx = BigNumContext::new().unwrap();
        let mut r = BigNum::new().unwrap();
        let order = get_order();
        r.mod_inverse(&self.bn, &order, &mut ctx).unwrap();
        return Scalar { bn: r };
    }
}

impl Clone for Scalar {
    fn clone(&self) -> Scalar {
        return Scalar { bn: BigNum::from_slice(&self.bn.to_vec()).unwrap() };
    }
}

impl Add for Scalar {
    type Output = Self;
    fn add(self, s: Self) -> Self {
        let mut ctx = BigNumContext::new().unwrap();
        let order = get_order();

        let mut r = BigNum::new().unwrap();
        r.mod_add(&self.bn, &s.bn, &order, &mut ctx).unwrap();
        return Scalar { bn: r };
    }
}

impl Sub for Scalar {
    type Output = Self;
    fn sub(self, s: Self) -> Self {
        let mut ctx = BigNumContext::new().unwrap();
        let order = get_order();

        let mut r = BigNum::new().unwrap();
        r.mod_sub(&self.bn, &s.bn, &order, &mut ctx).unwrap();
        return Scalar { bn: r };
    }
}

impl Mul for Scalar {
    type Output = Self;
    fn mul(self, s: Self) -> Self {
        let mut ctx = BigNumContext::new().unwrap();
        let order = get_order();

        let mut r = BigNum::new().unwrap();
        r.mod_mul(&self.bn, &s.bn, &order, &mut ctx).unwrap();
        return Scalar { bn: r };
    }
}

impl PartialEq for Scalar {
    fn eq(&self, other: &Self) -> bool {
        return self.bn.to_vec() == other.bn.to_vec();
    }
}

impl Point {
    pub fn infinity() -> Point {
        return Point { point: get_point_at_infinity() };
    }

    pub fn generator() -> Point {
        return Point { point: curve_generator() };
    }

    pub fn from_scalar(s: &Scalar) -> Point {
        let mut ctx = BigNumContext::new().unwrap();
        let grp = get_grp();
        let mut p = EcPoint::new(&grp).unwrap();
        p.mul_generator(&grp, &s.bn, &mut ctx).unwrap();
        return Point { point: p };
    }

    pub fn mul(&self, s: &Scalar) -> Point {
        let grp = get_grp();
        let mut ctx = BigNumContext::new().unwrap();
        let mut r = EcPoint::new(&grp).unwrap();
        r.mul(&grp, &self.point, &s.bn, &mut ctx).unwrap();
        return Point { point: r };
    }

    pub fn inverse(&self) -> Point {
        let grp = get_grp();
        let mut ctx = BigNumContext::new().unwrap();
        let bytes = self.point.to_bytes(&grp, POINT_CONVERSION_UNCOMPRESSED, &mut ctx).unwrap();
        let mut p = EcPoint::from_bytes(&grp, &bytes, &mut ctx).unwrap();
        p.invert(&grp, &mut ctx).unwrap();
        return Point { point: p };
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let grp = get_grp();
        let mut ctx = BigNumContext::new().unwrap();
        return self.point.to_bytes(&grp, POINT_CONVERSION_COMPRESSED, &mut ctx).unwrap();
    }
}

impl Clone for Point {
    fn clone(&self) -> Point {
        let mut ctx = BigNumContext::new().unwrap();
        let grp = get_grp();
        let bytes = self.point.to_bytes(&grp, POINT_CONVERSION_UNCOMPRESSED, &mut ctx).unwrap();
        return Point { point: EcPoint::from_bytes(&grp, &bytes, &mut ctx).unwrap() };
    }
}

impl Add for Point {
    type Output = Self;
    fn add(self, p: Self) -> Self {
        let grp = get_grp();
        let mut ctx = BigNumContext::new().unwrap();
        let mut r = EcPoint::new(&grp).unwrap();
        r.add(&grp, &self.point, &p.point, &mut ctx).unwrap();
        return Point { point: r };
    }
}
impl Sub for Point {
    type Output = Point;
    fn sub(self, p: Self) -> Self {
        let grp = get_grp();
        let mut ctx = BigNumContext::new().unwrap();
        let p_inv = p.inverse();
        let mut r = EcPoint::new(&grp).unwrap();
        r.add(&grp, &self.point, &p_inv.point, &mut ctx).unwrap();
        return Point { point: r };
    }
}

impl PartialEq for Point {
    fn eq(&self, other: &Self) -> bool {
        let mut ctx = BigNumContext::new().unwrap();
        let grp = get_grp();
        let b1 = self.point.to_bytes(&grp, POINT_CONVERSION_UNCOMPRESSED, &mut ctx).unwrap();
        let b2 = other.point.to_bytes(&grp, POINT_CONVERSION_UNCOMPRESSED, &mut ctx).unwrap();
        return b1 == b2;
    }
}