Created
June 5, 2025 19:56
-
-
Save IAnmar0/f16781ea99c5743f82cab45cb966debf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from Crypto.Util.number import * | |
| # global vars | |
| # Y^2= X + 497X + 1768 mod 9739 | |
| a=497 | |
| b=1768 | |
| p=9739 | |
| O = 'O' | |
| ## | |
| def elliptic_add(P, Q): | |
| # Handle special cases with point at infinity | |
| if P == 'O': # Identity element | |
| return Q | |
| if Q == 'O': | |
| return P | |
| x1, y1 = P | |
| x2, y2 = Q | |
| # If x1 == x2 and y1 == -y2 (mod p), the result is the point at infinity | |
| if x1 == x2 and (y1 + y2) % p == 0: | |
| return 'O' | |
| # Compute the slope (lambda) | |
| if P != Q: | |
| numerator = (y2 - y1) % p | |
| denominator = (x2 - x1) % p | |
| else: | |
| numerator = (3 * x1**2 + a) % p | |
| denominator = (2 * y1) % p | |
| # Compute lambda using modular inverse | |
| lam = (numerator * inverse(denominator, p)) % p | |
| # Calculate resulting point coordinates | |
| x3 = (lam**2 - x1 - x2) % p | |
| y3 = (lam * (x1 - x3) - y1) % p | |
| return (x3, y3) | |
| def scalar_mul(P,n): | |
| Q=P | |
| R=O | |
| while n > 0 : | |
| if n % 2 ==1 : | |
| R=elliptic_add(R,Q) | |
| # Q=[2]Q | |
| Q=elliptic_add(Q,Q) | |
| # and n=[n/2] | |
| n=n//2 | |
| return R | |
| P = (2339, 2213) | |
| n = 7863 | |
| Q = scalar_mul(P,n) | |
| # Print result | |
| print("Q =", Q) | |
| # Check if Q is on the curve | |
| x, y = Q | |
| on_curve = (y**2 % p) == (x**3 + a*x + b) % p | |
| print("Is Q on the curve?", on_curve) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment