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

feat(stdlib): Add keccak #1249

Merged
merged 10 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
31 changes: 30 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ termcolor = "1.1.2"
color-eyre = "0.6.2"

# Backends
acvm-backend-barretenberg = { git = "https://github.com/noir-lang/aztec_backend", rev = "c9fb9e806f1400a2ff7594a0669bec56025220bb", default-features=false }
phated marked this conversation as resolved.
Show resolved Hide resolved
acvm-backend-barretenberg = { git = "https://github.com/noir-lang/aztec_backend", rev = "ce2b9ed456bd8d2ad8357c15736d62c2a5812add", default-features = false }

[dev-dependencies]
tempdir = "0.3.7"
Expand All @@ -50,4 +50,3 @@ default = ["plonk_bn254"]
# The plonk backend can only use bn254, so we do not specify the field
plonk_bn254 = ["acvm-backend-barretenberg/native"]
plonk_bn254_wasm = ["acvm-backend-barretenberg/wasm"]

5 changes: 5 additions & 0 deletions crates/nargo_cli/tests/test_data/keccak256/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
35 changes: 35 additions & 0 deletions crates/nargo_cli/tests/test_data/keccak256/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
x = 0xbd
result = [
0x5a,
0x50,
0x2f,
0x9f,
0xca,
0x46,
0x7b,
0x26,
0x6d,
0x5b,
0x78,
0x33,
0x65,
0x19,
0x37,
0xe8,
0x05,
0x27,
0x0c,
0xa3,
0xf3,
0xaf,
0x1c,
0x0d,
0xd2,
0x46,
0x2d,
0xca,
0x4b,
0x3b,
0x1a,
0xbf,
]
10 changes: 10 additions & 0 deletions crates/nargo_cli/tests/test_data/keccak256/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Keccak256 example
//
use dep::std;

fn main(x: Field, result: [u8; 32]) {
// We use the `as` keyword here to denote the fact that we want to take just the first byte from the x Field
// The padding is taken care of by the program
let digest = std::hash::keccak256([x as u8]);
assert(digest == result);
}
9 changes: 2 additions & 7 deletions crates/noirc_evaluator/src/ssa/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Opcode {
match op {
// Pointers do not overflow
BlackBoxFunc::SHA256
| BlackBoxFunc::Keccak256
| BlackBoxFunc::Blake2s
| BlackBoxFunc::Pedersen
| BlackBoxFunc::FixedBaseScalarMul => BigUint::zero(),
Expand All @@ -84,9 +85,6 @@ impl Opcode {
BlackBoxFunc::AES => {
todo!("ICE: AES is unimplemented")
}
BlackBoxFunc::Keccak256 => {
todo!("ICE: Keccak256 is unimplemented")
}
BlackBoxFunc::RANGE | BlackBoxFunc::AND | BlackBoxFunc::XOR => {
unimplemented!("ICE: these opcodes do not have Noir builtin functions")
}
Expand All @@ -105,10 +103,7 @@ impl Opcode {
Opcode::LowLevel(op) => {
match op {
BlackBoxFunc::AES => todo!("ICE: AES is unimplemented"),
BlackBoxFunc::Keccak256 => {
todo!("ICE: Keccak256 is unimplemented")
}
BlackBoxFunc::SHA256 | BlackBoxFunc::Blake2s => {
BlackBoxFunc::SHA256 | BlackBoxFunc::Blake2s | BlackBoxFunc::Keccak256 => {
(32, ObjectType::unsigned_integer(8))
}
BlackBoxFunc::ComputeMerkleRoot | BlackBoxFunc::HashToField128Security => {
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions noir_stdlib/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ fn pedersen<N>(_input : [Field; N]) -> [Field; 2] {}
#[foreign(hash_to_field_128_security)]
fn hash_to_field<N>(_input : [Field; N]) -> Field {}

#[foreign(keccak256)]
fn keccak256<N>(_input : [u8; N]) -> [u8; 32] {}

// mimc-p/p implementation
// constants are (publicly generated) random numbers, for instance using keccak as a ROM.
// You must use constants generated for the native field
Expand Down