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 2 commits
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
members = [
"polynomial",
"univariate-polynomial-iop-zerotest",
"halo2-trials"
"halo2-trials",
"shamir-secret-sharing"
]
resolver = "2"

Expand Down
1 change: 1 addition & 0 deletions polynomial/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ version = "0.1.0"

[dependencies]
num-traits = "0.2.18"
nalgebra = "0.32.5"
62 changes: 60 additions & 2 deletions polynomial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::{
hash::Hash,
ops::{Mul, Sub},
};

extern crate nalgebra as na;
use na::{DMatrix, RowDVector, Scalar};
use num_traits::{One, Zero};

pub enum PolynomialRepr<T> {
Expand Down Expand Up @@ -60,7 +61,52 @@ where
}
}

impl<T: Zero + One + Mul<Output = T> + Sub<Output = T> + Clone + Debug> Polynomial<T> {
impl<T> Polynomial<T>
where
T: One + Clone,
{
/// Generate powers of `base` raised to a max degree of `deg-1`
fn generate_powers(base: T, deg: usize) -> Vec<T> {
let mut powers_vec = Vec::<T>::with_capacity(deg);
powers_vec.push(T::one());
for idx in 1..powers_vec.capacity() {
powers_vec.push(base.clone() * powers_vec[idx - 1].clone());
supragya marked this conversation as resolved.
Show resolved Hide resolved
}
powers_vec
}
}

impl<T> Polynomial<T>
where
T: Clone + Scalar + Debug + Mul<Output = T> + One,
{
/// Generate a polynomials from its evalutation points givent in
/// a tuple format `(a, b)` such that `poly(a) = b`. Given `n`
/// points of evaluation, `n-1` degree polynomial is generated
pub fn new_from_evals(evals: &[(T, T)]) {
// We know that if all evalutaions matrix `E` is multiplied by
// coefficient vector `C`, resultant would be `R`. `evals`
// essentially is `[E(x) | R]`
let x_powers_matrix = DMatrix::<T>::from_rows(
&evals
.iter()
.map(|(eval_point, _eval)| {
RowDVector::from_iterator(
evals.len(),
Self::generate_powers(eval_point.clone(), evals.len()).into_iter(),
)
})
.collect::<Vec<_>>()[..],
);

println!("{:#?}", x_powers_matrix);
supragya marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<T> Polynomial<T>
where
T: Zero + One + Mul<Output = T> + Sub<Output = T> + Clone + Debug,
{
/// Evaluates the polynomial at a point.
///
/// # Examples
Expand Down Expand Up @@ -120,4 +166,16 @@ mod tests {
assert_eq!(Polynomial::<u32>::new_from_roots(&[3, 2, 1]).degree(), 3);
assert_eq!(Polynomial::<u32>::new_from_roots(&[3, 2, 3]).degree(), 2);
}

#[test]
fn test_generate_power() {
assert_eq!(Polynomial::generate_powers(2, 5), vec![1, 2, 4, 8, 16]);
}

#[test]
fn polynomial_from_evals() {
// polynomial -> 1 + 4*x + x^2
let evals = [(1, 6), (2, 13), (3, 22)];
Polynomial::new_from_evals(&evals);
supragya marked this conversation as resolved.
Show resolved Hide resolved
}
}
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