-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1other.py
More file actions
126 lines (97 loc) · 3.44 KB
/
1other.py
File metadata and controls
126 lines (97 loc) · 3.44 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
import hashlib
from Crypto.Util import number
import gmpy2
from gmpy2 import mpz
import random
import timeit
import math
def setup_phase(bit):
p = number.getPrime(bit)
prime_number = mpz(p)
security_number = gmpy2.isqrt(prime_number)
log_p_10 = gmpy2.log10(p)
n = security_number*log_p_10
m = math.ceil(n * math.log(p))
m= int(m)
n= int(n)
return p, m, n
def key_generation(m, n, p):
B = generate_random_matrix(n, n, p)
C = generate_random_matrix(n, m, p)
D = np.dot(B, C)
return B, C, D
def mod_dot(A, B, p):
return np.mod(np.dot(A, B), p)
def sign_message(P, B, D):
a = generate_random_matrix(n, 1, p)
A1 = mod_dot(B,P, p)
A2 = mod_dot(a.T, A1, p)
A3 = mod_dot(a.T, B, p)
A4 = mod_dot(a.T, D, p)
A5= hash_function(A2.flatten(), m)
A6 = (A4 + A5) % p
return A3, A6
def verify_signature(P, A3, A6):
A4_dash = mod_dot(A3, C, p)
A5_dash = (A6 - A4_dash) % p
W = mod_dot(A3, P, p).flatten()
if ((np.all((W >= 0) & (W < p)))):
return np.array_equal(hash_function(W, m), A5_dash.flatten())
else :
print("check 1 false")
return False
def hash_function(input_vector, m):
input_bytes = bytearray()
for num in input_vector:
num = int(num)
input_bytes.extend(num.to_bytes((num.bit_length() + 7) // 8, 'big'))
hashed = hashlib.sha256(input_bytes).digest()
hashed_vector = []
counter = 0
while len(hashed_vector) < m:
h = hashlib.sha256(hashed + counter.to_bytes(4, 'big')).digest()
for i in range(0, len(h), 2):
if len(hashed_vector) < m:
hashed_vector.append(int.from_bytes(h[i:i+2], 'big') % p)
counter += 1
print("hashed_vector: ", hashed_vector)
return hashed_vector
# def hash_function(input_vector):
# """
# Hash function that takes input from Z_p and outputs in Z_p^m.
# Args:
# - input_vector: Input vector from Z_p (list of integers)
# - p: Prime number representing the modulus
# Returns:
# - hashed_vector: Hashed vector in Z_p^m (list of integers)
# """
# # Convert input vector to bytes
# input_bytes = bytearray()
# for num in input_vector:
# if isinstance(num, (int, np.integer)):
# input_bytes.extend(num.to_bytes((int(num).bit_length() + 7) // 8, byteorder='big'))
# # Compute hash using SHA-256
# hashed_bytes = hashlib.sha256(input_bytes).digest()
# # Convert hashed bytes back to integers in Z_q
# 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 np.array(hashed_vector)
def generate_random_matrix(n, m, p):
p = int(p)
n= int(n)
m= int(m)
return np.array([[random.randint(0, p-1) for _ in range(m)] for _ in range(n)])
start_time = timeit.default_timer()
p, m, n = setup_phase(8)
B, C, D = key_generation(m, n, p)
P = generate_random_matrix(n, 1, p)
A3, A6 = sign_message(P, B, D)
verification_result = verify_signature(P, A3, A6)
print("Verification Result:", verification_result)
end_time = timeit.default_timer()
execution_time = end_time - start_time
print("execution time: ", execution_time)