modular_math/mod_math/
mod_math_test.rs1
2
3#[cfg(test)]
4mod tests {
5 use primitive_types::U256;
6
7 use crate::mod_math::{ModMath, IntoU256};
8
9
10 #[test]
11 fn test_add() {
12 let modulus = 100;
14
15 let math = ModMath::new(modulus);
16 assert_eq!(math.add(U256::from(45), U256::from(60)), U256::from(5));
17 assert_eq!(math.add(U256::from(20), U256::from(75)), U256::from(95));
18 }
19
20 #[test]
21 fn test_sub() {
22 let modulus = U256::from(100);
23 let math = ModMath::new(modulus);
24 assert_eq!(math.sub(U256::from(60), U256::from(45)), U256::from(15));
25 assert_eq!(math.sub(U256::from(30), U256::from(40)), U256::from(90));
26 }
27
28 #[test]
29 fn test_multiply() {
30 let modulus = U256::from(100);
31 let math = ModMath::new(modulus);
32 assert_eq!(math.mul(U256::from(12), U256::from(25)), U256::from(0));
33 assert_eq!(math.mul(U256::from(7), U256::from(14)), U256::from(98));
34 }
35
36 #[test]
37 fn test_exp() {
38 let modulus = U256::from(100);
39 let math = ModMath::new(modulus);
40 assert_eq!(math.exp(U256::from(3), U256::from(4)), U256::from(81));
41 assert_eq!(math.exp(U256::from(2), U256::from(8)), U256::from(56));
42 }
43
44 #[test]
45 fn test_mod_inv() {
46 let modulus = U256::from(101);
47 let math = ModMath::new(modulus);
48
49 let a = U256::from(10);
50 let a_inv = math.inv(a).unwrap();
51 assert_eq!(math.mul(a, a_inv), U256::one());
52
53 let b = U256::from(10);
54 let b_inv = math.inv(a).unwrap();
55 assert_eq!(math.mul(b, b_inv), U256::one());
56 }
57
58 #[test]
59 fn test_div() {
60 let modulus = U256::from(101);
61 let math = ModMath::new(modulus);
62
63 let a = U256::from(10);
64 let b = U256::from(20);
65 assert_eq!(math.div(a, b), U256::from(51));
66
67 let a = U256::from(10);
68 let b = U256::from(10);
69 assert_eq!(math.div(a, b), U256::one());
70 }
71
72 #[test]
73 fn test_secp256k1() {
74 let p: U256 = U256::from_dec_str("115792089237316195423570985008687907852837564279074904382605163141518161494337").unwrap();
75 let math = ModMath::new(p);
76
77 let num = U256::from_dec_str("32670510020758816978083085130507043184471273380659243275938904335757337482424").unwrap();
78 let den = U256::from_dec_str("55066263022277343669578718895168534326250603453777594175500187360389116729240").unwrap();
79
80 let den_inv = math.inv(den).unwrap();
81
82 assert_eq!(math.mul(den, den_inv), U256::one());
83
84 assert_eq!(den_inv, U256::from_dec_str("13499648161236477938760301359943791721062504425530739546045302818736391397630").unwrap())
85
86 }
87
88 #[test]
89 fn test_into_u256() {
90 assert_eq!(10_u32.into_u256(), U256::from(10));
91 assert_eq!(10_i32.into_u256(), U256::from(10));
92 assert_eq!(10_u64.into_u256(), U256::from(10));
93 assert_eq!(10_i64.into_u256(), U256::from(10));
94 assert_eq!("10".into_u256(), U256::from(10));
95 assert_eq!(U256::from(10).into_u256(), U256::from(10));
96 }
97
98 #[test]
99 #[should_panic(expected = "Negative value cannot be converted to U256")]
100 fn test_into_u256_negative() {
101 let _ = (-10_i32).into_u256();
102 }
103
104 #[test]
105 fn test_square() {
106 let math = ModMath::new(100);
107 assert_eq!(math.square(10), U256::from(0));
108 }
109
110 #[test]
111 fn test_sqrt() {
112 let math = ModMath::new(113);
113 let num = 2;
114 let mod_sqrt = math.sqrt(num).unwrap();
115 println!("{}", mod_sqrt);
116 assert_eq!(math.exp(mod_sqrt, U256::from(2)), U256::from(num));
117 }
118
119 #[test]
148 fn test_big_number_modulus() {
149 let math = ModMath::new(U256::max_value());
150 let result = math.modulus(U256::max_value() - U256::from(10));
151 assert_eq!(result, U256::max_value() - U256::from(10));
152 }
153
154 }