From 9a975f634241ffe54dc33bc6c157002d105e44b4 Mon Sep 17 00:00:00 2001 From: mii8080 <39086319+morioka22@users.noreply.github.com> Date: Thu, 9 Jun 2022 08:33:44 +0000 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E5=AD=A6=E6=BA=96=E5=82=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/math.rs | 49 ++++++++++++++++++++++++++++ src/elliptic_curve/elliptic_curve.rs | 1 + src/main.rs | 7 ++-- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/common/math.rs b/src/common/math.rs index 74553a0..a1c7054 100644 --- a/src/common/math.rs +++ b/src/common/math.rs @@ -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() { diff --git a/src/elliptic_curve/elliptic_curve.rs b/src/elliptic_curve/elliptic_curve.rs index 0e25497..c60d4f6 100644 --- a/src/elliptic_curve/elliptic_curve.rs +++ b/src/elliptic_curve/elliptic_curve.rs @@ -46,6 +46,7 @@ impl EllipticCurvePoint { EllipticCurvePoint::Infinity => true, } } + } impl Neg for EllipticCurvePoint { diff --git a/src/main.rs b/src/main.rs index ce8b4db..fddf71e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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);