Much faster now

This commit is contained in:
K Shiva Kiran 2024-04-12 14:31:10 +05:30
parent 888c863628
commit c36aa12756
2 changed files with 31 additions and 25 deletions

View File

@ -1,22 +1,25 @@
use false_bottom::{FB128, FBAlgo, Encode};
use std::fs;
use std::time::Instant;
fn main() {
//let inp = fs::read("input1").unwrap();
let inp = vec![0_u8; 204800];
let inp = vec![0_u8; 1024000];
println!("Input size: {} Bytes", inp.len());
let mut fb = FB128::init(2, 2).unwrap();
println!("Encrypting...");
let now = Instant::now();
let _key = fb.add(&inp);
let elapsed = now.elapsed().as_secs_f32();
println!("Took {} secs", elapsed);
println!("Exporting...");
let key = fb.add(&inp);
let t_encr = now.elapsed().as_secs_f32();
let encr = fb.to_bytes().0;
let rate = encr.len() as f32 / elapsed;
println!("Rate: {rate:.1} B/sec");
let r_encr = encr.len() as f32 / t_encr;
println!("Encryption: {t_encr:.2} @ {r_encr:.1} B/sec");
println!("Decrypting...");
let now = Instant::now();
let decr = fb.decrypt(&key).unwrap();
let t_decr = now.elapsed().as_secs_f32();
let r_decr = decr.len() as f32 / t_decr;
println!("Decryption: {t_decr:.2} @ {r_decr:.1} B/sec");
let extra = encr.len() - inp.len();
let percent = extra as f32/encr.len() as f32 * 100_f32;
println!("Extra Bytes: {extra} Bytes ({percent}%)");

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::{FBError, FBKey, FBObj, FBObjTrait, FieldOps, Packing};
use crypto_bigint::{NonZero, RandomMod};
use rand::{Rng, seq::IteratorRandom};
use rand::{seq::index, Rng};
use rayon::iter::*;
use std::marker::Send;
use std::sync::RwLock;
@ -38,8 +38,8 @@ where
fn add(&mut self, msg: &[u8]) -> FBKey {
let indices = T::pack(msg)
.into_par_iter()
.map(|msg_uint| self.add_block(&msg_uint))
.collect();
.map(|msg_uint| self.add_block(&msg_uint))
.collect();
FBKey { indices }
}
@ -66,23 +66,27 @@ where
T: FieldOps + RandomMod,
{
fn add_block(&self, msg_uint: &T) -> Vec<(usize, usize)> {
let mut c_len = self.cipher().read().unwrap().len();
let mut c_len;
{
c_len = self.cipher().read().unwrap().len();
}
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 c_i = index::sample(&mut rng, c_len, n-1).into_vec();
let r_i = index::sample(&mut rng, r.len(), n).into_vec();
let ri_last = *r_i.last()
.expect("r_i will contain at least 2 elements");
let ri_last_inv = r[ri_last].field_inv();
let mut sum = T::ZERO;
let sum: T;
{
let c = self.cipher().read().unwrap();
for (&ci, &ri) in c_i.iter().zip( r_i.iter() ) {
sum = sum.field_add( &c[ci].field_mul(&r[ri]) );
}
sum = c_i.iter()
.zip( r_i.iter() )
.map(|(&ci, &ri)| c[ci].field_mul(&r[ri]) )
.reduce(|acc, i| acc.field_add(&i))
.unwrap();
}
let c_new_el = msg_uint.field_sub(&sum)
.field_mul(&ri_last_inv);
{
@ -119,9 +123,8 @@ where
fn encrypt_u128() {
use crypto_bigint::U128;
let msg = U128::from_u32(100);
let mut fb = FBObj::<U128>::init(18, 12).unwrap();
let lock = RwLock::new(&mut fb);
let key = FBObj::<U128>::add_block(&lock, &msg);
let fb = FBObj::<U128>::init(18, 12).unwrap();
let key = fb.add_block(&msg);
let decrypted = fb.decrypt_block(&key).unwrap();
assert_eq!(msg, decrypted);
}
@ -131,7 +134,7 @@ fn encrypt_bytes() {
use crypto_bigint::U128;
let input1 = vec![255_u8; 33];
let input2 = vec![0_u8; 102];
let mut fb = FBObj::<U128>::init(21, 9).unwrap();
let fb = FBObj::<U128>::init(21, 9).unwrap();
let key1 = fb.add(&input1);
let key2 = fb.add(&input2);
let decr1 = fb.decrypt(&key1).unwrap();