Skip to content

Commit

Permalink
Fix off-by-one coefficient upper bound; remove helper function; updat…
Browse files Browse the repository at this point in the history
…e version.
  • Loading branch information
lapets committed Jan 2, 2025
1 parent f3e15d7 commit ededcb7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "shamirs"
version = "3.0.2"
version = "3.0.3"
description = """\
Minimal pure-Python implementation of Shamir's secret \
sharing scheme.\
Expand Down
19 changes: 3 additions & 16 deletions src/shamirs/shamirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
creating secret shares if a prime modulus is not specified explicitly.
"""

def _randint(bound: int) -> int:
"""
Generate a random integer according to an approximately uniform distribution
via rejection sampling.
"""
length = 1 + (bound.bit_length() // 8)

value = int.from_bytes(secrets.token_bytes(length), 'little')
while value >= bound:
value = int.from_bytes(secrets.token_bytes(length), 'little')

return value

class share:
"""
Data structure for representing an individual secret share. Normally, the
Expand Down Expand Up @@ -527,14 +514,14 @@ def shares(
'quantity of shares should be at least the threshold to be reconstructable'
)

# Add the base coefficient.
coefficients = [value] + [_randint(modulus - 1) for _ in range(1, threshold)]
# Create the coefficients.
coefficients = [value] + [secrets.randbelow(modulus) for _ in range(1, threshold)]

# Compute each share value such that ``shares[i] = f(i)`` if the polynomial
# is ``f``.
shares_ = [
sum(
c_j * i ** j % modulus
(c_j * (i ** j)) % modulus
for j, c_j in enumerate(coefficients)
) % modulus
for i in range(1, quantity + 1)
Expand Down

0 comments on commit ededcb7

Please sign in to comment.