vss.rs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 a proactive update.
  88. pub fn create_update(numshares: u32, threshold: u32) -> Result<(Commitment, Vec<Share>), &'static str> {
  89. unimplemented!("Not yet implemented")
  90. }
  91. /// Apply the commitment for the update to the master commitment.
  92. pub fn apply_commitment_update(oldcommitment: &Commitment, update: &Commitment) -> Result<Commitment, &'static str> {
  93. unimplemented!("Not yet implemented")
  94. }
  95. /// Apply the share update to an existing share
  96. pub fn apply_share_update(oldshare: &Share, update: &Share) -> Result<Share, &'static str> {
  97. unimplemented!("Not yet implemented")
  98. }
  99. #[cfg(test)]
  100. mod tests {
  101. use crate::vss::*;
  102. #[test]
  103. fn share_simple() {
  104. let s = Secret::from(42u32);
  105. let res = generate_shares(s, 5, 2);
  106. assert!(res.is_ok());
  107. let (com, shares) = res.unwrap();
  108. assert!(shares.len() == 5);
  109. let mut recshares: Vec<Share> = Vec::new();
  110. recshares.push(shares[1]);
  111. recshares.push(shares[3]);
  112. let recres = reconstruct_secret(&recshares);
  113. assert!(recres.is_ok());
  114. assert_eq!(recres.unwrap(), s);
  115. }
  116. #[test]
  117. fn share_not_enough() {
  118. let s = Secret::from(42u32);
  119. let res = generate_shares(s, 5, 2);
  120. assert!(res.is_ok());
  121. let (com, shares) = res.unwrap();
  122. let mut recshares: Vec<Share> = Vec::new();
  123. recshares.push(shares[1]);
  124. let recres = reconstruct_secret(&recshares);
  125. assert!(recres.is_ok());
  126. assert_ne!(recres.unwrap(), s);
  127. }
  128. #[test]
  129. fn share_dup() {
  130. let s = Secret::from(42u32);
  131. let res = generate_shares(s, 5, 2);
  132. assert!(res.is_ok());
  133. let (com, shares) = res.unwrap();
  134. let mut recshares: Vec<Share> = Vec::new();
  135. recshares.push(shares[1]);
  136. recshares.push(shares[1]);
  137. let recres = reconstruct_secret(&recshares);
  138. assert!(recres.is_err());
  139. assert_eq!(recres.err(), Some("Duplicate shares provided"));
  140. }
  141. #[test]
  142. fn share_badparams() {
  143. let s = Secret::from(42u32);
  144. {
  145. let res = generate_shares(s, 5, 0);
  146. assert!(res.is_err());
  147. assert_eq!(res.err(), Some("Threshold cannot be 0"));
  148. }
  149. {
  150. let res = generate_shares(s, 0, 3);
  151. assert!(res.is_err());
  152. assert_eq!(res.err(), Some("Number of shares cannot be 0"));
  153. }
  154. {
  155. let res = generate_shares(s, 1, 3);
  156. assert!(res.is_err());
  157. assert_eq!(res.err(), Some("Threshold cannot exceed numshares"));
  158. }
  159. }
  160. #[test]
  161. fn share_commitment_valid() {
  162. let s = Secret::from(42u32);
  163. let res = generate_shares(s, 5, 2);
  164. assert!(res.is_ok());
  165. let (com, shares) = res.unwrap();
  166. for share in shares {
  167. let is_valid = verify_share(share, &com);
  168. assert!(is_valid.is_ok());
  169. assert!(is_valid.unwrap());
  170. }
  171. }
  172. #[test]
  173. fn share_commitment_invalid() {
  174. let s1 = Secret::from(42u32);
  175. let s2 = Secret::from(43u32);
  176. let res1 = generate_shares(s1, 5, 2);
  177. assert!(res1.is_ok());
  178. let (_, shares1) = res1.unwrap();
  179. let res2 = generate_shares(s2, 5, 2);
  180. assert!(res2.is_ok());
  181. let (com2, _) = res2.unwrap();
  182. for share in shares1 {
  183. // test that commitments to a different set of shares fails
  184. let is_valid = verify_share(share, &com2);
  185. assert!(is_valid.is_ok());
  186. assert_ne!(is_valid.unwrap(), true);
  187. }
  188. }
  189. }