mirror of
https://github.com/mii443/encrypt.git
synced 2025-08-22 15:05:33 +00:00
途中
This commit is contained in:
@ -1,32 +1,44 @@
|
|||||||
use std::ops::{Div, Add, Sub, Mul};
|
use std::ops::{Div, Add, Sub, Mul};
|
||||||
|
|
||||||
|
use primitive_types::U512;
|
||||||
|
|
||||||
|
use crate::common::finite_field::FiniteFieldElement;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct EllipticCurve<T> {
|
pub struct EllipticCurve {
|
||||||
pub a: T,
|
pub a: FiniteFieldElement,
|
||||||
pub b: T
|
pub b: FiniteFieldElement
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> EllipticCurve<T> {
|
impl EllipticCurve {
|
||||||
pub fn point(self, x: T, y: T) -> EllipticCurvePoint<T> {
|
pub fn point(self, x: FiniteFieldElement, y: FiniteFieldElement) -> EllipticCurvePoint {
|
||||||
EllipticCurvePoint::Point { x, y, a: self.a, b: self.b }
|
EllipticCurvePoint::Point { x, y, a: self.a, b: self.b }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum EllipticCurvePoint<T> {
|
pub enum EllipticCurvePoint {
|
||||||
Point {
|
Point {
|
||||||
x: T,
|
x: FiniteFieldElement,
|
||||||
y: T,
|
y: FiniteFieldElement,
|
||||||
a: T,
|
a: FiniteFieldElement,
|
||||||
b: T
|
b: FiniteFieldElement
|
||||||
},
|
},
|
||||||
Infinity
|
Infinity
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Add for EllipticCurvePoint<T>
|
impl EllipticCurvePoint {
|
||||||
where
|
pub fn check(self) -> bool {
|
||||||
T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Copy + PartialEq
|
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;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(self, rhs: Self) -> Self::Output {
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
@ -34,7 +46,9 @@ where
|
|||||||
EllipticCurvePoint::Point { x: x1, y: y1, a, b } => {
|
EllipticCurvePoint::Point { x: x1, y: y1, a, b } => {
|
||||||
match rhs {
|
match rhs {
|
||||||
EllipticCurvePoint::Point { x: x2, y: y2, a: a2, b: b2 } => {
|
EllipticCurvePoint::Point { x: x2, y: y2, a: a2, b: b2 } => {
|
||||||
|
println!("default plus");
|
||||||
|
|
||||||
|
let p = x1.p;
|
||||||
if a != a2 || b != b2 {
|
if a != a2 || b != b2 {
|
||||||
panic!("Cannot add different curve point.");
|
panic!("Cannot add different curve point.");
|
||||||
}
|
}
|
||||||
@ -43,12 +57,15 @@ where
|
|||||||
return EllipticCurvePoint::Infinity
|
return EllipticCurvePoint::Infinity
|
||||||
}
|
}
|
||||||
|
|
||||||
let one = b / b;
|
|
||||||
let two = one + one;
|
|
||||||
let three = two + one;
|
|
||||||
let l = if x1 == x2 && y1 == y2 {
|
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 {
|
} else {
|
||||||
|
println!("plus");
|
||||||
(y2 - y1) / (x2 - x1)
|
(y2 - y1) / (x2 - x1)
|
||||||
};
|
};
|
||||||
let x = l * l - x1 - x2;
|
let x = l * l - x1 - x2;
|
||||||
@ -64,21 +81,15 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> Mul<U> for EllipticCurvePoint<T>
|
impl Mul<U512> for EllipticCurvePoint {
|
||||||
where
|
|
||||||
T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Copy + PartialEq,
|
|
||||||
U: Sub<Output = U> + Div<Output = U> + Copy + PartialEq + PartialOrd
|
|
||||||
{
|
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn mul(self, rhs: U) -> Self::Output {
|
fn mul(self, rhs: U512) -> Self::Output {
|
||||||
let one = rhs / rhs;
|
|
||||||
let zero = rhs - rhs;
|
|
||||||
let mut n = rhs;
|
let mut n = rhs;
|
||||||
let mut r: EllipticCurvePoint<T> = EllipticCurvePoint::Infinity;
|
let mut r: EllipticCurvePoint = EllipticCurvePoint::Infinity;
|
||||||
while n > zero {
|
while n > U512::from(0) {
|
||||||
r = r + self;
|
r = r + self;
|
||||||
n = n - one;
|
n = n - U512::from(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
r
|
r
|
||||||
|
@ -2,24 +2,20 @@ use std::ops::{Mul, Add, Sub, Div};
|
|||||||
|
|
||||||
use primitive_types::U512;
|
use primitive_types::U512;
|
||||||
|
|
||||||
|
use crate::common::finite_field::FiniteFieldElement;
|
||||||
|
|
||||||
use super::elliptic_curve::{EllipticCurve, EllipticCurvePoint};
|
use super::elliptic_curve::{EllipticCurve, EllipticCurvePoint};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Encryption<T>
|
pub struct Encryption {
|
||||||
where
|
pub ellictic_curve: EllipticCurve,
|
||||||
T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Copy + PartialEq
|
pub base_point: EllipticCurvePoint,
|
||||||
{
|
pub order: FiniteFieldElement,
|
||||||
pub ellictic_curve: EllipticCurve<T>,
|
pub plain_mapping: Vec<EllipticCurvePoint>
|
||||||
pub base_point: EllipticCurvePoint<T>,
|
|
||||||
pub order: T,
|
|
||||||
pub plain_mapping: Vec<EllipticCurvePoint<T>>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Encryption<T>
|
impl Encryption {
|
||||||
where
|
pub fn ec_point_to_plain(&self, point: EllipticCurvePoint) -> U512 {
|
||||||
T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Copy + PartialEq
|
|
||||||
{
|
|
||||||
pub fn ec_point_to_plain(&self, point: EllipticCurvePoint<T>) -> U512 {
|
|
||||||
println!("ec point to plain");
|
println!("ec point to plain");
|
||||||
match point {
|
match point {
|
||||||
EllipticCurvePoint::Infinity => {
|
EllipticCurvePoint::Infinity => {
|
||||||
@ -75,8 +71,8 @@ where
|
|||||||
U512::from(x + 1)
|
U512::from(x + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn plain_to_ec_point(&self, m: U512) -> EllipticCurvePoint<T> {
|
pub fn plain_to_ec_point(&self, m: U512) -> EllipticCurvePoint {
|
||||||
if m == U512::from(0) {
|
if m == U512::from(0u8) {
|
||||||
return EllipticCurvePoint::Infinity
|
return EllipticCurvePoint::Infinity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
src/main.rs
16
src/main.rs
@ -36,12 +36,16 @@ fn main() {
|
|||||||
plain_mapping: vec![]
|
plain_mapping: vec![]
|
||||||
};
|
};
|
||||||
|
|
||||||
let twenty = encryption.plain_to_ec_point(U512::from(12u8));
|
//let twenty = encryption.plain_to_ec_point(U512::from(12u8));
|
||||||
let ten = encryption.plain_to_ec_point(U512::from(10u8));
|
//let ten = encryption.plain_to_ec_point(U512::from(10u8));
|
||||||
let two = encryption.plain_to_ec_point(U512::from(2u8));
|
//let two = encryption.plain_to_ec_point(U512::from(2u8));
|
||||||
println!("{:?}", twenty);
|
//println!("{:?}", twenty);
|
||||||
println!("{:?}", ten + two);
|
//println!("{:?}", ten + two);
|
||||||
println!("{:?}", encryption.ec_point_to_plain(ten));
|
//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;
|
let t = encryption.base_point + encryption.base_point;
|
||||||
println!("{:?}", t);
|
println!("{:?}", t);
|
||||||
|
Reference in New Issue
Block a user