Skip to content

Commit

Permalink
Use rejection sampling for random values; use a 128-bit Sophie Germai…
Browse files Browse the repository at this point in the history
…n prime.
  • Loading branch information
lapets committed Nov 8, 2023
1 parent 9769e90 commit cc5ecb4
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/tinynmc/tinynmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import doctest
import operator
import functools
import random
import secrets
from modulo import mod

def _prod(iterable):
Expand All @@ -22,7 +22,8 @@ def _merge(d, d_):
def _shares(s, modulus, quantity) -> list[mod]:
ss = []
for _ in range(quantity - 1):
ss.append(mod(random.randint(0, modulus - 1), modulus))
# Use rejection sampling to obtain a share value.
ss.append(mod(secrets.randbelow(modulus), modulus))

return [mod(s, modulus) - sum(ss)] + ss

Expand Down Expand Up @@ -107,8 +108,9 @@ def __init__(self):
"""
Instantiate an object that maintains the state of a node.
"""
(self.p, self.q) = (4215209819, 2107604909)
self.g = mod(3844384293, self.p)
self.q = 170141183460469231731687303715884098003
self.p = 340282366920938463463374607431768196007
self.g = mod(205482397601703717038466705921080247554, self.p)
self._masks = None
self._shares = None

Expand Down Expand Up @@ -163,11 +165,12 @@ def preprocess(signature, nodes):
Simulate a preprocessing phase for the supplied signature and collection
of nodes.
"""
(p, q) = (4215209819, 2107604909)
g = mod(3844384293, p)
q = 170141183460469231731687303715884098003
p = 340282366920938463463374607431768196007
g = mod(205482397601703717038466705921080247554, p)

randoms = [
random.randint(0, (q * 2) - 1)
secrets.randbelow(q * 2) # Use rejection sampling.
for term_index in range(len(signature))
]
node_to_exponent_shares = list(zip(*[
Expand Down

0 comments on commit cc5ecb4

Please sign in to comment.