Skip to content

Commit 09f54cb

Browse files
committed
simplify invert and FE conversions with checked mul
1 parent db7cd24 commit 09f54cb

File tree

5 files changed

+12
-15
lines changed

5 files changed

+12
-15
lines changed

p256/src/arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl PrimeCurveParams for NistP256 {
7575
FieldElement256::new_unchecked(crate::__risc0::SECP256R1_EQUATION_B_LE);
7676

7777
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
78-
fn from_u32_words_le(words: [u32; 8]) -> elliptic_curve::subtle::CtOption<FieldElement> {
78+
fn from_u32_words_le(words: [u32; 8]) -> FieldElement {
7979
FieldElement::from_words_le(words)
8080
}
8181
}

p256/src/arithmetic/field.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,18 @@ primeorder::impl_mont_field_element!(
6363

6464
impl FieldElement {
6565
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
66-
pub(crate) fn from_words_le(fe: [u32; 8]) -> CtOption<Self> {
66+
pub(crate) fn from_words_le(fe: [u32; 8]) -> Self {
6767
let fe = FieldElement256::new_unchecked(fe);
6868

6969
// Convert to montgomery form with aR mod p
7070
let mut mont = FieldElement256::default();
71-
fe.mul(&R_2_LE, &mut mont);
7271

73-
let buffer: [u32; 8] = mont.data;
72+
// This mul will check if the result is within the modulus.
73+
fe.mul(&R_2_LE, &mut mont);
7474

75-
use crate::elliptic_curve::subtle::ConstantTimeLess as _;
76-
let uint = U256::from_le_slice(bytemuck::cast_slice::<u32, u8>(&buffer));
77-
let is_within_modulus = uint.ct_lt(&MODULUS);
75+
let uint = U256::from_le_slice(bytemuck::cast_slice::<u32, u8>(&mont.data));
7876

79-
CtOption::new(Self(uint), is_within_modulus)
77+
Self(uint)
8078
}
8179

8280
#[cfg(all(target_os = "zkvm", target_arch = "riscv32"))]
@@ -108,7 +106,8 @@ impl FieldElement {
108106
&crate::__risc0::SECP256R1_PRIME,
109107
&mut output,
110108
);
111-
FieldElement::from_words_le(output)
109+
let element = FieldElement::from_words_le(output);
110+
return CtOption::new(element, Choice::from(1));
112111
}
113112
}
114113

primeorder/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ pub trait PrimeCurveParams:
9595
/// expected layout.
9696
const EQUATION_B_LE: __risc0::FieldElement256<Self>;
9797

98-
fn from_u32_words_le(words: [u32; 8]) -> elliptic_curve::subtle::CtOption<Self::FieldElement>;
98+
fn from_u32_words_le(words: [u32; 8]) -> Self::FieldElement;
9999
}

primeorder/src/projective.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ where
8686
&mut buffer,
8787
);
8888
let y = C::from_u32_words_le(buffer);
89-
return x
90-
.and_then(|x| y.map(|y| AffinePoint { x, y, infinity: 0 }))
91-
.unwrap_or(AffinePoint::IDENTITY);
89+
return AffinePoint { x, y, infinity: 0 };
9290
}
9391

9492
self.z

primeorder/src/risc0.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ where
235235
{
236236
if let Some(value) = affine.as_u32s() {
237237
// This should only not be within the modulus with a malicious host, panic in that case.
238-
let x = C::from_u32_words_le(value[0]).unwrap();
239-
let y = C::from_u32_words_le(value[1]).unwrap();
238+
let x = C::from_u32_words_le(value[0]);
239+
let y = C::from_u32_words_le(value[1]);
240240

241241
let affine = AffinePoint { x, y, infinity: 0 };
242242
ProjectivePoint::from(affine)

0 commit comments

Comments
 (0)