-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[blocked upstream] Evaluate hash-to-field options #81
Comments
The ECDSA implementation in Message hashed here: https://docs.rs/k256/0.9.6/src/k256/ecdsa/sign.rs.html#84 fn try_sign(&self, msg: &[u8]) -> Result<S, Error> {
self.try_sign_digest(Digest::chain(S::Digest::new(), msg))
} Message scalar derived from hash digest in ECDSA here: https://docs.rs/k256/0.9.6/src/k256/ecdsa/sign.rs.html#115 let msg_scalar = Scalar::from_digest(digest); Note that
Self::from_bytes_reduced(&digest.finalize()) and /// Subtracts the modulus when the byte array is larger than the modulus. and here's the low-level arithmetic code just to prove that I'm not lying: https://github.com/RustCrypto/elliptic-curves/blob/2178da6034a42794c09da380812c12cf80e95f42/k256/src/arithmetic/scalar/scalar_4x64.rs#L215-L229 /// Parses the given byte array as a scalar.
///
/// Subtracts the modulus when the byte array is larger than the modulus.
pub fn from_bytes_reduced(bytes: &[u8; 32]) -> Self {
// Interpret the bytes as a big-endian integer w.
let w3 = u64::from_be_bytes(bytes[0..8].try_into().unwrap());
let w2 = u64::from_be_bytes(bytes[8..16].try_into().unwrap());
let w1 = u64::from_be_bytes(bytes[16..24].try_into().unwrap());
let w0 = u64::from_be_bytes(bytes[24..32].try_into().unwrap());
let w = [w0, w1, w2, w3];
// If w is in the range [0, n) then w - n will underflow
let (r2, underflow) = sbb_array_with_underflow(&w, &MODULUS);
Self(conditional_select(&w, &r2, !underflow))
} |
We'll follow the lead of RustCrypto here. This issue can stay in a holding pattern for now until the dust settles. |
I opened a PR to add a Would appreciate any feedback. |
During an offline discussion, we discussed that Bitcoin seems to also hash the message to a scalar by naive reduction modulo n. Since the signature we create has to be a valid Bitcoin signature too, we can't change use another hash-to-field function for the message itself. |
Currently we convert a 32-byte hash digest to an integer modulo the curve order
n
viafrom_digest
orfrom_bytes_reduced
functions provided by RustCrypto. These functions do a naive reduction modn
, which produces a biased sample: some values are twice as likely to occur as others. Is this acceptable? If not, what to do about it?References
The text was updated successfully, but these errors were encountered: