-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_trusted_setup_ecc.py
92 lines (71 loc) · 2.64 KB
/
basic_trusted_setup_ecc.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
"""
This is an adaption of Trusted Setup using Mod (./basic_trusted_setup_mod.py) using Elliptic Curve Cryptography
Check out Trusted Setup using Mod (./basic_trusted_setup_mod.py) for detailed explanations
"""
import collections
from utils.ecc import ECC
from utils.number_theory import generate_random_prime
class TrustedSetup_ECC(ECC):
d = None # degree
g = None # generator
# values of encrypted exponents of x and encrypted exponents of x times a
# computed by the first party
base_crs: (list[int], list[int]) = None
def __init__(self, curve, d: int, x: int, a: int) -> None:
super().__init__(curve)
self.d = d
self.g = curve.g
encrypted_values_of_f = [
self.scalar_multiplication(
x ** i,
self.g) for i in range(
0,
d + 1)]
encrypted_values_of_f_times_a = [
self.scalar_multiplication(
(x ** i) * a,
self.g) for i in range(
0,
d + 1)]
self.base_crs = (encrypted_values_of_f, encrypted_values_of_f_times_a)
def compute_crs(self, x: int, a: int, crs: (
list[int], list[int])) -> (list[int], list[int]):
assert len(crs[0]) == self.d + 1, "wrong degree"
assert len(crs[1]) == self.d + 1, "wrong degree"
encrypted_values_of_f = crs[0]
encrypted_values_of_f_times_a = crs[1]
crs = ([self.scalar_multiplication(x, i) for i in encrypted_values_of_f], [
self.scalar_multiplication(a, i) for i in encrypted_values_of_f_times_a])
return crs
# USAGE
# Secret of First Participant
x = generate_random_prime(1, 0xffff)
a = generate_random_prime(1, 0xffff)
# Public
d = 3
g = 5
EllipticCurve = collections.namedtuple('EllipticCurve', 'name p a b g n h')
# Set the domain parameters specific to the curve
curve = EllipticCurve(
'secp256k1',
# Field characteristic.
p=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f,
# Curve coefficients.
a=0,
b=7,
# Base point.
g=(0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8),
# Subgroup order.
n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141,
# Subgroup cofactor.
h=1,
)
trusted_setup = TrustedSetup_ECC(curve, d, x, a)
crs = trusted_setup.base_crs
partipants = 10
for i in range(0, partipants):
x = generate_random_prime(1, 0xffff)
a = generate_random_prime(1, 0xffff)
crs = trusted_setup.compute_crs(x, a, crs)
print("Common Reference String:", crs)