Skip to content

Commit

Permalink
fixing curve opts (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
anupsv authored and shrimalmadhur committed Oct 7, 2024
1 parent e148bc0 commit 09a6265
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
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()
},
},
}
24 changes: 24 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 @@ -510,6 +511,29 @@ func NewKeyPair(
}, nil
}

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

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

keyPair, err := NewKeyPair(password, language)

if err != nil {
return nil, err
}

privateKeyFrBytes := ops.GeneratePrivateKeyFromBytes(keyPair.PrivateKey)
keyPair.PrivateKey = privateKeyFrBytes[:]
return keyPair, nil
}

func NewKeyPairFromMnemonic(
pkMnemonic string,
password string,
Expand Down

0 comments on commit 09a6265

Please sign in to comment.