1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| from os import urandom from Crypto.Util.number import * import random from hashlib import sha256 import string
p = 18446744073709551557 M = [[8, 56, 280], [18446744073709551543, 18446744073709551467, 18446744073709551123], [7, 35, 155]] M_inv = [[9511602413006487524, 10376293541461622753, 4611686018427387891], [720575940379279356, 5188146770730811374, 6917529027641081833], [8214565720323784678, 2882303761517117431, 6917529027641081834]] ConInv = [0x39a3f978106bac2d,0x2940e055f4a33725,0xfda9a7a293fb5bc9] Con = [[0x9c52c2de7a9373c4,0xf2135cb886d0fa21,0x957df7f3cd4879e9], [0xd54f837d2738d717,0x400ddf1ffaae436d,0xc2abb601d9a26b07], [0x1904359f1deb3495,0xc21aa09ba52b157b,0x3d45525db1b19a0c], [0xed66cf26a65afc73,0x1cee569b29ffa476,0x3da45abf4304849], [0x1c1a642fa0f3d96d,0x59a1c4fbb96aec86,0xa18e9ca93163f63d], [0x9621ec9fbcb402be,0xd69468353c31bee0,0x50655b3f20fee3b8], [0x109cde7a61c2c195,0x5ebbd9e98be60c59,0x334d2d15f6e43190], [0x47af2b0d63901977,0x67ace097bf8c6f34,0xb87da3296b70d64b], [0x52d6344b38f49899,0xad5773add31420e1,0xecd0b7480f8c8095], [0xe2afb6d20f5decda,0xb1767d8be7d1371,0x902fd6806a0ef4db]] assert len(Con) == 10 Inv = inverse(3, p-1) Round = 2
def add(x, y): return [(a + b)%p for a, b in zip(x, y)]
def reduce(x,y): return [(a - b)%p for a, b in zip(x, y)]
def multiply(x, M): result = [] for i in range(len(M[0])): temp = 0 for j in range(len(x)): temp += x[j] * M[j][i] result.append(temp%p) return result
def recover(R,X): Z = [0, 0, 0] for r in range(R-1,-1,-1): UM = reduce(X,Con[(2*r+1)%10]) U = multiply(UM,M_inv)
for i in range(3): Z[i] = pow(U[i],3,p)
YM = reduce(Z,Con[2*r%10]) Y = multiply(YM,M_inv)
for i in range(3): X[i] = pow(Y[i],Inv,p) P = reduce(X,ConInv) return P
def Rescue_Prime(R, P): X = add(P, ConInv) Y = [0, 0, 0] Z = [0, 0, 0] U = [0, 0, 0] for r in range(R): for i in range(3): Y[i] = pow(X[i], 3, p) Z = add(Con[2*r%10], multiply(Y, M)) for i in range(3): U[i] = pow(Z[i], Inv, p)
X = add(Con[(2*r+1)%10], multiply(U, M)) return X
def demo(P): X = add(P, ConInv) Y = [0, 0, 0] Z = [0, 0, 0] U = [0, 0, 0] for r in range(2): for i in range(3): Y[i] = pow(X[i], 3, p) Z = add(Con[2*r%10], multiply(Y, M)) for i in range(3): U[i] = pow(Z[i], Inv, p) X = add(Con[(2*r+1)%10], multiply(U, M)) return X
test = [5329202944861711021, 10075872277090249537, 6598944197421011167,0] result = Rescue_Prime(2,test) print(f'result = {result}') test = [1,1,0] tmp = recover(2,test) print(tmp) ''' result = [1, 1, 0] [5329202944861711021, 10075872277090249537, 6598944197421011167] '''
|