Initial commit

Encryption and Decryption for a single int block implemented.
This commit is contained in:
K Shiva Kiran 2024-02-13 08:23:59 +05:30
commit b10874e5d6
6 changed files with 225 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

109
Cargo.lock generated Normal file
View File

@ -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"

10
Cargo.toml Normal file
View File

@ -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"

6
src/errors.rs Normal file
View File

@ -0,0 +1,6 @@
#[derive(Debug)]
pub enum FalseBottomError {
IntegerTooLarge,
NoModInverse,
InvalidKey,
}

94
src/lib.rs Normal file
View File

@ -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<i32>,
pub c: Vec<i32>,
}
#[derive(Debug)]
pub struct FalseBottomKey {
pub key_indices: Vec<i32>,
pub cipher_indices: Vec<i32>,
}
const P: i32 = 9973;
impl FalseBottom {
pub fn init(k: i32) -> FalseBottom {
let mut rng = rand::thread_rng();
let mut r: Vec<i32> = Vec::new();
let mut c: Vec<i32> = 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<FalseBottomKey, FalseBottomError>
{
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<i32> = (0..self.c.len() as i32)
.choose_multiple(&mut rng, n as usize - 1);
let key_indices: Vec<i32> = (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<i32, FalseBottomError> {
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);
}
}

5
src/main.rs Normal file
View File

@ -0,0 +1,5 @@
//use false_bottom::FalseBottom;
fn main() {
}