/
ops.rs
52 lines (42 loc) · 1.61 KB
/
ops.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Traits for arithmetic operations on elliptic curve field elements.
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)
pub trait Invert {
/// Field element type
type Output;
/// Invert a field element.
fn invert(&self) -> CtOption<Self::Output>;
}
#[cfg(feature = "arithmetic")]
impl<F: ff::Field> Invert for F {
type Output = F;
fn invert(&self) -> CtOption<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))
}
}
/// Modular reduction to a non-zero output.
///
/// This trait is primarily intended for use by curve implementations.
///
/// End users can use the `Reduce` impl on `NonZeroScalar` instead.
pub trait ReduceNonZero<UInt: Integer + ArrayEncoding>: Sized {
/// Perform a modular reduction, returning a field element.
fn from_uint_reduced_non_zero(n: UInt) -> Self;
}