cha2 curve check

This commit is contained in:
Christoph J. Scherr 2023-06-13 12:13:49 +02:00
parent edf2f02d2a
commit b5b8886acd
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
1 changed files with 49 additions and 15 deletions

View File

@ -56,18 +56,36 @@ impl EllipticCurve {
F(X, Y) = Y² - X³ - {a}X - {b}") F(X, Y) = Y² - X³ - {a}X - {b}")
} }
// check diskriminante // check if the curve is valid
let d = 4*a.pow(3) + 27*b.pow(2); if field.cha > 2 {
if field.reduce::<_, u128>(d) == 0 { let d = 4*a.pow(3) + 27*b.pow(2);
if verbose { if field.reduce::<_, u128>(d) == 0 {
println!("4*{a}³ + 27*{b}² = {d} = {} != 0\n\ if verbose {
Check for Diskriminante not passed", field.reduce::<_, u128>(d)); println!("4*{a}³ + 27*{b}² = {d} = {} != 0\n\
Check for Diskriminante not passed", field.reduce::<_, u128>(d));
}
return Err(String::from("Diskriminante not 0"));
}
else if verbose {
println!("4*{a}³ + 27*{b}² = {d} = {} != 0\n\
Check for Diskriminante passed", field.reduce::<_, u128>(d));
} }
return Err(String::from("Diskriminante not 0"));
} }
else if verbose { else {
println!("4*{a}³ + 27*{b}² = {d} = {} != 0\n\ let valid: bool = field.reduce::<_, i128>(b) == 0;
Check for Diskriminante passed", field.reduce::<_, u128>(d));
if verbose {
if valid {
println!("b = {b} != 0 => Curve valid");
}
else {
println!("b = {b} == 0 => Curve invalid")
}
}
if !valid {
return Err(String::from("b == 0 => Curve invalid"));
}
} }
let mut infty = EllipticCurvePoint::new(0, 0); let mut infty = EllipticCurvePoint::new(0, 0);
@ -121,7 +139,9 @@ impl EllipticCurve {
pub fn check_point(&self, p: EllipticCurvePoint, verbose: bool) -> bool { pub fn check_point(&self, p: EllipticCurvePoint, verbose: bool) -> bool {
if p.is_infinity_point { if p.is_infinity_point {
println!("p is infinity: {p}"); if self.verbose {
println!("p is infinity: {p}");
}
return true; return true;
} }
let mut valid = true; let mut valid = true;
@ -310,8 +330,11 @@ impl EllipticCurve {
/// get negative of a point /// get negative of a point
pub fn neg(&self, p: EllipticCurvePoint) -> EllipticCurvePoint { pub fn neg(&self, p: EllipticCurvePoint) -> EllipticCurvePoint {
self.new_point(p.r, self.field.reduce::<_, u128>(-(p.s as i128))).expect("negation of \ if p.is_infinity_point {
point is not on field, math error") return p;
}
self.new_point(p.r, self.field.reduce(-(p.s as i128))).expect(format!("negation of \
point is not on field, math error: {}", p).as_str())
} }
/// multip.s a point by an integer /// multip.s a point by an integer
@ -327,7 +350,7 @@ impl EllipticCurve {
} }
let t: usize = num::cast(t).unwrap(); let t: usize = num::cast(t).unwrap();
if t < 1 { if t < 1 {
return Err(String::from("point multiplication works only if t > 0")); return Ok(self.INFINITY_POINT);
} }
if self.verbose { if self.verbose {
println!("h = t * g = {t} * {g}\n\ println!("h = t * g = {t} * {g}\n\
@ -453,7 +476,18 @@ impl EllipticCurve {
impl std::fmt::Display for EllipticCurve{ impl std::fmt::Display for EllipticCurve{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "F(X, Y) = Y² - X³ -{}X - {}", self.a, self.b) if self.field.prime_base {
write!(f, "F(X, Y) = Y² - X³ -{}X - {}", self.a, self.b)
}
else if self.field.cha == 2 {
write!(f, "F(X, Y) = Y² + XY + X³ + ({}) * X² + ({})",
self.field.display(self.a),
self.field.display(self.b),
)
}
else {
write!(f, "ERROR")
}
} }
} }