git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/ed25519_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestGenerateEd25519Keypair(t *testing.T) {
     8  	zeroPublicKey := make([]byte, Ed25519PublicKeySize)
     9  	zeroPrivateKey := make([]byte, Ed25519PrivateKeySize)
    10  
    11  	publicKey, privateKey, err := GenerateEd25519KeyPair()
    12  	if err != nil {
    13  		t.Error(err)
    14  	}
    15  
    16  	if ConstantTimeCompare(zeroPrivateKey, privateKey) {
    17  		t.Error("Generated private key is empty")
    18  	}
    19  
    20  	if ConstantTimeCompare(zeroPublicKey, publicKey) {
    21  		t.Error("Generated public key is empty")
    22  	}
    23  }
    24  
    25  func TestEd25519KeypairToCurve25519(t *testing.T) {
    26  	ed25519PublicKey, ed25519PrivateKey, err := GenerateEd25519KeyPair()
    27  	if err != nil {
    28  		t.Error(err)
    29  	}
    30  
    31  	curve25519PrivateKey := ed25519PrivateKey.ToCurve25519PrivateKey()
    32  
    33  	curve25519PublicKey, err := curve25519PrivateKey.Public()
    34  	if err != nil {
    35  		t.Error(err)
    36  	}
    37  
    38  	curve25519PublicKey2 := ed25519PublicKey.ToCurve25519PublicKey()
    39  
    40  	if !ConstantTimeCompare(curve25519PublicKey, curve25519PublicKey2) {
    41  		t.Errorf("curve25519PublicKey (%x) != curve25519PublicKey2 (%x)", curve25519PublicKey, curve25519PublicKey2)
    42  	}
    43  }