origin-crypto-sdk 0.6.2

Standalone cryptographic SDK with classical (Ed25519) and post-quantum (Falcon, SLH-DSA, ML-DSA, NTRU Prime, Curve41417) primitives. Hybrid signing by default.
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// SPDX-License-Identifier: Apache-2.0

//! secp256k1 elliptic curve point operations.
//!
//! Supports both affine and projective (Jacobian) coordinates.
//! Uses Jacobian coordinates for efficient scalar multiplication.

use super::{FieldElement, Scalar};
use crate::internal::subtle::{Choice, ConstantTimeEq, CtOption};

/// Generator point X coordinate
const G_X: [u64; 4] = [
    0x79BE_667E_F9DC_BBAC,
    0x55A0_6295_CE87_0B07,
    0x029B_FCDB_2DCE_28D9,
    0x59F2_815B_16F8_1798,
];

/// Generator point Y coordinate
const G_Y: [u64; 4] = [
    0x483A_DA77_26A3_C465,
    0x5DA4_FBFC_0E11_08A8,
    0xFD17_B448_A685_5419,
    0x9C47_D08F_FB10_D4B8,
];

/// Curve coefficient b = 7
const CURVE_B: FieldElement = FieldElement::from_limbs([
    0x0000_0000_0000_0007,
    0x0000_0000_0000_0000,
    0x0000_0000_0000_0000,
    0x0000_0000_0000_0000,
]);

/// Affine point (x, y) on secp256k1
#[derive(Clone, Copy, Debug)]
pub struct AffinePoint {
    pub(crate) x: FieldElement,
    pub(crate) y: FieldElement,
    infinity: bool,
}

/// Projective/Jacobian point (X, Y, Z) where:
/// - x = X / Z^2
/// - y = Y / Z^3
#[derive(Clone, Copy, Debug)]
pub struct ProjectivePoint {
    x: FieldElement,
    y: FieldElement,
    z: FieldElement,
}

/// Encoded point in SEC1 format (compressed or uncompressed)
#[derive(Clone, Debug)]
pub struct EncodedPoint {
    bytes: Vec<u8>,
}

impl AffinePoint {
    /// Point at infinity (identity)
    pub const IDENTITY: Self = Self {
        x: FieldElement::ZERO,
        y: FieldElement::ZERO,
        infinity: true,
    };

    /// Generator point G
    pub const GENERATOR: Self = Self {
        x: FieldElement::from_limbs(G_X),
        y: FieldElement::from_limbs(G_Y),
        infinity: false,
    };

    /// Create from coordinates
    pub fn from_coordinates(x: FieldElement, y: FieldElement) -> CtOption<Self> {
        let point = Self {
            x,
            y,
            infinity: false,
        };

        // Verify point is on curve: y^2 = x^3 + 7
        let y2 = y.mul(&y);
        let x3 = x.mul(&x).mul(&x);
        let rhs = x3.add(&CURVE_B);

        let on_curve = y2.ct_eq(&rhs);
        CtOption::new(point, on_curve)
    }

    /// Check if identity
    pub fn is_identity(&self) -> Choice {
        Choice::from_bool(self.infinity)
    }

    /// Convert to projective coordinates
    pub fn to_projective(&self) -> ProjectivePoint {
        if self.infinity {
            ProjectivePoint::IDENTITY
        } else {
            ProjectivePoint {
                x: self.x,
                y: self.y,
                z: FieldElement::ONE,
            }
        }
    }

