Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import decimal
- class ComplexDecimal:
- def __init__(self, x, y):
- if type(x) == decimal.Decimal:
- self.x = x
- self.y = y
- else:
- self.x = decimal.Decimal(x)
- self.y = decimal.Decimal(y)
- def __abs__(self):
- return (self.x ** 2 + self.y ** 2).sqrt()
- def __eq__(self, n):
- return self.x == n.x and self.y == n.y
- def __mul__(self, n):
- x = (self.x * n.x) - (self.y * n.x)
- y = (self.y * n.x) + (self.x * n.y)
- return ComplexDecimal(x, y)
- def __repr__(self):
- ctx = decimal.getcontext()
- return ('ComplexDecimal(' + ctx.to_sci_string(self.x) +
- ctx.to_sci_string(self.y) + ')')
- def __sub__(self, n):
- return ComplexDecimal(self.x - n.x, self.y - n.y)
- def is_zero(self):
- return (self.x.is_zero() and
- self.y.is_zero())
- class FractalSign:
- def __init__(self, p=100, e=1e-1):
- ctx = decimal.getcontext()
- ctx.prec = p
- # ctx.traps[decimal.Inexact] = 1
- self.error = decimal.Decimal(e)
- def mj(self, key, z=None, m=None):
- c, k, e = key
- cce = c * c * e
- if z is None:
- z = c
- if m is not None:
- z = z * m
- for i in range(k):
- z = z * cce
- if z.is_zero():
- raise ValueError
- return z
- def pubkey(self, key):
- return self.mj(key)
- def sign(self, key, pub, m):
- return self.mj(key, pub, m)
- def verify(self, key, pub, m, s):
- v = self.mj(key, pub, m)
- print(v - s)
- if abs(v - s) < abs(v) * self.error:
- return True
- return False
- def main():
- fs = FractalSign()
- c = ComplexDecimal(0.01309, 0.019)
- alice_priv = (c, 4321, (ComplexDecimal(0.01380792, 0.0180792)))
- alice_pub = fs.pubkey(alice_priv)
- bob_priv = (c, 1234, (ComplexDecimal(0.013407807929, 0.0134043)))
- bob_pub = fs.pubkey(bob_priv)
- m = ComplexDecimal(0.000123, 0.000321)
- s = fs.sign(alice_priv, bob_pub, m)
- print(s)
- if fs.verify(bob_priv, alice_pub, m, s):
- print('Good signature')
- else:
- print('Invalid signature')
- if __name__=='__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment