Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shamir's secret sharing algorithm #7

Merged
merged 8 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions shamir-secret-sharing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
edition = "2021"
name = "shamir-secret-sharing"
version = "0.1.0"

[dependencies]
polynomial = {path = "../polynomial"}
rand = "0.8.5"
rand_chacha = "0.3.1"
Empty file added shamir-secret-sharing/README.md
Empty file.
44 changes: 44 additions & 0 deletions shamir-secret-sharing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#[cfg(test)]
mod tests {
use polynomial::Polynomial;
use rand::prelude::*;

#[test]
fn shamir_secret_sharing() {
// Let's say we want to create a shamir secret scheme
// for hiding S = 119
let secret = 119;

// Assume that we want to break secret into 4 "parts",
// of which any 2 should be able to reconstruct the
// original secret
let parts = 4;
let threshold = 2;

// Hence, coefficients will be of form `secret + r1*x + r2*x^2...`
// where `r` is random values from `(0, threshold)`
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(3091);
let coeffs: Vec<i64> = [secret]
.into_iter()
.chain((0..threshold).map(|_| rng.next_u32() as i64))
.collect();

let polynomial = Polynomial::new_from_coeffs(&coeffs);

// Ensure that we can get the secret back given we evaluate
// at 0
assert_eq!(polynomial.eval(0), secret);

// Now evaulate at a few random points to make the secret
let secret_parts: Vec<(i64, i64)> = (0..threshold)
.map(|_| {
let point_of_eval = rng.next_u32() as i64;
let eval = polynomial.eval(point_of_eval);
(point_of_eval, eval)
})
.collect();

// Ensure reconstruction is possible
// let reconstructed_poly = Polynomial::new_from_evalutations()
}
supragya marked this conversation as resolved.
Show resolved Hide resolved
}
Loading