Skip to content
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
5 changes: 3 additions & 2 deletions elliptic-curve/src/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Elliptic curve arithmetic traits.

use crate::{Curve, FieldBytes, PrimeCurve, ScalarCore};
use crate::{ops::Reduce, Curve, FieldBytes, PrimeCurve, ScalarCore};
use core::fmt::Debug;
use subtle::{ConditionallySelectable, ConstantTimeEq};
use zeroize::DefaultIsZeroes;
Expand Down Expand Up @@ -73,8 +73,9 @@ pub trait ScalarArithmetic: Curve {
/// - [`Sync`]
type Scalar: DefaultIsZeroes
+ From<ScalarCore<Self>>
+ Into<Self::UInt>
+ Into<FieldBytes<Self>>
+ Into<Self::UInt>
+ Reduce<Self::UInt>
+ ff::Field
+ ff::PrimeField<Repr = FieldBytes<Self>>;
}
7 changes: 7 additions & 0 deletions elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{
bigint::U256,
error::{Error, Result},
ops::Reduce,
rand_core::RngCore,
sec1::{FromEncodedPoint, ToEncodedPoint},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
Expand Down Expand Up @@ -310,6 +311,12 @@ impl Neg for Scalar {
}
}

impl Reduce<U256> for Scalar {
fn from_uint_reduced(_: U256) -> Self {
unimplemented!();
}
}

impl From<u64> for Scalar {
fn from(n: u64) -> Scalar {
Self(n.into())
Expand Down
19 changes: 19 additions & 0 deletions elliptic-curve/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub use core::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};

use crypto_bigint::{ArrayEncoding, ByteArray, Integer};
use subtle::CtOption;

/// Perform an inversion on a field element (i.e. base field element or scalar)
Expand All @@ -21,3 +22,21 @@ impl<F: ff::Field> Invert for F {
ff::Field::invert(self)
}
}

/// Modular reduction.
pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
/// Perform a modular reduction, returning a field element.
fn from_uint_reduced(n: UInt) -> Self;

/// Interpret the given byte array as a big endian integer and perform a
/// modular reduction.
fn from_be_bytes_reduced(bytes: ByteArray<UInt>) -> Self {
Self::from_uint_reduced(UInt::from_be_byte_array(bytes))
}

/// Interpret the given byte array as a big endian integer and perform a
/// modular reduction.
fn from_le_bytes_reduced(bytes: ByteArray<UInt>) -> Self {
Self::from_uint_reduced(UInt::from_le_byte_array(bytes))
}
}