From c36aa12756bb888b5d3e3d4d7e3a238a13045862 Mon Sep 17 00:00:00 2001 From: K Shiva Kiran Date: Fri, 12 Apr 2024 14:31:10 +0530 Subject: [PATCH] Much faster now --- examples/file.rs | 23 +++++++++++++---------- src/algo.rs | 33 ++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/examples/file.rs b/examples/file.rs index 22ede5f..3e9ae0d 100644 --- a/examples/file.rs +++ b/examples/file.rs @@ -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}%)"); diff --git a/src/algo.rs b/src/algo.rs index 9f59fb5..1b98abd 100644 --- a/src/algo.rs +++ b/src/algo.rs @@ -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::::init(18, 12).unwrap(); - let lock = RwLock::new(&mut fb); - let key = FBObj::::add_block(&lock, &msg); + let fb = FBObj::::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::::init(21, 9).unwrap(); + let fb = FBObj::::init(21, 9).unwrap(); let key1 = fb.add(&input1); let key2 = fb.add(&input2); let decr1 = fb.decrypt(&key1).unwrap();