Skip to content

Commit

Permalink
feat: Add BIP32 master extended private key to test vectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkozlik committed Apr 26, 2024
1 parent c68bbb9 commit 7afce14
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The format is based on `Keep a Changelog`_, and this project adheres to
`Unreleased`_
-------------

No changes yet
- Added BIP32 master extended private key to test vectors.

.. _Unreleased: https://github.com/trezor/python-shamir-mnemonic/compare/v0.2.2...HEAD

Expand Down
11 changes: 7 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ command:
Test vectors
------------

The test vectors in vectors.json are given as a list of triples. The first member of the
triple is a description of the test vector, the second member is a list of mnemonics and
the third member is the master secret which results from combining the mnemonics. The
master secret is encoded as a string containing two hexadecimal digits for each byte. If
The test vectors in vectors.json are given as a list of quadruples:
* The first member is a description of the test vector.
* The second member is a list of mnemonics.
* The third member is the master secret which results from combining the mnemonics.
* The fourth member is the BIP32 master extended private key derived from the master secret.

The master secret is encoded as a string containing two hexadecimal digits for each byte. If
the string is empty, then attempting to combine the given set of mnemonics should result
in error. The passphrase "TREZOR" is used for all valid sets of mnemonics.
4 changes: 3 additions & 1 deletion generate_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random

import attr
from bip32utils import BIP32Key

from shamir_mnemonic import constants, rs1024, shamir, wordlist
from shamir_mnemonic.share import Share
Expand All @@ -14,7 +15,8 @@ def random_bytes(n):

def output(description, mnemonics, secret):
output.i += 1
output.data.append((f"{output.i}. {description}", mnemonics, secret.hex()))
xprv = BIP32Key.fromEntropy(secret).ExtendedKey() if secret else ""
output.data.append((f"{output.i}. {description}", mnemonics, secret.hex(), xprv))


def encode_mnemonic(*args):
Expand Down
11 changes: 8 additions & 3 deletions test_shamir.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from random import shuffle

import pytest
from bip32utils import BIP32Key

import shamir_mnemonic as shamir
from shamir_mnemonic import MnemonicError
Expand Down Expand Up @@ -134,11 +135,15 @@ def test_invalid_sharing():
def test_vectors():
with open("vectors.json", "r") as f:
vectors = json.load(f)
for description, mnemonics, secret in vectors:
if secret:
assert bytes.fromhex(secret) == shamir.combine_mnemonics(
for description, mnemonics, secret_hex, xprv in vectors:
if secret_hex:
secret = bytes.fromhex(secret_hex)
assert secret == shamir.combine_mnemonics(
mnemonics, b"TREZOR"
), 'Incorrect secret for test vector "{}".'.format(description)
assert (
BIP32Key.fromEntropy(secret).ExtendedKey() == xprv
), 'Incorrect xprv for test vector "{}".'.format(description)
else:
with pytest.raises(MnemonicError):
shamir.combine_mnemonics(mnemonics)
Expand Down
Loading

0 comments on commit 7afce14

Please sign in to comment.