Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta committed Mar 3, 2023
1 parent 86249fc commit 916f699
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/vm/runners/builtin_runner/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl KeccakBuiltinRunner {
.sub_usize(index)
.map_err(|_| RunnerError::KeccakNoFirstInput)?;

let mut input_felts_u64 = vec![];
let mut input_felts = vec![];

for i in 0..self.n_input_cells as usize {
let val = match memory.get(&(first_input_addr + i)) {
Expand All @@ -106,34 +106,15 @@ impl KeccakBuiltinRunner {
_ => return Ok(None),
};

input_felts_u64.push(val)
input_felts.push(val)
}

let input_message: Vec<u8> = input_felts_u64
let input_message: Vec<u8> = input_felts
.iter()
.flat_map(|x| Self::right_pad(&mut x.to_biguint().to_bytes_le(), KECCAK_FELT_BYTE_SIZE))
.flat_map(|x| Self::right_pad(&x.to_biguint().to_bytes_le(), KECCAK_FELT_BYTE_SIZE))
.collect();
// keccak_f start:
let bigint = BigUint::from_bytes_le(&input_message);
let mut keccak_input = vec![];
let w = 64;
for i in 0..25 {
keccak_input.push(
((&bigint >> (i * w)) & (BigUint::from(2_u8).pow(w) - BigUint::one()))
.to_u64()
.ok_or(RunnerError::KeccakInputCellsNotU64)?,
)
}
// This unwrap wont fail as the vec is 25 elements long // use from_fn here
let mut keccak_input: [u64; 25] = keccak_input.try_into().unwrap();
keccak::f1600(&mut keccak_input);
let keccak_result = keccak_input
.iter()
.enumerate()
.map(|(i, x)| BigUint::from(*x) << (i * w as usize))
.sum::<BigUint>()
.to_bytes_le();
// keccak_f end
let keccak_result = Self::keccak_f(&input_message)?;

let mut start_index = 0_usize;
for (i, bits) in self.state_rep.iter().enumerate() {
let end_index = start_index + *bits as usize / 8;
Expand Down Expand Up @@ -279,6 +260,28 @@ impl KeccakBuiltinRunner {
bytes_vector.extend(zeros);
bytes_vector
}

fn keccak_f(input_message: &[u8]) -> Result<Vec<u8>, RunnerError> {
let bigint = BigUint::from_bytes_le(input_message);
let mut keccak_input = vec![];
let w = 64;
for i in 0..25 {
keccak_input.push(
((&bigint >> (i * w)) & (BigUint::from(2_u8).pow(w) - BigUint::one()))
.to_u64()
.ok_or(RunnerError::KeccakInputCellsNotU64)?,
)
}
// This unwrap wont fail as the vec is 25 elements long // use from_fn here
let mut keccak_input: [u64; 25] = keccak_input.try_into().unwrap();
keccak::f1600(&mut keccak_input);
Ok(keccak_input
.iter()
.enumerate()
.map(|(i, x)| BigUint::from(*x) << (i * w as usize))
.sum::<BigUint>()
.to_bytes_le())
}
}

#[cfg(test)]
Expand Down

1 comment on commit 916f699

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 916f699 Previous: d5a7bea Ratio
cairo_run(cairo_programs/benchmarks/compare_arrays_200000.json) 837288484 ns/iter (± 2445724) 637231489 ns/iter (± 1969874) 1.31
cairo_run(cairo_programs/benchmarks/fibonacci_1000_multirun.json) 133572782 ns/iter (± 342423) 101045574 ns/iter (± 351582) 1.32
cairo_run(cairo_programs/benchmarks/linear_search.json) 104949653 ns/iter (± 445960) 79573059 ns/iter (± 5279566) 1.32
cairo_run(cairo_programs/benchmarks/keccak_integration_benchmark.json) 1618505812 ns/iter (± 11228437) 1240800087 ns/iter (± 8607617) 1.30
cairo_run(cairo_programs/benchmarks/secp_integration_benchmark.json) 1802293009 ns/iter (± 4275495) 1348564409 ns/iter (± 2660517) 1.34
cairo_run(cairo_programs/benchmarks/blake2s_integration_benchmark.json) 1420794440 ns/iter (± 8543285) 1082080349 ns/iter (± 8505744) 1.31
cairo_run(cairo_programs/benchmarks/uint256_integration_benchmark.json) 1423697490 ns/iter (± 6546642) 1081656715 ns/iter (± 3755716) 1.32

This comment was automatically generated by workflow using github-action-benchmark.

CC: @unbalancedparentheses

Please sign in to comment.