From 03237952a72ccef957046547a27a48a9739caaf4 Mon Sep 17 00:00:00 2001 From: mii8080 <39086319+morioka22@users.noreply.github.com> Date: Sat, 28 May 2022 12:26:34 +0000 Subject: [PATCH] =?UTF-8?q?=E9=80=94=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/elliptic_curve/elliptic_curve.rs | 71 ++++++++++++++++------------ src/elliptic_curve/encryption.rs | 26 +++++----- src/main.rs | 16 ++++--- 3 files changed, 62 insertions(+), 51 deletions(-) diff --git a/src/elliptic_curve/elliptic_curve.rs b/src/elliptic_curve/elliptic_curve.rs index d272bfb..adc1f3f 100644 --- a/src/elliptic_curve/elliptic_curve.rs +++ b/src/elliptic_curve/elliptic_curve.rs @@ -1,32 +1,44 @@ use std::ops::{Div, Add, Sub, Mul}; +use primitive_types::U512; + +use crate::common::finite_field::FiniteFieldElement; + #[derive(Debug, Clone, Copy, PartialEq)] -pub struct EllipticCurve { - pub a: T, - pub b: T +pub struct EllipticCurve { + pub a: FiniteFieldElement, + pub b: FiniteFieldElement } -impl EllipticCurve { - pub fn point(self, x: T, y: T) -> EllipticCurvePoint { +impl EllipticCurve { + pub fn point(self, x: FiniteFieldElement, y: FiniteFieldElement) -> EllipticCurvePoint { EllipticCurvePoint::Point { x, y, a: self.a, b: self.b } } } #[derive(Debug, Clone, Copy, PartialEq)] -pub enum EllipticCurvePoint { +pub enum EllipticCurvePoint { Point { - x: T, - y: T, - a: T, - b: T + x: FiniteFieldElement, + y: FiniteFieldElement, + a: FiniteFieldElement, + b: FiniteFieldElement }, Infinity } -impl Add for EllipticCurvePoint -where - T: Add + Sub + Mul + Div + Copy + PartialEq -{ +impl EllipticCurvePoint { + pub fn check(self) -> bool { + match self { + EllipticCurvePoint::Point { x, y, a, b } => { + y * y == x * x * x + a * x + b + }, + EllipticCurvePoint::Infinity => true, + } + } +} + +impl Add for EllipticCurvePoint { type Output = Self; fn add(self, rhs: Self) -> Self::Output { @@ -34,7 +46,9 @@ where EllipticCurvePoint::Point { x: x1, y: y1, a, b } => { match rhs { EllipticCurvePoint::Point { x: x2, y: y2, a: a2, b: b2 } => { - + println!("default plus"); + + let p = x1.p; if a != a2 || b != b2 { panic!("Cannot add different curve point."); } @@ -43,12 +57,15 @@ where return EllipticCurvePoint::Infinity } - let one = b / b; - let two = one + one; - let three = two + one; let l = if x1 == x2 && y1 == y2 { - (x1 * x1 * three + a) / (y1 * two) + println!("twice"); + let t = x1 * x1 * FiniteFieldElement::new(U512::from(3), p) + a; + let u = y1 * FiniteFieldElement::new(U512::from(2), p); + let a = t / u; + println!("t: {:?}\nu: {:?}\na: {:?}", t, u, a); + a } else { + println!("plus"); (y2 - y1) / (x2 - x1) }; let x = l * l - x1 - x2; @@ -64,21 +81,15 @@ where } } -impl Mul for EllipticCurvePoint -where - T: Add + Sub + Mul + Div + Copy + PartialEq, - U: Sub + Div + Copy + PartialEq + PartialOrd -{ +impl Mul for EllipticCurvePoint { type Output = Self; - fn mul(self, rhs: U) -> Self::Output { - let one = rhs / rhs; - let zero = rhs - rhs; + fn mul(self, rhs: U512) -> Self::Output { let mut n = rhs; - let mut r: EllipticCurvePoint = EllipticCurvePoint::Infinity; - while n > zero { + let mut r: EllipticCurvePoint = EllipticCurvePoint::Infinity; + while n > U512::from(0) { r = r + self; - n = n - one; + n = n - U512::from(1); } r diff --git a/src/elliptic_curve/encryption.rs b/src/elliptic_curve/encryption.rs index c7576de..086ff48 100644 --- a/src/elliptic_curve/encryption.rs +++ b/src/elliptic_curve/encryption.rs @@ -2,24 +2,20 @@ use std::ops::{Mul, Add, Sub, Div}; use primitive_types::U512; +use crate::common::finite_field::FiniteFieldElement; + use super::elliptic_curve::{EllipticCurve, EllipticCurvePoint}; #[derive(Debug)] -pub struct Encryption -where - T: Add + Sub + Mul + Div + Copy + PartialEq -{ - pub ellictic_curve: EllipticCurve, - pub base_point: EllipticCurvePoint, - pub order: T, - pub plain_mapping: Vec> +pub struct Encryption { + pub ellictic_curve: EllipticCurve, + pub base_point: EllipticCurvePoint, + pub order: FiniteFieldElement, + pub plain_mapping: Vec } -impl Encryption -where - T: Add + Sub + Mul + Div + Copy + PartialEq -{ - pub fn ec_point_to_plain(&self, point: EllipticCurvePoint) -> U512 { +impl Encryption { + pub fn ec_point_to_plain(&self, point: EllipticCurvePoint) -> U512 { println!("ec point to plain"); match point { EllipticCurvePoint::Infinity => { @@ -75,8 +71,8 @@ where U512::from(x + 1) } - pub fn plain_to_ec_point(&self, m: U512) -> EllipticCurvePoint { - if m == U512::from(0) { + pub fn plain_to_ec_point(&self, m: U512) -> EllipticCurvePoint { + if m == U512::from(0u8) { return EllipticCurvePoint::Infinity } diff --git a/src/main.rs b/src/main.rs index d2ab43c..643048c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,12 +36,16 @@ fn main() { plain_mapping: vec![] }; - let twenty = encryption.plain_to_ec_point(U512::from(12u8)); - let ten = encryption.plain_to_ec_point(U512::from(10u8)); - let two = encryption.plain_to_ec_point(U512::from(2u8)); - println!("{:?}", twenty); - println!("{:?}", ten + two); - println!("{:?}", encryption.ec_point_to_plain(ten)); + //let twenty = encryption.plain_to_ec_point(U512::from(12u8)); + //let ten = encryption.plain_to_ec_point(U512::from(10u8)); + //let two = encryption.plain_to_ec_point(U512::from(2u8)); + //println!("{:?}", twenty); + //println!("{:?}", ten + two); + //println!("{:?}", encryption.ec_point_to_plain(ten)); + let p = encryption.base_point + encryption.base_point; + println!("{:?}", p); + println!("{}", p.check()); + println!("{}", encryption.base_point.check()); /* let t = encryption.base_point + encryption.base_point; println!("{:?}", t);