Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing curve opts #8

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions curve/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package curve

import (
"encoding/hex"
bls12381Fr "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"math/big"

bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
bls12381Fr "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"github.com/consensys/gnark-crypto/ecc/bn254"
bn254Fr "github.com/consensys/gnark-crypto/ecc/bn254/fr"
)
Expand All @@ -18,8 +18,9 @@ const (
)

type Ops struct {
GenerateG2PubKey func(secret []byte) string
GenerateG1PubKey func(secret []byte) string
GenerateG2PubKey func(secret []byte) string
GenerateG1PubKey func(secret []byte) string
GeneratePrivateKeyFromBytes func(secret []byte) [32]byte
}

var OpsMap = map[Curve]Ops{
Expand All @@ -46,6 +47,9 @@ var OpsMap = map[Curve]Ops{
publicKeyBytes := pubKey.ScalarMultiplication(&g1Gen, &frBigInt).Bytes()
return hex.EncodeToString(publicKeyBytes[:])
},
GeneratePrivateKeyFromBytes: func(secret []byte) [32]byte {
return new(bls12381Fr.Element).SetBytes(secret).Bytes()
},
},
BN254: {
GenerateG2PubKey: func(secret []byte) string {
Expand All @@ -70,5 +74,8 @@ var OpsMap = map[Curve]Ops{
publicKeyBytes := pubKey.ScalarMultiplication(&g1Gen, &frBigInt).Bytes()
return hex.EncodeToString(publicKeyBytes[:])
},
GeneratePrivateKeyFromBytes: func(secret []byte) [32]byte {
return new(bn254Fr.Element).SetBytes(secret).Bytes()
},
},
}
34 changes: 34 additions & 0 deletions keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
curveOps "github.com/Layr-Labs/bn254-keystore-go/curve"
"os"
"path/filepath"
"unicode"
Expand Down Expand Up @@ -504,6 +505,39 @@ func NewKeyPair(
}, nil
}

// NewKeyPair generates a new key pair using the provided password and mnemonic language.
func NewKeyPairWithCurve(
password string,
language mnemonic.Language,
curve curve.Curve,
) (*KeyPair, error) {

ops, exists := curveOps.OpsMap[curveOps.Curve(curve)]
if !exists {
return nil, fmt.Errorf("curve '%s' not supported", curve)
}

// Get the mnemonic
pkMnemonic, err := mnemonic.GetMnemonic(language, DefaultWordListPath, nil)
if err != nil {
return nil, err
}

// Derive KeyPair
key, err := mnemonic.MnemonicAndPathToKey(pkMnemonic, password, DerivationPathBN254)
if err != nil {
return nil, err
}

privateKeyFrBytes := ops.GeneratePrivateKeyFromBytes(key.Bytes())
// Return key pair
return &KeyPair{
PrivateKey: privateKeyFrBytes[:],
Mnemonic: pkMnemonic,
Password: password,
}, nil
}

func NewKeyPairFromMnemonic(
pkMnemonic string,
password string,
Expand Down
Loading