From 14b18bd3450bba494939d10c7fff1b4fa15a1426 Mon Sep 17 00:00:00 2001 From: pawanjay176 Date: Wed, 17 Apr 2019 13:12:52 +0530 Subject: [PATCH] Tested utils --- src/main.rs | 15 +++++++++++---- src/utils.rs | 8 ++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7e87d79..5650247 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,16 @@ mod utils; - +#[allow(dead_code)] fn main() { - println!("Hello, world!"); + println!("Hello World"); println!("{:?}", utils::modinv(-18, 13)); println!("{:?}", utils::random_polynomial(10, 50, 41)); - println!("{:?}", utils::mod_lagrange_interpolation(vec![(1,2), (3,4), (5,6)], 100)); -} + let coeff = utils::random_polynomial(3, 13, 103); + println!("{:?}", coeff); + let points = utils::get_polynomial_points(coeff.clone(), 4, 103); + println!("{:?}", points); + let res = utils::mod_lagrange_interpolation(points.clone(), 103); + println!("{}", res); + println!("{}", utils::int_to_charset(0, "0123456789abcdef".to_string())); + println!("{}", utils::charset_to_int("abcdef123450123".to_string(), "0123456789abcdef".to_string())); +} \ No newline at end of file diff --git a/src/utils.rs b/src/utils.rs index 575fc54..6fb8799 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,4 @@ + extern crate rand; use rand::Rng; @@ -14,7 +15,6 @@ pub fn egcd(a: i64,b: i64) -> (i64, i64, i64) { pub fn modinv(a: i64, prime: i64) -> Option { let (gcd, x, _) = egcd(a, prime); - if gcd != 1 { None } @@ -67,7 +67,7 @@ pub fn mod_lagrange_interpolation(points: Vec<(i64, i64)>, prime: i64) -> i64 { let lagrange_polynomial = num * modinv(den, prime).unwrap(); res = (res + prime + (y_values[i] * lagrange_polynomial)) % prime; } - res + (res+prime) % prime } pub fn int_to_charset(val: u64, charset: String) -> String { @@ -78,11 +78,11 @@ pub fn int_to_charset(val: u64, charset: String) -> String { let mut res = String::new(); let mut value = val.clone(); while value > 0 { - value = value / charset.len() as u64; let digit = value % charset.len() as u64; + value = value / charset.len() as u64; res.push(charset.chars().nth(digit as usize).unwrap()); } - res + res.chars().rev().collect::() } }