Skip to content

Commit

Permalink
GE: Fix batch mul and rename from mul to batch_mul
Browse files Browse the repository at this point in the history
  • Loading branch information
real-or-random committed Mar 12, 2024
1 parent 9a036f7 commit bc16bbe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
14 changes: 7 additions & 7 deletions reference/secp256k1ref/secp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ def sum(*ps):
return sum(ps, start=GE())

@staticmethod
def mul(*aps):
def batch_mul(*aps):
"""Compute a (batch) scalar group element multiplication.
GE.mul((a1, p1), (a2, p2), (a3, p3)) is identical to a1*p1 + a2*p2 + a3*p3,
GE.batch_mul((a1, p1), (a2, p2), (a3, p3)) is identical to a1*p1 + a2*p2 + a3*p3,
but more efficient."""
# Reduce all the scalars modulo order first (so we can deal with negatives etc).
naps = [(a % GE.ORDER, p) for a, p in aps]
naps = [(int(a), p) for a, p in aps]
# Start with point at infinity.
r = GE()
# Iterate over all bit positions, from high to low.
Expand All @@ -282,8 +282,8 @@ def mul(*aps):
def __rmul__(self, a):
"""Multiply an integer with a group element."""
if self == G:
return FAST_G.mul(int(a))
return GE.mul((int(a), self))
return FAST_G.batch_mul(Scalar(a))
return GE.batch_mul((Scalar(a), self))

def __neg__(self):
"""Compute the negation of a group element."""
Expand Down Expand Up @@ -427,9 +427,9 @@ def __init__(self, p):
p = p + p
self.table.append(p)

def mul(self, a):
def batch_mul(self, a):
result = GE()
a = a % GE.ORDER
a = int(a)
for bit in range(a.bit_length()):
if a & (1 << bit):
result += self.table[bit]
Expand Down
21 changes: 7 additions & 14 deletions reference/vss.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class VSSCommitment(NamedTuple):
def t(self):
return len(self.ges)

def verify(self, signer_idx: int, share: Scalar) -> bool:
def verify(self, i: int, share: Scalar) -> bool:
P = share * G
Q = GE.mul(
*((pow(signer_idx + 1, j), self.ges[j]) for j in range(0, len(self.ges)))
Q = GE.batch_mul(
*(((i + 1) ** j, self.ges[j]) for j in range(0, len(self.ges)))
)
return P == Q

Expand All @@ -63,12 +63,9 @@ def group_info(self, n: int) -> GroupInfo:
"""Returns the shared public key and individual public keys of the participants"""
pk = self.ges[0]
participant_public_keys = []
for signer_idx in range(0, n):
pk_i = GE.mul(
*(
(pow(signer_idx + 1, j), self.ges[j])
for j in range(0, len(self.ges))
)
for i in range(0, n):
pk_i = GE.batch_mul(
*((Scalar((i + 1) ** j), self.ges[j]) for j in range(0, self.t()))
)
participant_public_keys += [pk_i]
return GroupInfo(pk, participant_public_keys)
Expand Down Expand Up @@ -100,11 +97,7 @@ def shares(self, n: int) -> List[Scalar]:
return [self.share_for(i) for i in range(0, n)]

def commit(self) -> VSSCommitment:
ges = []
for coeff in self.f.coeffs:
A_i = coeff * G
ges.append(A_i)
return VSSCommitment(ges)
return VSSCommitment([c * G for c in self.f.coeffs])

def secret(self):
return self.f.coeffs[0]

0 comments on commit bc16bbe

Please sign in to comment.