From 401fd928088f51e90e391377c9d696b162d1878f Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Tue, 2 Jul 2019 15:30:01 +0200 Subject: Réécriture de M et M² en Python MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pour que le lien avec la spec saute aux yeux. --- src/add_python/lilliput/multiplications.py | 73 ++++++++++++++++++------------ 1 file changed, 45 insertions(+), 28 deletions(-) (limited to 'src/add_python/lilliput/multiplications.py') diff --git a/src/add_python/lilliput/multiplications.py b/src/add_python/lilliput/multiplications.py index 2dea948..3b50987 100644 --- a/src/add_python/lilliput/multiplications.py +++ b/src/add_python/lilliput/multiplications.py @@ -19,40 +19,57 @@ from ALPHAS[0] = α₀ = I to ALPHAS[6] = α₆ = M_R³. """ -def _multiply_M(lane): - multiplied_lane = [lane[(byte-1) % 8] for byte in range(0, 8)] +from functools import reduce +from operator import xor - multiplied_lane[2] ^= ((lane[6] << 2) & 0xff) - multiplied_lane[4] ^= ((lane[4] >> 3) & 0xff) - multiplied_lane[5] ^= ((lane[5] << 3) & 0xff) - return multiplied_lane +def _Sl(n): + return lambda xi: (xi<>n -def _multiply_M2(lane): - multiplied_lane = [lane[(byte-2) % 8] for byte in range(0, 8)] +def _Id(xi): + return xi - multiplied_lane[2] ^= ((lane[5] << 2) & 0xff) - multiplied_lane[3] ^= ((lane[6] << 2) & 0xff) - multiplied_lane[4] ^= ((lane[3] >> 3) & 0xff) ^ ((lane[4] >> 6) & 0xff) - multiplied_lane[5] ^= ((lane[5] << 6) & 0xff) - multiplied_lane[6] ^= ((lane[5] << 3) & 0xff) +def _0(xi): + return 0 - # binary matrix M1 - multi_mat_l4_m1 = 0 - l4 = lane[4] - multi_mat_l4_m1 ^= ((l4 & 0x8) >> 3) - multi_mat_l4_m1 ^= ((l4 & 0x10) >> 3) - multi_mat_l4_m1 ^= ((l4 & 0x20) >> 3) - multi_mat_l4_m1 ^= ((l4 & 0x40) >> 3) ^ ((l4 & 0x1) << 3) - multi_mat_l4_m1 ^= ((l4 & 0x80) >> 3) ^ ((l4 & 0x2) << 3) - multi_mat_l4_m1 ^= ((l4 & 0x04) << 3) - multi_mat_l4_m1 ^= ((l4 & 0x08) << 3) - multi_mat_l4_m1 ^= ((l4 & 0x10) << 3) +def _M1(xi): + return (xi<<3 ^ xi>>3) & 0xff - multiplied_lane[5] ^= multi_mat_l4_m1 - return multiplied_lane +M = ( + ( _0, _Id, _0, _0, _0, _0, _0, _0), + ( _0, _0, _Id, _0, _0, _0, _0, _0), + ( _0, _0, _Sl(3), _Id, _0, _0, _0, _0), + ( _0, _0, _0, _Sr(3), _Id, _0, _0, _0), + ( _0, _0, _0, _0, _0, _Id, _0, _0), + ( _0, _Sl(2), _0, _0, _0, _0, _Id, _0), + ( _0, _0, _0, _0, _0, _0, _0, _Id), + (_Id, _0, _0, _0, _0, _0, _0, _0), +) + +M2 = ( + ( _0, _0, _Id, _0, _0, _0, _0, _0), + ( _0, _0, _Sl(3), _Id, _0, _0, _0, _0), + ( _0, _0, _Sl(6), _M1, _Id, _0, _0, _0), + ( _0, _0, _0, _Sr(6), _Sr(3), _Id, _0, _0), + ( _0, _Sl(2), _0, _0, _0, _0, _Id, _0), + ( _0, _0, _Sl(2), _0, _0, _0, _0, _Id), + (_Id, _0, _0, _0, _0, _0, _0, _0), + ( _0, _Id, _0, _0, _0, _0, _0, _0), +) + + +def _multiplication(m): + def _multiply(x): + return list(reversed([ + reduce(xor, (mj[i](xi) for i, xi in enumerate(reversed(x)))) + for mj in m + ])) + + return _multiply def _multiply_M3(lane): @@ -198,8 +215,8 @@ def _multiply_MR3(lane): ALPHAS = ( list, # Identity. - _multiply_M, - _multiply_M2, + _multiplication(M), + _multiplication(M2), _multiply_M3, _multiply_MR, _multiply_MR2, -- cgit v1.2.3