From 47cecb52ccc8837c71d6a463997fdf414dd745ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= <128649481+neotheprogramist@users.noreply.github.com> Date: Mon, 21 Aug 2023 11:11:45 +0200 Subject: [PATCH] fix shift bug in sha256 (#164) --- src/math/src/sha256.cairo | 20 +++++++------- src/math/src/tests/sha256_test.cairo | 41 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/math/src/sha256.cairo b/src/math/src/sha256.cairo index c9054a5a..bd6aa985 100644 --- a/src/math/src/sha256.cairo +++ b/src/math/src/sha256.cairo @@ -63,19 +63,19 @@ fn sha256(mut data: Array) -> Array { }; // add length to the end - let mut res = (data_len & 0xff00000000000000) / 0xffffffffffffff; + let mut res = (data_len & 0xff00000000000000) / 0x100000000000000; data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff000000000000) / 0xffffffffffff; + res = (data_len.into() & 0xff000000000000) / 0x1000000000000; data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff0000000000) / 0xffffffffff; + res = (data_len.into() & 0xff0000000000) / 0x10000000000; data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff00000000) / 0xffffffff; + res = (data_len.into() & 0xff00000000) / 0x100000000; data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff000000) / 0xffffff; + res = (data_len.into() & 0xff000000) / 0x1000000; data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff0000) / 0xffff; + res = (data_len.into() & 0xff0000) / 0x10000; data.append(res.try_into().unwrap()); - res = (data_len.into() & 0xff00) / 0xff; + res = (data_len.into() & 0xff00) / 0x100; data.append(res.try_into().unwrap()); res = data_len.into() & 0xff; data.append(res.try_into().unwrap()); @@ -93,11 +93,11 @@ fn from_u32Array_to_u8Array(mut data: Span) -> Array { loop { match data.pop_front() { Option::Some(val) => { - let mut res = (*val & 0xff000000) / 0xffffff; + let mut res = (*val & 0xff000000) / 0x1000000; result.append(res.try_into().unwrap()); - res = (*val & 0xff0000) / 0xffff; + res = (*val & 0xff0000) / 0x10000; result.append(res.try_into().unwrap()); - res = (*val & 0xff00) / 0xff; + res = (*val & 0xff00) / 0x100; result.append(res.try_into().unwrap()); res = *val & 0xff; result.append(res.try_into().unwrap()); diff --git a/src/math/src/tests/sha256_test.cairo b/src/math/src/tests/sha256_test.cairo index 91c8e68a..48237a5f 100644 --- a/src/math/src/tests/sha256_test.cairo +++ b/src/math/src/tests/sha256_test.cairo @@ -43,6 +43,47 @@ fn sha256_empty_test() { assert(*result[31] == 0x55, 'invalid result'); } +#[test] +#[available_gas(200000000000)] +fn sha256_random_data_test() { + let mut input: Array = array![ + 0x57, 0x77, 0x71, 0x71, 0x66, 0x50, 0x45, 0x51, 0x51, 0x43, 0x39, 0x48, 0x38 + ]; + let result = sha256::sha256(input); + assert(*result[0] == 61, 'invalid result'); + assert(*result[1] == 226, 'invalid result'); + assert(*result[2] == 188, 'invalid result'); + assert(*result[3] == 242, 'invalid result'); + assert(*result[4] == 118, 'invalid result'); + assert(*result[5] == 121, 'invalid result'); + assert(*result[6] == 7, 'invalid result'); + assert(*result[7] == 225, 'invalid result'); + assert(*result[8] == 150, 'invalid result'); + assert(*result[9] == 220, 'invalid result'); + assert(*result[10] == 105, 'invalid result'); + assert(*result[11] == 158, 'invalid result'); + assert(*result[12] == 185, 'invalid result'); + assert(*result[13] == 180, 'invalid result'); + assert(*result[14] == 139, 'invalid result'); + assert(*result[15] == 103, 'invalid result'); + assert(*result[16] == 221, 'invalid result'); + assert(*result[17] == 95, 'invalid result'); + assert(*result[18] == 56, 'invalid result'); + assert(*result[19] == 88, 'invalid result'); + assert(*result[20] == 209, 'invalid result'); + assert(*result[21] == 159, 'invalid result'); + assert(*result[22] == 255, 'invalid result'); + assert(*result[23] == 247, 'invalid result'); + assert(*result[24] == 145, 'invalid result'); + assert(*result[25] == 146, 'invalid result'); + assert(*result[26] == 83, 'invalid result'); + assert(*result[27] == 110, 'invalid result'); + assert(*result[28] == 185, 'invalid result'); + assert(*result[29] == 5, 'invalid result'); + assert(*result[30] == 248, 'invalid result'); + assert(*result[31] == 15, 'invalid result'); +} + #[test] #[available_gas(2000000000)] fn sha256_lorem_ipsum_test() {