def test_iteration_exponent(): mnemonics = shamir.generate_mnemonics(1, [(3, 5)], MS, b"TREZOR", 1)[0] assert MS == shamir.combine_mnemonics(mnemonics[1:4], b"TREZOR") assert MS != shamir.combine_mnemonics(mnemonics[1:4]) mnemonics = shamir.generate_mnemonics(1, [(3, 5)], MS, b"TREZOR", 2)[0] assert MS == shamir.combine_mnemonics(mnemonics[1:4], b"TREZOR") assert MS != shamir.combine_mnemonics(mnemonics[1:4])
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( mnemonics, b"TREZOR"), 'Incorrect secret for test vector "{}".'.format( description) else: with pytest.raises(MnemonicError): shamir.combine_mnemonics(mnemonics) pytest.fail( 'Failed to raise exception for test vector "{}".'.format( description))
def test_group_sharing(): group_threshold = 2 group_sizes = (5, 3, 5, 1) member_thresholds = (3, 2, 2, 1) mnemonics = shamir.generate_mnemonics( group_threshold, list(zip(member_thresholds, group_sizes)), MS) # Test all valid combinations of mnemonics. for groups in combinations(zip(mnemonics, member_thresholds), group_threshold): for group1_subset in combinations(groups[0][0], groups[0][1]): for group2_subset in combinations(groups[1][0], groups[1][1]): mnemonic_subset = list(group1_subset + group2_subset) shuffle(mnemonic_subset) assert MS == shamir.combine_mnemonics(mnemonic_subset) # Minimal sets of mnemonics. assert MS == shamir.combine_mnemonics( [mnemonics[2][0], mnemonics[2][2], mnemonics[3][0]]) assert MS == shamir.combine_mnemonics( [mnemonics[2][3], mnemonics[3][0], mnemonics[2][4]]) # One complete group and one incomplete group out of two groups required. with pytest.raises(MnemonicError): shamir.combine_mnemonics(mnemonics[0][2:] + [mnemonics[1][0]]) # One group of two required. with pytest.raises(MnemonicError): shamir.combine_mnemonics(mnemonics[0][1:4])
def test_split_ems(): identifier = 42 exponent = 1 encrypted_master_secret = shamir.encrypt(MS, b"TREZOR", exponent, identifier) mnemonics = shamir.split_ems(1, [(3, 5)], identifier, exponent, encrypted_master_secret) recovered = shamir.combine_mnemonics(mnemonics[0][:3], b"TREZOR") assert recovered == MS
def test_group_sharing_threshold_1(): group_threshold = 1 group_sizes = (5, 3, 5, 1) member_thresholds = (3, 2, 2, 1) mnemonics = shamir.generate_mnemonics( group_threshold, list(zip(member_thresholds, group_sizes)), MS) # Test all valid combinations of mnemonics. for group, threshold in zip(mnemonics, member_thresholds): for group_subset in combinations(group, threshold): mnemonic_subset = list(group_subset) shuffle(mnemonic_subset) assert MS == shamir.combine_mnemonics(mnemonic_subset)
def test_passphrase(): mnemonics = shamir.generate_mnemonics(1, [(3, 5)], MS, b"TREZOR")[0] assert MS == shamir.combine_mnemonics(mnemonics[1:4], b"TREZOR") assert MS != shamir.combine_mnemonics(mnemonics[1:4])
def test_basic_sharing_fixed(): mnemonics = shamir.generate_mnemonics(1, [(3, 5)], MS)[0] assert MS == shamir.combine_mnemonics(mnemonics[:3]) assert MS == shamir.combine_mnemonics(mnemonics[1:4]) with pytest.raises(MnemonicError): shamir.combine_mnemonics(mnemonics[1:3])
def test_basic_sharing_random(): secret = secrets.token_bytes(16) mnemonics = shamir.generate_mnemonics(1, [(3, 5)], secret)[0] assert shamir.combine_mnemonics(mnemonics[:3]) == shamir.combine_mnemonics( mnemonics[2:])