Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

feat: Add Keccak Hash function #259

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions acvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ stdlib.workspace = true

blake2 = "0.9.1"
sha2 = "0.9.3"
sha3 = "0.9.1"
crc32fast = "1.3.2"
k256 = { version = "0.7.2", features = [
"ecdsa",
Expand Down
18 changes: 18 additions & 0 deletions acvm/src/pwg/hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use acir::{circuit::opcodes::BlackBoxFuncCall, native_types::Witness, FieldElement};
use blake2::{Blake2s, Digest};
use sha2::Sha256;
use sha3::Keccak256;
use std::collections::BTreeMap;

use crate::{OpcodeResolution, OpcodeResolutionError};
Expand Down Expand Up @@ -41,6 +42,23 @@ pub fn sha256(
Ok(OpcodeResolution::Solved)
}

pub fn keccak256(
initial_witness: &mut BTreeMap<Witness, FieldElement>,
func_call: &BlackBoxFuncCall,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
let hash = generic_hash_256::<Keccak256>(initial_witness, func_call)?;

for (output_witness, value) in func_call.outputs.iter().zip(hash.iter()) {
insert_value(
output_witness,
FieldElement::from_be_bytes_reduce(&[*value]),
initial_witness,
)?;
}

Ok(OpcodeResolution::Solved)
}

pub fn hash_to_field_128_security(
initial_witness: &mut BTreeMap<Witness, FieldElement>,
func_call: &BlackBoxFuncCall,
Expand Down