-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement support for ed25519 (#504)
* implement support for ed25519 * moving crypto operation to pkg * increasing code coverage * increase coverage * refactor crypto folder * extend code coverage * fixing linting * increasing coverage * applying comments
- Loading branch information
Showing
15 changed files
with
670 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package ecdsa | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
func CreateSecp256k1(importedPrivKey []byte) (privKey, pubKey []byte, err error) { | ||
var ecdsaKey *ecdsa.PrivateKey | ||
if importedPrivKey != nil { | ||
ecdsaKey, err = crypto.ToECDSA(importedPrivKey) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} else { | ||
ecdsaKey, err = crypto.GenerateKey() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} | ||
|
||
privKey = crypto.FromECDSA(ecdsaKey) | ||
pubKey = crypto.FromECDSAPub(&ecdsaKey.PublicKey) | ||
return privKey, pubKey, nil | ||
} | ||
|
||
func SignSecp256k1(privKey, data []byte) ([]byte, error) { | ||
if len(data) != crypto.DigestLength { | ||
return nil, fmt.Errorf("data is required to be exactly %d bytes (%d)", crypto.DigestLength, len(data)) | ||
} | ||
|
||
ecdsaPrivKey, err := crypto.ToECDSA(privKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse private key. %s", err.Error()) | ||
} | ||
|
||
signature, err := crypto.Sign(data, ecdsaPrivKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to sign. %s", err.Error()) | ||
} | ||
|
||
// We remove the recID from the signature (last byte). | ||
return signature[:len(signature)-1], nil | ||
} | ||
|
||
func VerifySecp256k1Signature(publicKey, message, signature []byte) (bool, error) { | ||
pubKey, err := crypto.UnmarshalPubkey(publicKey) | ||
if err != nil { | ||
return false, err | ||
} | ||
if len(signature) != 64 { | ||
return false, fmt.Errorf("invalid secp256k1 signature length") | ||
} | ||
|
||
r := new(big.Int).SetBytes(signature[0:32]) | ||
s := new(big.Int).SetBytes(signature[32:64]) | ||
|
||
return ecdsa.Verify(pubKey, message, r, s), nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package eddsa | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"fmt" | ||
|
||
babyjubjub "github.com/consensys/gnark-crypto/ecc/bn254/twistededwards/eddsa" | ||
"github.com/consensys/gnark-crypto/hash" | ||
) | ||
|
||
func CreateBabyjubjub(importedPrivKey []byte) (privKey, pubKey []byte, err error) { | ||
babyJubJubPrivKey := babyjubjub.PrivateKey{} | ||
if importedPrivKey != nil { | ||
_, err = babyJubJubPrivKey.SetBytes(importedPrivKey) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} else { | ||
seed := make([]byte, 32) | ||
_, err = rand.Read(seed) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// Usually standards implementations of eddsa do not require the choice of a specific hash function (usually it's SHA256). | ||
// Here we needed to allow the choice of the hash, so we can choose a hash function that is easily programmable in a snark circuit. | ||
// Same hFunc should be used for sign and verify | ||
babyJubJubPrivKey, err = babyjubjub.GenerateKey(bytes.NewReader(seed)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} | ||
|
||
privKey = babyJubJubPrivKey.Bytes() | ||
pubKey = babyJubJubPrivKey.Public().Bytes() | ||
return privKey, pubKey, nil | ||
} | ||
|
||
func SignBabyjubjub(privKeyB, data []byte) ([]byte, error) { | ||
privKey := babyjubjub.PrivateKey{} | ||
_, err := privKey.SetBytes(privKeyB) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse private key. %s", err.Error()) | ||
} | ||
|
||
signature, err := privKey.Sign(data, hash.MIMC_BN254.New("seed")) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to sign. %s", err.Error()) | ||
} | ||
|
||
return signature, nil | ||
} | ||
|
||
func VerifyBabyJubJubSignature(publicKey, message, signature []byte) (bool, error) { | ||
pubKey := babyjubjub.PublicKey{} | ||
_, err := pubKey.SetBytes(publicKey) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
verified, err := pubKey.Verify(signature, message, hash.MIMC_BN254.New("seed")) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return verified, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package eddsa | ||
|
||
import ( | ||
"bytes" | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"fmt" | ||
) | ||
|
||
func CreateED25519(importedPrivKey []byte) (privKey, pubKey []byte, err error) { | ||
// https://pkg.go.dev/crypto/ed25519#section-documentation | ||
if importedPrivKey != nil { | ||
if len(importedPrivKey) != ed25519.PrivateKeySize { | ||
return nil, nil, fmt.Errorf("invalid private key value") | ||
} | ||
ed25519PrivKey := ed25519.PrivateKey(importedPrivKey) | ||
pubKey = ed25519PrivKey.Public().(ed25519.PublicKey) | ||
privKey = ed25519PrivKey | ||
} else { | ||
seed := make([]byte, 32) | ||
if _, err = rand.Read(seed); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
pubKey, privKey, err = ed25519.GenerateKey(bytes.NewReader(seed)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} | ||
|
||
return privKey, pubKey, nil | ||
} | ||
|
||
func SignED25519(privKeyB, data []byte) ([]byte, error) { | ||
if len(privKeyB) != ed25519.PrivateKeySize { | ||
return nil, fmt.Errorf("invalid ED25519 private key length") | ||
} | ||
privKey := ed25519.PrivateKey(privKeyB) | ||
signature := ed25519.Sign(privKey, data) | ||
return signature, nil | ||
} | ||
|
||
func VerifyED25519Signature(publicKey, message, signature []byte) (bool, error) { | ||
if len(publicKey) != ed25519.PublicKeySize { | ||
return false, fmt.Errorf("invalid ED25519 public key length") | ||
} | ||
if len(signature) != ed25519.SignatureSize { | ||
return false, fmt.Errorf("invalid ED25519 signature length") | ||
} | ||
pubKey := ed25519.PublicKey(publicKey) | ||
return ed25519.Verify(pubKey, message, signature), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.