mirror of
https://github.com/saymrwulf/anza-cryptography-source.git
synced 2026-09-05 20:30:51 +00:00
[ed25519] implement add for SW form (#55)
* impl add for SW * Update short_weierstrass.rs
This commit is contained in:
parent
dd4164eefe
commit
34e36a7c32
1 changed files with 27 additions and 0 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
//! This module provides a lightweight affine representation and conversion
|
//! This module provides a lightweight affine representation and conversion
|
||||||
//! utilities for moving between the Edwards and short Weierstrass models.
|
//! utilities for moving between the Edwards and short Weierstrass models.
|
||||||
|
|
||||||
|
use core::ops::Add;
|
||||||
|
|
||||||
use crate::edwards::EdwardsPoint;
|
use crate::edwards::EdwardsPoint;
|
||||||
use crate::field::FieldElement;
|
use crate::field::FieldElement;
|
||||||
use crate::traits::{Identity, IsIdentity};
|
use crate::traits::{Identity, IsIdentity};
|
||||||
|
|
@ -164,6 +166,16 @@ impl SwPoint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> Add<&'a SwPoint> for &SwPoint {
|
||||||
|
type Output = SwPoint;
|
||||||
|
|
||||||
|
fn add(self, other: &'a SwPoint) -> SwPoint {
|
||||||
|
SwPoint::add(self, other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
define_add_variants!(LHS = SwPoint, RHS = SwPoint, Output = SwPoint);
|
||||||
|
|
||||||
fn affine_coordinates_on_curve(x: &FieldElement, y: &FieldElement) -> bool {
|
fn affine_coordinates_on_curve(x: &FieldElement, y: &FieldElement) -> bool {
|
||||||
let y2 = y.square();
|
let y2 = y.square();
|
||||||
let x2 = x.square();
|
let x2 = x.square();
|
||||||
|
|
@ -266,6 +278,21 @@ mod tests {
|
||||||
Scalar::from_bytes_mod_order_wide(&wide)
|
Scalar::from_bytes_mod_order_wide(&wide)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sw_add_operator_matches_inherent_add() {
|
||||||
|
let p = SwPoint::from_edwards(&constants::ED25519_BASEPOINT_POINT);
|
||||||
|
let q = SwPoint::from_edwards(&(constants::ED25519_BASEPOINT_POINT * Scalar::from(7u64)));
|
||||||
|
let expected = p.add(&q);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
<&SwPoint as core::ops::Add<&SwPoint>>::add(&p, &q),
|
||||||
|
expected
|
||||||
|
);
|
||||||
|
assert_eq!(<SwPoint as core::ops::Add<&SwPoint>>::add(p, &q), expected);
|
||||||
|
assert_eq!(<&SwPoint as core::ops::Add<SwPoint>>::add(&p, q), expected);
|
||||||
|
assert_eq!(<SwPoint as core::ops::Add<SwPoint>>::add(p, q), expected);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sw_round_trip_add_matches_edwards() {
|
fn sw_round_trip_add_matches_edwards() {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue