github.com/quantosnetwork/Quantos@v0.0.0-20220306172517-e20b28c5a29a/crypto/kyber.go (about)

     1  package crypto
     2  
     3  import (
     4  	"errors"
     5  	"go.dedis.ch/kyber/v3"
     6  
     7  	"go.dedis.ch/kyber/v3/group/edwards25519"
     8  	"go.dedis.ch/kyber/v3/xof/blake2xb"
     9  )
    10  
    11  type HardenedKeys struct {
    12  	Group   kyber.Group
    13  	PubKey  kyber.Point
    14  	PrivKey kyber.Scalar
    15  	Suite   *edwards25519.SuiteEd25519
    16  }
    17  
    18  var PrivKey kyber.Scalar
    19  var PubKey kyber.Point
    20  
    21  func RestorePrivateKey(b []byte) {
    22  	err := PrivKey.UnmarshalBinary(b)
    23  	if err != nil {
    24  		return
    25  	}
    26  }
    27  
    28  func GenerateHardenedKeys() *HardenedKeys {
    29  	rng := blake2xb.New(nil)
    30  	suite := edwards25519.NewBlakeSHA256Ed25519WithRand(rng)
    31  	h := &HardenedKeys{}
    32  	sk := suite.Scalar().Pick(rng)   // private key
    33  	pk := suite.Point().Mul(sk, nil) // public key
    34  	h.PrivKey = sk
    35  	h.PubKey = pk
    36  	h.Suite = suite
    37  	return h
    38  }
    39  
    40  func GenerateAndVerifySharedKeys(h1 *HardenedKeys, h2 *HardenedKeys) (secret string, err error) {
    41  
    42  	S1 := h1.Suite.Point().Mul(h1.PrivKey, h2.PubKey)
    43  	S2 := h2.Suite.Point().Mul(h2.PrivKey, h1.PubKey)
    44  
    45  	if !S1.Equal(S2) {
    46  		err = errors.New("shared secrets exchange didn't work")
    47  		return "", err
    48  	}
    49  
    50  	return S1.String(), nil
    51  
    52  }