Elliptic Curve - Arithmetic overflow

68 views Asked by At

I'm trying to implement elliptic point doubling and addition in Rust, however, I'm running into arithmetic overflow, specifically at the line where the gradient is being calculated in the doubling function. Any ideas?

fn ec_point_double((x, y): (U256, U256)) -> (U256, U256) {
        let dec_gx = hex_to_dec("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798");
        let gx: U256 = U256::from_dec_str(dec_gx.as_str()).unwrap();
        let dec_gy = hex_to_dec("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8");
        let gy: U256 = U256::from_dec_str(dec_gy.as_str()).unwrap();
        let two: U256 = U256::from(2);
        let three: U256 = U256::from(3);
        let dec_n = hex_to_dec("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
        let n: U256 = U256::from_dec_str(dec_n.as_str()).unwrap();
        let lambda: U256 = (((((three % n) * (x % n)) % n * (x % n)) * two.mod_inverse(n)) % n
            * y.mod_inverse(n))
            % n;
        let new_x = ((lambda % n) * (lambda % n)) % n - ((x % n) * (two % n)) % n;
        let new_y: U256 =
            ((lambda % n * (x % n)) % n + (gy % n - (lambda % n * gx % n) % n) % n) % n;
        (new_x, new_y)

Initially I was getting arithmetic overflow because I used regular division, and not modular inverse. However, even when using modular inverse, still getting arithmetic overflow. Expecting no arithmetic overflow

0

There are 0 answers