diff --git a/plonky2/src/curve/ecdsa.rs b/plonky2/src/curve/ecdsa.rs index 0ed777d9..82eeba08 100644 --- a/plonky2/src/curve/ecdsa.rs +++ b/plonky2/src/curve/ecdsa.rs @@ -1,4 +1,5 @@ -use itertools::unfold; +use itertools::{unfold, Itertools}; +use num::BigUint; use crate::curve::curve_types::{AffinePoint, Curve, CurveScalar}; use crate::field::field_types::{Field, RichField}; @@ -35,15 +36,19 @@ pub fn hash_to_bits(x: F, num_bits: usize) -> Vec { pub fn hash_to_scalar(x: F, num_bits: usize) -> C::ScalarField { let h_bits = hash_to_bits(x, num_bits); - let h_u32 = h_bits + let h_vals: Vec<_> = h_bits .iter() - .zip(0..32) - .fold(0u32, |acc, (&bit, pow)| acc + (bit as u32) * (2 << pow)); - C::ScalarField::from_canonical_u32(h_u32) + .chunks(32) + .into_iter() + .map(|chunk| { + chunk.enumerate() + .fold(0u32, |acc, (pow, &bit)| acc + (bit as u32) * (2 << pow)) + }).collect(); + C::ScalarField::from_biguint(BigUint::new(h_vals)) } pub fn sign_message(msg: F, sk: ECDSASecretKey) -> ECDSASignature { - let h = hash_to_scalar::(msg, 32); + let h = hash_to_scalar::(msg, 256); let k = C::ScalarField::rand(); let rr = (CurveScalar(k) * C::GENERATOR_PROJECTIVE).to_affine(); @@ -60,7 +65,7 @@ pub fn verify_message( ) -> bool { let ECDSASignature { r, s } = sig; - let h = hash_to_scalar::(msg, 32); + let h = hash_to_scalar::(msg, 256); let c = s.inverse(); let u1 = h * c; diff --git a/plonky2/src/gadgets/ecdsa.rs b/plonky2/src/gadgets/ecdsa.rs index def3b5b4..03a3807d 100644 --- a/plonky2/src/gadgets/ecdsa.rs +++ b/plonky2/src/gadgets/ecdsa.rs @@ -55,7 +55,7 @@ impl, const D: usize> CircuitBuilder { ) { let ECDSASignatureTarget { r, s } = sig; - let h = self.hash_to_scalar::(msg, 32); + let h = self.hash_to_scalar::(msg, 256); let c = self.inv_nonnative(&s); let u1 = self.mul_nonnative(&h, &c);