Skip to content

Instantly share code, notes, and snippets.

Francis Lim thyeem

View GitHub Profile
@thyeem
thyeem / Shamir.hs
Last active Jun 12, 2020
My own impl of secret-sharing (Shamir scheme)
View Shamir.hs
{- Example: How to use------------------------------------------------------------
$ stack ghci
-- Import or load the below module: Shamir
> :l Shamir
-- Prepare any string "secret" less than 32-byte
> secret = "stop COVID-19"
-- Prepare parameter (n, k)
@thyeem
thyeem / build_hasher.rs
Created Apr 4, 2020
with a manual BuildHasher
View build_hasher.rs
use std::hash::{BuildHasher, Hasher};
use std::collections::HashMap;
fn bytes_to_int(bytes: &[u8]) -> usize {
let l = bytes.len();
(0..l).fold(0, |sum, i| {
sum + (1 << ((l - i - 1) * 8)) * bytes[i] as usize
})
}
@thyeem
thyeem / shuffle.rs
Last active Apr 2, 2020
impl shuffle fn based on Fisher-Yates
View shuffle.rs
extern crate rand;
use rand::Rng;
// Fisher-Yates shuffle
fn shuffle<T: Clone>(v: &mut Vec<T>) {
let mut rng = rand::thread_rng();
let s = v.len();
(0..s).for_each(|i|{
let q = rng.gen_range(0, s);
v.swap(i, q);
View integer.rs
use std::ops::{Add, Div, Mul, Range, Rem, Shl, Shr, Sub};
pub trait Uint:
Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ Rem<Output = Self>
+ Shl<Output = Self>
+ Shr<Output = Self>
You can’t perform that action at this time.