From bcc7aad8264bcff46491d5be5fab4d813e4a0159 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 2 May 2023 14:05:36 +0200 Subject: [PATCH] iterated squaring start --- Cargo.lock | 31 ++++++++++++++++++++++ Cargo.toml | 6 +++++ src/iterated_squaring.rs | 56 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 17 ++++++++++++ 5 files changed, 111 insertions(+) create mode 100644 src/iterated_squaring.rs create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 6ba8544..3665f5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,36 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.17.1" @@ -84,6 +114,7 @@ dependencies = [ name = "plexcryptool" version = "0.1.0" dependencies = [ + "num-bigint", "pyo3", ] diff --git a/Cargo.toml b/Cargo.toml index 5609980..ed9457c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,12 @@ edition = "2021" [lib] name = "plexcryptool" crate-type = ["cdylib"] +path = "src/lib.rs" + +[[bin]] +name = "plexcryptool" +path = "src/main.rs" [dependencies] +num-bigint = "0.4.3" pyo3 = "0.18.1" diff --git a/src/iterated_squaring.rs b/src/iterated_squaring.rs new file mode 100644 index 0000000..a96e1aa --- /dev/null +++ b/src/iterated_squaring.rs @@ -0,0 +1,56 @@ +use std::{str::FromStr, array}; + +use num_bigint::{BigInt, BigUint}; + +/// works, but is forbidden for class +pub fn calc_exp_in_field_lib( + base: BigInt, + exp: BigInt, + field: BigInt) -> BigInt { + base.modpow(&exp, &field) +} + +/** + * Umwandlung des Exponenten k in die zugehörige Binärdarstellung. + * Ersetzen jeder 0 durch Q und jeder 1 durch QM. + * Nun wird Q als Anweisung zum Quadrieren und M als Anweisung zum Multiplizieren aufgefasst. + * Somit bildet die resultierende Zeichenkette von links nach rechts gelesen eine Vorschrift zur Berechnung von x k . + * Man beginne mit 1, quadriere für jedes gelesene Q das bisherige Zwischenergebnis und + * multipliziere es für jedes gelesene M mit x . + */ +pub fn calc_exp_in_field( + base: BigInt, + exp: BigInt, + field: BigInt) -> BigInt { + let binary_repr = exp.to_bytes_be(); + dump_bin(&binary_repr.1); + + + let instructions: Vec = bytes_to_bools(&binary_repr.1); + dbg!(instructions); + + return base; +} + +fn bytes_to_bools(bytes: &Vec) -> Vec { + let mut result: Vec = Vec::new(); + for byte in bytes { + for c in format!("{:b}", byte).chars() { + result.push(c == '1'); + } + } + result +} + +fn dump_bin(bytes: &Vec) { + let mut total: Vec = Vec::new(); + for byte in bytes.iter() { + println!("{:#08b}\t| {:#02x}", byte, byte); + } + print!("0b"); + for byte in bytes.iter() { + print!("{:08b}", byte); + } + println!(); + +} diff --git a/src/lib.rs b/src/lib.rs index 29c21e3..f6e0153 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ use pyo3::prelude::*; mod binary; +mod iterated_squaring; #[pymodule] fn register_binary_module(py: Python, parent_module: &PyModule) -> PyResult<()> { diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6cd05e3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,17 @@ +mod binary; +mod iterated_squaring; + +use std::str::FromStr; + +use iterated_squaring::calc_exp_in_field; + +use num_bigint; + +pub fn main() { + let b = num_bigint::BigInt::from_str("17").expect("a"); + let e = num_bigint::BigInt::from_str("1011201391039").expect("a"); + let f = num_bigint::BigInt::from_str("101").expect("a"); + let r = calc_exp_in_field(b.clone(), e, f); + assert_eq!(r, b); + print!("res is {}\n", r) +}