def __init__(self, parent, value): """ TESTS:: sage: F = GF(3).algebraic_closure() sage: TestSuite(F.gen(2)).run(skip=['_test_pickling']) .. NOTE:: The ``_test_pickling`` test has to be skipped because there is no coercion map between the parents of ``x`` and ``loads(dumps(x))``. """ if is_FiniteFieldElement(value): n = value.parent().degree() else: from sage.rings.integer import Integer n = Integer(1) self._value = parent._subfield(n).coerce(value) self._level = n FieldElement.__init__(self, parent)
def __init__(self, parent, value): """ TEST:: sage: F = GF(3).algebraic_closure() sage: TestSuite(F.gen(2)).run(skip=['_test_pickling']) .. NOTE:: The ``_test_pickling`` test has to be skipped because there is no coercion map between the parents of ``x`` and ``loads(dumps(x))``. """ if is_FiniteFieldElement(value): n = value.parent().degree() else: from sage.rings.integer import Integer n = Integer(1) self._value = parent._subfield(n).coerce(value) self._level = n FieldElement.__init__(self, parent)
def __init__(self, *args, **kwargs): """ Construct a substitution box (S-box) for a given lookup table `S`. INPUT: - ``S`` - a finite iterable defining the S-box with integer or finite field elements - ``big_endian`` - controls whether bits shall be ordered in big endian order (default: ``True``) EXAMPLES: We construct a 3-bit S-box where e.g. the bits (0,0,1) are mapped to (1,1,1).:: sage: from sage.crypto.sbox import SBox sage: S = SBox(7,6,0,4,2,5,1,3); S (7, 6, 0, 4, 2, 5, 1, 3) sage: S(0) 7 TESTS:: sage: from sage.crypto.sbox import SBox sage: S = SBox() Traceback (most recent call last): ... TypeError: No lookup table provided. sage: S = SBox(1, 2, 3) Traceback (most recent call last): ... TypeError: Lookup table length is not a power of 2. sage: S = SBox(5, 6, 0, 3, 4, 2, 1, 2) sage: S.n 3 """ if "S" in kwargs: S = kwargs["S"] elif len(args) == 1: S = args[0] elif len(args) > 1: S = args else: raise TypeError("No lookup table provided.") _S = [] for e in S: if is_FiniteFieldElement(e): e = e.polynomial().change_ring(ZZ).subs( e.parent().characteristic() ) _S.append(e) S = _S if not ZZ(len(S)).is_power_of(2): raise TypeError("Lookup table length is not a power of 2.") self._S = S self.m = ZZ(len(S)).exact_log(2) self.n = ZZ(max(S)).nbits() self._F = GF(2) self._big_endian = kwargs.get("big_endian",True) self.differential_uniformity = self.maximal_difference_probability_absolute
def __init__(self, *args, **kwargs): """ Construct a substitution box (S-box) for a given lookup table `S`. INPUT: - ``S`` - a finite iterable defining the S-box with integer or finite field elements - ``big_endian`` - controls whether bits shall be ordered in big endian order (default: ``True``) EXAMPLE: We construct a 3-bit S-box where e.g. the bits (0,0,1) are mapped to (1,1,1).:: sage: S = mq.SBox(7,6,0,4,2,5,1,3); S (7, 6, 0, 4, 2, 5, 1, 3) sage: S(0) 7 TESTS:: sage: S = mq.SBox() Traceback (most recent call last): ... TypeError: No lookup table provided. sage: S = mq.SBox(1, 2, 3) Traceback (most recent call last): ... TypeError: Lookup table length is not a power of 2. sage: S = mq.SBox(5, 6, 0, 3, 4, 2, 1, 2) sage: S.n 3 """ if "S" in kwargs: S = kwargs["S"] elif len(args) == 1: S = args[0] elif len(args) > 1: S = args else: raise TypeError("No lookup table provided.") _S = [] for e in S: if is_FiniteFieldElement(e): e = e.polynomial().change_ring(ZZ).subs( e.parent().characteristic() ) _S.append(e) S = _S if not ZZ(len(S)).is_power_of(2): raise TypeError("Lookup table length is not a power of 2.") self._S = S self.m = ZZ(len(S)).exact_log(2) self.n = ZZ(max(S)).nbits() self._F = GF(2) self._big_endian = kwargs.get("big_endian",True) self.differential_uniformity = self.maximal_difference_probability_absolute