Skip to content

Commit

Permalink
perf: refactor modular add (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: j1mbo64 <[email protected]>
  • Loading branch information
j1mbo64 and j1mbo64 authored Oct 23, 2024
1 parent b56e8f3 commit 833e07d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cairo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v4
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.7.0"
scarb-version: "2.8.4"
- run: scarb fmt --check
- run: scarb build
- run: scarb test
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
name = "ripemd160"
description = "RIPEMD-160 hash function"
version = "0.1.0"
cairo-version = "2.7.0"
cairo-version = "2.8.4"
edition = "2024_07"
repository = "https://github.com/j1mbo64/ripemd160_cairo"
keywords = ["hash", "crypto"]

[dev-dependencies]
cairo_test = "2.7.0"
cairo_test = "2.8.4"
2 changes: 2 additions & 0 deletions src/ripemd160.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ fn ripemd160_process_block(ref ctx: RIPEMD160Context, block: @Array<u32>) {
l5(ref lh2, lh3, ref lh4, lh0, lh1, *block.at(15), 5);
l5(ref lh1, lh2, ref lh3, lh4, lh0, *block.at(13), 6);

// Ensure calculation of `left` is kept as local and not as temporary when compiling Sierra to
// CASM with `inlining-strategy = "avoid`.
core::internal::revoke_ap_tracking();

// Right round 1
Expand Down
24 changes: 14 additions & 10 deletions src/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use core::num::traits::OverflowingAdd;
use core::num::traits::OverflowingMul;

pub(crate) const POW_2_32: u64 = 0x100000000;
pub(crate) const POW_2_8: u32 = 256;

Expand Down Expand Up @@ -40,25 +43,26 @@ pub(crate) fn get_pow_2(n: u32) -> u32 {
}

pub(crate) fn u32_mod_add(a: u32, b: u32) -> u32 {
let a: u64 = a.into();
let b: u64 = b.into();
((a + b) % POW_2_32).try_into().unwrap()
let (res, _) = a.overflowing_add(b);
res
}

pub(crate) fn u32_mod_add_3(a: u32, b: u32, c: u32) -> u32 {
let result: u64 = (a.into() + b.into() + c.into()) % POW_2_32;
result.try_into().unwrap()
let (res, _) = a.overflowing_add(b);
let (res, _) = res.overflowing_add(c);
res
}

pub(crate) fn u32_mod_add_4(a: u32, b: u32, c: u32, d: u32) -> u32 {
let result: u64 = (a.into() + b.into() + c.into() + d.into()) % POW_2_32;
result.try_into().unwrap()
let (res, _) = a.overflowing_add(b);
let (res, _) = res.overflowing_add(c);
let (res, _) = res.overflowing_add(d);
res
}

pub(crate) fn u32_mod_mul(a: u32, b: u32) -> u32 {
let a: u64 = a.into();
let b: u64 = b.into();
((a * b) % POW_2_32).try_into().unwrap()
let (res, _) = a.overflowing_mul(b);
res
}

pub(crate) fn u32_leftrotate(x: u32, n: u32) -> u32 {
Expand Down

0 comments on commit 833e07d

Please sign in to comment.