Skip to content

Commit

Permalink
Tested utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Apr 17, 2019
1 parent 57a047b commit 14b18bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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()));
}
8 changes: 4 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

extern crate rand;

use rand::Rng;
Expand All @@ -14,7 +15,6 @@ pub fn egcd(a: i64,b: i64) -> (i64, i64, i64) {

pub fn modinv(a: i64, prime: i64) -> Option<i64> {
let (gcd, x, _) = egcd(a, prime);

if gcd != 1 {
None
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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::<String>()
}
}

Expand Down

0 comments on commit 14b18bd

Please sign in to comment.