From 8d51fa233e594b84b5b6e311a872978d60768076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Men=C3=A9ndez?= Date: Wed, 15 Jan 2025 09:11:01 +0100 Subject: [PATCH] remove the LE address return from address derivation primitive and fix test struct typo --- emulated/ecdsa/address.go | 19 +++++++------------ emulated/ecdsa/address_test.go | 13 ++++--------- testutil/utils.go | 6 +++--- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/emulated/ecdsa/address.go b/emulated/ecdsa/address.go index 3120a11..a179114 100644 --- a/emulated/ecdsa/address.go +++ b/emulated/ecdsa/address.go @@ -10,24 +10,23 @@ import ( // DeriveAddress derives an Ethereum address from a public key over Secp256k1 // by hashing the public key with Keccak256 and returning the last 20 bytes of -// the hash as an address into a variable. It also returns the address with the -// bytes in little-endian order. -func DeriveAddress(api frontend.API, pubKey ecdsa.PublicKey[emulated.Secp256k1Fp, emulated.Secp256k1Fr]) (frontend.Variable, frontend.Variable, error) { +// the hash as an address into a variable. +func DeriveAddress(api frontend.API, pubKey ecdsa.PublicKey[emulated.Secp256k1Fp, emulated.Secp256k1Fr]) (frontend.Variable, error) { // convert public key coords to uint8 and concatenate them xBytes, err := utils.ElemToU8(api, pubKey.X) if err != nil { - return 0, 0, err + return 0, err } yBytes, err := utils.ElemToU8(api, pubKey.Y) if err != nil { - return 0, 0, err + return 0, err } // swap endianness of the bytes and concatenate them pubBytes := append(utils.SwapEndianness(xBytes), utils.SwapEndianness(yBytes)...) // hash the public key keccak, err := sha3.NewLegacyKeccak256(api) if err != nil { - return 0, 0, err + return 0, err } keccak.Write(pubBytes) hash := keccak.Sum() @@ -35,11 +34,7 @@ func DeriveAddress(api frontend.API, pubKey ecdsa.PublicKey[emulated.Secp256k1Fp addrBytes := hash[12:] addr, err := utils.U8ToVar(api, addrBytes) if err != nil { - return 0, 0, err + return 0, err } - addrLE, err := utils.U8ToVar(api, utils.SwapEndianness(addrBytes)) - if err != nil { - return 0, 0, err - } - return addr, addrLE, nil + return addr, nil } diff --git a/emulated/ecdsa/address_test.go b/emulated/ecdsa/address_test.go index 7de4de4..e7d19c8 100644 --- a/emulated/ecdsa/address_test.go +++ b/emulated/ecdsa/address_test.go @@ -2,7 +2,6 @@ package ecdsa import ( "fmt" - "math/big" "testing" "time" @@ -20,18 +19,16 @@ import ( ) type testAddressCircuit struct { - Address frontend.Variable `gnark:",public"` - AddressLittleEndian frontend.Variable `gnark:",public"` - PublicKey gecdsa.PublicKey[emulated.Secp256k1Fp, emulated.Secp256k1Fr] + Address frontend.Variable `gnark:",public"` + PublicKey gecdsa.PublicKey[emulated.Secp256k1Fp, emulated.Secp256k1Fr] } func (c *testAddressCircuit) Define(api frontend.API) error { - addr, addrLE, err := DeriveAddress(api, c.PublicKey) + addr, err := DeriveAddress(api, c.PublicKey) if err != nil { return err } api.AssertIsEqual(c.Address, addr) - api.AssertIsEqual(c.AddressLittleEndian, addrLE) return nil } @@ -56,11 +53,9 @@ func TestAddressDerivation(t *testing.T) { input := crypto.Keccak256Hash([]byte("hello")).Bytes() testSig, err := testutil.GenerateAccountAndSign(input) c.Assert(err, qt.IsNil) - addrLE := new(big.Int).SetBytes(goSwapEndianness(testSig.Address.Bytes())) // init inputs witness := testAddressCircuit{ - Address: testSig.Address, - AddressLittleEndian: addrLE, + Address: testSig.Address, PublicKey: gecdsa.PublicKey[emulated.Secp256k1Fp, emulated.Secp256k1Fr]{ X: emulated.ValueOf[emulated.Secp256k1Fp](testSig.PublicKey.X), Y: emulated.ValueOf[emulated.Secp256k1Fp](testSig.PublicKey.Y), diff --git a/testutil/utils.go b/testutil/utils.go index 9387ea6..a533f1c 100644 --- a/testutil/utils.go +++ b/testutil/utils.go @@ -25,7 +25,7 @@ type CensusTestConfig struct { TotalSiblings int KeyLen int Hash arbotree.HashFunction - BaseFiled *big.Int + BaseField *big.Int } // TestCensusProofs is a structure to store the key, value, and siblings of a @@ -77,14 +77,14 @@ func GenerateCensusProofForTest(conf CensusTestConfig, ks, vs [][]byte) (*TestCe } // add the key-value pairs for i, k := range ks { - k = arbotree.BigToFF(conf.BaseFiled, new(big.Int).SetBytes(k)).Bytes() + k = arbotree.BigToFF(conf.BaseField, new(big.Int).SetBytes(k)).Bytes() if err = tree.Add(k, vs[i]); err != nil { return nil, err } } // add random addresses for i := 1; i < conf.ValidSiblings; i++ { - rk := arbotree.BigToFF(conf.BaseFiled, new(big.Int).SetBytes(util.RandomBytes(conf.KeyLen))).Bytes() + rk := arbotree.BigToFF(conf.BaseField, new(big.Int).SetBytes(util.RandomBytes(conf.KeyLen))).Bytes() rv := new(big.Int).SetBytes(util.RandomBytes(8)).Bytes() if err = tree.Add(rk, rv); err != nil { return nil, err