Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add hash_to_field_native function #3339

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
13 changes: 13 additions & 0 deletions noir_stdlib/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ pub fn pedersen_hash_with_separator<N>(_input : [Field; N], _separator : u32) ->

#[foreign(hash_to_field_128_security)]
pub fn hash_to_field<N>(_input : [Field; N]) -> Field {}
pub fn hash_to_field_native<N>(_input : [Field; N]) -> Field {
let mut inputs_as_bytes = [];

for i in 0..N {
let input_bytes = _input[i].to_le_bytes(32);
for i in 0..32 {
inputs_as_bytes = inputs_as_bytes.push_back(input_bytes[i]);
}
}

let hashed_input = blake2s(inputs_as_bytes);
crate::field::bytes32_to_field(hashed_input)
}

#[foreign(keccak256)]
pub fn keccak256<N>(_input : [u8; N], _message_size: u32) -> [u8; 32] {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use dep::std;

fn main(input : Field) -> pub Field {
let expected = std::hash::hash_to_field([input]);

let input_bytes = input.to_le_bytes(32);
let blake2s = std::hash::blake2s(input_bytes);
let got = dep::std::field::bytes32_to_field(blake2s);

let got = std::hash::hash_to_field_native([input]);
assert(expected == got);
expected
}