mirror of
https://github.com/mii443/encrypt.git
synced 2025-08-22 15:05:33 +00:00
数学準備
This commit is contained in:
@ -1,9 +1,58 @@
|
||||
use bigdecimal::{num_bigint::BigInt, Zero, One};
|
||||
use primitive_types::U512;
|
||||
|
||||
use super::finite_field::FiniteFieldElement;
|
||||
use bigdecimal::Num;
|
||||
|
||||
pub fn plus_mod(a: BigInt, m: BigInt) -> BigInt {
|
||||
(a.clone() % m.clone() + m.clone()) % m
|
||||
}
|
||||
|
||||
pub fn u512_to_bigint(i: U512) -> BigInt {
|
||||
BigInt::from_str_radix(&format!("{}", i), 10).unwrap()
|
||||
}
|
||||
|
||||
pub fn bigint_to_u512(i: BigInt) -> U512 {
|
||||
U512::from_str_radix(&format!("{}", i), 10).unwrap()
|
||||
}
|
||||
|
||||
pub fn abs(a: BigInt, p: BigInt) -> BigInt {
|
||||
if a >= BigInt::zero() {
|
||||
a % p
|
||||
} else {
|
||||
(p.clone()-(-a)%p.clone())%p
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pow_mod(a: BigInt, n: BigInt, p: BigInt) -> BigInt {
|
||||
let mut r = BigInt::one();
|
||||
let mut db = a.clone();
|
||||
let mut n = n;
|
||||
|
||||
while n > BigInt::zero() {
|
||||
if (n.clone() & BigInt::one()) == BigInt::one() {
|
||||
r = (r.clone() * db.clone()) % p.clone();
|
||||
}
|
||||
db = (db.clone() * db.clone()) % p.clone();
|
||||
n = n >> 1;
|
||||
}
|
||||
|
||||
abs(r, p)
|
||||
}
|
||||
|
||||
pub fn random_n_q(p: BigInt) -> BigInt {
|
||||
let mut i = BigInt::one();
|
||||
let k = (p.clone() - BigInt::one()) >> 1i32;
|
||||
while i < p {
|
||||
println!("pm {:?}", FiniteFieldElement::new(bigint_to_u512(i.clone()), bigint_to_u512(p.clone())).pow(bigint_to_u512(k.clone())).value);
|
||||
if bigint_to_u512(pow_mod(i.clone(),k.clone(),p.clone())) != U512::one() {
|
||||
break;
|
||||
}
|
||||
i += BigInt::one();
|
||||
}
|
||||
i
|
||||
}
|
||||
|
||||
pub fn mod_inv(a: BigInt, m: BigInt) -> BigInt {
|
||||
let mut a = a;
|
||||
if a < BigInt::zero() {
|
||||
|
@ -46,6 +46,7 @@ impl EllipticCurvePoint {
|
||||
EllipticCurvePoint::Infinity => true,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Neg for EllipticCurvePoint {
|
||||
|
@ -1,9 +1,12 @@
|
||||
use encrypt::{elliptic_curve::{elliptic_curve::EllipticCurve, encryption::Encryption}, common::finite_field::FiniteFieldElement};
|
||||
use bigdecimal::num_bigint::BigInt;
|
||||
use encrypt::{elliptic_curve::{elliptic_curve::EllipticCurve, encryption::Encryption}, common::{finite_field::FiniteFieldElement, math::random_n_q}};
|
||||
use primitive_types::U512;
|
||||
|
||||
fn main() {
|
||||
println!("Encryption Library");
|
||||
|
||||
println!("{}", random_n_q(BigInt::from(23)));
|
||||
|
||||
let p = U512::from_str_radix("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10).unwrap();
|
||||
|
||||
let secp256_k1_a = FiniteFieldElement::new(U512::from(0u8), p);
|
||||
@ -41,7 +44,7 @@ fn main() {
|
||||
let public_key = encryption.get_public_key(private_key);
|
||||
println!("public_key: {}", public_key);
|
||||
|
||||
for x in 0..100 {
|
||||
for x in 0..10 {
|
||||
let ten = encryption.plain_to_ec_point(U512::from(10u32));
|
||||
let e_ten = encryption.encrypt(ten, public_key, None);
|
||||
println!("10 -> {}", e_ten.data);
|
||||
|
Reference in New Issue
Block a user