class ecprng: # Curve P-256; source: https://safecurves.cr.yp.to/ p = 2**256 - 2**224 + 2**192 + 2**96 - 1 a = p - 3 b = 41058363725152142129326129780047268409114441015993725554835256314039467401291 ec = ecc.CurveFp(p, a, b) _Px = 115113149114637566422228202471255745041343462839792246702200996638778690567225 _Py = 88701990415124583444630570378020746694390711248186320283617457322869078545663 Point_P = ecc.Point(ec, _Px, _Py) _Qx = 75498749949015782244392151836890161743686522667385613237212787867797557116642 _Qy = 19586975827802643945708711597046872561784179836880328844627665993398229124361 Point_Q = ecc.Point(ec, _Qx, _Qy) def __init__(self, seed): self.seed = seed if self.seed: assert len(long_to_bytes(self.seed)) == 32 def update_seed(self, intermediate_state_S_1): self.seed = (intermediate_state_S_1 * ecprng.Point_P).x() assert len(long_to_bytes(self.seed)) == 32 def ec_generate(self): intermediate_state_S_1 = (self.seed * ecprng.Point_P).x() self.update_seed(intermediate_state_S_1) r_1 = long_to_bytes( (intermediate_state_S_1 * ecprng.Point_Q).x())[-30:] r_2 = long_to_bytes((self.seed * ecprng.Point_Q).x())[-30:][:2] assert len(r_1 + r_2) == 32 return bytes_to_long(r_1 + r_2)
def baby_step_giant_step(curve, G, H, order): m = int(math.ceil(gmpy2.sqrt(order))) L = {} # Baby steps for j in range(0, m): P_tmp = curve.mul(j, G) L[str(P_tmp)] = j mG = curve.mul(m, G) # Giant steps for i in range(0, m): P_tmp = curve.mul(i, mG) if not P_tmp.isInf(): P_tmp = ecc.Point(P_tmp.x, (-P_tmp.y) % curve.p) P = curve.add(H, P_tmp) index = str(P) if index in L: return (L[index] + i * m) % curve.p return None
def bits_to_point(p): (x_size, ) = struct.unpack('!H', p[:2]) if x_size == 0: return ecc.PointInf() x = bits_to_int(p[2:x_size + 2]) y = bits_to_int(p[x_size + 4:]) return ecc.Point(x, y)
def dechiffrement_Alice(curve): ''' Alice reçoit C1 et C2 elle calcule daC1 grâce à sa clé privée elle calcule l'inverse de daC1 elle déchiffre C2 qui correspond à M ''' C1, C2 = chiffrement_Bob(curve) f = open("cle.txt", "r") cle = int(f.read()) daC1 = ecc.Curve.mul(curve, cle, C1) daC1_inv = ecc.Point(daC1.x, -daC1.y) M = ecc.Curve.add(curve, C2, daC1_inv) return M
def find_next_e(e): r = long_to_bytes(e)[:-2] for i in trange(133, 140): for j in range(256): x = bytes_to_long(chr(i) + chr(j) + r) y = find_point(ec, x) if test_Point(x, y): R = ecc.Point(ec, x, y) r_2 = long_to_bytes( (((R * inverse(d, order)).x()) * Point_Q).x())[-30:][:2] if long_to_bytes(e)[-2:] == r_2: print "finally" return R return R
blue = '\033[34m' # New urandom seed for each session (Not really relevant for the challenge) prng_obj = prng(16793527392756720769, 2358102439659339126076356431940385122127543421625845446663, False) p = 2**256 - 2**224 + 2**192 + 2**96 - 1 a = p - 3 b = 41058363725152142129326129780047268409114441015993725554835256314039467401291 ec = ecc.CurveFp(p, a, b) _Px = 53881495764268889303293517690095107010093794097958309592680107528631746121613 _Py = 69534606358473748292927094386662082099432383517498778127513290350658945146669 Point_P = ecc.Point(ec, _Px, _Py) iteration = True counter = 0 while iteration == True and counter < 10: print colors.blue + "Choose one between authentication protocols listed below:" + colors.reset print colors.orange + "[1] Asynchronous SchnorrID" + colors.reset print colors.orange + "[2] Synchronous SchnorrID" + colors.reset choice = int(raw_input("Enter your choice: ")) print "" if choice == 1: print "Here are the coordinates of the base point P: ", _Px, _Py _Qx, _Qy = map( int,
return R return R st = lambda x: str(x).strip('L') if __name__ == "__main__": p = 2**256 - 2**224 + 2**192 + 2**96 - 1 a = p - 3 b = 41058363725152142129326129780047268409114441015993725554835256314039467401291 ec = ecc.CurveFp(p, a, b) _Px = 115113149114637566422228202471255745041343462839792246702200996638778690567225 _Py = 88701990415124583444630570378020746694390711248186320283617457322869078545663 Point_P = ecc.Point(ec, _Px, _Py) _Qx = 75498749949015782244392151836890161743686522667385613237212787867797557116642 _Qy = 19586975827802643945708711597046872561784179836880328844627665993398229124361 Point_Q = ecc.Point(ec, _Qx, _Qy) d = 1735 x = 53881495764268889303293517690095107010093794097958309592680107528631746121613 y = 69534606358473748292927094386662082099432383517498778127513290350658945146669 P = ecc.Point(ec, x, y) Q = 123 * P order = 115792089210356248762697446949407573529996955224135760342422259061068512044369 #io = process('./encrypt.py') io = remote('34.74.30.191', 3333)
(gcd, x0, x1) = xgcd(ni, tmp) x += x_prime * x1 * tmp return x % N # (A, B, N) A = 0 B = 0 N = 0 X = 0 Y = 0 curve = ecc.Curve(A, B, N) G = ecc.Point(X, Y) sent = [ 0x00, 0x30, 0x00, 0x16, 0x0d, 0x6c, 0x24, 0xb0, 0x5a, 0xf7, 0xff, 0x4f, 0xa6, 0x28, 0xeb, 0xce, 0xfd, 0x43, 0xdd, 0xad, 0x1a, 0x57, 0xac, 0xb9, 0xa4, 0x65, 0x00, 0x16, 0x0a, 0x00, 0x63, 0x5f, 0x98, 0x88, 0x1c, 0x47, 0x07, 0x50, 0x48, 0x3e, 0xa0, 0x59, 0x77, 0xc1, 0x93, 0x28, 0x9a, 0xeb, 0x50, 0x64 ] H = bits_to_point("".join(map(chr, sent)).encode()) x = pohlig_hellman(curve, G, H) print("x = %s" % x)
import os.path import sys import ecc sys.path.append(os.path.join(os.path.dirname(__file__), '..')) ''' Création de la courbe elliptiques ''' A = 109454571331697278617670725030735128145969349647868738157201323556196022393856 B = 107744541122042688792155207242782455150382764043089114141096634497567301547839 ''' Ordre du point P ''' l = 109454571331697278617670725030735128146004546811402412653072203207726079563233 ''' Ordre de la courbe ''' N = 109454571331697278617670725030735128145969349647868738157201323556196022393859 '''n pour la multiplication ''' n = 2 ''' Point de la courbe elliptique ''' P = ecc.Point( 82638672503301278923015998535776227331280144783487139112686874194432446389503, 43992510890276411535679659957604584722077886330284298232193264058442323471611 ) Q = ecc.Point( 100597391921786027039183722380481804805320476080319934670061678997404767442782, 80123073214026054915454326239165515159448240266403681526048086449062769463365 ) '''Point M du message''' M = ecc.Point( 100597391921786027039183722380481804805320476080319934670061678997404767442782, 80123073214026054915454326239165515159448240266403681526048086449062769463365 )