Skip to content

Commit

Permalink
feat(point): naive scalar point multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Elvis339 committed Aug 29, 2023
1 parent 857f368 commit 476940d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/ecc/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ecc::field_element::FieldElement;
use crate::ecc::point::Point;
use std::ops::Mul;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -31,9 +32,27 @@ impl Mul<FieldElement> for Scalar {
}
}

impl Mul<Point> for Scalar {
type Output = Result<Point, String>;

// Naive impl
fn mul(self, rhs: Point) -> Self::Output {
let mut result = Point::new(rhs.a.clone(), rhs.b.clone(), None, None).unwrap();

let end = self.value;

for _ in 0..end {
result = (result + rhs.clone())?
}

Ok(result)
}
}

#[cfg(test)]
mod tests {
use crate::ecc::field_element::FieldElement;
use crate::ecc::point::Point;
use crate::ecc::scalar::Scalar;

fn new_fe(num: i64, prime: i64) -> FieldElement {
Expand All @@ -46,4 +65,24 @@ mod tests {
let res = Scalar::new(2) * fe;
assert_eq!(res.unwrap(), new_fe(30, 223))
}

#[test]
fn scalar_multiplication_point() {
let prime = 223;
let a = new_fe(0, prime.clone());
let b = new_fe(7, prime.clone());
let x = new_fe(47, prime.clone());
let y = new_fe(71, prime.clone());
let p = Point::new(a.clone(), b.clone(), Some(x), Some(y)).unwrap();

assert_eq!(
Scalar::new(10) * p,
Point::new(
a,
b,
Some(new_fe(154, prime.clone())),
Some(new_fe(150, prime.clone()))
)
)
}
}

0 comments on commit 476940d

Please sign in to comment.