Skip to content

Commit

Permalink
Up-to-date batch_invert
Browse files Browse the repository at this point in the history
  • Loading branch information
ycscaly committed Nov 12, 2023
1 parent 052055d commit 3d1bedf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

20 changes: 11 additions & 9 deletions k256/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use field::FieldElement;

use self::{affine::AffinePoint, projective::ProjectivePoint, scalar::Scalar};
use crate::Secp256k1;
use elliptic_curve::ops::InvertBatch;
use elliptic_curve::ops::Invert;
use elliptic_curve::{CurveArithmetic, ToAffineBatch};

impl CurveArithmetic for Secp256k1 {
Expand All @@ -36,7 +36,7 @@ pub(crate) const CURVE_EQUATION_B: FieldElement = FieldElement::from_bytes_unche
]);

impl ToAffineBatch for Secp256k1 {
fn to_affine_batch_generic<const N: usize>(
fn to_affine_batch_array<const N: usize>(
points: &[Self::ProjectivePoint; N],
) -> [Self::AffinePoint; N] {
let mut zs = [FieldElement::ONE; N];
Expand All @@ -51,7 +51,7 @@ impl ToAffineBatch for Secp256k1 {
}

// This is safe to unwrap since we assured that all elements are non-zero
let zs_inverses = <FieldElement as InvertBatch>::invert_batch_generic(&zs).unwrap();
let zs_inverses = <FieldElement as Invert>::invert_batch_array(&zs).unwrap();

let mut affine_points = [AffinePoint::IDENTITY; N];
for i in 0..N {
Expand All @@ -66,7 +66,9 @@ impl ToAffineBatch for Secp256k1 {
}

#[cfg(feature = "alloc")]
fn to_affine_batch<B: FromIterator<Self::AffinePoint>>(points: &[Self::ProjectivePoint]) -> B {
fn to_affine_batch_slice<B: FromIterator<Self::AffinePoint>>(
points: &[Self::ProjectivePoint],
) -> B {
let mut zs: Vec<_> = (0..points.len()).map(|_| FieldElement::ONE).collect();

for i in 0..points.len() {
Expand All @@ -80,7 +82,7 @@ impl ToAffineBatch for Secp256k1 {

// This is safe to unwrap since we assured that all elements are non-zero
let zs_inverses: Vec<_> =
<FieldElement as InvertBatch>::invert_batch(zs.as_slice()).unwrap();
<FieldElement as Invert>::invert_batch_slice(zs.as_slice()).unwrap();

let mut affine_points: Vec<_> = (0..points.len()).map(|_| AffinePoint::IDENTITY).collect();
for i in 0..points.len() {
Expand Down Expand Up @@ -134,13 +136,13 @@ mod tests {

let expected = [g.to_affine(), h.to_affine()];
assert_eq!(
<Secp256k1 as ToAffineBatch>::to_affine_batch_generic(&[g, h]),
<Secp256k1 as ToAffineBatch>::to_affine_batch_array(&[g, h]),
expected
);

let expected = [g.to_affine(), AffinePoint::IDENTITY];
assert_eq!(
<Secp256k1 as ToAffineBatch>::to_affine_batch_generic(&[g, ProjectivePoint::IDENTITY]),
<Secp256k1 as ToAffineBatch>::to_affine_batch_array(&[g, ProjectivePoint::IDENTITY]),
expected
);
}
Expand All @@ -155,12 +157,12 @@ mod tests {
let h = ProjectivePoint::mul_by_generator(&l);

let expected = proptest::std_facade::vec![g.to_affine(), h.to_affine()];
let res: alloc::vec::Vec<_> = <Secp256k1 as ToAffineBatch>::to_affine_batch(&[g, h]);
let res: alloc::vec::Vec<_> = <Secp256k1 as ToAffineBatch>::to_affine_batch_slice(&[g, h]);
assert_eq!(res, expected);

let expected = proptest::std_facade::vec![g.to_affine(), AffinePoint::IDENTITY];
let res: alloc::vec::Vec<_> =
<Secp256k1 as ToAffineBatch>::to_affine_batch(&[g, ProjectivePoint::IDENTITY]);
<Secp256k1 as ToAffineBatch>::to_affine_batch_slice(&[g, ProjectivePoint::IDENTITY]);

assert_eq!(res, expected);
}
Expand Down
7 changes: 4 additions & 3 deletions k256/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ impl<'a> Product<&'a FieldElement> for FieldElement {
#[cfg(test)]
mod tests {
use elliptic_curve::ff::{Field, PrimeField};
use elliptic_curve::ops::InvertBatch;
use elliptic_curve::ops::Invert;
use num_bigint::{BigUint, ToBigUint};
use proptest::prelude::*;
use rand_core::OsRng;
Expand Down Expand Up @@ -690,7 +690,7 @@ mod tests {

let expected = [k.invert().unwrap(), l.invert().unwrap()];
assert_eq!(
<FieldElement as InvertBatch>::invert_batch_generic(&[k, l]).unwrap(),
<FieldElement as Invert>::invert_batch_array(&[k, l]).unwrap(),
expected
);
}
Expand All @@ -703,7 +703,8 @@ mod tests {
let l: FieldElement = FieldElement::random(&mut OsRng);

let expected = proptest::std_facade::vec![k.invert().unwrap(), l.invert().unwrap()];
let res: alloc::vec::Vec<_> = <FieldElement as InvertBatch>::invert_batch(&[k, l]).unwrap();
let res: alloc::vec::Vec<_> =
<FieldElement as Invert>::invert_batch_slice(&[k, l]).unwrap();
assert_eq!(res, expected);
}

Expand Down
2 changes: 1 addition & 1 deletion k256/src/arithmetic/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ProjectivePoint {
.unwrap_or_else(|| AffinePoint::IDENTITY)
}

pub(super) fn to_affine_internal(&self, zinv: FieldElement) -> AffinePoint {
pub(super) fn to_affine_internal(self, zinv: FieldElement) -> AffinePoint {
let x = self.x * &zinv;
let y = self.y * &zinv;
AffinePoint::new(x.normalize(), y.normalize())
Expand Down
5 changes: 2 additions & 3 deletions k256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ mod tests {
arithmetic::dev::{biguint_to_bytes, bytes_to_biguint},
FieldBytes, NonZeroScalar, WideBytes, ORDER,
};
use elliptic_curve::ops::InvertBatch;
use elliptic_curve::{
bigint::{ArrayEncoding, U256, U512},
ff::{Field, PrimeField},
Expand Down Expand Up @@ -958,7 +957,7 @@ mod tests {

let expected = [k.invert().unwrap(), l.invert().unwrap()];
assert_eq!(
<Scalar as InvertBatch>::invert_batch_generic(&[k, l]).unwrap(),
<Scalar as Invert>::invert_batch_array(&[k, l]).unwrap(),
expected
);
}
Expand All @@ -971,7 +970,7 @@ mod tests {
let l: Scalar = Scalar::random(&mut OsRng);

let expected = proptest::std_facade::vec![k.invert().unwrap(), l.invert().unwrap()];
let res: alloc::vec::Vec<_> = <Scalar as InvertBatch>::invert_batch(&[k, l]).unwrap();
let res: alloc::vec::Vec<_> = <Scalar as Invert>::invert_batch_slice(&[k, l]).unwrap();
assert_eq!(res, expected);
}

Expand Down

0 comments on commit 3d1bedf

Please sign in to comment.