add PPE substract multiply

This commit is contained in:
Masato Imai
2022-08-16 11:58:35 +09:00
parent 1b82fd3a59
commit f6342e17a4
3 changed files with 62 additions and 11 deletions

View File

@ -9,7 +9,8 @@ fn main() {
println("d: " + d) println("d: " + d)
let e = encrypt(24) let e = encrypt(24)
println("e: " + e) println("e: " + e)
let r = a + b + c + d + e let f = encrypt(15)
let r = a + b + c + d + e - f
println("r: " + decrypt(r)); println("r: " + decrypt(r));
println(decrypt(r) - 15) println(decrypt(r * 2))
} }

View File

@ -1,6 +1,6 @@
use std::{ use std::{
fmt::Display, fmt::Display,
ops::{Add, Sub}, ops::{Add, Mul, Sub},
sync::mpsc, sync::mpsc,
thread, thread,
}; };
@ -70,6 +70,29 @@ impl Sub for EncryptedEllipticCurvePoint {
} }
} }
impl Mul<U512> for EncryptedEllipticCurvePoint {
type Output = Self;
fn mul(self, rhs: U512) -> Self::Output {
let mut tmp = self;
let mut point: Option<EncryptedEllipticCurvePoint> = None;
let mut n = rhs;
while n > U512::zero() {
if n & U512::one() == U512::one() {
if let Some(s_point) = point {
point = Some(s_point + tmp);
} else {
point = Some(tmp);
}
}
n = n >> 1;
tmp = tmp + tmp;
}
point.unwrap()
}
}
impl Encryption { impl Encryption {
pub fn ec_point_to_plain(&self, point: EllipticCurvePoint) -> U512 { pub fn ec_point_to_plain(&self, point: EllipticCurvePoint) -> U512 {
match point { match point {

View File

@ -423,21 +423,48 @@ impl GPSL {
}, },
Err(err) => Err(err), Err(err) => Err(err),
}, },
NodeKind::MUL => match GPSL::extract_number(lhs) { NodeKind::MUL => match lhs.clone() {
Variable::Number { value: lhs } => match rhs {
Variable::Number { value: rhs } => {
Ok(Some(Variable::Number { value: lhs * rhs }))
}
_ => Err("Cannot multiply non-number to number.".to_string()),
},
Variable::PureEncrypted { value: lhs } => match rhs {
Variable::Number { value: rhs } => {
Ok(Some(Variable::PureEncrypted {
value: lhs * U512::from(rhs),
}))
}
Variable::U512 { value: rhs } => {
Ok(Some(Variable::PureEncrypted { value: lhs * rhs }))
}
_ => Err("Cannot multiply non-number to ppe.".to_string()),
},
_ => Err("Cannot multiply non-number.".to_string()),
},
/*match GPSL::extract_number(lhs) {
Ok(lhs) => match GPSL::extract_number(rhs) { Ok(lhs) => match GPSL::extract_number(rhs) {
Ok(rhs) => Ok(Some(Variable::Number { value: lhs * rhs })), Ok(rhs) => Ok(Some(Variable::Number { value: lhs * rhs })),
Err(err) => Err(err), Err(err) => Err(err),
}, },
Err(err) => Err(err), Err(err) => Err(err),
}, },*/
NodeKind::SUB => match GPSL::extract_number(lhs) { NodeKind::SUB => match lhs.clone() {
Ok(lhs) => match GPSL::extract_number(rhs) { Variable::Number { value: lhs } => match rhs {
Ok(rhs) => Ok(Some(Variable::Number { value: lhs - rhs })), Variable::Number { value: rhs } => {
Err(err) => Err(err), Ok(Some(Variable::Number { value: lhs - rhs }))
}
_ => Err("Cannot subtract non-number from number.".to_string()),
}, },
Err(err) => Err(err), Variable::PureEncrypted { value: lhs } => match rhs {
Variable::PureEncrypted { value: rhs } => {
Ok(Some(Variable::PureEncrypted { value: lhs - rhs }))
}
_ => Err("Cannot subtract non-ppe from ppe.".to_string()),
},
_ => Err("Cannot subtract non-number.".to_string()),
}, },
NodeKind::EQ => { NodeKind::EQ => {
if lhs == rhs { if lhs == rhs {
Ok(Some(Variable::Number { value: 1 })) Ok(Some(Variable::Number { value: 1 }))