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 primitive_types::U512;
|
||||
|
||||
use crate::common::finite_field::FiniteFieldElement;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct EllipticCurve<T> {
|
||||
pub a: T,
|
||||
pub b: T
|
||||
pub struct EllipticCurve {
|
||||
pub a: FiniteFieldElement,
|
||||
pub b: FiniteFieldElement
|
||||
}
|
||||
|
||||
impl<T> EllipticCurve<T> {
|
||||
pub fn point(self, x: T, y: T) -> EllipticCurvePoint<T> {
|
||||
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<T> {
|
||||
pub enum EllipticCurvePoint {
|
||||
Point {
|
||||
x: T,
|
||||
y: T,
|
||||
a: T,
|
||||
b: T
|
||||
x: FiniteFieldElement,
|
||||
y: FiniteFieldElement,
|
||||
a: FiniteFieldElement,
|
||||
b: FiniteFieldElement
|
||||
},
|
||||
Infinity
|
||||
}
|
||||
|
||||
impl<T> Add for EllipticCurvePoint<T>
|
||||
where
|
||||
T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 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<T, U> Mul<U> for EllipticCurvePoint<T>
|
||||
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
|
||||
{
|
||||
impl Mul<U512> 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<T> = 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
|
||||
|
@ -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<T>
|
||||
where
|
||||
T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Copy + PartialEq
|
||||
{
|
||||
pub ellictic_curve: EllipticCurve<T>,
|
||||
pub base_point: EllipticCurvePoint<T>,
|
||||
pub order: T,
|
||||
pub plain_mapping: Vec<EllipticCurvePoint<T>>
|
||||
pub struct Encryption {
|
||||
pub ellictic_curve: EllipticCurve,
|
||||
pub base_point: EllipticCurvePoint,
|
||||
pub order: FiniteFieldElement,
|
||||
pub plain_mapping: Vec<EllipticCurvePoint>
|
||||
}
|
||||
|
||||
impl<T> Encryption<T>
|
||||
where
|
||||
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 {
|
||||
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<T> {
|
||||
if m == U512::from(0) {
|
||||
pub fn plain_to_ec_point(&self, m: U512) -> EllipticCurvePoint {
|
||||
if m == U512::from(0u8) {
|
||||
return EllipticCurvePoint::Infinity
|
||||
}
|
||||
|
||||
|
16
src/main.rs
16
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);
|
||||
|
Reference in New Issue
Block a user