Eos/eos/core/bigint/python.py

102 lines
3.2 KiB
Python
Raw Permalink Normal View History

2017-09-20 22:30:38 +10:00
# Eos - Verifiable elections
# Copyright © 2017 RunasSudo (Yingtong Li)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from eos.core.objects import *
2017-09-20 22:30:38 +10:00
2017-09-26 21:09:56 +10:00
import math
2017-09-25 18:03:43 +10:00
import random
system_random = random.SystemRandom()
2017-09-24 15:18:27 +10:00
class BigInt(EosObject):
def __init__(self, a, b=10):
2017-09-25 18:03:43 +10:00
super().__init__()
2017-09-24 15:18:27 +10:00
self.impl = int(a, b) if isinstance(a, str) else int(a)
2017-10-05 19:12:02 +11:00
def __repr__(self):
return '<BigInt {}>'.format(str(self))
2017-09-24 15:18:27 +10:00
def __pow__(self, other, modulo=None):
if not isinstance(other, BigInt):
other = BigInt(other)
if modulo is None:
return BigInt(self.impl.__pow__(other.impl))
if not isinstance(modulo, BigInt):
modulo = BigInt(modulo)
return BigInt(self.impl.__pow__(other.impl, modulo.impl))
2017-09-25 18:03:43 +10:00
def __truediv__(self, other):
# Python will try to compute this as a float
return self.__floordiv__(other)
2017-09-26 21:09:56 +10:00
def nbits(self):
2017-09-26 23:09:33 +10:00
return math.ceil(math.log2(self.impl)) if self.impl > 0 else 0
2017-09-26 21:09:56 +10:00
def serialise(self, options=SerialiseOptions.DEFAULT):
2017-09-25 18:03:43 +10:00
return str(self)
@classmethod
def deserialise(cls, value):
2017-11-28 22:43:32 +11:00
if value is None:
return None
2017-09-25 18:03:43 +10:00
return cls(value)
# Returns a random BigInt from lower_bound to upper_bound, both inclusive
@classmethod
def noncrypto_random(cls, lower_bound, upper_bound):
return cls(random.randint(int(lower_bound), int(upper_bound)))
@classmethod
def crypto_random(cls, lower_bound, upper_bound):
return cls(system_random.randint(int(lower_bound), int(upper_bound)))
2017-09-24 15:18:27 +10:00
for func in ['__add__', '__sub__', '__mul__', '__floordiv__', '__mod__', '__and__', '__or__', '__lshift__', '__rshift__', '__xor__']:
2017-09-24 15:18:27 +10:00
def make_operator_func(func_):
# Create a closure
def operator_func(self, other):
if not isinstance(other, BigInt):
other = BigInt(other)
return BigInt(getattr(self.impl, func_)(other.impl))
return operator_func
setattr(BigInt, func, make_operator_func(func))
2017-09-25 18:03:43 +10:00
for func in ['__eq__', '__ne__', '__lt__', '__gt__', '__le__', '__ge__']:
def make_operator_func(func_):
# Create a closure
def operator_func(self, other):
if not isinstance(other, BigInt):
other = BigInt(other)
return getattr(self.impl, func_)(other.impl)
return operator_func
setattr(BigInt, func, make_operator_func(func))
for func in ['__neg__']:
def make_operator_func(func_):
# Create a closure
def operator_func(self):
return BigInt(getattr(self.impl, func_)())
return operator_func
setattr(BigInt, func, make_operator_func(func))
2017-09-25 18:03:43 +10:00
for func in ['__str__', '__int__']:
def make_operator_func(func_):
# Create a closure
def operator_func(self):
return getattr(self.impl, func_)()
return operator_func
setattr(BigInt, func, make_operator_func(func))