Guest User

Untitled

a guest
Dec 28th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import decimal
  4.  
  5. class ComplexDecimal:
  6.     def __init__(self, x, y):
  7.         if type(x) == decimal.Decimal:
  8.             self.x = x
  9.             self.y = y
  10.         else:
  11.             self.x = decimal.Decimal(x)
  12.             self.y = decimal.Decimal(y)
  13.     def __abs__(self):
  14.         return (self.x ** 2 + self.y ** 2).sqrt()
  15.     def __eq__(self, n):
  16.         return self.x == n.x and self.y == n.y
  17.     def __mul__(self, n):
  18.         x = (self.x * n.x) - (self.y * n.x)
  19.         y = (self.y * n.x) + (self.x * n.y)
  20.         return ComplexDecimal(x, y)
  21.     def __repr__(self):
  22.         ctx = decimal.getcontext()
  23.         return ('ComplexDecimal(' + ctx.to_sci_string(self.x) +
  24.                 ctx.to_sci_string(self.y) + ')')
  25.     def __sub__(self, n):
  26.         return ComplexDecimal(self.x - n.x, self.y - n.y)
  27.     def is_zero(self):
  28.         return (self.x.is_zero() and
  29.                 self.y.is_zero())
  30.  
  31. class FractalSign:
  32.     def __init__(self, p=100, e=1e-1):
  33.         ctx = decimal.getcontext()
  34.         ctx.prec = p
  35. #        ctx.traps[decimal.Inexact] = 1
  36.         self.error = decimal.Decimal(e)
  37.  
  38.     def mj(self, key, z=None, m=None):
  39.         c, k, e = key
  40.         cce = c * c * e
  41.         if z is None:
  42.             z = c
  43.         if m is not None:
  44.             z = z * m
  45.         for i in range(k):
  46.             z = z * cce
  47.             if z.is_zero():
  48.                 raise ValueError
  49.         return z
  50.  
  51.     def pubkey(self, key):
  52.         return self.mj(key)
  53.  
  54.     def sign(self, key, pub, m):
  55.         return self.mj(key, pub, m)
  56.  
  57.     def verify(self, key, pub, m, s):
  58.         v = self.mj(key, pub, m)
  59.         print(v - s)
  60.         if abs(v - s) < abs(v) * self.error:
  61.             return True
  62.         return False
  63.  
  64.  
  65.  
  66. def main():
  67.     fs = FractalSign()
  68.  
  69.     c = ComplexDecimal(0.01309, 0.019)
  70.  
  71.     alice_priv = (c, 4321, (ComplexDecimal(0.01380792, 0.0180792)))
  72.     alice_pub = fs.pubkey(alice_priv)
  73.     bob_priv = (c, 1234, (ComplexDecimal(0.013407807929, 0.0134043)))
  74.     bob_pub = fs.pubkey(bob_priv)
  75.  
  76.     m = ComplexDecimal(0.000123, 0.000321)
  77.     s = fs.sign(alice_priv, bob_pub, m)
  78.  
  79.     print(s)
  80.  
  81.     if fs.verify(bob_priv, alice_pub, m, s):
  82.         print('Good signature')
  83.     else:
  84.         print('Invalid signature')
  85.  
  86.  
  87.  
  88. if __name__=='__main__':
  89.     main()
Advertisement
Add Comment
Please, Sign In to add comment