    /// Decode from SEC1 format
    ///
    /// Uses reconstruction instead of validation for compressed points:
    /// since y is derived from x via sqrt(x³ + 7), the curve equation
    /// is satisfied by construction.
    pub fn from_encoded_point(encoded: &EncodedPoint) -> CtOption<Self> {
        let bytes = encoded.as_bytes();

        if bytes.is_empty() {
            return CtOption::new(Self::IDENTITY, Choice::from_bool(true));
        }

        match bytes[0] {
            0x00 => CtOption::new(Self::IDENTITY, Choice::from_bool(bytes.len() == 1)),
            0x02 | 0x03 => {
                // Compressed: 0x02 for even y, 0x03 for odd y
                if bytes.len() != 33 {
                    return CtOption::new(Self::IDENTITY, Choice::from_bool(false));
                }
                let mut x_bytes = [0u8; 32];
                x_bytes.copy_from_slice(&bytes[1..33]);
                let x = FieldElement::from_bytes(&x_bytes);

                // Reconstruct y from x: y^2 = x^3 + 7
                // The curve equation is satisfied by construction since
                // y = sqrt(x^3 + 7) implies y^2 = x^3 + 7
                let x3 = x.mul(&x).mul(&x);
                let y2 = x3.add(&CURVE_B);
                let y = y2.sqrt();

                // Check parity matches prefix
                let y_bytes = y.to_bytes();
                let is_odd = (y_bytes[31] & 1) == 1;
                let expected_odd = bytes[0] == 0x03;

                let y_final = if is_odd == expected_odd { y } else { y.neg() };

                // Reconstruct directly without redundant validation
                CtOption::new(
                    AffinePoint {
                        x,
                        y: y_final,
                        infinity: false,
                    },
                    Choice::from_bool(true),
                )
            }
            0x04 => {
                // Uncompressed: 0x04 || x || y
                // Validation is necessary here since y is provided externally
                if bytes.len() != 65 {
                    return CtOption::new(Self::IDENTITY, Choice::from_bool(false));
                }
                let mut x_bytes = [0u8; 32];
                let mut y_bytes = [0u8; 32];
                x_bytes.copy_from_slice(&bytes[1..33]);
                y_bytes.copy_from_slice(&bytes[33..65]);

                let x = FieldElement::from_bytes(&x_bytes);
                let y = FieldElement::from_bytes(&y_bytes);

                Self::from_coordinates(x, y)
            }
            _ => CtOption::new(Self::IDENTITY, Choice::from_bool(false)),
        }
    }

    /// Encode to SEC1 compressed format
    pub fn to_encoded_point(&self, compress: bool) -> EncodedPoint {
        if self.infinity {
            return EncodedPoint { bytes: vec![0x00] };
        }

        if compress {
            let x_bytes = self.x.to_bytes();
            let y_bytes = self.y.to_bytes();
            let is_odd = (y_bytes[31] & 1) == 1;
            let prefix = if is_odd { 0x03 } else { 0x02 };

            let mut bytes = Vec::with_capacity(33);
            bytes.push(prefix);
            bytes.extend_from_slice(&x_bytes);
            EncodedPoint { bytes }
        } else {
            let x_bytes = self.x.to_bytes();
            let y_bytes = self.y.to_bytes();

            let mut bytes = Vec::with_capacity(65);
            bytes.push(0x04);
            bytes.extend_from_slice(&x_bytes);
            bytes.extend_from_slice(&y_bytes);
            EncodedPoint { bytes }
        }
    }
}

impl ProjectivePoint {
    /// Point at infinity
    pub const IDENTITY: Self = Self {
        x: FieldElement::ONE,
        y: FieldElement::ONE,
        z: FieldElement::ZERO,
    };

    /// Generator point in projective form
    pub const GENERATOR: Self = Self {
        x: FieldElement::from_limbs(G_X),
        y: FieldElement::from_limbs(G_Y),
        z: FieldElement::ONE,
    };

    /// Check if identity (Z == 0)
    pub fn is_identity(&self) -> Choice {
        self.z.ct_eq(&FieldElement::ZERO)
    }

    /// Convert to affine coordinates
    pub fn to_affine(&self) -> AffinePoint {
        if self.is_identity().is_true() {
            return AffinePoint::IDENTITY;
        }

        let z_inv = self.z.invert();
        let z_inv2 = z_inv.mul(&z_inv);
        let z_inv3 = z_inv2.mul(&z_inv);

        let x = self.x.mul(&z_inv2);
        let y = self.y.mul(&z_inv3);

        AffinePoint {
            x,
            y,
            infinity: false,
        }
    }

    /// Add another projective point
    pub fn add(&self, other: &Self) -> Self {
        // Special cases with identity
        if other.is_identity().is_true() {
            return *self;
        }
        if self.is_identity().is_true() {
            return *other;
        }

        // Jacobian addition formula
        let z1z1 = self.z.mul(&self.z);
        let z2z2 = other.z.mul(&other.z);

        let u1 = self.x.mul(&z2z2);
        let u2 = other.x.mul(&z1z1);

        let s1 = self.y.mul(&other.z).mul(&z2z2);
        let s2 = other.y.mul(&self.z).mul(&z1z1);

        let h = u2.sub(&u1);
        let i = h.mul(&h).mul(&FieldElement::from_limbs([4, 0, 0, 0]));

        let j = h.mul(&i);
        let r = s2.sub(&s1).mul(&FieldElement::from_limbs([2, 0, 0, 0]));

        let v = u1.mul(&i);

        let x3 = r
            .mul(&r)
            .sub(&j)
            .sub(&v.mul(&FieldElement::from_limbs([2, 0, 0, 0])));
        let y3 = r
            .mul(&v.sub(&x3))
            .sub(&s1.mul(&j).mul(&FieldElement::from_limbs([2, 0, 0, 0])));
        let z3 = self.z.add(&other.z).mul(&h).sub(&z1z1).sub(&z2z2).mul(&h);

        Self {
            x: x3,
            y: y3,
            z: z3,
        }
    }

