ferric_crypto_lib 0.2.7

A library for Ferric Crypto
Documentation
/*
    TODO: impl the code below and optimize it
    Example code for calculating the modular inverse of a matrix in Rust.

    use nalgebra::*;

    // maybe implement EEA here and use nalgebras gcd function
    fn modular_inverse(x: f32, modulus: i32) -> Option<f32> {
        for i in 1..modulus {
            if (i as f32 * x % modulus as f32 + modulus as f32) % modulus as f32 == 1.0 {
                return Some(i as f32);
            }
        }
        None
    }

    fn minor(matrix: &DMatrix<f32>, row: usize, col: usize) -> f32 {
        let submatrix = matrix.clone().remove_row(row).remove_column(col);
        submatrix.determinant()
    }

    fn adjugate(matrix: &DMatrix<f32>) -> DMatrix<f32> {
        let cofactor_matrix = matrix.map_with_location(|(i, j), _| {
            let sign = if (i + j) % 2 == 0 { 1.0 } else { -1.0 };
            sign * minor(matrix, i, j)
        });

        cofactor_matrix.transpose()
    }


    fn main() {
        let m = Matrix3::new(
            5.0, 17.0, 6.0,
            2.0, 21.0, 14.0,
            19.0, 3.0, 11.0,
        );

        let determinant = m.determinant() as f32;  // Calculate the determinant

        // Adjust the determinant for modulo 28
        let det_mod: f32 = (determinant % 28.0 + 28.0) % 28.0;
        dbg!(&determinant);

        // Find the modular inverse of the determinant
        match modular_inverse(det_mod, 28) {
            Some(det_mod_inverse) => {
                // Manually compute the adjugate matrix
                let adjugate = Matrix3::new(
                    (m[(1, 1)] * m[(2, 2)] - m[(1, 2)] * m[(2, 1)]), - (m[(1, 0)] * m[(2, 2)] - m[(1, 2)] * m[(2, 0)]), (m[(1, 0)] * m[(2, 1)] - m[(1, 1)] * m[(2, 0)]),
                    - (m[(0, 1)] * m[(2, 2)] - m[(0, 2)] * m[(2, 1)]), (m[(0, 0)] * m[(2, 2)] - m[(0, 2)] * m[(2, 0)]), - (m[(0, 0)] * m[(2, 1)] - m[(0, 1)] * m[(2, 0)]),
                    (m[(0, 1)] * m[(1, 2)] - m[(0, 2)] * m[(1, 1)]), - (m[(0, 0)] * m[(1, 2)] - m[(0, 2)] * m[(1, 0)]), (m[(0, 0)] * m[(1, 1)] - m[(0, 1)] * m[(1, 0)]),
                ).transpose();

                // Apply the modular inverse to the adjugate and then apply modulo 28
                let modular_inverse = adjugate.map(|x| ((x * det_mod_inverse) % 28.0 + 28.0) % 28.0);

                println!("Modular Inverse:\n{}", modular_inverse);
            }
            None => println!("Matrix is not invertible in mod 28"),
        }

        let m = Matrix3::new(
            5.0, 17.0, 6.0,
            2.0, 21.0, 14.0,
            19.0, 3.0, 11.0,
        );
        let m = DMatrix::from_row_slice(3, 3, m.as_slice());

        let adj = adjugate(&m);
        println!("Adjugate Matrix:\n{}", &adj);

        let adj_trans = adj.transpose();
        println!("Transposed Adjugate Matrix:\n{}", &adj_trans);

        let determinant = m.determinant() as f32;  // Calculate the determinant

        // Adjust the determinant for modulo 28
        let det_mod: f32 = (determinant % 28.0 + 28.0) % 28.0;
        dbg!(&determinant);

        // Find the modular inverse of the determinant
        match modular_inverse(det_mod, 28) {
            Some(det_mod_inverse) => {
                // Apply the modular inverse to the adjugate and then apply modulo 28
                let modular_inverse = adj_trans.map(|x| ((x * det_mod_inverse) % 28.0 + 28.0) % 28.0);

                println!("Modular Inverse:\n{}", modular_inverse);
            }
            None => println!("Matrix is not invertible in mod 28"),
        }
    }

*/

use crate::crypto_systems::hill_crypto::{Hill, HillError};
use crate::prelude::*;
use crate::Traits::Decrypt;

impl Decrypt<HillError, String, String> for Hill {
    /// Decrypts a string using the RSA cipher.
    ///
    /// TODO: make decscription
    /// description goes here
    ///
    /// # Arguments
    ///
    /// * `cipher_text` - A `String` that holds the text to be decrypted.
    ///
    /// # Returns
    ///
    /// * A `Result<String, RSAError>` which is `Ok` if the decryption is successful, and `Err` otherwise.
    ///   The `Ok` variant contains the decrypted text, and the `Err` variant contains an error type.
    ///
    /// # Example
    ///
    /// TODO: make example
    fn decrypt(&self, cipher_text: String) -> Result<String, HillError> {
        unimplemented!()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::Traits::Decrypt;

    // TODO: make tests for RSA decrypt implementation
}