arxpy.differential.difference module¶
Manipulate differences.
-
class
arxpy.differential.difference.
Difference
(value)[source]¶ Bases:
object
Represent differences.
The difference between two
Term
\(x\) and \(y\) is defined as \(\alpha = y - x\), where the difference operation \(-\) is a bit-vectorOperation
. In other words, the pair \((x, x + \alpha)\) has difference \(\alpha\), where \(+\) is the inverse of the difference operation.The most common difference used in differential cryptanalysis is the XOR difference
XorDiff
(where the difference operation isBvXor
). Other examples are the additive difference (where the difference operation isBvSub
) or the rotational-XOR differenceRXDiff
.Note that arithmetic with differences is not supported. For example, two
Difference
objectsd1
andd2
cannot be XORed, i.e.,d1 ^ d2
. This can be done instead by performing the arithmetic with the difference values and converting the resultingTerm
to a difference, that is,Difference(d1.val ^ d2.val)
This class is not meant to be instantiated but to provide a base class for the different types of differences.
-
inv_diff_op
¶ the inverse of the difference operation.
-
xreplace
(rule)[source]¶ Replace occurrences of differences within the expression.
The argument
rule
is a dict-like object representing the replacement rule.This method is similar to SymPy xreplace but with the restriction that only differences objects are allowed in
rule
.
-
classmethod
from_pair
(x, y)[source]¶ Return the
Difference
\(\alpha = y - x\) given twoTerm
.
-
classmethod
derivative
(op, input_diff)[source]¶ Return the derivative of
op
at the pointinput_diff
.The derivative of an
Operation
\(f\) at the point \(\alpha\) (also called the input difference) is defined as \(f_{\alpha} (x) = f(x + \alpha) - f(x)\). Note that \(f_{\alpha} (x)\) is the difference of \((f(x), f(x + \alpha))\).If \(f\) has multiple operands, \(\alpha\) is a list containing the
Difference
of each operand and the computation \(x + \alpha\) is defined component-wise, that is, \(x = (x_1, \dots, x_n)\), \(\alpha = (\alpha_1, \dots, \alpha_n)\), and \(x + \alpha = (x_1 + \alpha_1, \dots, x_n + \alpha_n)\).For some operations, there is a unique output difference \(\beta\) for every input difference \(\alpha\), that is, \(f_{\alpha}(x) = \beta\) is a constant function. In this case, this method returns the
Difference
\(\beta\). Otherwise, it returns aDerivative
object representing \(f_{\alpha}\).Operations with scalar operands are not supported, but these operands can be removed with
make_partial_operation
and the derivative of the resulting operator can then be computed.- Parameters
op – a bit-vector operator
input_diff – a list containing the difference of each operand
-
-
class
arxpy.differential.difference.
XorDiff
(value)[source]¶ Bases:
arxpy.differential.difference.Difference
Represent XOR differences.
The XOR difference of two
Term
is given by the XOR of the terms. In other words, the difference operation ofXorDiff
is theBvXor
(seeDifference
).>>> from arxpy.bitvector.core import Constant, Variable >>> from arxpy.differential.difference import XorDiff >>> x, y = Constant(0b000, 3), Constant(0b000, 3) >>> alpha = XorDiff.from_pair(x, y) >>> alpha XorDiff(0b000) >>> alpha.get_pair_element(x) 0b000 >>> x, y = Constant(0b010, 3), Constant(0b101, 3) >>> alpha = XorDiff.from_pair(x, y) >>> alpha XorDiff(0b111) >>> alpha.get_pair_element(x) 0b101 >>> k = Variable("k", 8) >>> alpha = XorDiff.from_pair(k, k) >>> alpha XorDiff(0x00) >>> alpha.get_pair_element(k) k
-
diff_op
¶ alias of
arxpy.bitvector.operation.BvXor
-
inv_diff_op
¶ alias of
arxpy.bitvector.operation.BvXor
-
classmethod
derivative
(op, input_diff)[source]¶ Return the derivative of
op
at the pointinput_diff
.See
Difference.derivative
for more information.>>> from arxpy.bitvector.core import Variable, Constant >>> from arxpy.bitvector.operation import BvAdd, BvXor, RotateLeft, BvSub >>> from arxpy.bitvector.extraop import make_partial_operation >>> from arxpy.differential.difference import XorDiff >>> d1, d2 = XorDiff(Variable("d1", 8)), XorDiff(Variable("d2", 8)) >>> XorDiff.derivative(BvXor, [d1, d2]) XorDiff(d1 ^ d2) >>> Xor1 = make_partial_operation(BvXor, tuple([None, Constant(1, 8)])) >>> XorDiff.derivative(Xor1, d1) XorDiff(d1) >>> Rotate1 = make_partial_operation(RotateLeft, tuple([None, 1])) >>> XorDiff.derivative(Rotate1, d1) XorDiff(d1 <<< 1) >>> XorDiff.derivative(BvAdd, [d1, d2]) XDA(XorDiff(d1), XorDiff(d2)) >>> XorDiff.derivative(BvSub, [d1, d2]) XDS(XorDiff(d1), XorDiff(d2)) >>> CteAdd1 = make_partial_operation(BvAdd, tuple([None, Constant(1, 8)])) >>> XorDiff.derivative(CteAdd1, d1) XDCA_0x01(XorDiff(d1))
-
-
class
arxpy.differential.difference.
RXOp
[source]¶ Bases:
arxpy.bitvector.operation.Operation
The difference operation of
RXDiff
.
-
class
arxpy.differential.difference.
RXInvOp
[source]¶ Bases:
arxpy.bitvector.operation.Operation
The inverse of the difference operation of
RXDiff
.
-
class
arxpy.differential.difference.
RXDiff
(value)[source]¶ Bases:
arxpy.differential.difference.Difference
Represent rotational-XOR (RX) differences.
The pair
(x, (x <<< 1) ^ d)
has RX differenced
. In other words, the RX difference of twoTerms
x
andy
is defined as(x <<< 1) ^ y
.See
Difference
for more information.>>> from arxpy.bitvector.core import Constant, Variable >>> from arxpy.differential.difference import RXDiff >>> x, y = Constant(0b000, 3), Constant(0b000, 3) >>> alpha = RXDiff.from_pair(x, y) >>> alpha RXDiff(0b000) >>> alpha.get_pair_element(x) 0b000 >>> x, y = Constant(0b000, 3), Constant(0b001, 3) >>> alpha = RXDiff.from_pair(x, y) >>> alpha RXDiff(0b001) >>> alpha.get_pair_element(x) 0b001 >>> k = Variable("k", 8) >>> alpha = RXDiff.from_pair(k, k) >>> alpha RXDiff(k ^ (k <<< 1)) >>> alpha.get_pair_element(k) k
-
classmethod
derivative
(op, input_diff)[source]¶ Return the derivative of
op
at the pointinput_diff
.See
Difference.derivative
for more information.>>> from arxpy.bitvector.core import Variable, Constant >>> from arxpy.bitvector.operation import BvAdd, BvXor, RotateLeft >>> from arxpy.bitvector.extraop import make_partial_operation >>> from arxpy.differential.difference import RXDiff >>> d1, d2 = RXDiff(Variable("d1", 8)), RXDiff(Variable("d2", 8)) >>> RXDiff.derivative(BvXor, [d1, d2]) RXDiff(d1 ^ d2) >>> Xor1 = make_partial_operation(BvXor, tuple([None, Constant(1, 8)])) >>> RXDiff.derivative(Xor1, d1) RXDiff(0x03 ^ d1) >>> Rotate1 = make_partial_operation(RotateLeft, tuple([None, 1])) >>> RXDiff.derivative(Rotate1, d1) RXDiff(d1 <<< 1) >>> RXDiff.derivative(BvAdd, [d1, d2]) RXDA(RXDiff(d1), RXDiff(d2))
-
classmethod