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

feature: eliptic curves #589

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions lib/crypto/src/arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use core::{
use limb::Limb;
use zeroize::Zeroize;

use crate::bits::BitIteratorBE;

/// Defines a big integer with a constant length.
pub trait BigInteger:
'static
Expand Down Expand Up @@ -43,6 +45,7 @@ pub trait BigInteger:
+ for<'a> BitOrAssign<&'a Self>
+ BitOr<Self, Output = Self>
+ for<'a> BitOr<&'a Self, Output = Self>
+ BitIteratorBE
{
/// Number of `usize` limbs representing `Self`.
const NUM_LIMBS: usize;
Expand Down
6 changes: 6 additions & 0 deletions lib/crypto/src/arithmetic/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ impl<const N: usize> BitIteratorBE for Uint<N> {
}
}

impl BitIteratorBE for &[Limb] {
fn bit_be_iter(&self) -> impl Iterator<Item = bool> {
self.iter().rev().flat_map(Limb::bit_be_iter)
}
}

/// Parse a number from a string in a given radix.
///
/// This implementation can be slow on big numbers and possibly fail constant
Expand Down
170 changes: 170 additions & 0 deletions lib/crypto/src/curve/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
//! Helper macros for implementing common traits for curve types.

/// Implements additive operations by deferring to an implementation on &Self.
#[macro_export]
macro_rules! impl_additive_ops_from_ref {
($type:ident, $params:ident) => {
#[allow(unused_qualifications)]
impl<P: $params> core::ops::Add<Self> for $type<P> {
type Output = Self;

#[inline]
fn add(self, other: Self) -> Self {
let mut result = self;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you use just self + &other?

result += &other;
result
}
}

#[allow(unused_qualifications)]
impl<'a, P: $params> core::ops::Add<&'a mut Self> for $type<P> {
type Output = Self;

#[inline]
fn add(self, other: &'a mut Self) -> Self {
let mut result = self;
result += &*other;
result
}
}

impl<'b, P: $params> core::ops::Add<$type<P>> for &'b $type<P> {
type Output = $type<P>;

#[inline]
fn add(self, mut other: $type<P>) -> $type<P> {
other += self;
other
}
}

#[allow(unused_qualifications)]
impl<'a, 'b, P: $params> core::ops::Add<&'a $type<P>> for &'b $type<P> {
type Output = $type<P>;

#[inline]
fn add(self, other: &'a $type<P>) -> $type<P> {
let mut result = *self;
result += &*other;
result
}
}

#[allow(unused_qualifications)]
impl<'a, 'b, P: $params> core::ops::Add<&'a mut $type<P>>
for &'b $type<P>
{
type Output = $type<P>;

#[inline]
fn add(self, other: &'a mut $type<P>) -> $type<P> {
let mut result = *self;
result += &*other;
result
}
}

impl<'b, P: $params> core::ops::Sub<$type<P>> for &'b $type<P> {
type Output = $type<P>;

#[inline]
fn sub(self, other: $type<P>) -> $type<P> {
let mut result = *self;
result -= &other;
result
}
}

#[allow(unused_qualifications)]
impl<'a, 'b, P: $params> core::ops::Sub<&'a $type<P>> for &'b $type<P> {
type Output = $type<P>;

#[inline]
fn sub(self, other: &'a $type<P>) -> $type<P> {
let mut result = *self;
result -= &*other;
result
}
}

#[allow(unused_qualifications)]
impl<'a, 'b, P: $params> core::ops::Sub<&'a mut $type<P>>
for &'b $type<P>
{
type Output = $type<P>;

#[inline]
fn sub(self, other: &'a mut $type<P>) -> $type<P> {
let mut result = *self;
result -= &*other;
result
}
}

#[allow(unused_qualifications)]
impl<P: $params> core::ops::Sub<Self> for $type<P> {
type Output = Self;

#[inline]
fn sub(self, other: Self) -> Self {
let mut result = self;
result -= &other;
result
}
}

#[allow(unused_qualifications)]
impl<'a, P: $params> core::ops::Sub<&'a mut Self> for $type<P> {
type Output = Self;

#[inline]
fn sub(self, other: &'a mut Self) -> Self {
let mut result = self;
result -= &*other;
result
}
}

#[allow(unused_qualifications)]
impl<P: $params> core::iter::Sum<Self> for $type<P> {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self::zero(), core::ops::Add::add)
}
}

#[allow(unused_qualifications)]
impl<'a, P: $params> core::iter::Sum<&'a Self> for $type<P> {
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.fold(Self::zero(), core::ops::Add::add)
}
}

#[allow(unused_qualifications)]
impl<P: $params> core::ops::AddAssign<Self> for $type<P> {
fn add_assign(&mut self, other: Self) {
*self += &other
}
}

#[allow(unused_qualifications)]
impl<P: $params> core::ops::SubAssign<Self> for $type<P> {
fn sub_assign(&mut self, other: Self) {
*self -= &other
}
}

#[allow(unused_qualifications)]
impl<'a, P: $params> core::ops::AddAssign<&'a mut Self> for $type<P> {
fn add_assign(&mut self, other: &'a mut Self) {
*self += &*other
}
}

#[allow(unused_qualifications)]
impl<'a, P: $params> core::ops::SubAssign<&'a mut Self> for $type<P> {
fn sub_assign(&mut self, other: &'a mut Self) {
*self -= &*other
}
}
};
}
Loading
Loading