-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcript.py
69 lines (55 loc) · 2.12 KB
/
transcript.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
from curve import Scalar, G1Point
from merlin.merlin_transcript import MerlinTranscript
from py_ecc.secp256k1.secp256k1 import bytes_to_int
from dataclasses import dataclass
@dataclass
class Message1:
# Commitments in G1
m_comm_1: G1Point
@dataclass
class Message2:
# Commitments in G1
A_comm_1: G1Point
Q_A_comm_1: G1Point
f_comm_1: G1Point
B_0_comm_1: G1Point
Q_B_comm_1: G1Point
P_comm_1: G1Point
@dataclass
class Message3:
# Commitments in G1
b_0_at_gamma: G1Point
f_at_gamma: G1Point
a_at_0: G1Point
pi_gamma: G1Point
a_0_comm_1: G1Point
# https://merlin.cool/
class Transcript(MerlinTranscript):
def append(self, label: bytes, item: bytes) -> None:
self.append_message(label, item)
def append_scalar(self, label: bytes, item: Scalar):
self.append_message(label, item.n.to_bytes(32, "big"))
def append_point(self, label: bytes, item: G1Point):
self.append_message(label, item[0].n.to_bytes(32, "big"))
self.append_message(label, item[1].n.to_bytes(32, "big"))
def get_and_append_challenge(self, label: bytes) -> Scalar:
while True:
challenge_bytes = self.challenge_bytes(label, 255)
f = Scalar(bytes_to_int(challenge_bytes))
if f != Scalar.zero(): # Enforce challenge != 0
self.append(label, challenge_bytes)
return f
def round_1(self, message: Message1) -> tuple[Scalar]:
self.append_point(b"m_comm_1", message.m_comm_1)
beta = self.get_and_append_challenge(b"beta")
return beta
def round_2(self, message: Message2) -> tuple[Scalar, Scalar]:
self.append_point(b"A_comm_1", message.A_comm_1)
self.append_point(b"Q_A_comm_1", message.Q_A_comm_1)
self.append_point(b"f_comm_1", message.f_comm_1)
self.append_point(b"B_0_comm_1", message.B_0_comm_1)
self.append_point(b"Q_B_comm_1", message.Q_B_comm_1)
self.append_point(b"P_comm_1", message.P_comm_1)
gamma = self.get_and_append_challenge(b"gamma")
eta = self.get_and_append_challenge(b"eta")
return gamma, eta