vss.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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<Secret, &'static str> {
  51. let numshares = shares.len();
  52. if numshares < 1 { return Err("No shares provided"); }
  53. let mut lagrange_coeffs: Vec<Scalar> = Vec::with_capacity(numshares);
  54. for i in 0..numshares-1 {
  55. let mut num = Scalar::one();
  56. let mut den = Scalar::one();
  57. for j in 0..numshares-1 {
  58. if j==i { continue; }
  59. num *= Scalar::from(shares[j].index);
  60. den *= Scalar::from(shares[j].index - shares[i].index);
  61. }
  62. if den == Scalar::zero() {
  63. return Err("Duplicate shares provided");
  64. }
  65. lagrange_coeffs.push(num * den.invert());
  66. }
  67. let mut secret = Scalar::zero();
  68. for i in 0..numshares-1 {
  69. secret += lagrange_coeffs[i] * shares[i].value;
  70. }
  71. return Ok(secret)
  72. }
  73. /// Create a proactive update.
  74. pub fn create_update(numshares: u32, threshold: u32) -> Result<(Commitment, Vec<Share>), &'static str> {
  75. unimplemented!("Not yet implemented")
  76. }
  77. /// Apply the commitment for the update to the master commitment.
  78. pub fn apply_commitment_update(oldcommitment: &Commitment, update: &Commitment) -> Result<Commitment, &'static str> {
  79. unimplemented!("Not yet implemented")
  80. }
  81. /// Apply the share update to an existing share
  82. pub fn apply_share_update(oldshare: &Share, update: &Share) -> Result<Share, &'static str> {
  83. unimplemented!("Not yet implemented")
  84. }