From 570fce4ae3331ff635c2e0fd9091144eca7f8bcb Mon Sep 17 00:00:00 2001 From: arnaucube Date: Sun, 31 Oct 2021 14:42:56 +0100 Subject: [PATCH] Add Iota go impl --- go-keccak256-bits-impl/ref.go | 6 ++++++ go-keccak256-bits-impl/stepmappings.go | 19 +++++++++++++++++++ go-keccak256-bits-impl/stepmappings_test.go | 15 +++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/go-keccak256-bits-impl/ref.go b/go-keccak256-bits-impl/ref.go index f0ee5eb..2f56b20 100644 --- a/go-keccak256-bits-impl/ref.go +++ b/go-keccak256-bits-impl/ref.go @@ -135,3 +135,9 @@ func chiU64Version(a [25]uint64) [25]uint64 { a[24] ^= (^bc0) & bc1 return a } + +func iotU64Version(a [25]uint64, r int) [25]uint64 { + // iota + a[0] ^= roundConstantsU64[r] + return a +} diff --git a/go-keccak256-bits-impl/stepmappings.go b/go-keccak256-bits-impl/stepmappings.go index 73375f6..f825dd4 100644 --- a/go-keccak256-bits-impl/stepmappings.go +++ b/go-keccak256-bits-impl/stepmappings.go @@ -1,5 +1,18 @@ package keccak +var roundConstantsU64 = []uint64{ + 0x0000000000000001, 0x0000000000008082, 0x800000000000808A, + 0x8000000080008000, 0x000000000000808B, 0x0000000080000001, + 0x8000000080008081, 0x8000000000008009, 0x000000000000008A, + 0x0000000000000088, 0x0000000080008009, 0x000000008000000A, + 0x000000008000808B, 0x800000000000008B, 0x8000000000008089, + 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, + 0x000000000000800A, 0x800000008000000A, 0x8000000080008081, + 0x8000000000008080, 0x0000000080000001, 0x8000000080008008, +} + +var roundConstants = u64ArrayToBits(roundConstantsU64) + func theta(a [25 * 64]bool) [25 * 64]bool { var c0, c1, c2, c3, c4, d [64]bool var r [25 * 64]bool @@ -216,3 +229,9 @@ func chi(a [25 * 64]bool) [25 * 64]bool { copy(r[24*64:25*64], xor(a[24*64:25*64], and(xorSingle(c0[:]), c1[:]))) return r } + +func iot(a [25 * 64]bool, r int) [25 * 64]bool { + // iota + copy(a[0:64], xor(a[0:64], roundConstants[r*64:r*64+64])) + return a +} diff --git a/go-keccak256-bits-impl/stepmappings_test.go b/go-keccak256-bits-impl/stepmappings_test.go index df690a0..4e367bf 100644 --- a/go-keccak256-bits-impl/stepmappings_test.go +++ b/go-keccak256-bits-impl/stepmappings_test.go @@ -81,3 +81,18 @@ func TestChi(t *testing.T) { sU64 = chiU64Version(sU64) qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:]) } + +func TestIota(t *testing.T) { + s, sU64 := newS() + + s = iot(s, 3) + sU64 = iotU64Version(sU64, 3) + + qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:]) + qt.Assert(t, bitsToU64(s[0:64]), qt.Equals, uint64(9223372039002292224)) + + // compute again theta on the current state + s = iot(s, 10) + sU64 = iotU64Version(sU64, 10) + qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:]) +}