From b10874e5d69a89cd879db39975b2bbd3965e45b7 Mon Sep 17 00:00:00 2001 From: K Shiva Kiran Date: Tue, 13 Feb 2024 08:23:59 +0530 Subject: [PATCH] Initial commit Encryption and Decryption for a single int block implemented. --- .gitignore | 1 + Cargo.lock | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 +++++ src/errors.rs | 6 +++ src/lib.rs | 94 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 +++ 6 files changed, 225 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/errors.rs create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7a18bd2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,109 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "false-bottom" +version = "0.1.0" +dependencies = [ + "modinverse", + "rand", +] + +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "modinverse" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f62f577f148cc1a9466e7065a22e59466a7d537cceba5e77e57181d0f706633" +dependencies = [ + "num-integer", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2700d76 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "false-bottom" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "^0.8.5" +modinverse = "0.1.1" \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..8f9126a --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,6 @@ +#[derive(Debug)] +pub enum FalseBottomError { + IntegerTooLarge, + NoModInverse, + InvalidKey, +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0bd85a6 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,94 @@ +pub mod errors; + +use rand::Rng; +use rand::seq::IteratorRandom; +use crate::errors::FalseBottomError; +use modinverse::modinverse; + +#[derive(Debug)] +pub struct FalseBottom { + pub k: i32, + pub r: Vec, + pub c: Vec, +} + +#[derive(Debug)] +pub struct FalseBottomKey { + pub key_indices: Vec, + pub cipher_indices: Vec, +} + +const P: i32 = 9973; + +impl FalseBottom { + pub fn init(k: i32) -> FalseBottom { + let mut rng = rand::thread_rng(); + let mut r: Vec = Vec::new(); + let mut c: Vec = Vec::new(); + for _ in 0..k { + r.push(rng.gen_range(1..k)); + c.push(rng.gen_range(1..P)); + } + FalseBottom {k, r, c} + } + + pub fn add(&mut self, m: i32) -> + Result + { + if m > P as i32 { + return Err(FalseBottomError::IntegerTooLarge) + } + let mut rng = rand::thread_rng(); + let n = rng.gen_range(2..=self.k); + + let mut cipher_indices: Vec = (0..self.c.len() as i32) + .choose_multiple(&mut rng, n as usize - 1); + + let key_indices: Vec = (0..self.k) + .choose_multiple(&mut rng, n as usize); + + let mut sum = 0; + for (j, rho) in cipher_indices.iter() + .zip(key_indices.iter()) + { + sum += self.c[*j as usize] * self.r[*rho as usize]; + sum %= P; + } + + let key_last_el = *key_indices.last().unwrap() as usize; + let mod_inv = modinverse(self.r[key_last_el], P) + .ok_or(FalseBottomError::NoModInverse)?; + let cipher_new_element = ((m - sum) * mod_inv) + .rem_euclid(P); + self.c.push(cipher_new_element); + cipher_indices.push(self.c.len() as i32 - 1); + Ok(FalseBottomKey {key_indices, cipher_indices}) + } + + pub fn decrypt(&self, indices: &FalseBottomKey) -> Result { + let keyi = &indices.key_indices; + let cipheri = &indices.cipher_indices; + if keyi.len() > self.r.len() || cipheri.len() > self.c.len() { + return Err(FalseBottomError::InvalidKey) + } + let mut m: i32 = 0; + for (ki, ci) in keyi.iter().zip(cipheri.iter()) { + m += self.c[*ci as usize] * self.r[*ki as usize]; + m %= P; + } + Ok(m) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn encrypt_decrypt_100() { + let msg = 100; + let mut fb = FalseBottom::init(9); + let key = fb.add(msg).unwrap(); + let decrypted = fb.decrypt(&key).unwrap(); + assert_eq!(decrypted, msg); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e79cf53 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +//use false_bottom::FalseBottom; + +fn main() { + +}