Skip to content

Commit

Permalink
Remove returned error from crypto.StarknetKeccak function (#1963)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Jul 23, 2024
1 parent 0ea8fe1 commit f48be08
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
5 changes: 1 addition & 4 deletions adapters/p2p2core/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions core/crypto/keccak.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package crypto

import (
"fmt"

"github.com/NethermindEth/juno/core/felt"
"golang.org/x/crypto/sha3"
)

// 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)
}
5 changes: 1 addition & 4 deletions core/crypto/keccak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit f48be08

Please sign in to comment.