Skip to content

Commit

Permalink
fix shift bug in sha256 (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
neotheprogramist authored Aug 21, 2023
1 parent b519245 commit 47cecb5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/math/src/sha256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ fn sha256(mut data: Array<u8>) -> Array<u8> {
};

// 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());
Expand All @@ -93,11 +93,11 @@ fn from_u32Array_to_u8Array(mut data: Span<u32>) -> Array<u8> {
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());
Expand Down
41 changes: 41 additions & 0 deletions src/math/src/tests/sha256_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = 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() {
Expand Down

0 comments on commit 47cecb5

Please sign in to comment.