#![feature(const_trait_impl)] #![allow(incomplete_features)] #![feature(generic_const_exprs)] use cryptix_bigint::{BigUInt, property::IsBigInt, bigint}; type U256 = BigUInt; type U512 = BigUInt; const ZERO256: U256 = U256::ZERO; const ZERO512: U512 = U512::ZERO; #[test] fn test_add_sub() { let a = bigint!(U256, "5d80e2b873b4e99a7b46d208f51fb915eaa8155f82d315760c5487d751ca68fb"); let b = bigint!(U256, "dffae1b8f9d8e3b4a3fd09ca3245f64dce54000e15232187cbd9ddd45f91c84f"); let (mone256, _) = ZERO256 - 1; // round wrap test assert_eq!(mone256, bigint!(U256, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); assert_eq!((mone256 + 1).0, ZERO256); // simple add sub test let a_p_b = bigint!(U256, "3d7bc4716d8dcd4f1f43dbd32765af63b8fc156d97f636fdd82e65abb15c314a"); assert_eq!((a + b).0, a_p_b); assert_eq!((a_p_b - a).0, b); assert_eq!((a_p_b - b).0, a); } #[test] fn test_mul() { let a = bigint!(U256, "5d80e2b873b4e99a7b46d208f51fb915eaa8155f82d315760c5487d751ca68fb"); let b = bigint!(U256, "dffae1b8f9d8e3b4a3fd09ca3245f64dce54000e15232187cbd9ddd45f91c84f"); // multiplication test let mut res: U512; res = (a * (ZERO256 + 1).0).into(); assert_eq!(res, a.resize::<{ U512::DIG_LEN }>()); res = (a * ZERO256).into(); assert_eq!(res, ZERO512); res = (a * b).into(); assert_eq!(res, bigint!(U512, "51cee7cdec143cded552cdf6ca5da13099bc34f5362f9a5ed6be0bbef131fe935834ca29fe074fd47d80d7e56df605e82c0cba147b9420742e8832cefaa57d75")); } #[test] fn test_shift() { let a = bigint!(U256, "5d80e2b873b4e99a7b46d208f51fb915eaa8155f82d315760c5487d751ca68fb"); // shift test assert_eq!(a >> 0, a); assert_eq!(a << 0, a); assert_eq!(a >> 64, bigint!(U256, "5d80e2b873b4e99a7b46d208f51fb915eaa8155f82d31576")); assert_eq!(a << 64, bigint!(U256, "7b46d208f51fb915eaa8155f82d315760c5487d751ca68fb0000000000000000")); assert_eq!(a << 256, ZERO256); assert_eq!(a >> 123, bigint!(U256, "0bb01c570e769d334f68da411ea3f722bd")); assert_eq!(a >> 1, bigint!(U256, "2ec0715c39da74cd3da369047a8fdc8af5540aafc1698abb062a43eba8e5347d")); assert_eq!(a << 1, bigint!(U256, "bb01c570e769d334f68da411ea3f722bd5502abf05a62aec18a90faea394d1f6")); } #[test] fn test_mod() { let m = bigint!(U256, "0bb01c570e769d334f68da411ea3f722bd"); assert_eq!(U256::ZERO % m, U256::ZERO); assert_eq!(bigint!(U256, "01") % m, bigint!(U256, "01")); assert_eq!(m % m, U256::ZERO); assert_eq!(bigint!(U256, "223aaa29fbe7facafd4b9d38d42b72fb6b1e9f4bb555ce568cca2366897b7b7c") % m, bigint!(U256, "02cfb8b82427f1069c37d836d7353a0d52")); }