Skip to content

Commit cb36c5c

Browse files
committed
Remove entropy_to_codex32 & fingerprint until cli
1 parent e42354e commit cb36c5c

File tree

1 file changed

+0
-86
lines changed

1 file changed

+0
-86
lines changed

src/codex32/codex32.py

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -416,89 +416,3 @@ def __eq__(self, other):
416416

417417
def __hash__(self):
418418
return hash((self.hrp, self.k, self.id, self.share_index, self.payload, self.checksum))
419-
420-
421-
def fingerprint(seed):
422-
"""Generate a 4-character bech32 fingerprint from a master seed."""
423-
BIP32Node._generator = secp256k1_generator
424-
node = BIP32Node.from_master_secret(seed)
425-
return convertbits(node.fingerprint(), 8, 5)[:4]
426-
427-
428-
def entropy_to_codex32(hrp: str, k: int, ident: str, length: int, user_entropy: bytes, n: int, indices: str):
429-
"""If caller does not provide entropy use secrets.token_bytes"""
430-
id = []
431-
for c in ident.ljust(4,"?"):
432-
if c not in CHARSET + "?":
433-
raise ValueError(f"Invalid character '{c}' in ident.")
434-
id.append(CHARSET.find(c))
435-
ms32_ident = id
436-
if length < 16 or length > 64:
437-
raise ValueError("length must be between 16 and 64.")
438-
if n > 31:
439-
raise ValueError("n must be under 32.")
440-
if len(indices) > n:
441-
raise ValueError("Length of indices can't exceed n.")
442-
if len(set(indices)) != len(indices):
443-
raise ValueError("indices must contain unique characters.")
444-
if k:
445-
r = secrets.SystemRandom()
446-
available_indices = list(CHARSET)
447-
for c in indices + 's': # 's' is reserved for k=0
448-
if c not in CHARSET:
449-
raise ValueError(f"Invalid character '{c}' in indices.")
450-
if c in available_indices:
451-
available_indices.remove(c)
452-
r.shuffle(available_indices)
453-
ms32_indices = [CHARSET.find(c) for c in indices]
454-
ms32_indices += [CHARSET.find(c) for c in available_indices[:n - len(indices)]]
455-
n_entropy_bytes = length * max(k, 1)
456-
entropy = user_entropy if user_entropy else secrets.token_bytes(length * max(k, 1))
457-
difference = len(entropy) - n_entropy_bytes
458-
if user_entropy and (difference < 0):
459-
# only warn if the user provided the entropy (always trust token_bytes)
460-
warnings.warn(
461-
(
462-
f"{len(user_entropy)} bytes in, {n_entropy_bytes} bytes out."
463-
" Input more entropy?"
464-
)
465-
)
466-
assert len(entropy) == n_entropy_bytes, "Unexpected unused entropy"
467-
468-
alphabetized_charset = 'sacdefghjk' # threshold above 9 is invalid
469-
initial_codex32_data = []
470-
id = [0 if x < 0 else x for x in id]
471-
# produce k initial shares (accounting for the extra secret share when n<k)
472-
for i in range(bool(n<k), min(n, max(1, k)) + bool(n<k)):
473-
header = [CHARSET.find(str(k))] + id + [CHARSET.find(alphabetized_charset[i])]
474-
payload = convertbits(entropy[i * length:(i + 1) * length], 8, 5)
475-
if payload:
476-
initial_codex32_data.append(header + payload)
477-
codex32_secret = initial_codex32_data[0] if n >= k or n == 1 else ms32_recover(initial_codex32_data)
478-
bip32_fp = bip32_fingerprint(convertbits(codex32_secret[6:], 5, 8))
479-
bech32_fp = convertbits(bip32_fp, 8, 5)
480-
if bech32_fp and len(bech32_fp) >= 4:
481-
bech32_fp = bech32_fp[:4]
482-
else:
483-
raise ValueError("Failed to compute BIP32 fingerprint.")
484-
for data in initial_codex32_data:
485-
for i in range(1,5): # relabel shares with the BIP32 fingerprint
486-
data[i] = data[i] if ms32_ident[i - 1] >= 0 else bech32_fp[i - 1]
487-
strings = []
488-
if k and n >= k:
489-
for share_index in ms32_indices:
490-
# Try to find an initial share at this index
491-
share_data = next((data for data in initial_codex32_data if data[5] == share_index), None)
492-
if share_data is None:
493-
# Interpolate a new share at this index
494-
share_data = ms32_interpolate(initial_codex32_data, share_index)
495-
strings.append(ms32_encode(hrp, share_data))
496-
else:
497-
strings = [ms32_encode(hrp, data) for data in initial_codex32_data]
498-
if k == 0:
499-
strings *= n # replicate the single share n times
500-
501-
assert not k or validate_set(strings, len_must_match_k=False)
502-
503-
return strings[0][4:8], bip32_fp if n >=k else b'', strings
504-

0 commit comments

Comments
 (0)