dcrypt_common/
ec_common.rs

1//! Common elliptic curve operations
2
3#[cfg(feature = "std")]
4use std::vec::Vec;
5
6#[cfg(all(not(feature = "std"), feature = "alloc"))]
7extern crate alloc;
8
9#[cfg(all(not(feature = "std"), feature = "alloc"))]
10use alloc::vec::Vec;
11
12/// Point on an elliptic curve
13#[cfg(any(feature = "std", feature = "alloc"))]
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct Point {
16    pub x: Vec<u8>,
17    pub y: Vec<u8>,
18    pub z: Option<Vec<u8>>, // For projective coordinates (None for affine)
19}
20
21#[cfg(any(feature = "std", feature = "alloc"))]
22impl Point {
23    /// Create a new affine point (x, y)
24    pub fn new_affine(x: Vec<u8>, y: Vec<u8>) -> Self {
25        Self { x, y, z: None }
26    }
27
28    /// Create a new projective point (x, y, z)
29    pub fn new_projective(x: Vec<u8>, y: Vec<u8>, z: Vec<u8>) -> Self {
30        Self { x, y, z: Some(z) }
31    }
32
33    /// Check if this is the point at infinity
34    pub fn is_infinity(&self) -> bool {
35        match &self.z {
36            Some(z) => z.iter().all(|&b| b == 0),
37            None => self.x.is_empty() && self.y.is_empty(),
38        }
39    }
40}
41
42/// Elliptic curve parameters in short Weierstrass form: y^2 = x^3 + ax + b
43#[cfg(any(feature = "std", feature = "alloc"))]
44#[derive(Clone, Debug)]
45pub struct CurveParams {
46    /// The 'a' coefficient
47    pub a: Vec<u8>,
48
49    /// The 'b' coefficient
50    pub b: Vec<u8>,
51
52    /// The prime field modulus
53    pub p: Vec<u8>,
54
55    /// The order of the curve (number of points)
56    pub order: Vec<u8>,
57
58    /// The cofactor
59    pub cofactor: Vec<u8>,
60
61    /// Generator point
62    pub generator: Point,
63}