Skip to main content

ec_demo/
ec_demo.rs

1use crypto_bigint::{const_prime_monty_params, Uint};
2
3use ec::curve_edwards::EdwardsCurve;
4use ec::curve_jacobi_intersection::JacobiIntersectionCurve;
5use ec::curve_jacobi_quartic::JacobiQuarticCurve;
6use ec::curve_montgomery::MontgomeryCurve;
7use ec::curve_ops::Curve;
8use ec::curve_weierstrass::WeierstrassCurve;
9
10use fp::fp_element::FpElement;
11
12use rand::rngs::ThreadRng;
13
14// Small demo field: F_19
15const_prime_monty_params!(Fp19Mod, Uint<1>, "0000000000000013", 2);
16type F19 = FpElement<Fp19Mod, 1>;
17
18fn fp(x: u64) -> F19 {
19    F19::from_u64(x)
20}
21
22fn show_curve<C>(name: &str, curve: &C, rng: &mut ThreadRng)
23where
24    C: Curve + core::fmt::Display,
25    C::Point: core::fmt::Display,
26{
27    println!("============================================================");
28    println!("{name}");
29    println!("------------------------------------------------------------");
30    println!("curve compact : {}", curve);
31    println!("curve pretty  :\n{:#}", curve);
32
33    let p = curve.random_point(rng);
34
35    println!("point compact : {}", p);
36    println!("point pretty  :\n{:#}", p);
37    println!("on curve?     : {}", curve.is_on_curve(&p));
38    println!();
39}
40
41fn main() {
42    let mut rng = rand::rng();
43    // 1. Short Weierstrass over F_19: y^2 = x^3 + 2x + 3
44    let w = WeierstrassCurve::new_short(fp(2), fp(3));
45    show_curve("Weierstrass", &w, &mut rng);
46
47    // 2. Montgomery over F_19: B y^2 = x(x^2 + A x + 1)
48    // Smooth if B != 0 and A != ±2 in odd characteristic.
49    let m = MontgomeryCurve::new(fp(3), fp(1));
50    show_curve("Montgomery", &m, &mut rng);
51
52    // 3. Edwards over F_19: x^2 + y^2 = 1 + d x^2 y^2
53    // Pick d = 2 (nonzero, not 1; also a nonsquare in F_19).
54    let e = EdwardsCurve::new(fp(2));
55    show_curve("Edwards", &e, &mut rng);
56
57    // 4. Jacobi quartic over F_19: y^2 = d x^4 + 2 a x^2 + 1
58    // Need d != 0 and a^2 != d.
59    let jq = JacobiQuarticCurve::new(fp(3), fp(5));
60    show_curve("Jacobi quartic", &jq, &mut rng);
61
62    // 5. Jacobi intersection over F_19:
63    // s^2 + c^2 = 1,  a s^2 + d^2 = 1
64    // Need a != 0, 1.
65    let ji = JacobiIntersectionCurve::new(fp(2));
66    show_curve("Jacobi intersection", &ji, &mut rng);
67}