Skip to content

Commit

Permalink
refactor: impl test for empty inputs, update procedure docs, fix typo…
Browse files Browse the repository at this point in the history
… in changelog
  • Loading branch information
Fumuran committed Oct 1, 2024
1 parent 1393698 commit 4cf59b6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
#### Stdlib

- Added `init_no_padding` procedure to `std::crypto::hashes::native` (#1313).
- [BREAKING] `native` module was renamed to the `pro`, `hash_memory` procedure was renamed to the `hash_memory_words` (#1368).
- [BREAKING] `native` module was renamed to the `rpo`, `hash_memory` procedure was renamed to the `hash_memory_words` (#1368).
- Added `hash_memory` procedure to `std::crypto::hashes::rpo` (#1368).

#### VM Internals
Expand Down
30 changes: 17 additions & 13 deletions stdlib/asm/crypto/hashes/rpo.masm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#!
#! Input: []
#! Ouptut: [PERM, PERM, PERM, ...]
#!
#! Cycles: 12
export.init_no_padding
padw padw padw
Expand All @@ -14,7 +15,11 @@ end
#!
#! Input: [C, B, A, ...]
#! Ouptut: [HASH, ...]
#! where: For the native RPO hasher HASH is B.
#!
#! Where :
#! - `A` is the capacity word that will be used by the hashing function.
#! - `B` is the hash output.
#!
#! Cycles: 9
export.squeeze_digest
# drop the first rate word (4 cycles)
Expand All @@ -32,12 +37,14 @@ end
#! This requires that `end_addr=start_addr + 2n + 1`, otherwise the procedure will enter an infinite
#! loop. `end_addr` is not inclusive.
#!
#! Stack transition:
#! Input: [C, B, A, start_addr, end_addr, ...]
#! Output: [C', B', A', end_addr, end_addr ...]
#! Cycles: 4 + 3 * words, where `words` is the `start_addr - end_addr - 1`
#!
#! Where `A` is the capacity word that will be used by the hashing function, and `B'` the hash output.
#! Where :
#! - `A` is the capacity word that will be used by the hashing function.
#! - `B` is the hash output.
#!
#! Cycles: 4 + 3 * words, where `words` is the `start_addr - end_addr - 1`
export.absorb_double_words_from_memory
dup.13 dup.13 neq # (4 cycles )
while.true
Expand All @@ -50,12 +57,13 @@ end
#!
#! Requires `start_addr < end_addr`, `end_addr` is not inclusive.
#!
#! Stack transition:
#! Input: [start_addr, end_addr, ...]
#! Output: [H, ...]
#!
#! Cycles:
#! even words: 49 cycles + 3 * words
#! odd words: 61 cycles + 3 * words
#! - even words: 49 cycles + 3 * words
#! - odd words: 61 cycles + 3 * words
#! where `words` is the `start_addr - end_addr - 1`
export.hash_memory_words
# enforce `start_addr < end_addr`
dup.1 dup.1 u32assert2 u32gt assert
Expand Down Expand Up @@ -109,20 +117,16 @@ end
#!
#! Inputs: [ptr, num_elements]
#! Outputs: [HASH]
#!
#! Cycles:
#! - If number of elements divides by 8: 47 cycles + 3 * words
#! - Else: 180 cycles + 3 * words
#!
#! Panics if number of inputs equals 0.
#! where `words` is the number of quads of input values.
export.hash_memory
# move number of inputs to the top of the stack
swap
# => [num_elements, ptr]

# check that number of inputs greater than 0
dup eq.0 assertz
# => [num_elements, ptr]

# get the number of double words
u32divmod.8 swap
# => [num_elements/8, num_elements%8, ptr]
Expand Down
4 changes: 2 additions & 2 deletions stdlib/tests/crypto/rpo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ fn test_hash_memory() {
}

#[test]
fn test_hash_memory_fail() {
fn test_hash_memory_empty() {
// try to hash 0 values
let compute_inputs_hash = "
use.std::crypto::hashes::rpo
Expand All @@ -365,5 +365,5 @@ fn test_hash_memory_fail() {
end
";

assert!(build_test!(compute_inputs_hash, &[]).execute().is_err());
build_test!(compute_inputs_hash, &[]).expect_stack(&[0; 16]);
}

0 comments on commit 4cf59b6

Please sign in to comment.