Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assignment operators #120

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion secp256kfun/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{
marker::*,
op, Scalar,
};
use core::marker::PhantomData;
use core::{
marker::PhantomData,
ops::{AddAssign, SubAssign},
};
use rand_core::RngCore;

/// A point on the secp256k1 elliptic curve.
Expand Down Expand Up @@ -498,6 +501,30 @@ crate::impl_fromstr_deserialize! {
}
}

impl<TR, SL, SR, ZR> AddAssign<Point<TR, SR, ZR>> for Point<NonNormal, SL, Zero> {
fn add_assign(&mut self, rhs: Point<TR, SR, ZR>) {
*self = crate::op::point_add(self, &rhs).set_secrecy::<SL>()
}
}

impl<TR, SL, SR, ZR> AddAssign<&Point<TR, SR, ZR>> for Point<NonNormal, SL, Zero> {
fn add_assign(&mut self, rhs: &Point<TR, SR, ZR>) {
*self = crate::op::point_add(self, rhs).set_secrecy::<SL>()
}
}

impl<TR, SL, SR, ZR> SubAssign<&Point<TR, SR, ZR>> for Point<NonNormal, SL, Zero> {
fn sub_assign(&mut self, rhs: &Point<TR, SR, ZR>) {
*self = crate::op::point_sub(self, rhs).set_secrecy::<SL>()
}
}

impl<TR, SL, SR, ZR> SubAssign<Point<TR, SR, ZR>> for Point<NonNormal, SL, Zero> {
fn sub_assign(&mut self, rhs: Point<TR, SR, ZR>) {
*self = crate::op::point_sub(self, &rhs).set_secrecy::<SL>()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -732,4 +759,17 @@ mod test {
let mult_point = g!({ Scalar::random(&mut rand::thread_rng()) } * G);
assert!(format!("{:?}", mult_point).starts_with("Point<NonNormal,Public,NonZero>"));
}

#[test]
fn assign_tests() {
let a_orig = Point::random(&mut rand::thread_rng())
.mark_zero()
.non_normal();
let mut a = a_orig.clone();
let b = Point::random(&mut rand::thread_rng());
a += b;
assert_eq!(a, op::point_add(&a_orig, &b));
a -= b;
assert_eq!(a, a_orig);
}
}
65 changes: 64 additions & 1 deletion secp256kfun/src/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Scalar arithmetic (integers mod the secp256k1 group order)
use crate::{backend, hash::HashInto, marker::*, op};
use core::marker::PhantomData;
use core::{
marker::PhantomData,
ops::{AddAssign, MulAssign, SubAssign},
};
use digest::{generic_array::typenum::U32, Digest};
use rand_core::RngCore;

Expand Down Expand Up @@ -348,6 +351,54 @@ where
}
}

impl<SL, SR, ZR> AddAssign<Scalar<SR, ZR>> for Scalar<SL, Zero> {
fn add_assign(&mut self, rhs: Scalar<SR, ZR>) {
*self = crate::op::scalar_add(&self, &rhs).set_secrecy::<SL>();
}
}

impl<SL, SR, ZR> AddAssign<&Scalar<SR, ZR>> for Scalar<SL, Zero> {
fn add_assign(&mut self, rhs: &Scalar<SR, ZR>) {
*self = crate::op::scalar_add(&self, rhs).set_secrecy::<SL>();
}
}

impl<SL, SR, ZR> SubAssign<&Scalar<SR, ZR>> for Scalar<SL, Zero> {
fn sub_assign(&mut self, rhs: &Scalar<SR, ZR>) {
*self = crate::op::scalar_sub(&self, rhs).set_secrecy::<SL>();
}
}

impl<SL, SR, ZR> SubAssign<Scalar<SR, ZR>> for Scalar<SL, Zero> {
fn sub_assign(&mut self, rhs: Scalar<SR, ZR>) {
*self = crate::op::scalar_sub(&self, &rhs).set_secrecy::<SL>();
}
}

impl<SL, SR> MulAssign<Scalar<SR, NonZero>> for Scalar<SL, NonZero> {
fn mul_assign(&mut self, rhs: Scalar<SR, NonZero>) {
*self = crate::op::scalar_mul(&self, &rhs).set_secrecy::<SL>();
}
}

impl<SL, SR> MulAssign<&Scalar<SR, NonZero>> for Scalar<SL, NonZero> {
fn mul_assign(&mut self, rhs: &Scalar<SR, NonZero>) {
*self = crate::op::scalar_mul(&self, rhs).set_secrecy::<SL>();
}
}

impl<SL, SR, ZR: ZeroChoice> MulAssign<Scalar<SR, ZR>> for Scalar<SL, Zero> {
fn mul_assign(&mut self, rhs: Scalar<SR, ZR>) {
*self = crate::op::scalar_mul(&self, &rhs).set_secrecy::<SL>();
}
}

impl<SL, SR, ZR: ZeroChoice> MulAssign<&Scalar<SR, ZR>> for Scalar<SL, Zero> {
fn mul_assign(&mut self, rhs: &Scalar<SR, ZR>) {
*self = crate::op::scalar_mul(&self, rhs).set_secrecy::<SL>();
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -484,4 +535,16 @@ mod test {
)
);
}

#[test]
fn assign_tests() {
let mut a = Scalar::from(42);
let b = Scalar::from(1337).public();
a += b;
assert_eq!(a, Scalar::from(1379));
a -= b;
assert_eq!(a, Scalar::from(42));
a *= b;
assert_eq!(a, Scalar::from(42 * 1337));
}
}