    /// Double this point
    pub fn double(&self) -> Self {
        if self.is_identity().is_true() || self.y.ct_eq(&FieldElement::ZERO).is_true() {
            return Self::IDENTITY;
        }

        // Jacobian doubling formula
        let xx = self.x.mul(&self.x);
        let yy = self.y.mul(&self.y);
        let yyyy = yy.mul(&yy);

        let s = self
            .x
            .add(&yy)
            .mul(&self.x.add(&yy))
            .sub(&xx)
            .sub(&yyyy)
            .mul(&FieldElement::from_limbs([2, 0, 0, 0]));

        let m = xx.mul(&FieldElement::from_limbs([3, 0, 0, 0]));

        let x3 = m
            .mul(&m)
            .sub(&s.mul(&FieldElement::from_limbs([2, 0, 0, 0])));
        let y3 = m
            .mul(&s.sub(&x3))
            .sub(&yyyy.mul(&FieldElement::from_limbs([8, 0, 0, 0])));
        let z3 = self
            .y
            .mul(&self.z)
            .mul(&FieldElement::from_limbs([2, 0, 0, 0]));

        Self {
            x: x3,
            y: y3,
            z: z3,
        }
    }

    /// Scalar multiplication: k * P
    pub fn mul(&self, scalar: &Scalar) -> Self {
        let mut result = Self::IDENTITY;
        let addend = *self;

        let scalar_bytes = scalar.to_bytes();

        for byte in scalar_bytes.iter() {
            for i in 0..8 {
                result = result.double();
                if ((byte >> (7 - i)) & 1) == 1 {
                    result = result.add(&addend);
                }
            }
        }

        result
    }

    /// Encode to SEC1 format
    pub fn to_encoded_point(&self, compress: bool) -> EncodedPoint {
        self.to_affine().to_encoded_point(compress)
    }
}

impl From<AffinePoint> for ProjectivePoint {
    fn from(affine: AffinePoint) -> Self {
        affine.to_projective()
    }
}

impl From<ProjectivePoint> for AffinePoint {
    fn from(projective: ProjectivePoint) -> Self {
        projective.to_affine()
    }
}

impl ConstantTimeEq for AffinePoint {
    fn ct_eq(&self, other: &Self) -> Choice {
        let both_identity = self.is_identity() & other.is_identity();
        let both_valid = (!self.is_identity()) & (!other.is_identity());
        let coords_eq = self.x.ct_eq(&other.x) & self.y.ct_eq(&other.y);

        both_identity | (both_valid & coords_eq)
    }
}

impl ConstantTimeEq for ProjectivePoint {
    fn ct_eq(&self, other: &Self) -> Choice {
        // Convert both to affine for comparison
        self.to_affine().ct_eq(&other.to_affine())
    }
}

impl core::ops::Mul<Scalar> for &ProjectivePoint {
    type Output = ProjectivePoint;
    fn mul(self, rhs: Scalar) -> Self::Output {
        self.mul(&rhs)
    }
}

impl core::ops::Mul<&Scalar> for &ProjectivePoint {
    type Output = ProjectivePoint;
    fn mul(self, rhs: &Scalar) -> Self::Output {
        self.mul(rhs)
    }
}

impl EncodedPoint {
    /// Create from bytes
    pub fn from_bytes(bytes: &[u8]) -> CtOption<Self> {
        if bytes.is_empty() {
            return CtOption::new(Self { bytes: vec![] }, Choice::from_bool(false));
        }

        let valid = match bytes[0] {
            0x00 => bytes.len() == 1,
            0x02 | 0x03 => bytes.len() == 33,
            0x04 => bytes.len() == 65,
            _ => false,
        };

        CtOption::new(
            Self {
                bytes: bytes.to_vec(),
            },
            Choice::from_bool(valid),
        )
    }

    /// Get byte representation
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }
}