Skip to content

Commit faf77e1

Browse files
committed
Add test to ensure old share compatibility
1 parent 7afc2db commit faf77e1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/qos_crypto/src/shamir.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod test {
3030
use rand::prelude::SliceRandom;
3131

3232
use super::*;
33+
3334
#[test]
3435
fn make_and_reconstruct_shares() {
3536
let secret = b"this is a crazy secret";
@@ -65,4 +66,54 @@ mod test {
6566
assert_eq!(secret.to_vec(), reconstructed);
6667
}
6768
}
69+
70+
#[test]
71+
fn can_reconstruct_from_old_shares() {
72+
// This test if fundamental to ensure updates to the Shamir Secret
73+
// Sharing logic can be made safely. Here we hardcode shares that were
74+
// created with the oldest version of this logic, and ensure that we can
75+
// reconstruct. If this test starts failing please do _not_ ignore it,
76+
// it's telling you the current quorum key shares will become invalid
77+
// when combined!
78+
// --------
79+
// These shares were generated with the following QOS commit:
80+
// `31ad6ac8458781f592a442b7dc0e0e019e03f2f4` (2022-05-12)
81+
// with the following test code:
82+
// #[test]
83+
// fn make_shares() {
84+
// let secret = b"my cute little secret";
85+
// let n = 3;
86+
// let k = 2;
87+
//
88+
// let all_shares = shares_generate(secret, n, k);
89+
// for share in all_shares {
90+
// println!("share: {}", hex::encode(share));
91+
// }
92+
// }
93+
let shares = [
94+
qos_hex::decode("01661fc0cc265daa4e7bde354c281dcc23a80c590249")
95+
.unwrap(),
96+
qos_hex::decode("027bb5fb26d326e0fc421cf604e495e3d3e4bd24ab0e")
97+
.unwrap(),
98+
qos_hex::decode("0370d31b89800f2f9255abb73ca0ed0f8329d20fcc33")
99+
.unwrap(),
100+
];
101+
102+
// Setting is 2-out-of-3. Let's try 3 ways.
103+
let reconstructed1 =
104+
shares_reconstruct(vec![shares[0].clone(), shares[1].clone()])
105+
.unwrap();
106+
let reconstructed2 =
107+
shares_reconstruct(vec![shares[1].clone(), shares[2].clone()])
108+
.unwrap();
109+
let reconstructed3 =
110+
shares_reconstruct(vec![shares[0].clone(), shares[2].clone()])
111+
.unwrap();
112+
113+
// Regardless of the combination we should get the same secret
114+
let expected_secret = b"my cute little secret";
115+
assert_eq!(reconstructed1, expected_secret);
116+
assert_eq!(reconstructed2, expected_secret);
117+
assert_eq!(reconstructed3, expected_secret);
118+
}
68119
}

0 commit comments

Comments
 (0)