Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ fn scalar_math(c: &mut Criterion) {
)
});

group.bench_function("hash_to_scalar", |b| {
b.iter_batched_ref(
|| rng.r#gen::<[u8; 32]>(),
|s| EccScalar::hash_to_scalar(curve_type, s, b"hash-to-scalar-bench"),
BatchSize::SmallInput,
)
});

for n in [2, 4, 16, 32] {
group.bench_function(format!("batch_invert_vartime_{}", n), |b| {
b.iter_batched_ref(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hex_literal::hex;
use k256::elliptic_curve::{
Field, Group,
Group,
group::{GroupEncoding, ff::PrimeField},
ops::{Invert, LinearCombination, MulByGenerator, Reduce},
scalar::IsHigh,
Expand Down Expand Up @@ -174,11 +174,11 @@ impl Scalar {
/// group order.
pub fn from_wide_bytes(bytes: &[u8]) -> Option<Self> {
/*
As the k256 crates is lacking a native function that reduces an input
As the k256 crate is lacking a native function that reduces an input
modulo the group order we have to synthesize it using other operations.

Do so by splitting up the input into two parts each of which is at most
scalar_len bytes long. Then compute s0*2^X + s1
scalar_len bytes long. Then compute s0*2^256 + s1
*/

if bytes.len() > Self::BYTES * 2 {
Expand All @@ -192,15 +192,16 @@ impl Scalar {
let fb0 = k256::FieldBytes::from_slice(&extended[..Self::BYTES]);
let fb1 = k256::FieldBytes::from_slice(&extended[Self::BYTES..]);

let mut s0 = <k256::Scalar as Reduce<k256::U256>>::reduce_bytes(fb0);
let s0 = <k256::Scalar as Reduce<k256::U256>>::reduce_bytes(fb0);
let s1 = <k256::Scalar as Reduce<k256::U256>>::reduce_bytes(fb1);

for _bit in 1..=Self::BYTES * 8 {
s0 = s0.double();
}
s0 += s1;
// 2^256 mod n (secp256k1 group order)
let shift = k256::Scalar::from_repr(k256::FieldBytes::from(hex!(
"000000000000000000000000000000014551231950b75fc4402da1732fc9bebf"
)))
.unwrap();

Some(Self::new(s0))
Some(Self::new(s0 * shift + s1))
}

/// Return constant zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ impl Scalar {
/// group order.
pub fn from_wide_bytes(bytes: &[u8]) -> Option<Self> {
/*
As the p256 crates is lacking a native function that reduces an input
As the p256 crate is lacking a native function that reduces an input
modulo the group order we have to synthesize it using other operations.

Do so by splitting up the input into two parts each of which is at most
scalar_len bytes long. Then compute s0*2^X + s1
scalar_len bytes long. Then compute s0*2^256 + s1
*/

if bytes.len() > Self::BYTES * 2 {
Expand All @@ -94,15 +94,16 @@ impl Scalar {
let fb0 = p256::FieldBytes::from_slice(&extended[..Self::BYTES]);
let fb1 = p256::FieldBytes::from_slice(&extended[Self::BYTES..]);

let mut s0 = <p256::Scalar as Reduce<p256::U256>>::reduce_bytes(fb0);
let s0 = <p256::Scalar as Reduce<p256::U256>>::reduce_bytes(fb0);
let s1 = <p256::Scalar as Reduce<p256::U256>>::reduce_bytes(fb1);

for _bit in 1..=Self::BYTES * 8 {
s0 = s0.double();
}
s0 += s1;
// 2^256 mod n (secp256r1 group order)
let shift = p256::Scalar::from_repr(p256::FieldBytes::from(hex!(
"00000000ffffffff00000000000000004319055258e8617b0c46353d039cdaaf"
)))
.unwrap();

Some(Self::new(s0))
Some(Self::new(s0 * shift + s1))
}

/// Return constant zero
Expand Down
Loading