ecc

Elliptic Curve Cryptograpy library from scratch in python.
Log | Files | Refs | README | LICENSE

commit 5d02f8a497b4e3ad4517d7a3eac1021033f30222
parent aa731f23b10f6d403d34d878341c033bf82df1e0
Author: Shivakumar <[email protected]>
Date:   Sat,  2 Aug 2025 01:28:35 +0530

add RFC 6979 nonce to ecdsa.py

RFC 6979, deterministic nonce for more secure signing.

Diffstat:
Mecc/ecdsa.py | 130+++++++++++++++++++++----------------------------------------------------------
1 file changed, 34 insertions(+), 96 deletions(-)

diff --git a/ecc/ecdsa.py b/ecc/ecdsa.py @@ -1,116 +1,54 @@ -# Implementation of ECDSA (Elliptic Curve Digital Signature Algorithm) -# -# ECDSA is a digital signature algorithm that uses elliptic curve cryptography. -# -# The algorithm consists of two main operations: -# 1. Signature Generation (Sign) -# 2. Signature Verification (Verify) -# -# For signing a message: -# 1. Generate a random number k in range [1, n-1] -# 2. Calculate point (x1, y1) = k * G -# 3. Calculate r = x1 mod n (if r = 0, go back to step 1) -# 4. Calculate s = k^(-1) * (z + r * private_key) mod n (if s = 0, go back to step 1) -# 5. The signature is (r, s) -# -# For verifying a signature: -# 1. Calculate w = s^(-1) mod n -# 2. Calculate u1 = z * w mod n -# 3. Calculate u2 = r * w mod n -# 4. Calculate point (x1, y1) = u1 * G + u2 * public_key -# 5. Verify that r ≡ x1 (mod n) - +""" +This module implements the core logic for the Elliptic Curve Digital Signature +Algorithm (ECDSA), including deterministic nonce generation as specified in +RFC 6979. +""" import hashlib -from secrets import randbelow - -from .point import Point -from .keys import PublicKey, PrivateKey +import hmac class Signature: - """Represents an ECDSA signature (r, s).""" + """Represents an ECDSA signature, consisting of two integers (r, s).""" + def __init__(self, r: int, s: int): self.r = r self.s = s - def __repr__(self): - return f"<Signature \n\tr=0x{self.r:x}, \n\ts=0x{self.s:x}\n>" - - -def _hash_msg(message: bytes, n: int) -> int: - """ - Hashes the message with SHA-256 and truncates to - modulo n to produce an integer in [0, n-1]. - """ - h = hashlib.sha256(message).digest() - z = int.from_bytes(h, 'big') - - return z % n - + def __eq__(self, other: object) -> bool: + if not isinstance(other, Signature): + return NotImplemented + return self.r == other.r and self.s == other.s -def sign(message: bytes, private: PrivateKey) -> Signature: - """ - Generate an ECDSA signature on 'message' using 'private' (PrivateKey). - Returns a Signature(r, s). - """ - curve = private.curve - n = curve.n - Gx, Gy = curve.G - - z = _hash_msg(message, n) - G = Point(Gx, Gy, curve) - - while True: - # Generate nonce k [1, n-1] - k = randbelow(n-1) + 1 - - # Compute for r - R = k * G - r = R.x.num % n + def __repr__(self) -> str: + return f"Signature(\n\tr=0x{self.r:x}, \n\ts=0x{self.s:x}\n)" - if r == 0: - continue - - # Compute s = k^-1(z + rp) mod n - k_inv = pow(k, -1, n) - s = (k_inv * (z + r * private.secret)) % n - if s == 0: - continue - - return Signature(r, s) - - -def verify(message: bytes, sign: Signature, public: PublicKey) -> bool: +def _rfc6979_nonce(secret: int, n: int, message_hash: bytes) -> int: """ - Verify an ECDSA 'sign' on 'message' using the public key 'public'. - Returns True if valid, False otherwise. + Generates a deterministic nonce 'k' as per RFC 6979. + This is safer than using a random number generator. """ - curve = public.curve - n = curve.n - Gx, Gy = curve.G - - r, s = sign.r, sign.s - - # Check if s and r in between 1 and n-1 - if not (1 <= r < n and 1 <= s < n): - return False + hash_len = 32 # For SHA-256 - z = _hash_msg(message, n) - s_inv = pow(s, -1, n) + # Set values for HMAC key 'K' and value to hash 'V' + V = b"\x01" * hash_len + K = b"\x00" * hash_len - # Compute u1 = z·s⁻¹ mod n, u2 = r·s⁻¹ mod n - u1 = (z * s_inv) % n - u2 = (r * s_inv) % n + # Private Key in bytes + private_key = secret.to_bytes(hash_len, "big") - # Compute P = u1*G + u2*Q - G = Point(Gx, Gy, curve) - P = u1 * G + u2 * public.point - - if P.x is None: - return False + K = hmac.new(K, V + b"\x00" + private_key + message_hash, hashlib.sha256).digest() + V = hmac.new(K, V, hashlib.sha256).digest() + K = hmac.new(K, V + b"\x01" + private_key + message_hash, hashlib.sha256).digest() + V = hmac.new(K, V, hashlib.sha256).digest() - return (P.x.num % n) == r + while True: + V = hmac.new(K, V, hashlib.sha256).digest() + k = int.from_bytes(V, "big") + if 1 <= k < n: + return k + K = hmac.new(K, V + b"\x00", hashlib.sha256).digest() + V = hmac.new(K, V, hashlib.sha256).digest()