Skip to content

Commit

Permalink
Improve code semantic
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Aug 12, 2024
1 parent 5ea1f16 commit f9119ce
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 29 deletions.
2 changes: 0 additions & 2 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountType, c
if txn.Version == "" || txn.ResourceBounds == (rpc.ResourceBoundsMapping{}) || txn.Nonce == nil || txn.PayMasterData == nil {
return nil, ErrNotAllParametersSet
}
calldata := []*felt.Felt{txn.ClassHash, txn.ContractAddressSalt}
calldata = append(calldata, txn.ConstructorCalldata...) //nolint:all

txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
Expand Down
11 changes: 3 additions & 8 deletions contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ func UnmarshalCasmClass(filePath string) (*CasmClass, error) {
// Returns:
// - *felt.Felt: the precomputed address as a *felt.Felt
func PrecomputeAddress(deployerAddress *felt.Felt, salt *felt.Felt, classHash *felt.Felt, constructorCalldata []*felt.Felt) *felt.Felt {

feltArr := []*felt.Felt{
return curve.ComputeHashOnElementsFelt([]*felt.Felt{
PREFIX_CONTRACT_ADDRESS,
deployerAddress,
salt,
classHash,
}

constructorCallDataHash := curve.ComputeHashOnElementsFelt(constructorCalldata)
feltArr = append(feltArr, constructorCallDataHash)

return curve.ComputeHashOnElementsFelt(feltArr)
curve.ComputeHashOnElementsFelt(constructorCalldata),
})
}
8 changes: 4 additions & 4 deletions curve/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,12 @@ func (sc StarkCurve) SignFelt(msgHash, privKey *felt.Felt) (*felt.Felt, *felt.Fe
return xFelt, yFelt, nil
}

// HashElements calculates the hash of a list of elements using a golang Pedersen Hash.
// HashPedersenElements calculates the hash of a list of elements using a golang Pedersen Hash.
// Parameters:
// - elems: slice of big.Int pointers to be hashed
// Returns:
// - hash: The hash of the list of elements
func HashElements(elems []*big.Int) (hash *big.Int) {
func HashPedersenElements(elems []*big.Int) (hash *big.Int) {
feltArr := utils.BigIntArrToFeltArr(elems)
if len(elems) == 0 {
feltArr = append(feltArr, new(felt.Felt))
Expand All @@ -543,7 +543,7 @@ func HashElements(elems []*big.Int) (hash *big.Int) {

// ComputeHashOnElements computes the hash on the given elements using a golang Pedersen Hash implementation.
//
// The function appends the length of `elems` to the slice and then calls the `HashElements` method
// The function appends the length of `elems` to the slice and then calls the `HashPedersenElements` method
// passing in `elems` as an argument. The resulting hash is returned.
//
// Parameters:
Expand All @@ -552,7 +552,7 @@ func HashElements(elems []*big.Int) (hash *big.Int) {
// - hash: The hash of the list of elements
func ComputeHashOnElements(elems []*big.Int) (hash *big.Int) {
elems = append(elems, big.NewInt(int64(len(elems))))
return HashElements(elems)
return HashPedersenElements(elems)
}

// ComputeHashOnElementsFelt computes the hash on elements of a Felt array.
Expand Down
2 changes: 1 addition & 1 deletion curve/curve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func TestGeneral_ComputeHashOnElements(t *testing.T) {
//
// none
func TestGeneral_HashAndSign(t *testing.T) {
hashy := HashElements([]*big.Int{
hashy := HashPedersenElements([]*big.Int{
big.NewInt(1953658213),
big.NewInt(126947999705460),
big.NewInt(1953658213),
Expand Down
7 changes: 3 additions & 4 deletions merkle/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func NewFixedSizeMerkleTree(leaves ...*big.Int) *FixedSizeMerkleTree {
Leaves: leaves,
Branches: [][]*big.Int{},
}
root := mt.build(leaves)
mt.Root = root
mt.Root = mt.build(leaves)
return mt
}

Expand All @@ -41,9 +40,9 @@ func NewFixedSizeMerkleTree(leaves ...*big.Int) *FixedSizeMerkleTree {
// - *big.Int: the Merkle hash of the two big integers
func MerkleHash(x, y *big.Int) *big.Int {
if x.Cmp(y) <= 0 {
return curve.HashElements([]*big.Int{x, y})
return curve.HashPedersenElements([]*big.Int{x, y})
}
return curve.HashElements([]*big.Int{y, x})
return curve.HashPedersenElements([]*big.Int{y, x})
}

// build recursively constructs a Merkle tree from the given leaves.
Expand Down
12 changes: 5 additions & 7 deletions typed/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ func (td TypedData) GetMessageHash(account *big.Int, msg TypedMessage) (hash *bi
msgEnc := td.GetTypedMessageHash(td.PrimaryType, msg)

elements = append(elements, msgEnc)
hash = curve.ComputeHashOnElements(elements)
return hash

return curve.ComputeHashOnElements(elements)
}

// GetTypedMessageHash calculates the hash of a typed message using the provided StarkCurve.
Expand Down Expand Up @@ -171,12 +171,11 @@ func (td TypedData) GetTypedMessageHash(inType string, msg TypedMessage) (hash *
innerElements = append(innerElements, fmtDefinitions...)
innerElements = append(innerElements, big.NewInt(int64(len(innerElements))))

innerHash := curve.HashElements(innerElements)
innerHash := curve.HashPedersenElements(innerElements)
elements = append(elements, innerHash)
}

hash = curve.ComputeHashOnElements(elements)
return hash
return curve.ComputeHashOnElements(elements)
}

// GetTypeHash returns the hash of the given type.
Expand All @@ -191,8 +190,7 @@ func (td TypedData) GetTypeHash(inType string) (ret *big.Int, err error) {
if err != nil {
return ret, err
}
sel := utils.GetSelectorFromName(enc)
return sel, nil
return utils.GetSelectorFromName(enc), nil
}

// EncodeType encodes the given inType using the TypedData struct.
Expand Down
5 changes: 2 additions & 3 deletions utils/keccak.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ func HexToBN(hexString string) *big.Int {
// - *big.Int: the converted array
func HexArrToBNArr(hexArr []string) []*big.Int {
bigNumArr := make([]*big.Int, len(hexArr))
for i, e := range hexArr {
bigNum := HexToBN(e)
bigNumArr[i] = bigNum
for i, hexStr := range hexArr {
bigNumArr[i] = HexToBN(hexStr)
}
return bigNumArr
}
Expand Down

0 comments on commit f9119ce

Please sign in to comment.