Skip to content

Commit 4a66334

Browse files
authored
elliptic-curve: remove references from batch generic params (#1403)
Previously the traits were generic around a reference type. The problem with that is reference types have lifetimes, which requires notating them in generic code, in this case requiring an HRTB per generic parameter. HRTBs work because the output has no lifetime dependencies on the input, but if that's the case we can just explicitly borrow in the trait, which means no HRTB is needed in generic code, which makes notating the trait bounds significantly simpler.
1 parent 2af0350 commit 4a66334

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

elliptic-curve/src/arithmetic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ pub trait PrimeCurveArithmetic:
8686
}
8787

8888
/// Normalize point(s) in projective representation by converting them to their affine ones.
89-
pub trait BatchNormalize<Points>: group::Curve {
89+
pub trait BatchNormalize<Points: ?Sized>: group::Curve {
9090
/// The output of the batch normalization; a container of affine points.
9191
type Output: AsRef<[Self::AffineRepr]>;
9292

9393
/// Perform a batched conversion to affine representation on a sequence of projective points
9494
/// at an amortized cost that should be practically as efficient as a single conversion.
9595
/// Internally, implementors should rely upon `InvertBatch`.
96-
fn batch_normalize(points: Points) -> <Self as BatchNormalize<Points>>::Output;
96+
fn batch_normalize(points: &Points) -> <Self as BatchNormalize<Points>>::Output;
9797
}

elliptic-curve/src/ops.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,27 @@ pub trait Invert {
3131

3232
/// Perform a batched inversion on a sequence of field elements (i.e. base field elements or scalars)
3333
/// at an amortized cost that should be practically as efficient as a single inversion.
34-
pub trait BatchInvert<FieldElements>: Invert {
34+
pub trait BatchInvert<FieldElements: ?Sized>: Invert + Sized {
3535
/// The output of batch inversion. A container of field elements.
36-
type Output;
36+
type Output: AsRef<[Self]>;
3737

3838
/// Invert a batch of field elements.
39-
fn batch_invert(field_elements: FieldElements) -> <Self as BatchInvert<FieldElements>>::Output;
39+
fn batch_invert(
40+
field_elements: &FieldElements,
41+
) -> CtOption<<Self as BatchInvert<FieldElements>>::Output>;
4042
}
4143

42-
impl<const N: usize, T> BatchInvert<&[T; N]> for T
44+
impl<const N: usize, T> BatchInvert<[T; N]> for T
4345
where
4446
T: Invert<Output = CtOption<Self>>
4547
+ Mul<Self, Output = Self>
4648
+ Copy
4749
+ Default
4850
+ ConditionallySelectable,
4951
{
50-
type Output = CtOption<[Self; N]>;
52+
type Output = [Self; N];
5153

52-
fn batch_invert(field_elements: &[Self; N]) -> <Self as BatchInvert<&[T; N]>>::Output {
54+
fn batch_invert(field_elements: &[Self; N]) -> CtOption<[Self; N]> {
5355
let mut field_elements_multiples = [Self::default(); N];
5456
let mut field_elements_multiples_inverses = [Self::default(); N];
5557
let mut field_elements_inverses = [Self::default(); N];
@@ -66,17 +68,17 @@ where
6668
}
6769

6870
#[cfg(feature = "alloc")]
69-
impl<T> BatchInvert<&[T]> for T
71+
impl<T> BatchInvert<[T]> for T
7072
where
7173
T: Invert<Output = CtOption<Self>>
7274
+ Mul<Self, Output = Self>
7375
+ Copy
7476
+ Default
7577
+ ConditionallySelectable,
7678
{
77-
type Output = CtOption<Vec<Self>>;
79+
type Output = Vec<Self>;
7880

79-
fn batch_invert(field_elements: &[Self]) -> <Self as BatchInvert<&[T]>>::Output {
81+
fn batch_invert(field_elements: &[Self]) -> CtOption<Vec<Self>> {
8082
let mut field_elements_multiples: Vec<Self> = vec![Self::default(); field_elements.len()];
8183
let mut field_elements_multiples_inverses: Vec<Self> =
8284
vec![Self::default(); field_elements.len()];

0 commit comments

Comments
 (0)