Skip to content

Commit ab9a3d1

Browse files
authored
primefield: better field element hex parsing (#1473)
Ensures hex strings are always sized appropriately to the modulus, which also ensures the size is consistent on 32-bit and 64-bit targets. Notably this impacts P-224 where there was previously some special casing on `target_pointer_width`.
1 parent 7c6f898 commit ab9a3d1

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

p224/src/arithmetic.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,17 @@ impl PrimeCurveParams for NistP224 {
4141
const EQUATION_A: FieldElement = FieldElement::from_u64(3).neg();
4242

4343
/// b = 0xb4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4
44-
#[cfg(target_pointer_width = "32")]
4544
const EQUATION_B: FieldElement =
4645
FieldElement::from_hex_vartime("b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4");
4746

48-
/// b = 0xb4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4
49-
#[cfg(target_pointer_width = "64")]
50-
const EQUATION_B: FieldElement = FieldElement::from_hex_vartime(
51-
"00000000b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4",
52-
);
53-
5447
/// Base point of P-224.
5548
///
5649
/// ```text
5750
/// Gₓ = 0xb70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21
5851
/// Gᵧ = 0xbd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34
5952
/// ```
60-
#[cfg(target_pointer_width = "32")]
6153
const GENERATOR: (FieldElement, FieldElement) = (
6254
FieldElement::from_hex_vartime("b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21"),
6355
FieldElement::from_hex_vartime("bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"),
6456
);
65-
66-
/// Base point of P-224.
67-
///
68-
/// ```text
69-
/// Gₓ = 0xb70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21
70-
/// Gᵧ = 0xbd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34
71-
/// ```
72-
#[cfg(target_pointer_width = "64")]
73-
const GENERATOR: (FieldElement, FieldElement) = (
74-
FieldElement::from_hex_vartime(
75-
"00000000b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21",
76-
),
77-
FieldElement::from_hex_vartime(
78-
"00000000bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34",
79-
),
80-
);
8157
}

primefield/src/macros.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,35 @@ macro_rules! monty_field_element {
176176
/// - When input is the wrong length
177177
/// - If input overflows the modulus
178178
pub const fn from_hex_vartime(hex: &str) -> Self {
179-
Self(
180-
$crate::MontyFieldElement::<$params, { <$params>::LIMBS }>::from_hex_vartime(hex),
181-
)
179+
use $crate::array::typenum::Unsigned;
180+
181+
assert!(
182+
hex.len() == <$params as $crate::MontyFieldParams<{ <$params>::LIMBS }>>::ByteSize::USIZE * 2,
183+
"hex is the wrong length"
184+
);
185+
186+
// Build a hex string of the expected size, regardless of the size of `Uint`
187+
let mut hex_bytes = [b'0'; { <$uint>::BITS as usize / 4 }];
188+
189+
let offset = match <$params as $crate::MontyFieldParams<{ <$params>::LIMBS }>>::BYTE_ORDER {
190+
$crate::ByteOrder::BigEndian => hex_bytes.len() - hex.len(),
191+
$crate::ByteOrder::LittleEndian => 0
192+
};
193+
194+
let mut i = 0;
195+
while i < hex.len() {
196+
hex_bytes[i + offset] = hex.as_bytes()[i];
197+
i += 1;
198+
}
199+
200+
match core::str::from_utf8(&hex_bytes) {
201+
Ok(padded_hex) => Self(
202+
$crate::MontyFieldElement::<$params, { <$params>::LIMBS }>::from_hex_vartime(padded_hex),
203+
),
204+
Err(_) => panic!("invalid hex string"),
205+
}
206+
207+
182208
}
183209

184210
/// Decode [`

0 commit comments

Comments
 (0)