INNER CODE UNIT · Python
hash_to_scalar
schollz/pake · ed25519_points.py:15
def hash_to_scalar(seed: str) -> bytes:
"""
Generate a 32-byte scalar from a seed string using SHA-256.
This mimics the hash-based point generation approach.
"""
# Hash the seed multiple times to get sufficient entropy
hash_input = seed.encode("utf-8")
for _ in range(1000): # Multiple rounds for better distribution
hash_input = hashlib.sha256(hash_input).digest()
# Take the first 32 bytes and clamp for ed25519
scalar = bytearray(hash_input[:32])
# Apply ed25519 scalar clamping
scalar[0] &= 248 # Clear bottom 3 bits
scalar[31] &= 127 # Clear top bit
scalar[31] |= 64 # Set second-highest bit