Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 24, 2025 12:36
Show Gist options
  • Save rust-play/5bd6c3f398d920f84cadb2ffd7412d75 to your computer and use it in GitHub Desktop.
Save rust-play/5bd6c3f398d920f84cadb2ffd7412d75 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::f64::consts::PI;
// Note: This implementation uses f64 (floating-point) for the polar coordinates (r, theta)
// and trigonometric functions (sin, cos). This approach is for **geometric demonstration** // and is NOT how Elliptic Curve Cryptography (ECC) is performed, as ECC requires
// all arithmetic to be done with **integers** in a finite field (modulo P).
// The equation evaluated is:
// r^2 * sin^2(θ) = (A_std * r^3 * cos^3(θ) + B_std * r * cos(θ) + C_std) mod P
/// Evaluates the polar form of the Elliptic Curve equation.
///
/// Due to the mix of float and integer math, this function returns a pair:
/// (LHS_value_mod_P, RHS_value_mod_P)
///
/// # Arguments
/// * `r` - The radial distance (must be a float).
/// * `theta` - The angle in radians (must be a float).
/// * `a_std`, `b_std`, `c_std` - Elliptic curve coefficients (u128).
/// * `p` - The prime modulus P (u128).
///
/// # Returns
/// A tuple `(lhs_mod_p, rhs_mod_p)` where both are u128.
fn evaluate_ecc_polar(
r: f64,
theta: f64,
a_std: u128,
b_std: u128,
c_std: u128,
p: u128,
) -> (u128, u128) {
let cos_theta = theta.cos();
let sin_theta = theta.sin();
// --- 1. Calculate the Right-Hand Side (RHS) ---
// RHS = A_std * r^3 * cos^3(θ) + B_std * r * cos(θ) + C_std
// x-coordinate in Cartesian terms: x = r * cos(θ)
let x_float = r * cos_theta;
// Calculate terms using f64 first
let term1_float = a_std as f64 * x_float.powi(3);
let term2_float = b_std as f64 * x_float;
let rhs_float = term1_float + term2_float + c_std as f64;
// Convert RHS back to integer and apply modulo P
// NOTE: This conversion involves significant precision loss/rounding.
let rhs_integer = rhs_float.round().abs() as u128; // abs() because f64 can be negative
let rhs_mod_p = rhs_integer % p;
// --- 2. Calculate the Left-Hand Side (LHS) ---
// LHS = r^2 * sin^2(θ)
// y-coordinate in Cartesian terms: y = r * sin(θ)
let y_float = r * sin_theta;
let lhs_float = y_float.powi(2);
// Convert LHS back to integer and apply modulo P
let lhs_integer = lhs_float.round().abs() as u128;
let lhs_mod_p = lhs_integer % p;
(lhs_mod_p, rhs_mod_p)
}
fn main() {
println!("--- Conceptual ECC Polar Evaluation (f64 + u128 Modulo) ---");
// ECC Parameters (same as previous example)
let p: u128 = 34028236692093846346337460743176821145;
let a_std: u128 = 25;
let b_std: u128 = 100;
let c_std: u128 = 15;
// --- Test Point (r, theta) ---
// This example chooses a point that, when converted to Cartesian,
// is roughly close to the previous x_coord for scale comparison.
let test_r: f64 = 1.8e37;
let test_theta: f64 = PI / 4.0; // 45 degrees
// --- EVALUATION ---
let (lhs_result, rhs_result) = evaluate_ecc_polar(
test_r, test_theta, a_std, b_std, c_std, p
);
// Calculate the Cartesian x/y coordinates from the polar input
let cartesian_x = test_r * test_theta.cos();
let cartesian_y = test_r * test_theta.sin();
println!("\nCurve Equation: r^2*sin^2(θ) = {}*r^3*cos^3(θ) + {}*r*cos(θ) + {} (mod P)", a_std, b_std, c_std);
println!("P (Modulus): {}", p);
println!("Test Point (r, θ): r={:.2e}, θ={:.4} rad", test_r, test_theta);
println!("(Approx. Cartesian x, y): x={:.2e}, y={:.2e}", cartesian_x, cartesian_y);
println!("\n--- Result ---");
println!("LHS (r^2*sin^2(θ)) mod P: {}", lhs_result);
println!("RHS (A*x^3 + B*x + C) mod P: {}", rhs_result);
// The point is on the curve IF lhs_result == rhs_result.
let on_curve = if lhs_result == rhs_result { "YES" } else { "NO" };
println!("\nPoint is on the curve (LHS == RHS)? {}", on_curve);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment