Skip to content

Commit fc7059c

Browse files
naijauserNnamdi Aninyecptarturksew1
authored
Improve the data-transformer::cairo-types::CairoU256 creation logic (#3622)
<!-- Reference any GitHub issues resolved by this PR --> Closes [#2576](#2576) ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Performed self-review of the code --------- Co-authored-by: Nnamdi Aninye <[email protected]> Co-authored-by: Artur Michałek <[email protected]> Co-authored-by: ksew1 <[email protected]>
1 parent 7e0e1a0 commit fc7059c

File tree

2 files changed

+34
-31
lines changed
  • crates
    • cheatnet/src/runtime_extensions/forge_runtime_extension
    • data-transformer/src/cairo_types

2 files changed

+34
-31
lines changed

crates/cheatnet/src/runtime_extensions/forge_runtime_extension/mod.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,26 +364,46 @@ impl<'a> ExtensionLogic for ForgeExtension<'a> {
364364
let curve: Felt = input_reader.read()?;
365365
let curve = curve.to_short_string().ok();
366366

367-
let (signing_key_bytes, verifying_key_bytes) = {
367+
let (signing_key_bytes, x_coordinate_bytes, y_coordinate_bytes) = {
368+
let extract_coordinates_from_verifying_key = |verifying_key: Box<[u8]>| {
369+
let verifying_key = verifying_key.iter().as_slice();
370+
(
371+
verifying_key[1..33].try_into().unwrap(),
372+
verifying_key[33..65].try_into().unwrap(),
373+
)
374+
};
375+
368376
match curve.as_deref() {
369377
Some("Secp256k1") => {
370378
let signing_key = k256::ecdsa::SigningKey::random(
371379
&mut k256::elliptic_curve::rand_core::OsRng,
372380
);
373-
let verifying_key = signing_key.verifying_key();
381+
let verifying_key = signing_key
382+
.verifying_key()
383+
.to_encoded_point(false)
384+
.to_bytes();
385+
let (x_coordinate, y_coordinate) =
386+
extract_coordinates_from_verifying_key(verifying_key);
374387
(
375-
signing_key.to_bytes(),
376-
verifying_key.to_encoded_point(false).to_bytes(),
388+
signing_key.to_bytes().as_slice()[0..32].try_into().unwrap(),
389+
x_coordinate,
390+
y_coordinate,
377391
)
378392
}
379393
Some("Secp256r1") => {
380394
let signing_key = p256::ecdsa::SigningKey::random(
381395
&mut p256::elliptic_curve::rand_core::OsRng,
382396
);
383-
let verifying_key = signing_key.verifying_key();
397+
let verifying_key = signing_key
398+
.verifying_key()
399+
.to_encoded_point(false)
400+
.to_bytes();
401+
let (x_coordinate, y_coordinate) =
402+
extract_coordinates_from_verifying_key(verifying_key);
384403
(
385-
signing_key.to_bytes(),
386-
verifying_key.to_encoded_point(false).to_bytes(),
404+
signing_key.to_bytes().as_slice()[0..32].try_into().unwrap(),
405+
x_coordinate,
406+
y_coordinate,
387407
)
388408
}
389409
_ => return Ok(CheatcodeHandlingResult::Forwarded),
@@ -392,8 +412,8 @@ impl<'a> ExtensionLogic for ForgeExtension<'a> {
392412

393413
Ok(CheatcodeHandlingResult::from_serializable((
394414
CairoU256::from_bytes(&signing_key_bytes),
395-
CairoU256::from_bytes(&verifying_key_bytes[1..]), // bytes of public_key's x-coordinate
396-
CairoU256::from_bytes(&verifying_key_bytes[33..]), // bytes of public_key's y-coordinate
415+
CairoU256::from_bytes(&x_coordinate_bytes), // bytes of public_key's x-coordinate
416+
CairoU256::from_bytes(&y_coordinate_bytes), // bytes of public_key's y-coordinate
397417
)))
398418
}
399419
"ecdsa_sign_message" => {
@@ -441,6 +461,8 @@ impl<'a> ExtensionLogic for ForgeExtension<'a> {
441461
};
442462

443463
let result = result.map(|(r_bytes, s_bytes)| {
464+
let r_bytes: [u8; 32] = r_bytes.as_slice()[0..32].try_into().unwrap();
465+
let s_bytes: [u8; 32] = s_bytes.as_slice()[0..32].try_into().unwrap();
444466
(
445467
CairoU256::from_bytes(&r_bytes),
446468
CairoU256::from_bytes(&s_bytes),

crates/data-transformer/src/cairo_types/u256.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ pub struct CairoU256 {
1515

1616
impl CairoU256 {
1717
#[must_use]
18-
pub fn from_bytes(bytes: &[u8]) -> Self {
19-
// Takes slice without explicit size because of cheatnet's specific usages (See Issue #2575)
18+
pub fn from_bytes(bytes: &[u8; 32]) -> Self {
2019
Self {
2120
low: u128::from_be_bytes(bytes[16..32].try_into().unwrap()),
2221
high: u128::from_be_bytes(bytes[0..16].try_into().unwrap()),
@@ -85,37 +84,19 @@ mod tests {
8584
244, 192, 163, 138, 253, 42, 253, 125, 53, 127, 44,
8685
];
8786

88-
const TOO_BIG_NUMBER_BYTES: [u8; 48] = [
89-
222, 9, 69, 194, 71, 77, 155, 51, 51, 49, 35, 229, 62, 55, 169, 63, 93, 228, 186, 10, 219,
90-
244, 192, 163, 138, 253, 42, 253, 125, 53, 127, 44, 21, 37, 21, 37, 21, 37, 21, 37, 21, 37,
91-
21, 37, 21, 37, 21, 37,
92-
];
93-
9487
const BIG_NUMBER_LIMBS: [u128; 2] = [
9588
124_805_820_680_284_125_994_760_982_863_763_832_620,
9689
295_136_760_614_571_572_862_546_075_274_463_127_871,
9790
];
9891

9992
#[test_case(&[0; 32], [0, 0] ; "zeros")]
100-
#[test_case(&BIG_NUMBER_BYTES[..], BIG_NUMBER_LIMBS; "big")]
101-
fn test_happy_case_from_bytes(bytes: &[u8], expected_limbs: [u128; 2]) {
93+
#[test_case(&BIG_NUMBER_BYTES, BIG_NUMBER_LIMBS; "big")]
94+
fn test_happy_case_from_bytes(bytes: &[u8; 32], expected_limbs: [u128; 2]) {
10295
let result = CairoU256::from_bytes(bytes);
10396

10497
assert_eq!([result.low, result.high], expected_limbs);
10598
}
10699

107-
#[should_panic(expected = "range end index 32 out of range for slice of length 4")]
108-
#[test]
109-
fn test_from_bytes_input_too_short() {
110-
let _result = CairoU256::from_bytes(&[2, 1, 3, 7]);
111-
}
112-
113-
#[test]
114-
fn test_happy_case_from_bytes_longer_input() {
115-
let result = CairoU256::from_bytes(&TOO_BIG_NUMBER_BYTES);
116-
assert_eq!([result.low, result.high], BIG_NUMBER_LIMBS);
117-
}
118-
119100
#[test_case("0x0", [0, 0] ; "zero_hex")]
120101
#[test_case("0", [0, 0] ; "zero_dec")]
121102
#[test_case("0x237abc", [2_325_180, 0] ; "small_hex")]

0 commit comments

Comments
 (0)