From f48be088146911fd52997933f19112db695d28d9 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 23 Jul 2024 21:24:16 +0300 Subject: [PATCH] Remove returned error from crypto.StarknetKeccak function (#1963) --- adapters/p2p2core/class.go | 5 +---- adapters/sn2core/sn2core.go | 6 +----- core/crypto/keccak.go | 10 +++++++--- core/crypto/keccak_test.go | 5 +---- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/adapters/p2p2core/class.go b/adapters/p2p2core/class.go index 8094a03006..40c5a7bc51 100644 --- a/adapters/p2p2core/class.go +++ b/adapters/p2p2core/class.go @@ -35,10 +35,7 @@ func AdaptClass(class *spec.Class) core.Class { } case *spec.Class_Cairo1: cairo1 := cls.Cairo1 - abiHash, err := crypto.StarknetKeccak([]byte(cairo1.Abi)) - if err != nil { - panic(err) - } + abiHash := crypto.StarknetKeccak([]byte(cairo1.Abi)) program := utils.Map(cairo1.Program, AdaptFelt) compiled, err := createCompiledClass(cairo1) diff --git a/adapters/sn2core/sn2core.go b/adapters/sn2core/sn2core.go index 326a643a3b..27b774cf6a 100644 --- a/adapters/sn2core/sn2core.go +++ b/adapters/sn2core/sn2core.go @@ -256,15 +256,11 @@ func AdaptCairo1Class(response *starknet.SierraDefinition, compiledClass *starkn class.ProgramHash = crypto.PoseidonArray(class.Program...) class.Abi = response.Abi - class.AbiHash, err = crypto.StarknetKeccak([]byte(class.Abi)) - if err != nil { - return nil, err - } + class.AbiHash = crypto.StarknetKeccak([]byte(class.Abi)) adapt := func(ep starknet.SierraEntryPoint) core.SierraEntryPoint { return core.SierraEntryPoint{Index: ep.Index, Selector: ep.Selector} } - class.EntryPoints.External = utils.Map(utils.NonNilSlice(response.EntryPoints.External), adapt) class.EntryPoints.L1Handler = utils.Map(utils.NonNilSlice(response.EntryPoints.L1Handler), adapt) class.EntryPoints.Constructor = utils.Map(utils.NonNilSlice(response.EntryPoints.Constructor), adapt) diff --git a/core/crypto/keccak.go b/core/crypto/keccak.go index 8bded1232b..5e2554d91a 100644 --- a/core/crypto/keccak.go +++ b/core/crypto/keccak.go @@ -1,6 +1,8 @@ package crypto import ( + "fmt" + "github.com/NethermindEth/juno/core/felt" "golang.org/x/crypto/sha3" ) @@ -8,14 +10,16 @@ import ( // StarknetKeccak implements [Starknet keccak] // // [Starknet keccak]: https://docs.starknet.io/architecture-and-concepts/cryptography/hash-functions/#starknet_keccak -func StarknetKeccak(b []byte) (*felt.Felt, error) { +func StarknetKeccak(b []byte) *felt.Felt { h := sha3.NewLegacyKeccak256() _, err := h.Write(b) if err != nil { - return nil, err + // actual implementation (sha3.state{} type) doesn't return error in Write method + // we keep this panic as an assertion in case they will modify the implementation + panic(fmt.Errorf("failed to write to LegacyKeccak256 hash: %w", err)) } d := h.Sum(nil) // Remove the first 6 bits from the first byte d[0] &= 3 - return new(felt.Felt).SetBytes(d), nil + return new(felt.Felt).SetBytes(d) } diff --git a/core/crypto/keccak_test.go b/core/crypto/keccak_test.go index 39dc24a0e7..5b5d7cbccc 100644 --- a/core/crypto/keccak_test.go +++ b/core/crypto/keccak_test.go @@ -6,7 +6,6 @@ import ( "github.com/NethermindEth/juno/core/crypto" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestStarknetKeccak(t *testing.T) { @@ -22,11 +21,9 @@ func TestStarknetKeccak(t *testing.T) { t.Parallel() for _, test := range tests { - test := test t.Run(test.input, func(t *testing.T) { t.Parallel() - d, err := crypto.StarknetKeccak([]byte(test.input)) - require.NoError(t, err) + d := crypto.StarknetKeccak([]byte(test.input)) got := fmt.Sprintf("%x", d.Bytes()) assert.Equal(t, test.want, got)