Skip to content

Commit

Permalink
group files into new pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
cicr99 committed Oct 5, 2023
1 parent 742a824 commit 2daf075
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 323 deletions.
10 changes: 5 additions & 5 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"

"github.com/NethermindEth/juno/core/felt"
starknetgo "github.com/NethermindEth/starknet.go"
"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)
Expand Down Expand Up @@ -45,10 +45,10 @@ type Account struct {
ChainId *felt.Felt
AccountAddress *felt.Felt
publicKey string
ks starknetgo.Keystore
ks curve.Keystore
}

func NewAccount(provider rpc.RpcProvider, accountAddress *felt.Felt, publicKey string, keystore starknetgo.Keystore) (*Account, error) {
func NewAccount(provider rpc.RpcProvider, accountAddress *felt.Felt, publicKey string, keystore curve.Keystore) (*Account, error) {
account := &Account{
provider: provider,
AccountAddress: accountAddress,
Expand Down Expand Up @@ -282,10 +282,10 @@ func (account *Account) PrecomputeAddress(deployerAddress *felt.Felt, salt *felt
})

constructorCalldataBigIntArr := utils.FeltArrToBigIntArr(constructorCalldata)
constructorCallDataHashInt, _ := starknetgo.Curve.ComputeHashOnElements(constructorCalldataBigIntArr)
constructorCallDataHashInt, _ := curve.Curve.ComputeHashOnElements(constructorCalldataBigIntArr)
bigIntArr = append(bigIntArr, constructorCallDataHashInt)

preBigInt, err := starknetgo.Curve.ComputeHashOnElements(bigIntArr)
preBigInt, err := curve.Curve.ComputeHashOnElements(bigIntArr)
if err != nil {
return nil, err
}
Expand Down
23 changes: 11 additions & 12 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import (
"os"
"testing"

"github.com/NethermindEth/juno/core/felt"
starknetgo "github.com/NethermindEth/starknet.go"
"github.com/golang/mock/gomock"
"github.com/joho/godotenv"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/account"
"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/mocks"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/test"
"github.com/NethermindEth/starknet.go/utils"
"github.com/golang/mock/gomock"
"github.com/test-go/testify/require"
)

Expand Down Expand Up @@ -129,7 +128,7 @@ func TestTransactionHashInvoke(t *testing.T) {
for _, test := range testSet {

t.Run("Transaction hash", func(t *testing.T) {
ks := starknetgo.NewMemKeystore()
ks := curve.NewMemKeystore()
if test.SetKS {
privKeyBI, ok := new(big.Int).SetString(test.PrivKey.String(), 0)
require.True(t, ok)
Expand Down Expand Up @@ -220,7 +219,7 @@ func TestChainIdMOCK(t *testing.T) {

for _, test := range testSet {
mockRpcProvider.EXPECT().ChainID(context.Background()).Return(test.ChainID, nil)
account, err := account.NewAccount(mockRpcProvider, &felt.Zero, "pubkey", starknetgo.NewMemKeystore())
account, err := account.NewAccount(mockRpcProvider, &felt.Zero, "pubkey", curve.NewMemKeystore())
require.NoError(t, err)
require.Equal(t, account.ChainId.String(), test.ExpectedID)
}
Expand Down Expand Up @@ -251,7 +250,7 @@ func TestChainId(t *testing.T) {
require.NoError(t, err, "Error in rpc.NewClient")
provider := rpc.NewProvider(client)

account, err := account.NewAccount(provider, &felt.Zero, "pubkey", starknetgo.NewMemKeystore())
account, err := account.NewAccount(provider, &felt.Zero, "pubkey", curve.NewMemKeystore())
require.NoError(t, err)
require.Equal(t, account.ChainId.String(), test.ExpectedID)
}
Expand Down Expand Up @@ -292,7 +291,7 @@ func TestSignMOCK(t *testing.T) {
for _, test := range testSet {
privKeyBI, ok := new(big.Int).SetString(test.PrivKey.String(), 0)
require.True(t, ok)
ks := starknetgo.NewMemKeystore()
ks := curve.NewMemKeystore()
ks.Put(test.Address.String(), privKeyBI)

mockRpcProvider.EXPECT().ChainID(context.Background()).Return(test.ChainId, nil)
Expand Down Expand Up @@ -383,7 +382,7 @@ func TestAddInvoke(t *testing.T) {
provider := rpc.NewProvider(client)

// Set up ks
ks := starknetgo.NewMemKeystore()
ks := curve.NewMemKeystore()
if test.SetKS {
fakePrivKeyBI, ok := new(big.Int).SetString(test.PrivKey.String(), 0)
require.True(t, ok)
Expand Down Expand Up @@ -420,7 +419,7 @@ func TestAddDeployAccountDevnet(t *testing.T) {
fakeUserPub := utils.TestHexToFelt(t, fakeUser.PublicKey)

// Set up ks
ks := starknetgo.NewMemKeystore()
ks := curve.NewMemKeystore()
fakePrivKeyBI, ok := new(big.Int).SetString(fakeUser.PrivateKey, 0)
require.True(t, ok)
ks.Put(fakeUser.PublicKey, fakePrivKeyBI)
Expand Down Expand Up @@ -466,7 +465,7 @@ func TestTransactionHashDeployAccountTestnet(t *testing.T) {

ExpectedHash := utils.TestHexToFelt(t, "0x5b6b5927cd70ad7a80efdbe898244525871875c76540b239f6730118598b9cb")
ExpectedPrecomputeAddr := utils.TestHexToFelt(t, "0x88d0038623a89bf853c70ea68b1062ccf32b094d1d7e5f924cda8404dc73e1")
ks := starknetgo.NewMemKeystore()
ks := curve.NewMemKeystore()
fakePrivKeyBI, ok := new(big.Int).SetString(PrivKey.String(), 0)
require.True(t, ok)
ks.Put(PubKey.String(), fakePrivKeyBI)
Expand Down Expand Up @@ -510,7 +509,7 @@ func TestTransactionHashDeclare(t *testing.T) {
require.NoError(t, err, "Error in rpc.NewClient")
provider := rpc.NewProvider(client)

acnt, err := account.NewAccount(provider, &felt.Zero, "", starknetgo.NewMemKeystore())
acnt, err := account.NewAccount(provider, &felt.Zero, "", curve.NewMemKeystore())
require.NoError(t, err)

tx := rpc.DeclareTxnV2{
Expand Down
4 changes: 2 additions & 2 deletions account/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package account

import (
"github.com/NethermindEth/juno/core/felt"
starknetgo "github.com/NethermindEth/starknet.go"
"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)

// computeHashOnElementsFelt hashes the array of felts provided as input
func computeHashOnElementsFelt(feltArr []*felt.Felt) (*felt.Felt, error) {
bigIntArr := utils.FeltArrToBigIntArr(feltArr)
hash, err := starknetgo.Curve.ComputeHashOnElements(bigIntArr)
hash, err := curve.Curve.ComputeHashOnElements(bigIntArr)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"os"

"github.com/NethermindEth/juno/core/felt"
starknetgo "github.com/NethermindEth/starknet.go"
"github.com/NethermindEth/starknet.go/artifacts"
"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)
Expand Down Expand Up @@ -74,7 +74,7 @@ func InstallAndWaitForAccount[V *rpc.Provider](ctx context.Context, provider V,
return nil, errors.New("empty account")
}
privateKeyString := fmt.Sprintf("0x%x", privateKey)
publicKey, _, err := starknetgo.Curve.PrivateToPoint(privateKey)
publicKey, _, err := curve.Curve.PrivateToPoint(privateKey)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion curve.go → curve/curve.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknetgo
package curve

/*
Although the library adheres to the 'elliptic/curve' interface.
Expand Down
2 changes: 1 addition & 1 deletion curve_test.go → curve/curve_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknetgo
package curve

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion keystore.go → curve/keystore.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknetgo
package curve

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion opts.go → curve/opts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknetgo
package curve

type curveOptions struct {
initConstants bool
Expand Down
File renamed without changes.
34 changes: 33 additions & 1 deletion starknetgo.go → curve/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package starknetgo
package curve

import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"fmt"
"hash"
Expand Down Expand Up @@ -267,6 +268,37 @@ func (sc StarkCurve) GenerateSecret(msgHash, privKey, seed *big.Int) (secret *bi
}
}

// obtain random primary key on stark curve
// NOTE: to be used for testing purposes
func (sc StarkCurve) GetRandomPrivateKey() (priv *big.Int, err error) {
max := new(big.Int).Sub(sc.Max, big.NewInt(1))

priv, err = rand.Int(rand.Reader, max)
if err != nil {
return priv, err
}

x, y, err := sc.PrivateToPoint(priv)
if err != nil {
return priv, err
}

if !sc.IsOnCurve(x, y) {
return priv, fmt.Errorf("key gen is not on stark cruve")
}

return priv, nil
}

// obtain public key coordinates from stark curve given the private key
func (sc StarkCurve) PrivateToPoint(privKey *big.Int) (x, y *big.Int, err error) {
if privKey.Cmp(big.NewInt(0)) != 1 || privKey.Cmp(sc.N) != -1 {
return x, y, fmt.Errorf("private key not in curve range")
}
x, y = sc.EcMult(privKey, sc.EcGenX, sc.EcGenY)
return x, y, nil
}

// https://tools.ietf.org/html/rfc6979#section-2.3.3
func int2octets(v *big.Int, rolen int) []byte {
out := v.Bytes()
Expand Down
18 changes: 17 additions & 1 deletion starknetgo_test.go → curve/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknetgo
package curve

import (
"crypto/elliptic"
Expand Down Expand Up @@ -197,3 +197,19 @@ func TestGeneral_Signature(t *testing.T) {
}
}
}

func TestGeneral_SplitFactStr(t *testing.T) {
data := []map[string]string{
{"input": "0x3", "h": "0x0", "l": "0x3"},
{"input": "0x300000000000000000000000000000000", "h": "0x3", "l": "0x0"},
}
for _, d := range data {
l, h := utils.SplitFactStr(d["input"]) // 0x3
if l != d["l"] {
t.Errorf("expected %s, got %s", d["l"], l)
}
if h != d["h"] {
t.Errorf("expected %s, got %s", d["h"], h)
}
}
}
12 changes: 0 additions & 12 deletions examples/curve/go.mod

This file was deleted.

6 changes: 0 additions & 6 deletions examples/curve/go.work

This file was deleted.

40 changes: 0 additions & 40 deletions examples/curve/main.go

This file was deleted.

9 changes: 6 additions & 3 deletions merkle.go → merkle/merkle.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package starknetgo
package merkle

import (
"fmt"
"math/big"


"github.com/NethermindEth/starknet.go/curve"
)

type FixedSizeMerkleTree struct {
Expand All @@ -26,9 +29,9 @@ func NewFixedSizeMerkleTree(leaves ...*big.Int) (*FixedSizeMerkleTree, error) {

func MerkleHash(x, y *big.Int) (*big.Int, error) {
if x.Cmp(y) <= 0 {
return Curve.HashElements([]*big.Int{x, y})
return curve.Curve.HashElements([]*big.Int{x, y})
}
return Curve.HashElements([]*big.Int{y, x})
return curve.Curve.HashElements([]*big.Int{y, x})
}

func (mt *FixedSizeMerkleTree) build(leaves []*big.Int) (*big.Int, error) {
Expand Down
2 changes: 1 addition & 1 deletion merkle_test.go → merkle/merkle_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package starknetgo
package merkle

import (
"math/big"
Expand Down
Loading

0 comments on commit 2daf075

Please sign in to comment.