|
| 1 | +use ethereum_types::H256; |
| 2 | + |
| 3 | +// ECDSA signature validation constants for secp256k1 curve |
| 4 | + |
| 5 | +/// Minimum valid value for signature components r and s (must be >= 1) |
| 6 | +pub const SIGNATURE_LOWER_BOUND: H256 = H256([ |
| 7 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 8 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 9 | +]); |
| 10 | + |
| 11 | +/// Maximum valid value for signature components r and s (must be < secp256k1 curve order) |
| 12 | +/// This is the secp256k1 curve order: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141 |
| 13 | +pub const SIGNATURE_UPPER_BOUND: H256 = H256([ |
| 14 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, |
| 15 | + 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41, |
| 16 | +]); |
| 17 | + |
| 18 | +/// Maximum value for low-s signature enforcement (half of curve order) |
| 19 | +/// This is used to prevent signature malleability |
| 20 | +/// Value: 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0 |
| 21 | +pub const SIGNATURE_LOW_S_BOUND: H256 = H256([ |
| 22 | + 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 23 | + 0x5d, 0x57, 0x6e, 0x73, 0x57, 0xa4, 0x50, 0x1d, 0xdf, 0xe9, 0x2f, 0x46, 0x68, 0x1b, 0x20, 0xa0, |
| 24 | +]); |
| 25 | + |
| 26 | +/// Validates that a signature component (r or s) is within valid range |
| 27 | +/// |
| 28 | +/// A valid signature component must satisfy: |
| 29 | +/// - Greater than or equal to 1 (SIGNATURE_LOWER_BOUND) |
| 30 | +/// - Less than the secp256k1 curve order (SIGNATURE_UPPER_BOUND) |
| 31 | +#[inline] |
| 32 | +pub fn is_valid_signature_component(component: &H256) -> bool { |
| 33 | + *component >= SIGNATURE_LOWER_BOUND && *component < SIGNATURE_UPPER_BOUND |
| 34 | +} |
| 35 | + |
| 36 | +/// Checks if the s component satisfies the low-s requirement |
| 37 | +/// |
| 38 | +/// The low-s requirement helps prevent signature malleability by requiring |
| 39 | +/// that s <= n/2 where n is the curve order. |
| 40 | +#[inline] |
| 41 | +pub fn is_low_s(s: &H256) -> bool { |
| 42 | + *s <= SIGNATURE_LOW_S_BOUND |
| 43 | +} |
| 44 | + |
| 45 | +#[cfg(test)] |
| 46 | +mod tests { |
| 47 | + use super::*; |
| 48 | + use ethereum_types::U256; |
| 49 | + |
| 50 | + /// Helper function to convert H256 to U256 for arithmetic operations in tests |
| 51 | + #[inline] |
| 52 | + fn h256_to_u256(h: &H256) -> U256 { |
| 53 | + U256::from_big_endian(h.as_bytes()) |
| 54 | + } |
| 55 | + |
| 56 | + /// Helper function to convert U256 to H256 |
| 57 | + #[inline] |
| 58 | + fn u256_to_h256(u: U256) -> H256 { |
| 59 | + H256::from(u.to_big_endian()) |
| 60 | + } |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn test_low_s_bound_is_half_curve_order() { |
| 64 | + // SIGNATURE_LOW_S_BOUND should be exactly n/2 where n is the curve order |
| 65 | + let n = h256_to_u256(&SIGNATURE_UPPER_BOUND); |
| 66 | + let expected_half_n = u256_to_h256(n / 2); |
| 67 | + |
| 68 | + assert_eq!( |
| 69 | + SIGNATURE_LOW_S_BOUND, expected_half_n, |
| 70 | + "SIGNATURE_LOW_S_BOUND must be exactly half of the curve order" |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn test_signature_bounds() { |
| 76 | + // Lower bound is 1 |
| 77 | + assert_eq!(SIGNATURE_LOWER_BOUND, H256::from_low_u64_be(1)); |
| 78 | + |
| 79 | + // Verify that 0 is invalid |
| 80 | + assert!(!is_valid_signature_component(&H256::zero())); |
| 81 | + |
| 82 | + // Verify that 1 is valid (minimum) |
| 83 | + assert!(is_valid_signature_component(&H256::from_low_u64_be(1))); |
| 84 | + |
| 85 | + // Verify that curve_order - 1 is valid (maximum) |
| 86 | + let max_valid = u256_to_h256(h256_to_u256(&SIGNATURE_UPPER_BOUND) - 1); |
| 87 | + assert!(is_valid_signature_component(&max_valid)); |
| 88 | + |
| 89 | + // Verify that curve_order itself is invalid |
| 90 | + assert!(!is_valid_signature_component(&SIGNATURE_UPPER_BOUND)); |
| 91 | + |
| 92 | + // Verify that values above curve_order are invalid |
| 93 | + let above_max = u256_to_h256(h256_to_u256(&SIGNATURE_UPPER_BOUND) + 1); |
| 94 | + assert!(!is_valid_signature_component(&above_max)); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_low_s_validation() { |
| 99 | + // s = 0 is invalid (below lower bound) |
| 100 | + assert!(!is_valid_signature_component(&H256::zero())); |
| 101 | + |
| 102 | + // s = 1 satisfies low-s requirement |
| 103 | + assert!(is_low_s(&u256_to_h256(U256::one()))); |
| 104 | + |
| 105 | + // s = low_s_bound satisfies low-s requirement (boundary) |
| 106 | + assert!(is_low_s(&SIGNATURE_LOW_S_BOUND)); |
| 107 | + |
| 108 | + // s = low_s_bound + 1 does NOT satisfy low-s requirement |
| 109 | + let above_low_s = u256_to_h256(h256_to_u256(&SIGNATURE_LOW_S_BOUND) + 1); |
| 110 | + assert!(!is_low_s(&above_low_s)); |
| 111 | + |
| 112 | + // s = curve_order - 1 is valid but does NOT satisfy low-s |
| 113 | + let high_s = u256_to_h256(h256_to_u256(&SIGNATURE_UPPER_BOUND) - 1); |
| 114 | + assert!(is_valid_signature_component(&high_s)); |
| 115 | + assert!(!is_low_s(&high_s)); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_boundary_conditions() { |
| 120 | + // Test exact boundary values |
| 121 | + assert_eq!(h256_to_u256(&SIGNATURE_LOWER_BOUND), U256::one()); |
| 122 | + |
| 123 | + // Ensure low-s bound is exactly half the curve order (curve_order / 2) |
| 124 | + // Note: The curve order is odd, so half_order * 2 + 1 = curve_order |
| 125 | + let curve_order = h256_to_u256(&SIGNATURE_UPPER_BOUND); |
| 126 | + let half_order = h256_to_u256(&SIGNATURE_LOW_S_BOUND); |
| 127 | + assert_eq!(curve_order / 2, half_order); |
| 128 | + } |
| 129 | +} |
0 commit comments