mirror of
https://github.com/saymrwulf/risc0-curve25519-dalek-source.git
synced 2026-09-05 20:10:35 +00:00
Add an optimisation to succeed fast for scalars whose 4th MSB is unset.
This is only done during signature verification.
This commit is contained in:
parent
de3eb13dcf
commit
deca36d074
1 changed files with 12 additions and 0 deletions
|
|
@ -90,6 +90,18 @@ fn check_scalar(bytes: [u8; 32]) -> Result<Scalar, SignatureError> {
|
|||
#[cfg(not(feature = "legacy_compatibility"))]
|
||||
#[inline(always)]
|
||||
fn check_scalar(bytes: [u8; 32]) -> Result<Scalar, SignatureError> {
|
||||
// Since this is only used in signature deserialisation (i.e. upon
|
||||
// verification), we can do a "succeed fast" trick by checking that the most
|
||||
// significant 4 bits are unset. If they are unset, we can succeed fast
|
||||
// because we are guaranteed that the scalar is fully reduced. However, if
|
||||
// the 4th most significant bit is set, we must do the full reduction check,
|
||||
// as the order of the basepoint is roughly a 2^(252.5) bit number.
|
||||
//
|
||||
// This succeed-fast trick should succeed for roughly half of all scalars.
|
||||
if bytes[31] & 240 == 0 {
|
||||
return Ok(Scalar::from_bits(bytes))
|
||||
}
|
||||
|
||||
match Scalar::from_canonical_bytes(bytes) {
|
||||
None => return Err(SignatureError(InternalError::ScalarFormatError)),
|
||||
Some(x) => return Ok(x),
|
||||
|
|
|
|||
Loading…
Reference in a new issue