vss.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. pub struct Share {
  7. index: u32,
  8. value: Scalar,
  9. }
  10. pub struct Commitment {
  11. coms: Vec<EdwardsPoint>,
  12. }
  13. /// Create secret shares for a given secret.
  14. pub fn generate_shares(secret: Secret, numshares: u32, threshold: u32) -> Result<(Commitment, Vec<Share>), &'static str> {
  15. if threshold < 1 { return Err("Threshold cannot be 0") }
  16. if numshares < 1 { return Err("Number of shares cannot be 0") }
  17. if threshold > numshares { return Err("Threshold cannot exceed numshares") }
  18. let numcoeffs = (threshold-1) as usize;
  19. let mut coeffs: Vec<Scalar> = Vec::with_capacity(numcoeffs);
  20. let mut rng: ThreadRng = rand::thread_rng();
  21. let mut shares: Vec<Share> = Vec::with_capacity(numshares as usize);
  22. let mut comm: Commitment = Commitment { coms: Vec::with_capacity(threshold as usize) };
  23. for _ in 1..numcoeffs {
  24. coeffs.push(Scalar::random(&mut rng));
  25. }
  26. for share_index in 1..numshares {
  27. // Evaluate the polynomial with `secret` as the constant term
  28. // and `coeffs` as the other coefficients at the point x=share_index
  29. // using Horner's method
  30. let scalar_index = Scalar::from(share_index);
  31. let mut value = Scalar::zero();
  32. for i in (0..numcoeffs-1).rev() {
  33. value += coeffs[i];
  34. value *= scalar_index;
  35. }
  36. value += secret;
  37. shares.push(Share{ index: share_index, value: value });
  38. }
  39. comm.coms.push(ED25519_BASEPOINT_POINT * secret);
  40. for c in coeffs {
  41. comm.coms.push(ED25519_BASEPOINT_POINT * c);
  42. }
  43. Ok((comm, shares))
  44. }
  45. /// Verify that a share is consistent with a commitment.
  46. pub fn verify_share(share: Share, commitment: Commitment) -> Result<bool, &'static str> {
  47. unimplemented!("Not yet implemented")
  48. }
  49. /// Reconstruct the secret from enough (at least the threshold) already-verified shares.
  50. pub fn reconstruct_secret(shares: &Vec<Share>) -> Result<Share, &'static str> {
  51. unimplemented!("Not yet implemented")
  52. }
  53. /// Create a proactive update.
  54. pub fn create_update(numshares: u32, threshold: u32) -> Result<(Commitment, Vec<Share>), &'static str> {
  55. unimplemented!("Not yet implemented")
  56. }
  57. /// Apply the commitment for the update to the master commitment.
  58. pub fn apply_commitment_update(oldcommitment: &Commitment, update: &Commitment) -> Result<Commitment, &'static str> {
  59. unimplemented!("Not yet implemented")
  60. }
  61. /// Apply the share update to an existing share
  62. pub fn apply_share_update(oldshare: &Share, update: &Share) -> Result<Share, &'static str> {
  63. unimplemented!("Not yet implemented")
  64. }