Skip to content

Commit

Permalink
remove the LE address return from address derivation primitive and fi…
Browse files Browse the repository at this point in the history
…x test struct typo
  • Loading branch information
lucasmenendez committed Jan 15, 2025
1 parent cf8bbfb commit 8d51fa2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
19 changes: 7 additions & 12 deletions emulated/ecdsa/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,31 @@ 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()
// return the last 20 bytes of the hash as an address
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
}
13 changes: 4 additions & 9 deletions emulated/ecdsa/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ecdsa

import (
"fmt"
"math/big"
"testing"
"time"

Expand All @@ -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
}

Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions testutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8d51fa2

Please sign in to comment.