def test_field_caching(self): f2_cached = pfield.GF(2) self.assertEqual(self.f2(1), f2_cached(1)) self.assertEqual(self.f2(1) * f2_cached(1), 1) f19_cached = pfield.GF(19) self.assertEqual(self.f19(3), f19_cached(3)) self.assertEqual(self.f19(3) * f19_cached(3), 9) f101_cached = pfield.GF(101) self.assertEqual(self.f101(3), f101_cached(3)) self.assertEqual(self.f101(3) * f101_cached(23), 69)
def SecFld(p=None, l=None): """Secure prime field of l-bit order p.""" if p is None: if l is None: l = 1 p = pfield.find_prime_root(l, blum=False)[0] else: l = p.bit_length() assert p > len(Share.runtime.parties ), 'Prime field order must exceed number of parties.' # p >= number of parties for MDS field = pfield.GF(p) field.is_signed = False if (l, p) not in _sectypes: class SecureFld(Share): __slots__ = () def __init__(self, value=None): super().__init__(field, value) SecureFld.field = field SecureFld.bit_length = l name = 'SecFld' + str(SecureFld.bit_length) + '(' + str( SecureFld.field.modulus) + ')' _sectypes[(l, p)] = type(name, (SecureFld, ), {'__slots__': ()}) return _sectypes[(l, p)]
def _pfield(l, f, p, n): k = runtime.options.sec_param if p is None: p = pfield.find_prime_root(l + max(f, k + 1) + 1, n=n) else: assert p.bit_length() > l + max(f, k + 1), f'Prime {p} too small.' return pfield.GF(p, f)
def SecFld(order=None, modulus=None, char2=None, l=None): """Secure prime or binary field of (l+1)-bit order. Field is prime by default, and if order (or modulus) is prime. Field is binary if order is a power of 2, if modulus is a polynomial, or if char2 is True. """ if isinstance(modulus, str): modulus = gf2x.Polynomial(modulus) if isinstance(modulus, gf2x.Polynomial): char2 = char2 or (char2 is None) assert char2 # binary field modulus = int(modulus) if order is not None: if order == 2: assert modulus is None or modulus == 2 or modulus == 3 if modulus is None or modulus == 2: # default: prime field char2 = char2 or False else: char2 = char2 or (char2 is None) assert char2 # binary field elif gmpy.is_prime(order): modulus = modulus or order assert modulus == order char2 = char2 or False assert not char2 # prime field elif order % 2 == 0: assert modulus is None or modulus.bit_length() == order.bit_length( ) char2 = char2 or (char2 is None) assert char2 # binary field else: raise ValueError('only prime fields and binary fields supported') l = l or order.bit_length() - 1 assert l == order.bit_length() - 1 if modulus is None: l = l or 1 if char2: modulus = int(bfield.find_irreducible(l)) else: modulus = pfield.find_prime_root(l + 1, blum=False)[0] l = modulus.bit_length() - 1 if char2: field = bfield.GF(modulus) else: field = pfield.GF(modulus) assert runtime.threshold == 0 or field.order > len(runtime.parties), \ 'Field order must exceed number of parties, unless threshold is 0.' # field.order >= number of parties for MDS field.is_signed = False return _SecFld(l, field)
def _SecNum(l, f, n=2): k = Share.runtime.options.security_parameter field = pfield.GF(pfield.find_prime_root(l + max(f, k + 1) + 1, n=n), f) class SecureNum(Share): __slots__ = () def __init__(self, value=None): super().__init__(field, value) SecureNum.field = field SecureNum.bit_length = l return SecureNum
def _SecNum(l, f, p, n): k = runtime.options.security_parameter if p is None: p = pfield.find_prime_root(l + max(f, k + 1) + 1, n=n) else: assert p.bit_length() > l + max(f, k + 1), f'Prime {p} too small.' field = pfield.GF(p, f) class SecureNum(Share): __slots__ = () def __init__(self, value=None): super().__init__(field, value) SecureNum.field = field SecureNum.bit_length = l return SecureNum
def SecFld(order=None, modulus=None, char2=None, l=None): """Secure prime or binary field of (l+1)-bit order. Field is prime by default, and if order (or modulus) is prime. Field is binary if order is a power of 2, if modulus is a polynomial, or if char2 is True. """ if isinstance(modulus, str): modulus = gf2x.Polynomial(modulus) if isinstance(modulus, gf2x.Polynomial): char2 = char2 or (char2 is None) assert char2 # binary field modulus = int(modulus) if order is not None: if order == 2: assert modulus is None or modulus == 2 or modulus == 3 if modulus is None or modulus == 2: # default: prime field char2 = char2 or False else: char2 = char2 or (char2 is None) assert char2 # binary field elif gmpy.is_prime(order): modulus = modulus or order assert modulus == order char2 = char2 or False assert not char2 # prime field elif order % 2 == 0: assert modulus is None or modulus.bit_length() == order.bit_length( ) char2 = char2 or (char2 is None) assert char2 # binary field else: raise ValueError('only prime fields and binary fields supported') l = l or order.bit_length() - 1 assert l == order.bit_length() - 1 if modulus is None: l = l or 1 if char2: modulus = int(bfield.find_irreducible(l)) else: modulus = pfield.find_prime_root(l + 1, blum=False)[0] l = modulus.bit_length() - 1 if char2: field = bfield.GF(modulus) else: field = pfield.GF(modulus) assert runtime.threshold == 0 or field.order > len(runtime.parties), \ 'Field order must exceed number of parties, unless threshold is 0.' # field.order >= number of parties for MDS field.is_signed = False if (modulus, char2) not in _sectypes: class SecureFld(Share): __slots__ = () def __init__(self, value=None): super().__init__(field, value) SecureFld.field = field SecureFld.bit_length = l name = f'SecFld{SecureFld.bit_length}({SecureFld.field.modulus})' _sectypes[(modulus, char2)] = type(name, (SecureFld, ), {'__slots__': ()}) return _sectypes[(modulus, char2)]
def setUp(self): self.f2 = pfield.GF(2) self.f19 = pfield.GF(19) self.f256 = bfield.GF(283)
def setUp(self): self.f2 = pfield.GF(2) self.f19 = pfield.GF(19) # 19 % 4 = 3 self.f101 = pfield.GF(101) # 101 % 4 = 1 self.f19.is_signed = False self.f101.is_signed = False
def setUp(self): self.f2 = pfield.GF(2) self.f19 = pfield.GF(19) # 19 % 4 = 3 self.f101 = pfield.GF(101) # 101 % 4 = 1