From 260d87d1ef86069a1fcf0f9b4969589273e381d1 Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Tue, 9 May 2023 02:28:17 +0200 Subject: [PATCH] feat(stdlib): Add keccak (#1249) * Add keccak in stdlib * add simple keccak example * use assert * update to latest aztec_backend * update barretenberg to e66f1ef38c3c87c223456d8a77878c2bd3d346eb * change sha256 to keccak256 * update flake.lock * update commit * update cargo.lock --------- Co-authored-by: Kevaundray Wedderburn --- Cargo.lock | 31 +++++++++++++++- crates/nargo_cli/Cargo.toml | 3 +- .../tests/test_data/keccak256/Nargo.toml | 5 +++ .../tests/test_data/keccak256/Prover.toml | 35 +++++++++++++++++++ .../tests/test_data/keccak256/src/main.nr | 10 ++++++ crates/noirc_evaluator/src/ssa/builtin.rs | 9 ++--- flake.lock | 6 ++-- noir_stdlib/src/hash.nr | 3 ++ 8 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 crates/nargo_cli/tests/test_data/keccak256/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data/keccak256/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data/keccak256/src/main.nr diff --git a/Cargo.lock b/Cargo.lock index b8647a9622d..6fe210ab93a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,7 +49,7 @@ dependencies = [ [[package]] name = "acvm-backend-barretenberg" version = "0.0.0" -source = "git+https://github.com/noir-lang/aztec_backend?rev=c9fb9e806f1400a2ff7594a0669bec56025220bb#c9fb9e806f1400a2ff7594a0669bec56025220bb" +source = "git+https://github.com/noir-lang/aztec_backend?rev=677f10e07011849f8aa0d75fe80390bb3081b1e5#677f10e07011849f8aa0d75fe80390bb3081b1e5" dependencies = [ "acvm", "barretenberg-sys", @@ -61,6 +61,7 @@ dependencies = [ "pkg-config", "reqwest", "rust-embed", + "sha3", "tokio", "wasmer", ] @@ -386,6 +387,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ + "block-padding", "generic-array", ] @@ -398,6 +400,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-padding" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" + [[package]] name = "bstr" version = "1.4.0" @@ -1676,6 +1684,15 @@ dependencies = [ "sha2 0.9.9", ] +[[package]] +name = "keccak" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940" +dependencies = [ + "cpufeatures", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -2820,6 +2837,18 @@ dependencies = [ "digest 0.10.6", ] +[[package]] +name = "sha3" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" +dependencies = [ + "block-buffer 0.9.0", + "digest 0.9.0", + "keccak", + "opaque-debug", +] + [[package]] name = "sharded-slab" version = "0.1.4" diff --git a/crates/nargo_cli/Cargo.toml b/crates/nargo_cli/Cargo.toml index 2568f2a86f5..6e8801301b1 100644 --- a/crates/nargo_cli/Cargo.toml +++ b/crates/nargo_cli/Cargo.toml @@ -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 } +acvm-backend-barretenberg = { git = "https://github.com/noir-lang/aztec_backend", rev = "677f10e07011849f8aa0d75fe80390bb3081b1e5", default-features = false } [dev-dependencies] tempdir = "0.3.7" @@ -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"] - diff --git a/crates/nargo_cli/tests/test_data/keccak256/Nargo.toml b/crates/nargo_cli/tests/test_data/keccak256/Nargo.toml new file mode 100644 index 00000000000..e0b467ce5da --- /dev/null +++ b/crates/nargo_cli/tests/test_data/keccak256/Nargo.toml @@ -0,0 +1,5 @@ +[package] +authors = [""] +compiler_version = "0.1" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data/keccak256/Prover.toml b/crates/nargo_cli/tests/test_data/keccak256/Prover.toml new file mode 100644 index 00000000000..d65c4011d3f --- /dev/null +++ b/crates/nargo_cli/tests/test_data/keccak256/Prover.toml @@ -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, +] diff --git a/crates/nargo_cli/tests/test_data/keccak256/src/main.nr b/crates/nargo_cli/tests/test_data/keccak256/src/main.nr new file mode 100644 index 00000000000..8fafbaa4e27 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/keccak256/src/main.nr @@ -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); +} diff --git a/crates/noirc_evaluator/src/ssa/builtin.rs b/crates/noirc_evaluator/src/ssa/builtin.rs index 8e402343bb5..4f3741583d8 100644 --- a/crates/noirc_evaluator/src/ssa/builtin.rs +++ b/crates/noirc_evaluator/src/ssa/builtin.rs @@ -73,6 +73,7 @@ impl Opcode { match op { // Pointers do not overflow BlackBoxFunc::SHA256 + | BlackBoxFunc::Keccak256 | BlackBoxFunc::Blake2s | BlackBoxFunc::Pedersen | BlackBoxFunc::FixedBaseScalarMul => BigUint::zero(), @@ -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") } @@ -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 => { diff --git a/flake.lock b/flake.lock index 6a6aff10f73..8d6445d3cf6 100644 --- a/flake.lock +++ b/flake.lock @@ -10,11 +10,11 @@ ] }, "locked": { - "lastModified": 1682626614, - "narHash": "sha256-TC535frlYLUTDZ2iHAtUtKpMJWngL1MFxzEXhOfeCo8=", + "lastModified": 1683314474, + "narHash": "sha256-gfHYpOnVTfS+4fhScBhfkB/e5z+jPFCi8zSy+aEh+8s=", "owner": "AztecProtocol", "repo": "barretenberg", - "rev": "7b5598890c1fa4ee485a4a0015fcb23b5389392e", + "rev": "ad615ee7dc931d3dbea041e47c96b9d8dccebf98", "type": "github" }, "original": { diff --git a/noir_stdlib/src/hash.nr b/noir_stdlib/src/hash.nr index 614918ae492..0618897d203 100644 --- a/noir_stdlib/src/hash.nr +++ b/noir_stdlib/src/hash.nr @@ -12,6 +12,9 @@ fn pedersen(_input : [Field; N]) -> [Field; 2] {} #[foreign(hash_to_field_128_security)] fn hash_to_field(_input : [Field; N]) -> Field {} +#[foreign(keccak256)] +fn keccak256(_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