Path: blob/master/attacks/ecc/ecdsa_nonce_reuse.py
2589 views
import os1import sys23path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(os.path.abspath(__file__)))))4if sys.path[1] != path:5sys.path.insert(1, path)67from shared import solve_congruence8910def attack(n, m1, r1, s1, m2, r2, s2):11"""12Recovers the nonce and private key from two messages signed using the same nonce.13:param n: the order of the elliptic curve14:param m1: the first message15:param r1: the signature of the first message16:param s1: the signature of the first message17:param m2: the second message18:param r2: the signature of the second message19:param s2: the signature of the second message20:return: generates tuples containing the possible nonce and private key21"""22for k in solve_congruence(int(s1 - s2), int(m1 - m2), int(n)):23for x in solve_congruence(int(r1), int(k * s1 - m1), int(n)):24yield int(k), int(x)252627