github.com/annchain/OG@v0.0.9/tests/crypto/ecies_test.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 crypto
    15  
    16  import (
    17  	"encoding/hex"
    18  	"fmt"
    19  	"github.com/annchain/OG/common/crypto"
    20  	"github.com/annchain/OG/poc/extra25519"
    21  	"github.com/annchain/kyber/v3/group/edwards25519"
    22  	"testing"
    23  )
    24  
    25  type Stream struct {
    26  }
    27  
    28  func (t *Stream) XORKeyStream(dst, src []byte) {
    29  	copy(dst, src)
    30  	return
    31  }
    32  
    33  func TestEcies(t *testing.T) {
    34  	s := crypto.NewSigner(crypto.CryptoTypeEd25519)
    35  	b, err := hex.DecodeString("fbe35f7844e2434cec0f1ea3ec97080d52a1d10394f9c19d8b6e38a92bd0072f6fbf9980134f18a5f864b4e763ffbc78dbdee3ced30334c3efd8b8b3d7b363d2")
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  	priv := crypto.PrivateKeyFromBytes(s.GetCryptoType(), b)
    40  	pub := priv.PublicKey()
    41  	//*pub, priv, _ = s.RandomKeyPair()
    42  	fmt.Println("pub ", pub.String())
    43  	fmt.Println("priv", priv.String())
    44  
    45  	var curvPriv [32]byte
    46  	var edPriv [64]byte
    47  	copy(edPriv[:], priv.KeyBytes[:32])
    48  	extra25519.PrivateKeyToCurve25519(&curvPriv, &edPriv)
    49  	fmt.Println("edPriv ", hex.EncodeToString(edPriv[:]))
    50  	fmt.Println("curvPriv ", hex.EncodeToString(curvPriv[:]))
    51  
    52  	//suite := edwards25519.NewBlakeSHA256Ed25519WithRand(&Stream{})
    53  	suite := edwards25519.NewBlakeSHA256Ed25519()
    54  	//private := suite.Scalar().Pick(random.New())
    55  	//public := suite.Point().Mul(private, nil)
    56  	//fmt.Println("priv", private)
    57  	//fmt.Println("pub", public)
    58  	sc, err := edwards25519.UnmarshalBinaryScalar(curvPriv[:32])
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  	pc := suite.Point().Mul(sc, nil)
    63  	pk, _ := edwards25519.UnmarshalBinaryPoint(pub.KeyBytes)
    64  	fmt.Println("kyber  prive  key ", sc)
    65  	fmt.Println("kyber pub key ", pc, pk)
    66  	var edPubkey [32]byte
    67  	var curvPubkey [32]byte
    68  	copy(edPubkey[:], pub.KeyBytes[:])
    69  	extra25519.PublicKeyToCurve25519(&curvPubkey, &edPubkey)
    70  	fmt.Println("ed pubkey ", hex.EncodeToString(edPubkey[:]))
    71  	fmt.Println(" cur pubkey", hex.EncodeToString(curvPubkey[:]))
    72  }