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

Generic arithmetic for Weierstrass curves #218

Closed
wants to merge 20 commits into from
Closed
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
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ members = [
"k256",
"p256",
"p384",
"weierstrass",
]
13 changes: 13 additions & 0 deletions weierstrass/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "weierstrass"
version = "0.1.0"
authors = ["RustCrypto Developers"]
edition = "2018"

[dependencies]
generic-array = "0.14"
byteorder = { version = "1", default-features = false }
subtle = { version = "2.3", default-features = false }
rand_core = { version = "0.5", default-features = false }
biguint-literal = { git = "https://github.com/RustCrypto/utils", branch = "biguint" }
hex-literal = "0.3"
173 changes: 173 additions & 0 deletions weierstrass/src/affine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use core::ops::{Mul, Neg, Shl};
use generic_array::{ArrayLength, typenum::{B1, U1}};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
use core::ops::Add;

use crate::{
WeierstrassCurve, Word,
Words, WordsLen,
DoubleWordsLen,
WordsBytesLen,
WordsP1Len,
};
use crate::scalar::Scalar;
use crate::projective::ProjectivePoint;
use crate::field::FieldElement;

#[derive(Clone, Copy)]
pub struct AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{
pub x: FieldElement<C>,
pub y: FieldElement<C>,
pub infinity: Choice,
}

impl<C> AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{
/// Returns the base point
pub fn generator() -> Self {
Self {
x: C::GENERATOR_X,
y: C::GENERATOR_Y,
infinity: Choice::from(0),
}
}

/// Returns the identity of the group: the point at infinity.
pub fn identity() -> Self {
Self {
x: FieldElement::zero(),
y: FieldElement::zero(),
infinity: Choice::from(1),
}
}

/// Is this point the identity point?
pub fn is_identity(&self) -> Choice {
self.infinity
}
}

impl<C> ConditionallySelectable for AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
AffinePoint {
x: FieldElement::conditional_select(&a.x, &b.x, choice),
y: FieldElement::conditional_select(&a.y, &b.y, choice),
infinity: Choice::conditional_select(&a.infinity, &b.infinity, choice),
}
}
}

impl<C> ConstantTimeEq for AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{
fn ct_eq(&self, other: &Self) -> Choice {
self.x.ct_eq(&other.x)
& self.y.ct_eq(&other.y)
& self.infinity.ct_eq(&other.infinity)
}
}

impl<C> PartialEq for AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).into()
}
}

impl<C> Eq for AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{}

impl<C> Mul<Scalar<C>> for AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{
type Output = Self;

fn mul(self, scalar: Scalar<C>) -> Self {
(ProjectivePoint::from(self) * scalar).to_affine()
}
}

impl<C> Neg for AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{
type Output = Self;

fn neg(self) -> Self::Output {
AffinePoint {
x: self.x,
y: -self.y,
infinity: self.infinity,
}
}
}

#[cfg(feature = "zeroize")]
impl<C> Zeroize for AffinePoint<C>
where
C: WeierstrassCurve,
WordsLen<C>: ArrayLength<Word> + Add<U1> + Shl<B1>,
DoubleWordsLen<C>: ArrayLength<Word>,
WordsP1Len<C>: ArrayLength<Word>,
WordsBytesLen<C>: ArrayLength<u8>,
Words<C>: Copy,
{
fn zeroize(&mut self) {
self.x.zeroize();
self.y.zeroize();
}
}
Loading