refactor tests
This commit is contained in:
parent
74abd24423
commit
ae4ff59b1c
|
@ -126,6 +126,7 @@ impl Product for GF256 {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{EXP_TABLE, GF256, LOG_TABLE};
|
||||
use alloc::vec;
|
||||
|
||||
#[test]
|
||||
fn add_works() {
|
||||
|
@ -206,13 +207,13 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn sum_works() {
|
||||
let values = alloc::vec![GF256(0x53), GF256(0xCA), GF256(0)];
|
||||
let values = vec![GF256(0x53), GF256(0xCA), GF256(0)];
|
||||
assert_eq!(values.into_iter().sum::<GF256>().0, 0x99);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn product_works() {
|
||||
let values = alloc::vec![GF256(1), GF256(1), GF256(4)];
|
||||
let values = vec![GF256(1), GF256(1), GF256(4)];
|
||||
assert_eq!(values.into_iter().product::<GF256>().0, 4);
|
||||
}
|
||||
}
|
||||
|
|
54
src/lib.rs
54
src/lib.rs
|
@ -167,23 +167,27 @@ impl Sharks {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Share, Sharks};
|
||||
use alloc::vec::Vec;
|
||||
#[cfg(not(feature = "std"))]
|
||||
use rand_chacha::rand_core::SeedableRng;
|
||||
use alloc::{vec, vec::Vec};
|
||||
|
||||
impl Sharks {
|
||||
#[cfg(not(feature = "std"))]
|
||||
fn make_shares(&self, secret: &[u8]) -> impl Iterator<Item = Share> {
|
||||
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
|
||||
|
||||
let mut rng = ChaCha8Rng::from_seed([0x90; 32]);
|
||||
self.dealer_rng(secret, &mut rng)
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn make_shares(&self, secret: &[u8]) -> impl Iterator<Item = Share> {
|
||||
self.dealer(secret)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insufficient_shares_err() {
|
||||
let sharks = Sharks(255);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
let dealer = sharks.dealer(&[1]);
|
||||
#[cfg(not(feature = "std"))]
|
||||
let dealer = sharks.dealer_rng(&[1], &mut rng);
|
||||
|
||||
let shares: Vec<Share> = dealer.take(254).collect();
|
||||
let shares: Vec<Share> = sharks.make_shares(&[1]).take(254).collect();
|
||||
let secret = sharks.recover(&shares);
|
||||
assert!(secret.is_err());
|
||||
}
|
||||
|
@ -191,16 +195,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_duplicate_shares_err() {
|
||||
let sharks = Sharks(255);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
let dealer = sharks.dealer(&[1]);
|
||||
#[cfg(not(feature = "std"))]
|
||||
let dealer = sharks.dealer_rng(&[1], &mut rng);
|
||||
|
||||
let mut shares: Vec<Share> = dealer.take(255).collect();
|
||||
let mut shares: Vec<Share> = sharks.make_shares(&[1]).take(255).collect();
|
||||
shares[1] = Share {
|
||||
x: shares[0].x,
|
||||
y: shares[0].y.clone(),
|
||||
|
@ -212,17 +207,8 @@ mod tests {
|
|||
#[test]
|
||||
fn test_integration_works() {
|
||||
let sharks = Sharks(255);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
let dealer = sharks.dealer(&[1, 2, 3, 4]);
|
||||
#[cfg(not(feature = "std"))]
|
||||
let dealer = sharks.dealer_rng(&[1, 2, 3, 4], &mut rng);
|
||||
|
||||
let shares: Vec<Share> = dealer.take(255).collect();
|
||||
let shares: Vec<Share> = sharks.make_shares(&[1, 2, 3, 4]).take(255).collect();
|
||||
let secret = sharks.recover(&shares).unwrap();
|
||||
assert_eq!(secret, alloc::vec![1, 2, 3, 4]);
|
||||
assert_eq!(secret, vec![1, 2, 3, 4]);
|
||||
}
|
||||
}
|
||||
|
|
13
src/math.rs
13
src/math.rs
|
@ -62,7 +62,7 @@ pub fn get_evaluator(polys: Vec<Vec<GF256>>) -> impl Iterator<Item = Share> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{get_evaluator, interpolate, random_polynomial, Share, GF256};
|
||||
use alloc::vec::Vec;
|
||||
use alloc::{vec, vec::Vec};
|
||||
use rand_chacha::rand_core::SeedableRng;
|
||||
|
||||
#[test]
|
||||
|
@ -75,14 +75,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn evaluator_works() {
|
||||
let iter = get_evaluator(alloc::vec![alloc::vec![GF256(3), GF256(2), GF256(5)]]);
|
||||
let iter = get_evaluator(vec![vec![GF256(3), GF256(2), GF256(5)]]);
|
||||
let values: Vec<_> = iter.take(2).map(|s| (s.x, s.y)).collect();
|
||||
assert_eq!(
|
||||
values,
|
||||
alloc::vec![
|
||||
(GF256(1), alloc::vec![GF256(4)]),
|
||||
(GF256(2), alloc::vec![GF256(13)])
|
||||
]
|
||||
vec![(GF256(1), vec![GF256(4)]), (GF256(2), vec![GF256(13)])]
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -90,9 +87,9 @@ mod tests {
|
|||
fn interpolate_works() {
|
||||
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
|
||||
let poly = random_polynomial(GF256(185), 10, &mut rng);
|
||||
let iter = get_evaluator(alloc::vec![poly]);
|
||||
let iter = get_evaluator(vec![poly]);
|
||||
let shares: Vec<Share> = iter.take(10).collect();
|
||||
let root = interpolate(&shares);
|
||||
assert_eq!(root, alloc::vec![185]);
|
||||
assert_eq!(root, vec![185]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,16 +53,16 @@ impl From<&[u8]> for Share {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Share, GF256};
|
||||
use alloc::vec::Vec;
|
||||
use alloc::{vec, vec::Vec};
|
||||
|
||||
#[test]
|
||||
fn vec_from_share_works() {
|
||||
let share = Share {
|
||||
x: GF256(1),
|
||||
y: alloc::vec![GF256(2), GF256(3)],
|
||||
y: vec![GF256(2), GF256(3)],
|
||||
};
|
||||
let bytes = Vec::from(&share);
|
||||
assert_eq!(bytes, alloc::vec![1, 2, 3]);
|
||||
assert_eq!(bytes, vec![1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -70,6 +70,6 @@ mod tests {
|
|||
let bytes = [1, 2, 3];
|
||||
let share = Share::from(&bytes[..]);
|
||||
assert_eq!(share.x, GF256(1));
|
||||
assert_eq!(share.y, alloc::vec![GF256(2), GF256(3)]);
|
||||
assert_eq!(share.y, vec![GF256(2), GF256(3)]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue