This repository has been archived by the owner on May 3, 2024. It is now read-only.
BLAKE2b hash function and BLAKE2b-512 compression function of EIP-152 #154
This run and associated checks have been archived and are scheduled for deletion.
Learn more about checks retention
openai-review.yml
on: pull_request
OpenAI PR Comment
18s
Annotations
2 errors
OpenAI PR Comment
Unable to process file command 'env' successfully.
|
OpenAI PR Comment
Invalid format 'GIT_PATCH_OUTPUT=From 9dbcce9c7575b8b2809954b28c3a8e58b7245899 Mon Sep 17 00:00:00 2001 From: AlekseiVambol <[email protected]> Date: Tue, 17 Jan 2023 15:07:32 +0200 Subject: [PATCH 01/30] Create Cargo.toml --- zkevm-circuits/src/blake2b/Cargo.toml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 zkevm-circuits/src/blake2b/Cargo.toml diff --git a/zkevm-circuits/src/blake2b/Cargo.toml b/zkevm-circuits/src/blake2b/Cargo.toml new file mode 100644 index 0000000000..05d9edac03 --- /dev/null +++ b/zkevm-circuits/src/blake2b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "blake2b" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +hex = "0.4.3" From 4e867dab2c7dbd7c3dd5121196010a145551c256 Mon Sep 17 00:00:00 2001 From: AlekseiVambol <[email protected]> Date: Tue, 17 Jan 2023 15:09:08 +0200 Subject: [PATCH 02/30] Add files via upload --- zkevm-circuits/src/blake2b/src/lib.rs | 178 +++++++++++++++++ .../src/blake2b/tests/integration.rs | 187 ++++++++++++++++++ 2 files changed, 365 insertions(+) create mode 100644 zkevm-circuits/src/blake2b/src/lib.rs create mode 100644 zkevm-circuits/src/blake2b/tests/integration.rs diff --git a/zkevm-circuits/src/blake2b/src/lib.rs b/zkevm-circuits/src/blake2b/src/lib.rs new file mode 100644 index 0000000000..cc278198b9 --- /dev/null +++ b/zkevm-circuits/src/blake2b/src/lib.rs @@ -0,0 +1,178 @@ +// Safe Rust implementation of the BLAKE2b hash function (as "digest") in accordance +// with RFC-7693 [1] and BLAKE2b-512 compression function (as "contract") described in +// EIP-152 [2]. The API and implementation details can be understood using only [1] for +// all functions except for "contract". Understanding this one also requires using [2]. + +// References +// [1] https://www.rfc-editor.org/rfc/rfc7693 +// [2] https://eips.ethereum.org/EIPS/eip-152 + +use core::panic; + +const IV: [u64; 8] = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, + 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, + 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]; + +const SIGMA:[[usize; 16]; 10] = [ + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], + [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3], + [11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4], + [7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8], + [9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13], + [2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9], + [12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11], + [13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10], + [6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5], + [10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0]]; + +fn g(v: &mut [u64; 16], a: usize, b: usize, c: usize, d: usize, x: u64, y: u64) { + v[a] = v[a].wrapping_add(v[b]).wrapping_add(x); + v[d] = (v[d] ^ v[a]).rotate_right(32); + + v[c] = v[c].wrapping_add(v[d]); + v[b] = (v[b] ^ v[c]).rotate_right(24); + + v[a] = v[a].wrapping_add(v[b]).wrapping_add(y); + v[d] = (v[d] ^ v[a]).rotate_right(16); + + v[c] = v[c].wrapping_add(v[d]); + v[b] = (v[b] ^ v[c]).rotate_right(63); +} + +fn compress(h: &mut [u64; 8], m: &[u64; 16], t: u128, f: bool, r: u32) { + let mut v = [0u64; 16]; + + for i in 0..8 { + v[i] = h[i]; + v[i + 8] = IV[i]; + } + + v[12] ^= t as u64; + v[13] ^= (t >> 64) as u64; + + if f { v[14] ^= 0xFFFF_FFFF_FFFF_FFFF; } + + for i in 0..(r as usize) { + let s = &SIGMA[i % 10]; + + g(&mut v, 0, 4, 8, 12, m[s[0]], m[s[1]]); + g(&mut v, 1, 5, 9, 13, m[s[2]], m[s[3]]); + g(&mut v, 2, 6, 10, 14, m[s[4]], m[s[5]]); + g(&mut v, 3, 7, 11, 15, m[s[6]], m[s[7]]); + + g(&mut v, 0, 5, 10, 15, m[s[8]], m[s[9]]); + g(&mut v, 1, 6, 11, 12, m[s[10]], m[s[11]]); + g(&mut v, 2, 7, 8, 13, m[s[12]], m[s[13]]); + g(&mut v, 3, 4, 9, 14, m[s[14]], m[s[15]]); + } + + for i in 0..8 { + h[i] ^= v[i] ^ v[i + 8]; + } +} + +fn load_bytes(d: &mut [u8], s: &[u64]) { + if s.len() CONTRIBUTING.md Cargo.lock Cargo.toml Makefi
|