Elliptic Curve Cryptography (ECC) là nền tảng của nhiều hệ mật mã hiện đại, từ Bitcoin đến TLS.
Elliptic Curve Cryptography (ECC) underpins many modern cryptographic systems, from Bitcoin to TLS.
// Defining Elliptic Curves
Một đường cong elliptic trên trường thực có dạng Weierstrass:
An elliptic curve over the reals takes the Weierstrass form:
Với điều kiện discriminant khác 0:
With non-zero discriminant:
// Point Addition
Cho $P \neq Q$ trên đường cong, công thức cộng điểm:
Given $P \neq Q$ on the curve, the addition formula:
Point doubling ($P = Q$):
// Curves over Finite Fields
Trong cryptography, ta làm việc trên $\mathbb{F}_p$:
In cryptography, we work over $\mathbb{F}_p$:
Hasse's theorem:
// ECDLP
Cho $G$ (generator) và $Q = kG$, tìm $k$ là bài toán ECDLP. Thuật toán tốt nhất: $O(\sqrt{n})$.
Given $G$ (generator) and $Q = kG$, finding $k$ is the ECDLP. Best known: $O(\sqrt{n})$.
// ECDH Key Exchange
// secp256k1 (Bitcoin)
// secp256k1: y² = x³ + 7 over F_p
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
a = 0
b = 7
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
text
// Rust Implementation
use num_bigint::BigUint;
use num_traits::{One, Zero};
#[derive(Clone, Debug)]
struct Point {
x: Option<BigUint>,
y: Option<BigUint>,
}
struct EllipticCurve {
a: BigUint,
b: BigUint,
p: BigUint,
}
impl EllipticCurve {
/// Scalar multiplication: k * P using double-and-add
fn scalar_mul(&self, k: &BigUint, p: &Point) -> Point {
let mut result = Point { x: None, y: None };
let mut temp = p.clone();
let mut n = k.clone();
while !n.is_zero() {
if &n % 2u32 == BigUint::one() {
result = self.add(&result, &temp);
}
temp = self.add(&temp, &temp);
n >>= 1;
}
result
}
}
rust
// bình luận (đăng nhập GitHub) // comments (login with GitHub)