Browse Source

Reconstruction

Ian Goldberg 5 years ago
parent
commit
f56daf8c92
1 changed files with 28 additions and 2 deletions
  1. 28 2
      src/vss.rs

+ 28 - 2
src/vss.rs

@@ -62,8 +62,34 @@ pub fn verify_share(share: Share, commitment: Commitment) -> Result<bool, &'stat
 }
 
 /// Reconstruct the secret from enough (at least the threshold) already-verified shares.
-pub fn reconstruct_secret(shares: &Vec<Share>) -> Result<Share, &'static str> {
-    unimplemented!("Not yet implemented")
+pub fn reconstruct_secret(shares: &Vec<Share>) -> Result<Secret, &'static str> {
+    let numshares = shares.len();
+
+    if numshares < 1 { return Err("No shares provided"); }
+
+    let mut lagrange_coeffs: Vec<Scalar> = Vec::with_capacity(numshares);
+
+    for i in 0..numshares-1 {
+        let mut num = Scalar::one();
+        let mut den = Scalar::one();
+        for j in 0..numshares-1 {
+            if j==i { continue; }
+            num *= Scalar::from(shares[j].index);
+            den *= Scalar::from(shares[j].index - shares[i].index);
+        }
+        if den == Scalar::zero() {
+            return Err("Duplicate shares provided");
+        }
+        lagrange_coeffs.push(num * den.invert());
+    }
+
+    let mut secret = Scalar::zero();
+
+    for i in 0..numshares-1 {
+        secret += lagrange_coeffs[i] * shares[i].value;
+    }
+
+    return Ok(secret)
 }
 
 /// Create a proactive update.