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_all_groups_exist(): for threshold in (1, 2, 5): mnemonics = shamir.generate_mnemonics(threshold, [(3, 5), (1, 1), (2, 3), (2, 5), (3, 5)], MS) assert len(mnemonics) == 5 assert len(sum(mnemonics, [])) == 19
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_recover_ems(): mnemonics = shamir.generate_mnemonics(1, [(3, 5)], MS, b"TREZOR")[0] identifier, exponent, encrypted_master_secret = shamir.recover_ems( mnemonics[:3]) recovered = shamir.decrypt(encrypted_master_secret, b"TREZOR", exponent, identifier) 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_invalid_sharing(): # Short master secret. with pytest.raises(ValueError): shamir.generate_mnemonics(1, [(2, 3)], MS[:14]) # Odd length master secret. with pytest.raises(ValueError): shamir.generate_mnemonics(1, [(2, 3)], MS + b"X") # Group threshold exceeds number of groups. with pytest.raises(ValueError): shamir.generate_mnemonics(3, [(3, 5), (2, 5)], MS) # Invalid group threshold. with pytest.raises(ValueError): shamir.generate_mnemonics(0, [(3, 5), (2, 5)], MS) # Member threshold exceeds number of members. with pytest.raises(ValueError): shamir.generate_mnemonics(2, [(3, 2), (2, 5)], MS) # Invalid member threshold. with pytest.raises(ValueError): shamir.generate_mnemonics(2, [(0, 2), (2, 5)], MS) # Group with multiple members and threshold 1. with pytest.raises(ValueError): shamir.generate_mnemonics(2, [(3, 5), (1, 3), (2, 5)], MS)
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:])