Пример #1
0
 def test_field_caching(self):
     f2_cached = bfield.GF(2)
     self.assertEqual(self.f2(1), f2_cached(1))
     self.assertEqual(self.f2(1) * f2_cached(1), self.f2(1))
     f256_cached = bfield.GF(283)
     self.assertEqual(self.f256(3), f256_cached(3))
     self.assertEqual(self.f256(3) * f256_cached(3), self.f256(5))
     self.assertEqual(self.f256(48) * f256_cached(16), self.f256(45))
Пример #2
0
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)
Пример #3
0
    def test_f256(self):
        f256 = self.f256
        self.assertFalse(f256(0))
        self.assertTrue(f256(1))
        self.assertEqual(f256(1) + 0, f256(0) + f256(1))
        self.assertEqual(f256(1) + 1, f256(0))
        self.assertEqual(f256(3) * 0, f256(0))
        self.assertEqual(f256(3) * 1, f256(3))
        self.assertEqual(f256(16) * f256(16), f256(27))
        self.assertEqual(f256(32) * f256(16), f256(54))
        self.assertEqual(f256(57) * f256(67), f256(137))
        self.assertEqual(f256(67) * f256(57), f256(137))
        self.assertEqual(f256(137) / f256(57), f256(67))
        self.assertEqual(f256(137) / f256(67), f256(57))

        a = f256(0)
        b = f256(1)
        a += b
        self.assertEqual(a, f256(1))
        a += 1
        self.assertEqual(a, f256(0))
        a -= b
        self.assertEqual(a, f256(1))
        a *= b
        self.assertEqual(a, f256(1))
        a *= 1
        self.assertEqual(a, f256(1))
        a /= 1
        self.assertEqual(a, f256(1))
        a <<= 0
        a >>= 0
        self.assertEqual(a, f256(1))
        a <<= 2
        self.assertEqual(a, f256(4))
        a >>= 2
        self.assertEqual(a, f256(1))

        a = f256(3)  # generator x + 1
        s = [int((a**i).value) for i in range(255)]
        self.assertListEqual(sorted(s), list(range(1, 256)))
        s = [int((a**i).value) for i in range(-255, 0)]
        self.assertListEqual(sorted(s), list(range(1, 256)))

        f256 = bfield.GF(391)  # primitive polynomial x^8 + x^7 + x^2 + x + 1
        a = f256(2)  # generator x
        s = [int((a**i).value) for i in range(255)]
        self.assertListEqual(sorted(s), list(range(1, 256)))
Пример #4
0
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)]
Пример #5
0
 def setUp(self):
     self.f2 = pfield.GF(2)
     self.f19 = pfield.GF(19)
     self.f256 = bfield.GF(283)
Пример #6
0
 def setUp(self):
     self.f2 = bfield.GF(2)
     self.f256 = bfield.GF(283)  # AES polynomial (283)_2 = x^8 + x^4 + x^3 + x + 1