Working on Traits
This commit is contained in:
parent
182832bc6e
commit
778a045be6
|
@ -1,5 +1,4 @@
|
||||||
|
use false_bottom::{FB128, FalseBottom};
|
||||||
use false_bottom::FBCrypt;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Input messages
|
// Input messages
|
||||||
|
@ -8,7 +7,7 @@ fn main() {
|
||||||
let real_msg2 = "Please meet me at the Paradise hotel at 5:30 PM";
|
let real_msg2 = "Please meet me at the Paradise hotel at 5:30 PM";
|
||||||
|
|
||||||
// Cipher initialization
|
// Cipher initialization
|
||||||
let mut fb = FBCrypt::init(18, 12).unwrap();
|
let mut fb = FB128::init(18, 12).unwrap();
|
||||||
|
|
||||||
// Encryption
|
// Encryption
|
||||||
let real_key1 = fb.add(&real_msg1.as_bytes());
|
let real_key1 = fb.add(&real_msg1.as_bytes());
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use false_bottom::{FBCrypt, FBKey};
|
use false_bottom::{FBObj, FBKey};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Cipher Initialization
|
// Cipher Initialization
|
||||||
let mut fb = FBCrypt::init(18, 9).unwrap();
|
let mut fb = FBObj::init(18, 9).unwrap();
|
||||||
|
|
||||||
// Encryption
|
// Encryption
|
||||||
let msg1 = "This is a message";
|
let msg1 = "This is a message";
|
||||||
|
@ -16,7 +16,7 @@ fn main() {
|
||||||
let key2_exp = key2.export();
|
let key2_exp = key2.export();
|
||||||
|
|
||||||
// Import
|
// Import
|
||||||
let fb_new = FBCrypt::import(&cipher, &keybase).unwrap();
|
let fb_new = FBObj::import(&cipher, &keybase).unwrap();
|
||||||
let key1_imp = FBKey::import(&key1_exp).unwrap();
|
let key1_imp = FBKey::import(&key1_exp).unwrap();
|
||||||
let key2_imp = FBKey::import(&key2_exp).unwrap();
|
let key2_imp = FBKey::import(&key2_exp).unwrap();
|
||||||
|
|
||||||
|
|
130
src/crypt.rs
130
src/crypt.rs
|
@ -1,130 +0,0 @@
|
||||||
use crypto_bigint::{U128, Limb, RandomMod, NonZero, Encoding};
|
|
||||||
use crate::{FBError, FBKey, pack::Packing};
|
|
||||||
use rand::{Rng, prelude::IteratorRandom};
|
|
||||||
mod fb128;
|
|
||||||
|
|
||||||
pub trait FB<T>: FBBlock<T>
|
|
||||||
where
|
|
||||||
T: RandomMod + SpecialMod + InvMod + Encoding + std::cmp::PartialOrd + Packing<T>
|
|
||||||
{
|
|
||||||
const MODULUS: NonZero<T>;
|
|
||||||
|
|
||||||
fn init(n: usize, k: usize) -> Result<FBStruct<T>, FBError>
|
|
||||||
where
|
|
||||||
T: RandomMod
|
|
||||||
{
|
|
||||||
if n < k || k < 2 {
|
|
||||||
return Err(FBError::InvalidParams);
|
|
||||||
}
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
let r = (0..k)
|
|
||||||
.map(|_| T::random_mod(&mut rng, &Self::MODULUS))
|
|
||||||
.collect();
|
|
||||||
let c = (0..n)
|
|
||||||
.map(|_| T::random_mod(&mut rng, &Self::MODULUS))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Ok(FBStruct { c, r })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add(&mut self, msg: &[u8]) -> FBKey {
|
|
||||||
let indices = T::pack(msg).into_iter()
|
|
||||||
.map(|msg_uint| self.add_u128(&msg_uint))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
FBKey { indices }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decrypt(&self, key: &FBKey) -> Result<Vec<u8>, FBError> {
|
|
||||||
let decr = key.indices.iter()
|
|
||||||
.map(|index_row| self.decrypt_u128(&index_row))
|
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
|
||||||
let msg = T::unpack(&decr)?;
|
|
||||||
|
|
||||||
Ok(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trait SpecialMod {
|
|
||||||
fn mul_mod_special(&self, rhs: &Self, c: Limb) -> Self;
|
|
||||||
fn add_mod_special(&self, rhs: &Self, c: Limb) -> Self;
|
|
||||||
fn sub_mod_special(&self, rhs: &Self, c: Limb) -> Self;
|
|
||||||
}
|
|
||||||
|
|
||||||
use crypto_bigint::CtChoice;
|
|
||||||
trait InvMod {
|
|
||||||
fn inv_mod(&self, modulus: &Self) -> (Self, CtChoice)
|
|
||||||
where Self: Sized;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SpecialMod for U128 {
|
|
||||||
fn mul_mod_special(&self, rhs: &Self, c: Limb) -> Self {self.mul_mod_special(rhs, c)}
|
|
||||||
fn add_mod_special(&self, rhs: &Self, c: Limb) -> Self {self.add_mod_special(rhs, c)}
|
|
||||||
fn sub_mod_special(&self, rhs: &Self, c: Limb) -> Self {self.sub_mod_special(rhs, c)}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InvMod for U128 {
|
|
||||||
fn inv_mod(&self, modulus: &Self) -> (Self, CtChoice) {
|
|
||||||
self.inv_mod(modulus)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trait FBBlock<T>
|
|
||||||
where
|
|
||||||
T: RandomMod + SpecialMod + InvMod
|
|
||||||
{
|
|
||||||
const P: T;
|
|
||||||
const P_POS: Limb;
|
|
||||||
|
|
||||||
fn cipher(&self) -> &Vec<T>;
|
|
||||||
fn cipher_mut(&mut self) -> &mut Vec<T>;
|
|
||||||
fn keybase(&self) -> &Vec<T>;
|
|
||||||
|
|
||||||
fn add_u128(&mut self, msg_uint: &T) -> Vec<(usize, usize)> {
|
|
||||||
let c = self.cipher();
|
|
||||||
let r = self.keybase();
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
let n = rng.gen_range(2..=r.len());
|
|
||||||
let mut c_i = (0..c.len()).choose_multiple(&mut rng, n - 1);
|
|
||||||
let r_i = (0..r.len()).choose_multiple(&mut rng, n);
|
|
||||||
let mut sum = T::ZERO;
|
|
||||||
for (&ci, &ri) in c_i.iter().zip(r_i.iter()) {
|
|
||||||
sum = sum.add_mod_special(
|
|
||||||
&c[ci].mul_mod_special(&r[ri], Self::P_POS),
|
|
||||||
Self::P_POS);
|
|
||||||
}
|
|
||||||
let ri_last = *r_i.last().expect("r_i will contain at least 2 elements");
|
|
||||||
let mod_inv = r[ri_last].inv_mod(&Self::P).0;
|
|
||||||
let c_new_el = msg_uint.sub_mod_special(&sum, Self::P_POS)
|
|
||||||
.mul_mod_special(&mod_inv, Self::P_POS);
|
|
||||||
let c = self.cipher_mut();
|
|
||||||
c.push(c_new_el);
|
|
||||||
c_i.push(c.len() - 1);
|
|
||||||
let indices = c_i.into_iter()
|
|
||||||
.zip(r_i.into_iter())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
indices
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decrypt_u128(&self, indices: &[(usize, usize)]) -> Result<T, FBError> {
|
|
||||||
let (r, c) = (self.keybase(), self.cipher());
|
|
||||||
if indices.len() > r.len() {
|
|
||||||
return Err(FBError::InvalidKey);
|
|
||||||
}
|
|
||||||
let mut msg = T::ZERO;
|
|
||||||
for &(ci, ri) in indices {
|
|
||||||
let c_el = c.get(ci).ok_or(FBError::InvalidKey)?;
|
|
||||||
let r_el = r.get(ri).ok_or(FBError::InvalidKey)?;
|
|
||||||
msg = msg.add_mod_special(
|
|
||||||
&c_el.mul_mod_special(&r_el, Self::P_POS),
|
|
||||||
Self::P_POS);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub struct FBStruct<T> {
|
|
||||||
pub(crate) c: Vec<T>,
|
|
||||||
pub(crate) r: Vec<T>,
|
|
||||||
}
|
|
|
@ -1,53 +0,0 @@
|
||||||
use crypto_bigint::{U128, Limb, NonZero};
|
|
||||||
use crate::{
|
|
||||||
crypt::{FB, FBStruct, FBBlock},
|
|
||||||
pack::Packing,
|
|
||||||
};
|
|
||||||
|
|
||||||
impl FB<U128> for FBStruct<U128> {
|
|
||||||
const MODULUS: NonZero<U128> = NonZero::<U128>::const_new(Self::P).0;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FBBlock<U128> for FBStruct<U128> {
|
|
||||||
const P: U128 = U128::MAX.wrapping_sub(&U128::from_u8(159 - 1));
|
|
||||||
const P_POS: Limb = Limb::from_u8(159);
|
|
||||||
|
|
||||||
fn cipher(&self) -> &Vec<U128> {
|
|
||||||
&self.c
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cipher_mut(&mut self) -> &mut Vec<U128> {
|
|
||||||
&mut self.c
|
|
||||||
}
|
|
||||||
|
|
||||||
fn keybase(&self) -> &Vec<U128> {
|
|
||||||
&self.r
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Packing<U128> for U128 {
|
|
||||||
const P_MINUS_ONE: U128 = U128::MAX.wrapping_sub(&U128::from_u8(159));
|
|
||||||
const BYTES: usize = 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn encrypt_u128() {
|
|
||||||
let msg = U128::from_u32(100);
|
|
||||||
let mut fb = FBStruct::init(20, 12).unwrap();
|
|
||||||
let key = fb.add_u128(&msg);
|
|
||||||
let decrypted = fb.decrypt_u128(&key).unwrap();
|
|
||||||
assert_eq!(msg, decrypted);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn encrypt_bytes() {
|
|
||||||
let input1 = vec![255_u8; 150];
|
|
||||||
let input2 = vec![0_u8; 102];
|
|
||||||
let mut fb = FBStruct::init(21, 12).unwrap();
|
|
||||||
let key1 = fb.add(&input1);
|
|
||||||
let key2 = fb.add(&input2);
|
|
||||||
let decr1 = fb.decrypt(&key1).unwrap();
|
|
||||||
let decr2 = fb.decrypt(&key2).unwrap();
|
|
||||||
assert_eq!(input1, decr1);
|
|
||||||
assert_eq!(input2, decr2);
|
|
||||||
}
|
|
|
@ -1,60 +1,14 @@
|
||||||
use base64::prelude::{BASE64_STANDARD, Engine};
|
use crypto_bigint::Encoding;
|
||||||
use bincode::{Options, DefaultOptions};
|
use crate::FBError;
|
||||||
use crypto_bigint::{U128, Encoding};
|
|
||||||
use crate::{FBCrypt, FBKey, errors::FBError};
|
|
||||||
|
|
||||||
impl FBCrypt {
|
pub trait Encode<T>
|
||||||
|
where
|
||||||
pub fn export(&self) -> (String, String) {
|
T: Encoding
|
||||||
let c_bytes: Vec<u8> = self.c.iter()
|
{
|
||||||
.flat_map(|bigint| bigint.to_le_bytes())
|
const BYTES: usize;
|
||||||
.collect();
|
|
||||||
let r_bytes: Vec<u8> = self.r.iter()
|
fn export(&self) -> (String, String);
|
||||||
.flat_map(|bigint| bigint.to_le_bytes())
|
fn import(cipher: &str, keybase: &str) -> Result<Self, FBError>
|
||||||
.collect();
|
where
|
||||||
|
Self: Sized;
|
||||||
(BASE64_STANDARD.encode(c_bytes), BASE64_STANDARD.encode(r_bytes))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn import(cipher: &str, keybase: &str) -> Result<FBCrypt, FBError> {
|
|
||||||
let c_bytes = BASE64_STANDARD.decode(cipher)
|
|
||||||
.map_err(|_| FBError::DecodeError)?;
|
|
||||||
let c: Vec<U128> = c_bytes.chunks_exact(16)
|
|
||||||
.map(|chunk| U128::from_le_bytes(chunk.try_into().unwrap()))
|
|
||||||
.collect();
|
|
||||||
let r_bytes = BASE64_STANDARD.decode(keybase)
|
|
||||||
.map_err(|_| FBError::DecodeError)?;
|
|
||||||
let r: Vec<U128> = r_bytes.chunks_exact(16)
|
|
||||||
.map(|chunk| U128::from_le_bytes(chunk.try_into().unwrap()))
|
|
||||||
.collect();
|
|
||||||
if c.len() < r.len() || r.len() < 2 {
|
|
||||||
return Err(FBError::InvalidParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(FBCrypt {c, r})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FBKey {
|
|
||||||
|
|
||||||
pub fn export(&self) -> String {
|
|
||||||
let binc = DefaultOptions::new();
|
|
||||||
let indice_bytes = binc.serialize(&self.indices)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
BASE64_STANDARD.encode(&indice_bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn import(key_str: &str) -> Result<FBKey, FBError> {
|
|
||||||
let binc = DefaultOptions::new();
|
|
||||||
let indice_bytes = BASE64_STANDARD.decode(key_str)
|
|
||||||
.map_err(|_| FBError::DecodeError)?;
|
|
||||||
let indices: Vec<_> = binc.deserialize(&indice_bytes)
|
|
||||||
.map_err(|_| FBError::DecodeError)?;
|
|
||||||
if indices.len() < 2 {
|
|
||||||
return Err(FBError::DecodeError);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok (FBKey {indices})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,71 +1,96 @@
|
||||||
use crypto_bigint::{U128, Limb, RandomMod, NonZero};
|
pub mod fb128;
|
||||||
use rand::{seq::IteratorRandom, Rng};
|
|
||||||
use crate::{errors::FBError, packing};
|
|
||||||
|
|
||||||
pub struct FBCrypt {
|
use crypto_bigint::{Limb, RandomMod, NonZero, Encoding};
|
||||||
pub(crate) c: Vec<U128>, // Ciphertext
|
use base64::prelude::{BASE64_STANDARD, Engine};
|
||||||
pub(crate) r: Vec<U128>, // Keybase
|
use crate::{FBError, Packing, FBKey, Encode};
|
||||||
|
use rand::{Rng, prelude::IteratorRandom};
|
||||||
|
use std::cmp::PartialOrd;
|
||||||
|
|
||||||
|
pub struct FBObj<T> {
|
||||||
|
pub(crate) c: Vec<T>,
|
||||||
|
pub(crate) r: Vec<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FBKey {
|
pub trait FalseBottom<T>: FBBlockOperations<T>
|
||||||
// 2D Vec containing (cipher_index, keybase_index) pairs
|
where
|
||||||
pub(crate) indices: Vec<Vec<(usize, usize)>>,
|
T: RandomMod + ModPrime<T> + Encoding + PartialOrd + Packing<T>
|
||||||
}
|
{
|
||||||
|
const MODULUS: NonZero<T>;
|
||||||
|
|
||||||
// Value and position of the Prime used (2^128 - 159)
|
fn init(n: usize, k: usize) -> Result<FBObj<T>, FBError>
|
||||||
const P: U128 = U128::MAX.wrapping_sub(&U128::from_u8(159 - 1));
|
where
|
||||||
const P_POS: Limb = Limb::from_u8(159);
|
T: RandomMod
|
||||||
|
{
|
||||||
impl FBCrypt {
|
|
||||||
pub fn init(n: usize, k: usize) -> Result<FBCrypt, FBError> {
|
|
||||||
if n < k || k < 2 {
|
if n < k || k < 2 {
|
||||||
return Err(FBError::InvalidParams);
|
return Err(FBError::InvalidParams);
|
||||||
}
|
}
|
||||||
const MODULUS: NonZero<U128> = NonZero::<U128>::const_new(P).0;
|
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let r = (0..k)
|
let r = (0..k)
|
||||||
.map(|_| U128::random_mod(&mut rng, &MODULUS))
|
.map(|_| T::random_mod(&mut rng, &Self::MODULUS))
|
||||||
.collect();
|
.collect();
|
||||||
let c = (0..n)
|
let c = (0..n)
|
||||||
.map(|_| U128::random_mod(&mut rng, &MODULUS))
|
.map(|_| T::random_mod(&mut rng, &Self::MODULUS))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(FBCrypt { c, r })
|
Ok(FBObj { c, r })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, msg: &[u8]) -> FBKey {
|
fn add(&mut self, msg: &[u8]) -> FBKey {
|
||||||
let indices = packing::pack(msg).into_iter()
|
let indices = T::pack(msg).into_iter()
|
||||||
.map(|msg_uint| self.add_u128(&msg_uint))
|
.map(|msg_uint| self.add_u128(&msg_uint))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
FBKey { indices }
|
FBKey { indices }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrypt(&self, key: &FBKey) -> Result<Vec<u8>, FBError> {
|
fn decrypt(&self, key: &FBKey) -> Result<Vec<u8>, FBError> {
|
||||||
let decr = key.indices.iter()
|
let decr = key.indices.iter()
|
||||||
.map(|index_row| self.decrypt_u128(&index_row))
|
.map(|index_row| self.decrypt_u128(&index_row))
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
let msg = packing::unpack(&decr)?;
|
let msg = T::unpack(&decr)?;
|
||||||
|
|
||||||
Ok(msg)
|
Ok(msg)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn add_u128(&mut self, msg_uint: &U128) -> Vec<(usize, usize)> {
|
pub trait ModPrime<T> {
|
||||||
let (r, c) = (&self.r, &mut self.c);
|
const PRIME_POS: Limb;
|
||||||
|
const PRIME: T;
|
||||||
|
fn mul_mod_prime(&self, rhs: &Self) -> Self;
|
||||||
|
fn add_mod_prime(&self, rhs: &Self) -> Self;
|
||||||
|
fn sub_mod_prime(&self, rhs: &Self) -> Self;
|
||||||
|
fn inv_mod_prime(&self) -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub trait FBBlockOperations<T>
|
||||||
|
where
|
||||||
|
T: RandomMod + ModPrime<T>
|
||||||
|
{
|
||||||
|
const P: T;
|
||||||
|
const P_POS: Limb;
|
||||||
|
|
||||||
|
fn cipher(&self) -> &Vec<T>;
|
||||||
|
fn cipher_mut(&mut self) -> &mut Vec<T>;
|
||||||
|
fn keybase(&self) -> &Vec<T>;
|
||||||
|
|
||||||
|
fn add_u128(&mut self, msg_uint: &T) -> Vec<(usize, usize)> {
|
||||||
|
let c = self.cipher();
|
||||||
|
let r = self.keybase();
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let n = rng.gen_range(2..=r.len());
|
let n = rng.gen_range(2..=r.len());
|
||||||
let mut c_i = (0..c.len()).choose_multiple(&mut rng, n - 1);
|
let mut c_i = (0..c.len()).choose_multiple(&mut rng, n - 1);
|
||||||
let r_i = (0..r.len()).choose_multiple(&mut rng, n);
|
let r_i = (0..r.len()).choose_multiple(&mut rng, n);
|
||||||
let mut sum = U128::ZERO;
|
let mut sum = T::ZERO;
|
||||||
for (&ci, &ri) in c_i.iter().zip(r_i.iter()) {
|
for (&ci, &ri) in c_i.iter().zip( r_i.iter() ) {
|
||||||
sum = sum.add_mod_special(
|
sum = sum.add_mod_prime( &c[ci].mul_mod_prime(&r[ri]) );
|
||||||
&c[ci].mul_mod_special(&r[ri], P_POS),
|
|
||||||
P_POS);
|
|
||||||
}
|
}
|
||||||
let ri_last = *r_i.last().expect("r_i will contain at least 2 elements");
|
let ri_last = *r_i.last()
|
||||||
let mod_inv = r[ri_last].inv_mod(&P).0;
|
.expect("r_i will contain at least 2 elements");
|
||||||
let c_new_el = msg_uint.sub_mod_special(&sum, P_POS)
|
let mod_inv = r[ri_last].inv_mod_prime();
|
||||||
.mul_mod_special(&mod_inv, P_POS);
|
let c_new_el = msg_uint.sub_mod_prime(&sum)
|
||||||
|
.mul_mod_prime(&mod_inv);
|
||||||
|
let c = self.cipher_mut();
|
||||||
c.push(c_new_el);
|
c.push(c_new_el);
|
||||||
c_i.push(c.len() - 1);
|
c_i.push(c.len() - 1);
|
||||||
let indices = c_i.into_iter()
|
let indices = c_i.into_iter()
|
||||||
|
@ -75,41 +100,59 @@ impl FBCrypt {
|
||||||
indices
|
indices
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decrypt_u128(&self, indices: &[(usize, usize)]) -> Result<U128, FBError> {
|
fn decrypt_u128(&self, indices: &[(usize, usize)]) -> Result<T, FBError> {
|
||||||
if indices.len() > self.r.len() {
|
let (r, c) = (self.keybase(), self.cipher());
|
||||||
|
if indices.len() > r.len() {
|
||||||
return Err(FBError::InvalidKey);
|
return Err(FBError::InvalidKey);
|
||||||
}
|
}
|
||||||
let mut msg = U128::ZERO;
|
let mut msg = T::ZERO;
|
||||||
for &(ci, ri) in indices {
|
for &(ci, ri) in indices {
|
||||||
let c_el = self.c.get(ci).ok_or(FBError::InvalidKey)?;
|
let c_el = c.get(ci).ok_or(FBError::InvalidKey)?;
|
||||||
let r_el = self.r.get(ri).ok_or(FBError::InvalidKey)?;
|
let r_el = r.get(ri).ok_or(FBError::InvalidKey)?;
|
||||||
msg = msg.add_mod_special(
|
msg = msg.add_mod_prime(&c_el.mul_mod_prime(&r_el));
|
||||||
&c_el.mul_mod_special(&r_el, P_POS),
|
|
||||||
P_POS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(msg)
|
Ok(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
impl<T> Encode<T> for FBObj<T>
|
||||||
fn encrypt_u128() {
|
where
|
||||||
let msg = U128::from_u32(100);
|
T: Encoding + crypto_bigint::Bounded,
|
||||||
let mut fb = FBCrypt::init(20, 12).unwrap();
|
<T as Encoding>::Repr: Iterator + for <'a> From<&'a [u8]>,
|
||||||
let key = fb.add_u128(&msg);
|
Vec<u8>: FromIterator<<<T as Encoding>::Repr as Iterator>::Item>
|
||||||
let decrypted = fb.decrypt_u128(&key).unwrap();
|
{
|
||||||
assert_eq!(msg, decrypted);
|
const BYTES: usize = T::BYTES;
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
fn export(&self) -> (String, String) {
|
||||||
fn encrypt_bytes() {
|
let c_bytes: Vec<u8> = self.c.iter()
|
||||||
let input1 = vec![255_u8; 150];
|
.flat_map(|bigint| bigint.to_le_bytes())
|
||||||
let input2 = vec![0_u8; 102];
|
.collect();
|
||||||
let mut fb = FBCrypt::init(21, 12).unwrap();
|
let r_bytes: Vec<u8> = self.r.iter()
|
||||||
let key1 = fb.add(&input1);
|
.flat_map(|bigint| bigint.to_le_bytes())
|
||||||
let key2 = fb.add(&input2);
|
.collect();
|
||||||
let decr1 = fb.decrypt(&key1).unwrap();
|
|
||||||
let decr2 = fb.decrypt(&key2).unwrap();
|
(BASE64_STANDARD.encode(c_bytes), BASE64_STANDARD.encode(r_bytes))
|
||||||
assert_eq!(input1, decr1);
|
}
|
||||||
assert_eq!(input2, decr2);
|
|
||||||
|
fn import(cipher: &str, keybase: &str) -> Result<Self, FBError>
|
||||||
|
where
|
||||||
|
Self: Sized
|
||||||
|
{
|
||||||
|
let c_bytes = BASE64_STANDARD.decode(cipher)
|
||||||
|
.map_err(|_| FBError::DecodeError)?;
|
||||||
|
let c: Vec<T> = c_bytes.chunks_exact(Self::BYTES)
|
||||||
|
.map(|chunk| T::from_le_bytes(chunk.try_into().unwrap()))
|
||||||
|
.collect();
|
||||||
|
let r_bytes = BASE64_STANDARD.decode(keybase)
|
||||||
|
.map_err(|_| FBError::DecodeError)?;
|
||||||
|
let r: Vec<T> = r_bytes.chunks_exact(Self::BYTES)
|
||||||
|
.map(|chunk| T::from_le_bytes(chunk.try_into().unwrap()))
|
||||||
|
.collect();
|
||||||
|
if c.len() < r.len() || r.len() < 2 {
|
||||||
|
return Err(FBError::InvalidParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Self {c, r})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,126 @@
|
||||||
|
use crypto_bigint::{U128, Limb, NonZero, Encoding};
|
||||||
|
use crate::{
|
||||||
|
false_bottom::{FalseBottom, ModPrime, FBBlockOperations, FBObj},
|
||||||
|
pack::Packing, FBError,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type FB128 = FBObj<U128>;
|
||||||
|
|
||||||
|
impl FalseBottom<U128> for FBObj<U128> {
|
||||||
|
const MODULUS: NonZero<U128> = NonZero::<U128>::const_new(Self::P).0;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FBBlockOperations<U128> for FBObj<U128> {
|
||||||
|
const P: U128 = U128::MAX.wrapping_sub(&U128::from_u8(159 - 1));
|
||||||
|
const P_POS: Limb = Limb::from_u8(159);
|
||||||
|
|
||||||
|
fn cipher(&self) -> &Vec<U128> {
|
||||||
|
&self.c
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cipher_mut(&mut self) -> &mut Vec<U128> {
|
||||||
|
&mut self.c
|
||||||
|
}
|
||||||
|
|
||||||
|
fn keybase(&self) -> &Vec<U128> {
|
||||||
|
&self.r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ModPrime<U128> for U128 {
|
||||||
|
const PRIME_POS: Limb = Limb::from_u32(159);
|
||||||
|
const PRIME: U128 = U128::MAX.wrapping_sub(&U128::from_u8(159 - 1));
|
||||||
|
|
||||||
|
fn mul_mod_prime(&self, rhs: &Self) -> Self {
|
||||||
|
self.mul_mod_special(rhs, Self::PRIME_POS)
|
||||||
|
}
|
||||||
|
fn add_mod_prime(&self, rhs: &Self) -> Self {
|
||||||
|
self.add_mod_special(rhs, Self::PRIME_POS)
|
||||||
|
}
|
||||||
|
fn sub_mod_prime(&self, rhs: &Self) -> Self {
|
||||||
|
self.sub_mod_special(rhs, Self::PRIME_POS)
|
||||||
|
}
|
||||||
|
fn inv_mod_prime(&self) -> Self {
|
||||||
|
self.inv_mod(&Self::PRIME).0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Packing<U128> for U128 {
|
||||||
|
const P_MINUS_ONE: U128 = U128::MAX.wrapping_sub(&U128::from_u8(159));
|
||||||
|
|
||||||
|
fn pack(inp: &[u8]) -> Vec<U128> {
|
||||||
|
let mut out = Vec::with_capacity(inp.len()/16 + 2);
|
||||||
|
inp.chunks(16)
|
||||||
|
.for_each(|inp_chunk| {
|
||||||
|
let mut out_chunk = [0_u8; 16];
|
||||||
|
out_chunk[..inp_chunk.len()].copy_from_slice(inp_chunk);
|
||||||
|
let mut out_uint = U128::from_le_bytes(out_chunk);
|
||||||
|
if out_uint >= Self::P_MINUS_ONE {
|
||||||
|
out.push(Self::P_MINUS_ONE);
|
||||||
|
out_uint = out_uint.wrapping_sub(&U128::from_u16(3000));
|
||||||
|
}
|
||||||
|
out.push(out_uint);
|
||||||
|
});
|
||||||
|
let inp_chunk_last = inp.chunks_exact(16)
|
||||||
|
.remainder();
|
||||||
|
let mut pad_chunk: [u8; 16] = [16; 16];
|
||||||
|
if inp_chunk_last.len() > 0 {
|
||||||
|
pad_chunk[15] += 16 - inp_chunk_last.len() as u8;
|
||||||
|
}
|
||||||
|
out.push(U128::from_le_bytes(pad_chunk));
|
||||||
|
out.shrink_to_fit();
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unpack(inp: &[U128]) -> Result<Vec<u8>, FBError> {
|
||||||
|
let pad_len = inp.last()
|
||||||
|
.ok_or(FBError::InvalidKey)?
|
||||||
|
.to_le_bytes()[15] as usize;
|
||||||
|
if pad_len > 31 {
|
||||||
|
return Err(FBError::InvalidKey);
|
||||||
|
}
|
||||||
|
let mut out = Vec::with_capacity(inp.len()*16);
|
||||||
|
let mut add_3k = false;
|
||||||
|
for i in inp {
|
||||||
|
if add_3k {
|
||||||
|
let orig = i.wrapping_add(&U128::from_u16(3000));
|
||||||
|
out.extend(orig.to_le_bytes().into_iter());
|
||||||
|
add_3k = false;
|
||||||
|
} else if *i == Self::P_MINUS_ONE {
|
||||||
|
add_3k = true;
|
||||||
|
} else {
|
||||||
|
out.extend(i.to_le_bytes().into_iter());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let trunc_len = out.len().checked_sub(pad_len)
|
||||||
|
.ok_or(FBError::InvalidKey)?;
|
||||||
|
out.truncate(trunc_len);
|
||||||
|
out.shrink_to_fit();
|
||||||
|
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn encrypt_u128() {
|
||||||
|
let msg = U128::from_u32(100);
|
||||||
|
let mut fb = FBObj::init(20, 12).unwrap();
|
||||||
|
let key = fb.add_u128(&msg);
|
||||||
|
let decrypted = fb.decrypt_u128(&key).unwrap();
|
||||||
|
assert_eq!(msg, decrypted);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn encrypt_bytes() {
|
||||||
|
let input1 = vec![255_u8; 150];
|
||||||
|
let input2 = vec![0_u8; 102];
|
||||||
|
let mut fb = FBObj::init(21, 12).unwrap();
|
||||||
|
let key1 = fb.add(&input1);
|
||||||
|
let key2 = fb.add(&input2);
|
||||||
|
let decr1 = fb.decrypt(&key1).unwrap();
|
||||||
|
let decr2 = fb.decrypt(&key2).unwrap();
|
||||||
|
assert_eq!(input1, decr1);
|
||||||
|
assert_eq!(input2, decr2);
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
use crate::FBError;
|
||||||
|
use base64::prelude::{BASE64_STANDARD, Engine};
|
||||||
|
use bincode::{Options, DefaultOptions};
|
||||||
|
|
||||||
|
pub struct FBKey {
|
||||||
|
pub(crate) indices: Vec<Vec<(usize, usize)>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FBKey {
|
||||||
|
|
||||||
|
pub fn export(&self) -> String {
|
||||||
|
let binc = DefaultOptions::new();
|
||||||
|
let indice_bytes = binc.serialize(&self.indices)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
BASE64_STANDARD.encode(&indice_bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn import(key_str: &str) -> Result<FBKey, FBError> {
|
||||||
|
let binc = DefaultOptions::new();
|
||||||
|
let indice_bytes = BASE64_STANDARD.decode(key_str)
|
||||||
|
.map_err(|_| FBError::DecodeError)?;
|
||||||
|
let indices: Vec<_> = binc.deserialize(&indice_bytes)
|
||||||
|
.map_err(|_| FBError::DecodeError)?;
|
||||||
|
if indices.len() < 2 {
|
||||||
|
return Err(FBError::DecodeError);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok (FBKey {indices})
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,15 @@
|
||||||
mod encoding;
|
mod encoding;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod false_bottom;
|
mod false_bottom;
|
||||||
|
mod key;
|
||||||
mod packing;
|
mod packing;
|
||||||
mod pack;
|
mod pack;
|
||||||
mod crypt;
|
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
errors::FBError,
|
errors::FBError,
|
||||||
false_bottom::{FBCrypt, FBKey},
|
false_bottom::fb128::FB128,
|
||||||
|
key::FBKey,
|
||||||
|
encoding::Encode,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub(crate) use crate::pack::Packing;
|
||||||
|
|
58
src/pack.rs
58
src/pack.rs
|
@ -1,4 +1,5 @@
|
||||||
use crypto_bigint::{Encoding};
|
use crypto_bigint::{Encoding};
|
||||||
|
use std::cmp::PartialOrd;
|
||||||
use crate::FBError;
|
use crate::FBError;
|
||||||
|
|
||||||
/* PACKING SCHEME
|
/* PACKING SCHEME
|
||||||
|
@ -11,61 +12,10 @@ use crate::FBError;
|
||||||
|
|
||||||
pub(crate) trait Packing<T>
|
pub(crate) trait Packing<T>
|
||||||
where
|
where
|
||||||
T: Encoding + std::cmp::PartialOrd
|
T: Encoding + PartialOrd
|
||||||
{
|
{
|
||||||
const P_MINUS_ONE: T;
|
const P_MINUS_ONE: T;
|
||||||
const BYTES: usize;
|
|
||||||
|
|
||||||
fn pack(inp: &[u8]) -> Vec<T> {
|
fn pack(inp: &[u8]) -> Vec<T>;
|
||||||
let mut out = Vec::with_capacity(inp.len()/Self::BYTES + 2);
|
fn unpack(inp: &[T]) -> Result<Vec<u8>, FBError>;
|
||||||
inp.chunks(Self::BYTES)
|
|
||||||
.for_each(|inp_chunk| {
|
|
||||||
let mut out_chunk = [0_u8; Self::BYTES];
|
|
||||||
out_chunk[..inp_chunk.len()].copy_from_slice(inp_chunk);
|
|
||||||
let mut out_uint = T::from_le_bytes(out_chunk);
|
|
||||||
if out_uint >= Self::P_MINUS_ONE {
|
|
||||||
out.push(Self::P_MINUS_ONE);
|
|
||||||
out_uint = out_uint.wrapping_sub(&T::from_u16(3000));
|
|
||||||
}
|
|
||||||
out.push(out_uint);
|
|
||||||
});
|
|
||||||
let inp_chunk_last = inp.chunks_exact(Self::BYTES)
|
|
||||||
.remainder();
|
|
||||||
let mut pad_chunk = [Self::BYTES as u8; Self::BYTES];
|
|
||||||
if inp_chunk_last.len() > 0 {
|
|
||||||
pad_chunk[Self::BYTES as u8 - 1] += Self::BYTES as u8 - inp_chunk_last.len() as u8;
|
|
||||||
}
|
|
||||||
out.push(T::from_le_bytes(pad_chunk));
|
|
||||||
out.shrink_to_fit();
|
|
||||||
|
|
||||||
out
|
|
||||||
}
|
|
||||||
|
|
||||||
fn unpack(inp: &[T]) -> Result<Vec<u8>, FBError> {
|
|
||||||
let pad_len = inp.last()
|
|
||||||
.ok_or(FBError::InvalidKey)?
|
|
||||||
.to_le_bytes()[15] as usize;
|
|
||||||
if pad_len > 31 {
|
|
||||||
return Err(FBError::InvalidKey);
|
|
||||||
}
|
|
||||||
let mut out = Vec::with_capacity(inp.len()*16);
|
|
||||||
let mut add_3k = false;
|
|
||||||
for i in inp {
|
|
||||||
if add_3k {
|
|
||||||
let orig = i.wrapping_add(&T::from_u16(3000));
|
|
||||||
out.extend(orig.to_le_bytes().into_iter());
|
|
||||||
add_3k = false;
|
|
||||||
} else if *i == Self::P_MINUS_ONE {
|
|
||||||
add_3k = true;
|
|
||||||
} else {
|
|
||||||
out.extend(i.to_le_bytes().into_iter());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let trunc_len = out.len().checked_sub(pad_len)
|
|
||||||
.ok_or(FBError::InvalidKey)?;
|
|
||||||
out.truncate(trunc_len);
|
|
||||||
out.shrink_to_fit();
|
|
||||||
|
|
||||||
Ok(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue