def __iadd__(self, other): if type(other ) is GaloisFieldNumber and self.exponent == other.exponent: self.encoding = gmpy2.mod(gmpy2.add(self.encoding, other.encoding), self.gfp.p) return self else: return self + other
def _batchgcd(xs): tree = _product_tree(xs) rems = tree.pop() while tree: LOGGER.info('Calculating batch GCDs: %10d' % (len(tree))) xs = tree.pop() rems = [ gmpy2.mod(rems[i // 2], gmpy2.mul(xs[i], xs[i])) for i in range(len(xs)) ] return [gmpy2.gcd(gmpy2.t_div(r, n), n) for r, n in zip(rems, xs)]
def common_modulus_attack(modulus, exp1, exp2, msg1, msg2): """ Perform RSA Common Modulus Attack, given the modulus, two exponents and two ciphertexts as integers. Returns the plaintext as an integer. """ g, s, t = gmpy2.gcdext(exp1, exp2) if g != 1: print("Error: GCD of the two exponents is not 1!") exit(1) tmp1 = gmpy2.powmod(msg1, s, modulus) tmp2 = gmpy2.powmod(msg2, t, modulus) return int(gmpy2.mod(tmp1 * tmp2, modulus))
def __add__(self, other): if type(other) in [int, float]: return self + GaloisFieldNumber.encode(other, gfp=self.gfp) elif type(other) is GaloisFieldNumber: if self.exponent == other.exponent: return GaloisFieldNumber(gmpy2.mod( gmpy2.add(self.encoding, other.encoding), self.gfp.p), exponent=self.exponent, gfp=self.gfp) elif self.exponent > other.exponent: return self.decrease_exponent_to(other.exponent) + other else: return other.decrease_exponent_to(self.exponent) + self else: raise NotImplementedError
def encode(cls, scalar, gfp: GaloisFieldParams, exponent=FULL_PRECISION): int_rep = round(scalar * cls.BASE**-exponent) return GaloisFieldNumber(gmpy2.mod(int_rep, gfp.p), exponent=exponent, gfp=gfp)