-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa_with_delete.py
80 lines (64 loc) · 2.08 KB
/
rsa_with_delete.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
import secrets
import json
from data import main
from final import setup, batch_add, create_all_membership_witnesses, verify_membership, hash_to_prime, ACCUMULATED_PRIME_SIZE
# Setup
n, A0, S = setup()
# Retrieve transaction hashes
transaction_hashes = main()
def ensure_even_length_hex(hex_str):
if hex_str.startswith("0x"):
hex_str = hex_str[2:]
if len(hex_str) % 2 != 0:
hex_str = '0' + hex_str
return '0x' + hex_str
# Clean transaction hashes
x_values = [hash[2:] if hash.startswith('0x') else hash for hash in transaction_hashes]
# Display initial setup values
print("n", hex(n))
print("A0", A0)
print("S", S)
print("x:", x_values)
# Batch add elements
A1, Map = batch_add(A0, S, x_values, n)
print("A1", A1)
print("A1_hex", ensure_even_length_hex(hex(A1)))
# Create membership witnesses
witness, map = create_all_membership_witnesses(A0, S, n)
# Save witnesses and hashes to files
with open('witness.json', 'w') as f:
json.dump(map, f, indent=4, default=str)
with open('hashes.json', 'w') as f:
json.dump(Map, f, indent=4, default=str)
# Verify membership for each value
for i in range(len(x_values)):
result, hash = verify_membership(A1, x_values[i], 0, witness[i], n)
print(result, hash)
# Delete function
def delete(A0, A, S, x, n):
if x not in S.keys():
return A
else:
del S[x]
product = 1
for element in S.keys():
nonce = S[element]
product *= hash_to_prime(element, ACCUMULATED_PRIME_SIZE, nonce)[0]
Anew = pow(A0, product, n)
return Anew
# Batch delete function
def batch_delete(A0, S, x_list, n):
for x in x_list:
del S[x]
if len(S) == 0:
return A0
return batch_add(A0, S, x_list, n)
# Example usage of delete and batch_delete functions
# Deleting a single element
element_to_delete = x_values[0]
A_new = delete(A0, A1, S, element_to_delete, n)
print("A_new after delete:", A_new)
# Batch deleting elements
elements_to_delete = x_values[:2]
A_new_batch = batch_delete(A0, S, elements_to_delete, n)
print("A_new after batch delete:", A_new_batch)