github.com/annchain/OG@v0.0.9/deprecated/ogcrypto/signer_ed25519.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package ogcrypto
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/annchain/OG/deprecated"
    19  	"github.com/annchain/OG/deprecated/ogcrypto/extra25519"
    20  	"github.com/annchain/OG/deprecated/ogcrypto_interface"
    21  	"github.com/annchain/kyber/v3/encrypt/ecies"
    22  	"github.com/annchain/kyber/v3/group/edwards25519"
    23  	"github.com/sirupsen/logrus"
    24  	"golang.org/x/crypto/ed25519"
    25  	"strconv"
    26  )
    27  
    28  type SignerEd25519 struct {
    29  }
    30  
    31  func (s *SignerEd25519) GetCryptoType() ogcrypto_interface.CryptoType {
    32  	return ogcrypto_interface.CryptoTypeEd25519
    33  }
    34  
    35  func (s *SignerEd25519) CanRecoverPubFromSig() bool {
    36  	return false
    37  }
    38  
    39  func (s *SignerEd25519) Sign(privKey ogcrypto_interface.PrivateKey, msg []byte) ogcrypto_interface.Signature {
    40  	signatureBytes := ed25519.Sign(privKey.KeyBytes, msg)
    41  	return deprecated.SignatureFromBytes(ogcrypto_interface.CryptoTypeEd25519, signatureBytes)
    42  }
    43  
    44  func (s *SignerEd25519) PubKey(privKey ogcrypto_interface.PrivateKey) ogcrypto_interface.PublicKey {
    45  	pubkey := ed25519.PrivateKey(privKey.KeyBytes).Public()
    46  	return deprecated.PublicKeyFromBytes(ogcrypto_interface.CryptoTypeEd25519, []byte(pubkey.(ed25519.PublicKey)))
    47  }
    48  
    49  func (s *SignerEd25519) PublicKeyFromBytes(b []byte) ogcrypto_interface.PublicKey {
    50  	return deprecated.PublicKeyFromBytes(s.GetCryptoType(), b)
    51  }
    52  
    53  func (s *SignerEd25519) Verify(pubKey ogcrypto_interface.PublicKey, signature ogcrypto_interface.Signature, msg []byte) bool {
    54  	//validate to prevent panic
    55  	if l := len(pubKey.KeyBytes); l != ed25519.PublicKeySize {
    56  		err := fmt.Errorf("ed25519: bad public key length: " + strconv.Itoa(l))
    57  		logrus.WithError(err).Warn("verify fail")
    58  		return false
    59  	}
    60  	return ed25519.Verify(pubKey.KeyBytes, msg, signature.SignatureBytes)
    61  }
    62  
    63  func (s *SignerEd25519) RandomKeyPair() (publicKey ogcrypto_interface.PublicKey, privateKey ogcrypto_interface.PrivateKey) {
    64  	public, private, err := ed25519.GenerateKey(nil)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	publicKey = deprecated.PublicKeyFromBytes(ogcrypto_interface.CryptoTypeEd25519, public)
    69  	privateKey = deprecated.PrivateKeyFromBytes(ogcrypto_interface.CryptoTypeEd25519, private)
    70  	return
    71  }
    72  
    73  func (s *SignerEd25519) Encrypt(publicKey ogcrypto_interface.PublicKey, m []byte) (ct []byte, err error) {
    74  	//convert our pubkey key to kyber pubkey
    75  	suite := edwards25519.NewBlakeSHA256Ed25519()
    76  	pubKey, err := edwards25519.UnmarshalBinaryPoint(publicKey.KeyBytes)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	return ecies.Encrypt(suite, pubKey, m, suite.Hash)
    81  }
    82  
    83  func (s *SignerEd25519) Decrypt(p ogcrypto_interface.PrivateKey, ct []byte) (m []byte, err error) {
    84  	//convert our priv key to kyber privkey
    85  	var edPrivKey [32]byte
    86  	var curvPrivKey [64]byte
    87  	copy(curvPrivKey[:], p.KeyBytes[:64])
    88  	extra25519.PrivateKeyToCurve25519(&edPrivKey, &curvPrivKey)
    89  	privateKey, err := edwards25519.UnmarshalBinaryScalar(edPrivKey[:32])
    90  	if err != nil {
    91  		panic(err)
    92  	}
    93  	suite := edwards25519.NewBlakeSHA256Ed25519()
    94  	return ecies.Decrypt(suite, privateKey, ct, suite.Hash)
    95  }