iterated squaring start
This commit is contained in:
parent
bfdb72dfed
commit
bcc7aad826
|
@ -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",
|
||||
]
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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<bool> = bytes_to_bools(&binary_repr.1);
|
||||
dbg!(instructions);
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
fn bytes_to_bools(bytes: &Vec<u8>) -> Vec<bool> {
|
||||
let mut result: Vec<bool> = Vec::new();
|
||||
for byte in bytes {
|
||||
for c in format!("{:b}", byte).chars() {
|
||||
result.push(c == '1');
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn dump_bin(bytes: &Vec<u8>) {
|
||||
let mut total: Vec<u8> = Vec::new();
|
||||
for byte in bytes.iter() {
|
||||
println!("{:#08b}\t| {:#02x}", byte, byte);
|
||||
}
|
||||
print!("0b");
|
||||
for byte in bytes.iter() {
|
||||
print!("{:08b}", byte);
|
||||
}
|
||||
println!();
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
use pyo3::prelude::*;
|
||||
|
||||
mod binary;
|
||||
mod iterated_squaring;
|
||||
|
||||
#[pymodule]
|
||||
fn register_binary_module(py: Python, parent_module: &PyModule) -> PyResult<()> {
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue