-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2nd_scheme_countermeasure.py
102 lines (83 loc) · 2.83 KB
/
2nd_scheme_countermeasure.py
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
92
93
94
95
96
97
98
99
100
101
102
import numpy as np
import hashlib
from Crypto.Util import number
import gmpy2
from gmpy2 import mpz
import random
import timeit
def setup_phase(bit):
p = number.getPrime(bit)
prime_number = mpz(p)
security_number = gmpy2.isqrt(prime_number)
log_p_10 = gmpy2.log10(p)
m = security_number*log_p_10
n = 2*m
m = int(m)
n = int(n)
return p, m, n
def key_generation(m, n, p):
#B = np.random.randint(0, p, (n, n))
B = generate_random_matrix(n, n, p)
#C = np.random.randint(0, p, (n, m))
C = generate_random_matrix(n, m, p)
D = np.dot(B, C)
return B, C, D
def sign_message(P, B, D):
#a = np.random.randint(0, p, n)
a = generate_random_matrix(n, 1, p)
#A1 = np.dot(B.T,a.reshape(n, 1))
A1 = np.dot(np.transpose(B),a)
#A2 = P.T + np.dot(a.reshape(1, n), D)
A2 = np.transpose(P) + np.dot(np.transpose(a), D)
#print("A1: ", A1) #
#print("A2: ", A2) #
return A1, A2
def verify_signature( P, A1, A2, C):
W = np.dot(A1.T, C)
#print("W: ", W)
w_modp = W % p
if (W.shape == (1, m) and (np.all((w_modp >= 0) & (w_modp < p)))):
P1_T = (A2 - W) % p
#print("P1_T: ", P1_T)
return np.array_equal(hash_function((np.transpose(P1_T.tolist())).tolist()), hash_function(P))
else :
print("check 1 false")
return False
def hash_function(input_vector):
"""
Hash function that takes input from Z_q^m and outputs in Z_q^m.
Args:
- input_vector: Input vector from Z_q^m (list of integers)
- q: Prime number representing the modulus
Returns:
- hashed_vector: Hashed vector in Z_q^m (list of integers)
"""
input_bytes = bytearray()
for num in input_vector:
if isinstance(num, int):
input_bytes.extend(num.to_bytes((num.bit_length() + 7) // 8, byteorder='big'))
hashed_bytes = hashlib.sha256(input_bytes).digest()
hashed_vector = []
for i in range(len(input_vector)):
hashed_int = int.from_bytes(hashed_bytes[i*2:(i+1)*2], byteorder='big') % p
hashed_vector.append(hashed_int)
print("hashed_vector: ", hashed_vector)
return hashed_vector
def generate_random_matrix(n, m, p):
n = int(n)
m = int(m)
return [[random.randint(0, p-1) for _ in range(m)] for _ in range(n)]
start_time = timeit.default_timer()
p, m, n = setup_phase(12)
#p = number.getPrime(28)
#m = 5
#n = 3
B, C, D = key_generation(m, n, p)
#P = np.random.randint(0, p, (n, 1))
P = generate_random_matrix(m, 1, p)
A1, A2 = sign_message(P, B, D)
verification_result = verify_signature(P, A1, A2, C)
print("Verification Result:", verification_result)
end_time = timeit.default_timer()
execution_time = end_time - start_time
print("execution time: ", execution_time)