Merge branch 'main' of https://github.com/morioka22/encrypt into main

This commit is contained in:
Masato Imai
2022-06-02 16:33:29 +09:00
parent ee9900601a
commit 52b85c63a8
3 changed files with 39 additions and 10 deletions

View File

@ -1,4 +1,4 @@
use std::{ops::{Add, Mul, Neg}, fmt::Display};
use std::{ops::{Add, Mul, Neg}, fmt::Display, sync::mpsc};
use primitive_types::U512;
@ -78,8 +78,18 @@ impl Add for EllipticCurvePoint {
}
let l = if x1 == x2 && y1 == y2 {
let t = x1 * x1 * FiniteFieldElement::new(U512::from(3), p) + a;
let u = y1 * FiniteFieldElement::new(U512::from(2), p);
let (t_tx, t_rx) = mpsc::channel();
let (u_tx, u_rx) = mpsc::channel();
std::thread::spawn(move || {
let val = x1 * x1 * FiniteFieldElement::new(U512::from(3u8), p) + a;
t_tx.send(val).unwrap();
});
std::thread::spawn(move || {
let val = y1 * FiniteFieldElement::new(U512::from(2), p);
u_tx.send(val).unwrap();
});
let t = t_rx.recv().unwrap();
let u = u_rx.recv().unwrap();
let a = t / u;
a
} else {

View File

@ -1,4 +1,4 @@
use std::ops::{Add, Sub};
use std::{ops::{Add, Sub}, sync::mpsc, thread};
use primitive_types::{U512, U256};
@ -117,7 +117,24 @@ impl Encryption {
Self::random()
};
EncryptedEllipticCurvePoint { data: message + public_key * ra, rp: self.base_point * ra }
let (data_tx, data_rx) = mpsc::channel();
let (rp_tx, rp_rx) = mpsc::channel();
let s = self.clone();
thread::spawn(move || {
let val = s.base_point * ra;
rp_tx.send(val).unwrap();
});
thread::spawn(move || {
let val = message + public_key * ra;
data_tx.send(val).unwrap();
});
let data_received = data_rx.recv().unwrap();
let rp_received = rp_rx.recv().unwrap();
EncryptedEllipticCurvePoint { data: data_received, rp: rp_received }
}
pub fn get_public_key(&self, private_key: U512) -> EllipticCurvePoint {

View File

@ -41,17 +41,19 @@ fn main() {
let public_key = encryption.get_public_key(private_key);
println!("public_key: {}", public_key);
let ten = encryption.plain_to_ec_point(U512::from(10u32));
let e_ten = encryption.encrypt(ten, public_key, None);
println!("10 -> {}", e_ten.data);
for x in 0..100 {
let ten = encryption.plain_to_ec_point(U512::from(10u32));
let e_ten = encryption.encrypt(ten, public_key, None);
println!("10 -> {}", e_ten.data);
}
let two = encryption.plain_to_ec_point(U512::from(2u32));
let e_two = encryption.encrypt(two, public_key, None);
println!("2 -> {}", e_two.data);
/*
println!("10 + 2 -> {}", (e_ten + e_two).data);
println!("decrypt: {:?}", encryption.ec_point_to_plain(Encryption::decrypt(e_ten + e_two, private_key)));
*/
/*
let twenty = encryption.plain_to_ec_point(U512::from(12u8));
let ten = encryption.plain_to_ec_point(U512::from(10u8));