vss.rs 8.9 KB

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