checkpoint ecc works

This commit is contained in:
Christoph J. Scherr 2023-06-08 15:34:57 +02:00
parent dbbd466a56
commit 0c8ee362b4
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
1 changed files with 74 additions and 36 deletions

View File

@ -30,7 +30,21 @@ pub struct ElipticCurve {
} }
impl ElipticCurve { impl ElipticCurve {
pub fn new(f: GalloisField, a: i128, b: i128, verbose: bool) -> Self { pub fn new(f: GalloisField, a: i128, b: i128, verbose: bool) -> Result<Self, String> {
// check diskriminante
let d = 4*a.pow(3) + 27*b.pow(2);
if f.reduce(d) == 0 {
if verbose {
println!("4*{a}³ + 27*{b}² = {d} = {} != 0\n\
Check for Diskriminante not passed", f.reduce(d));
}
return Err(String::from("Diskriminante not 0"));
}
else if verbose {
println!("4*{a}³ + 27*{b}² = {d} = {} != 0\n
Check for Diskriminante passed", f.reduce(d));
}
let mut e = ElipticCurve { let mut e = ElipticCurve {
f, f,
a, a,
@ -41,7 +55,7 @@ impl ElipticCurve {
}; };
let infty = ElipticCurvePoint::new(0, 0, e.f); let infty = ElipticCurvePoint::new(0, 0, e.f);
e.INFINITY_POINT = Some(infty); e.INFINITY_POINT = Some(infty);
return e; return Ok(e);
} }
/// calculate a value for coordinates /// calculate a value for coordinates
@ -69,39 +83,63 @@ impl ElipticCurve {
pub fn check_point(self, p: ElipticCurvePoint) -> bool { pub fn check_point(self, p: ElipticCurvePoint) -> bool {
let mut valid = true; let mut valid = true;
let res = self.f.reduce(self.poly(p.r, p.s));
// insert into poly
let left = self.f.reduce(p.s.pow(2));
let right = self.f.reduce(p.r.pow(3) + self.a*p.r + self.b);
if self.verbose { if self.verbose {
println!("F({}, {}) = {}² - {}³ - {} * {} - {} = {res}", let unred_left = p.s.pow(2);
p.r, p.s, p.s, p.r, self.a, p.r, self.b let unred_right = p.r.pow(3) + self.a*p.r + self.b;
) println!("All Points need to fullfill this equation:\n\
y²\t= x³ + ax + b\n\
{}²\t= {}³ + {}*{} +{}\n\
{unred_left}\t= {unred_right}\n\
{left}\t= {right}\n\
<=> {}\n",
p.s,
p.r,
self.a,
p.r,
self.b,
left == right
);
} }
valid &= res == 0; valid &= left == right;
return valid; return valid;
} }
} }
#[test] #[test]
fn test_check_point() { fn test_check_point() {
let f = GalloisField::new(1151, true, None); let f = GalloisField::new(13, true, None);
let ec = ElipticCurve::new(f, 1, 679, true); let ec = ElipticCurve::new(f, -3, 3, true).expect("ec cant be created");
// real points // real points
let p = vec![ let p = vec![
ElipticCurvePoint::new(298, 531, f), ElipticCurvePoint::new(0, 4, f),
ElipticCurvePoint::new(600, 127, f), ElipticCurvePoint::new(0, 9, f),
ElipticCurvePoint::new(846, 176, f), ElipticCurvePoint::new(1, 1, f),
ElipticCurvePoint::new(1, 12, f),
ElipticCurvePoint::new(4, 4, f),
ElipticCurvePoint::new(4, 9, f),
ElipticCurvePoint::new(5, 3, f),
ElipticCurvePoint::new(5, 10, f),
ElipticCurvePoint::new(7, 0, f),
ElipticCurvePoint::new(8, 6, f),
ElipticCurvePoint::new(9, 4, f),
ElipticCurvePoint::new(9, 9, f),
ElipticCurvePoint::new(11, 1, f),
ElipticCurvePoint::new(11, 12, f),
]; ];
// random values, not part of the e, fc. // random values, not part of the e, fc.
let np = vec![ let np = vec![
ElipticCurvePoint::new(198, 331, f), ElipticCurvePoint::new(0, 5, f),
ElipticCurvePoint::new(100, 927, f), ElipticCurvePoint::new(1, 9, f),
ElipticCurvePoint::new(446, 876, f), ElipticCurvePoint::new(1, 4, f),
]; ];
for i in p { for i in p {
dbg!(&i);
assert!(ec.clone().check_point(i)); assert!(ec.clone().check_point(i));
} }
for i in np { for i in np {
dbg!(&i);
assert!(!ec.clone().check_point(i)); assert!(!ec.clone().check_point(i));
} }
} }