Advertisement
huyhung94

Chương trình tính toán trên EC( a Thứ)

Sep 22nd, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. __author__ = 'Mr.DangThu_KMA'
  2. # Basics of Elliptic Curve Cryptography implementation on Python
  3. import collections
  4.  
  5.  
  6. def inv(n, q):
  7.     for i in range(q):
  8.         if (n * i) % q == 1:
  9.             return i
  10.         pass
  11.     assert False, "unreached"
  12.     pass
  13.  
  14.  
  15. def sqrt(n, q):
  16.  
  17.     assert n < q
  18.     for i in range(1, q):
  19.         if i * i % q == n:
  20.             return (i, q - i)
  21.         pass
  22.  
  23.  
  24. Coord = collections.namedtuple("Coord", ["x", "y"])
  25.  
  26.  
  27. class EC(object):
  28.     """System of Elliptic Curve"""
  29.     def __init__(self, a, b, q):
  30.         assert 0 < a and a < q and 0 < b and b < q and q > 2
  31.         assert (4 * (a ** 3) + 27 * (b ** 2))  % q != 0
  32.         self.a = a
  33.         self.b = b
  34.         self.q = q
  35.         self.zero = Coord(0, 0)
  36.         pass
  37.  
  38.     def is_valid(self, p):
  39.         if p == self.zero: return True
  40.         l = (p.y ** 2) % self.q
  41.         r = ((p.x ** 3) + self.a * p.x + self.b) % self.q
  42.         return l == r
  43.  
  44.     def at(self, x):
  45.         assert x < self.q
  46.         ysq = (x ** 3 + self.a * x + self.b) % self.q
  47.         y, my = sqrt(ysq, self.q)
  48.         return Coord(x, y), Coord(x, my)
  49.  
  50.     def neg(self, p):
  51.         return Coord(p.x, -p.y % self.q)
  52.  
  53.     def add(self, p1, p2):
  54.         """<add> of elliptic curve: negate of 3rd cross point of (p1,p2) line
  55.       """
  56.         if p1 == self.zero: return p2
  57.         if p2 == self.zero: return p1
  58.         if p1.x == p2.x and (p1.y != p2.y or p1.y == 0):
  59.             # p1 + -p1 == 0
  60.             return self.zero
  61.         if p1.x == p2.x:
  62.             # p1 + p1: use tangent line of p1 as (p1,p1) line
  63.             l = (3 * p1.x * p1.x + self.a) * inv(2 * p1.y, self.q) % self.q
  64.             pass
  65.         else:
  66.             l = (p2.y - p1.y) * inv(p2.x - p1.x, self.q) % self.q
  67.             pass
  68.         x = (l * l - p1.x - p2.x) % self.q
  69.         y = (l * (p1.x - x) - p1.y) % self.q
  70.         return Coord(x, y)
  71.  
  72.     def mul(self, p, n):
  73.         r = self.zero
  74.         m2 = p
  75.         # O(log2(n)) add
  76.         while 0 < n:
  77.             if n & 1 == 1:
  78.                 r = self.add(r, m2)
  79.                 pass
  80.             n, m2 = n >> 1, self.add(m2, m2)
  81.             pass
  82.         # [ref] O(n) add
  83.         #for i in range(n):
  84.         #    r = self.add(r, p)
  85.         #    pass
  86.         return r
  87.  
  88.     def order(self, g):
  89.         assert self.is_valid(g) and g != self.zero
  90.         for i in range(1, self.q + 1):
  91.             if self.mul(g, i) == self.zero:
  92.                 return i
  93.             pass
  94.     pass
  95.  
  96.  
  97. if __name__=='__main__':
  98.     print "Welcome to EC calculator"
  99.     #input EC
  100.     print "Please input p a b:"
  101.     e = raw_input(">")
  102.     e = e.split(" ")
  103.     q = int(e[0])
  104.     a = int(e[1])
  105.     b = int(e[2])
  106.     E = EC(a,b,q)
  107.     #input P
  108.     print "Please input P(x1,y1): "
  109.     x1 = int(raw_input("x1="))
  110.     y1 = int(raw_input("y1="))
  111.     p1 = Coord(x1, y1)
  112.     answer = raw_input("1: Add\n2: Multiplication\n")
  113.     if (answer not in ['1','2']):
  114.         answer = raw_input("1: Add\n2: Multiplication\n")
  115.     if (answer=='1'):
  116.         print "Please input Q(x2,y2)"
  117.         x2 = int(raw_input("x2="))
  118.         y2 = int(raw_input("y2="))
  119.         p2 = Coord(x2, y2)
  120.         kq = E.add(p1,p2)
  121.         print "P + Q = ", kq
  122.     if (answer == '2'):
  123.         print "Please input k:"
  124.         k = int(raw_input(">"))
  125.         kq = E.mul(p1,k)
  126.         print "%dP = " % k, kq
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement