vss.rs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. use curve25519_dalek::edwards::EdwardsPoint;
  2. use curve25519_dalek::traits::Identity;
  3. use curve25519_dalek::scalar::Scalar;
  4. use curve25519_dalek::constants::ED25519_BASEPOINT_POINT;
  5. use rand::rngs::ThreadRng;
  6. pub type Secret = Scalar;
  7. #[derive(Copy, Clone)]
  8. pub struct Share {
  9. index: u32,
  10. value: Scalar,
  11. }
  12. pub struct Commitment {
  13. coms: Vec<EdwardsPoint>,
  14. }
  15. /// Create secret shares for a given secret.
  16. pub fn generate_shares(secret: Secret, numshares: u32, threshold: u32) -> Result<(Commitment, Vec<Share>), &'static str> {
  17. if threshold < 1 { return Err("Threshold cannot be 0") }
  18. if numshares < 1 { return Err("Number of shares cannot be 0") }
  19. if threshold > numshares { return Err("Threshold cannot exceed numshares") }
  20. let numcoeffs = (threshold-1) as usize;
  21. let mut coeffs: Vec<Scalar> = Vec::with_capacity(numcoeffs);
  22. let mut rng: ThreadRng = rand::thread_rng();
  23. let mut shares: Vec<Share> = Vec::with_capacity(numshares as usize);
  24. let mut comm: Commitment = Commitment { coms: Vec::with_capacity(threshold as usize) };
  25. for _ in 0..numcoeffs {
  26. coeffs.push(Scalar::random(&mut rng));
  27. }
  28. for share_index in 1..numshares+1 {
  29. // Evaluate the polynomial with `secret` as the constant term
  30. // and `coeffs` as the other coefficients at the point x=share_index
  31. // using Horner's method
  32. let scalar_index = Scalar::from(share_index);
  33. let mut value = Scalar::zero();
  34. for i in (0..numcoeffs).rev() {
  35. value += coeffs[i];
  36. value *= scalar_index;
  37. }
  38. value += secret;
  39. shares.push(Share{ index: share_index, value: value });
  40. }
  41. comm.coms.push(ED25519_BASEPOINT_POINT * secret);
  42. for c in coeffs {
  43. comm.coms.push(ED25519_BASEPOINT_POINT * c);
  44. }
  45. Ok((comm, shares))
  46. }
  47. /// Verify that a share is consistent with a commitment.
  48. pub fn verify_share(share: Share, commitment: &Commitment) -> Result<bool, &'static str> {
  49. let f_result = ED25519_BASEPOINT_POINT * share.value;
  50. let x = Scalar::from(share.index);
  51. let (_, result) = commitment.coms.iter().fold(
  52. (Scalar::one(), EdwardsPoint::identity()),
  53. |(x_to_the_i, sum_so_far), comm_i|
  54. (x_to_the_i * x, sum_so_far + x_to_the_i * comm_i));
  55. let is_valid = f_result == result;
  56. Ok(is_valid)
  57. }
  58. /// Reconstruct the secret from enough (at least the threshold) already-verified shares.
  59. pub fn reconstruct_secret(shares: &Vec<Share>) -> Result<Secret, &'static str> {
  60. let numshares = shares.len();
  61. if numshares < 1 { return Err("No shares provided"); }
  62. let mut lagrange_coeffs: Vec<Scalar> = Vec::with_capacity(numshares);
  63. for i in 0..numshares {
  64. let mut num = Scalar::one();
  65. let mut den = Scalar::one();
  66. for j in 0..numshares {
  67. if j==i { continue; }
  68. num *= Scalar::from(shares[j].index);
  69. den *= Scalar::from(shares[j].index) - Scalar::from(shares[i].index);
  70. }
  71. if den == Scalar::zero() {
  72. return Err("Duplicate shares provided");
  73. }
  74. lagrange_coeffs.push(num * den.invert());
  75. }
  76. let mut secret = Scalar::zero();
  77. for i in 0..numshares {
  78. secret += lagrange_coeffs[i] * shares[i].value;
  79. }
  80. return Ok(secret)
  81. }
  82. /// Create and apply a proactive update to a master commitment.
  83. pub fn update_shares(old_commitment: &Commitment, old_shares: &Vec<Share>) -> Result<(Commitment, Vec<Share>), &'static str> {
  84. let num_shares = old_shares.len() as u32;
  85. let threshold = old_commitment.coms.len() as u32;
  86. let res = generate_shares(Scalar::zero(), num_shares, threshold);
  87. if !res.is_ok() {
  88. return res;
  89. }
  90. let (update_commitments, update_shares) = res.unwrap();
  91. let mut new_shares: Vec<Share> = Vec::with_capacity(num_shares as usize);
  92. for old_share in old_shares {
  93. let update_share = update_shares[(old_share.index-1) as usize];
  94. let new_share = Share{
  95. index: old_share.index,
  96. value: old_share.value + update_share.value,
  97. };
  98. new_shares.push(new_share);
  99. }
  100. let mut new_commitments: Commitment = Commitment { coms: Vec::with_capacity(threshold as usize) };
  101. for i in 0..old_commitment.coms.len() {
  102. let new_commitment = old_commitment.coms[i] + update_commitments.coms[i];
  103. new_commitments.coms.push(new_commitment);
  104. }
  105. Ok((new_commitments, new_shares))
  106. }
  107. #[cfg(test)]
  108. mod tests {
  109. use crate::vss::*;
  110. #[test]
  111. fn share_simple() {
  112. let s = Secret::from(42u32);
  113. let res = generate_shares(s, 5, 2);
  114. assert!(res.is_ok());
  115. let (com, shares) = res.unwrap();
  116. assert!(shares.len() == 5);
  117. assert!(com.coms.len() == 2);
  118. let mut recshares: Vec<Share> = Vec::new();
  119. recshares.push(shares[1]);
  120. recshares.push(shares[3]);
  121. let recres = reconstruct_secret(&recshares);
  122. assert!(recres.is_ok());
  123. assert_eq!(recres.unwrap(), s);
  124. }
  125. #[test]
  126. fn share_not_enough() {
  127. let s = Secret::from(42u32);
  128. let res = generate_shares(s, 5, 2);
  129. assert!(res.is_ok());
  130. let (_, shares) = res.unwrap();
  131. let mut recshares: Vec<Share> = Vec::new();
  132. recshares.push(shares[1]);
  133. let recres = reconstruct_secret(&recshares);
  134. assert!(recres.is_ok());
  135. assert_ne!(recres.unwrap(), s);
  136. }
  137. #[test]
  138. fn share_dup() {
  139. let s = Secret::from(42u32);
  140. let res = generate_shares(s, 5, 2);
  141. assert!(res.is_ok());
  142. let (_, shares) = res.unwrap();
  143. let mut recshares: Vec<Share> = Vec::new();
  144. recshares.push(shares[1]);
  145. recshares.push(shares[1]);
  146. let recres = reconstruct_secret(&recshares);
  147. assert!(recres.is_err());
  148. assert_eq!(recres.err(), Some("Duplicate shares provided"));
  149. }
  150. #[test]
  151. fn share_badparams() {
  152. let s = Secret::from(42u32);
  153. {
  154. let res = generate_shares(s, 5, 0);
  155. assert!(res.is_err());
  156. assert_eq!(res.err(), Some("Threshold cannot be 0"));
  157. }
  158. {
  159. let res = generate_shares(s, 0, 3);
  160. assert!(res.is_err());
  161. assert_eq!(res.err(), Some("Number of shares cannot be 0"));
  162. }
  163. {
  164. let res = generate_shares(s, 1, 3);
  165. assert!(res.is_err());
  166. assert_eq!(res.err(), Some("Threshold cannot exceed numshares"));
  167. }
  168. }
  169. #[test]
  170. fn share_commitment_valid() {
  171. let s = Secret::from(42u32);
  172. let res = generate_shares(s, 8, 3);
  173. assert!(res.is_ok());
  174. let (com, shares) = res.unwrap();
  175. for share in shares {
  176. let is_valid = verify_share(share, &com);
  177. assert!(is_valid.is_ok());
  178. assert!(is_valid.unwrap());
  179. }
  180. }
  181. #[test]
  182. fn share_commitment_invalid() {
  183. let s1 = Secret::from(42u32);
  184. let s2 = Secret::from(42u32);
  185. let res1 = generate_shares(s1, 8, 3);
  186. assert!(res1.is_ok());
  187. let (_, shares1) = res1.unwrap();
  188. let res2 = generate_shares(s2, 8, 3);
  189. assert!(res2.is_ok());
  190. let (com2, _) = res2.unwrap();
  191. for share in shares1 {
  192. // test that commitments to a different set of shares fails
  193. let is_valid = verify_share(share, &com2);
  194. assert!(is_valid.is_ok());
  195. assert_ne!(is_valid.unwrap(), true);
  196. }
  197. }
  198. #[test]
  199. fn share_update_valid() {
  200. let s = Secret::from(42u32);
  201. let res = generate_shares(s, 5, 2);
  202. assert!(res.is_ok());
  203. let (com, shares) = res.unwrap();
  204. let update_res = update_shares(&com, &shares);
  205. assert!(update_res.is_ok());
  206. let (update_com, update_shares) = update_res.unwrap();
  207. for share in update_shares {
  208. let is_valid = verify_share(share, &update_com);
  209. assert!(is_valid.is_ok());
  210. assert!(is_valid.unwrap());
  211. }
  212. }
  213. #[test]
  214. fn share_update_invalid() {
  215. let s = Secret::from(42u32);
  216. let res = generate_shares(s, 5, 2);
  217. assert!(res.is_ok());
  218. let (com, shares) = res.unwrap();
  219. let update_res = update_shares(&com, &shares);
  220. assert!(update_res.is_ok());
  221. let (update_com, update_shares) = update_res.unwrap();
  222. // test that the old shares are not valid with the updated commitment
  223. for share in shares {
  224. let is_valid = verify_share(share, &update_com);
  225. assert!(is_valid.is_ok());
  226. assert!(!is_valid.unwrap());
  227. }
  228. // assert that we can recover the original secret using the updated shares
  229. let recres = reconstruct_secret(&update_shares);
  230. assert!(recres.is_ok());
  231. assert_eq!(recres.unwrap(), s);
  232. }
  233. #[test]
  234. fn share_update_valid_multiple_updates() {
  235. let s = Secret::from(42u32);
  236. let res = generate_shares(s, 5, 2);
  237. assert!(res.is_ok());
  238. let (com, shares) = res.unwrap();
  239. for _ in 0..5 {
  240. let update_res = update_shares(&com, &shares);
  241. assert!(update_res.is_ok());
  242. let (update_com, update_shares) = update_res.unwrap();
  243. for share in update_shares {
  244. let is_valid = verify_share(share, &update_com);
  245. assert!(is_valid.is_ok());
  246. assert!(is_valid.unwrap());
  247. }
  248. }
  249. }
  250. }