github.com/mad-day/Yawning-crypto@v0.0.0-20190711051033-5a5f8cca32ec/bcns/ringlwe_test.go (about)

     1  //
     2  // Ring Learning With Errors (Interface) integration tests
     3  //
     4  // To the extent possible under law, Yawning Angel waived all copyright
     5  // and related or neighboring rights to bcns, using the Creative
     6  // Commons "CC0" public domain dedication. See LICENSE or
     7  // <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
     8  
     9  package bcns
    10  
    11  import (
    12  	"bytes"
    13  	"crypto/rand"
    14  	"testing"
    15  )
    16  
    17  func TestInterfaceIntegration(t *testing.T) {
    18  	// Initiator side:
    19  	//  1. Generate a key pair.
    20  	aliceSk, alicePk, err := GenerateKeyPair(rand.Reader)
    21  	if err != nil {
    22  		t.Fatalf("failed GenerateKeyPair(Alice): %v", err)
    23  	}
    24  
    25  	//  2. Serialize and transmit the public key.
    26  	alicePkBlob := alicePk.Bytes()
    27  
    28  	// Responder side:
    29  	//  a. Generate a key pair.
    30  	bobSk, bobPk, err := GenerateKeyPair(rand.Reader)
    31  	if err != nil {
    32  		t.Fatalf("failed GenerateKeyPair(Bob): %v", err)
    33  	}
    34  
    35  	//  b. Deserialize the initiator's public key.
    36  	bAlicePk := &PublicKey{}
    37  	if err = bAlicePk.FromBytes(alicePkBlob); err != nil {
    38  		t.Fatalf("failed PublicKey.FromBytes(Alice): %v", err)
    39  	}
    40  
    41  	//  c. Complete the handshake, generating rec data and the shared secret.
    42  	recData, ssBob, err := KeyExchangeBob(rand.Reader, bAlicePk, bobSk)
    43  	if err != nil {
    44  		t.Fatalf("failed KeyExchangeBob(): %v", err)
    45  	}
    46  
    47  	//  d. Serialize and transmit the public key + recData.
    48  	recDataBlob := recData.Bytes()
    49  	bobPkBlob := bobPk.Bytes()
    50  
    51  	//  3. Deserialize the public key + recData.
    52  	aRecData := &RecData{}
    53  	if err = aRecData.FromBytes(recDataBlob); err != nil {
    54  		t.Fatalf("failed RecData.FromBytes(): %v", err)
    55  	}
    56  	aBobPk := &PublicKey{}
    57  	if err = aBobPk.FromBytes(bobPkBlob); err != nil {
    58  		t.Fatalf("failed PublicKey.FromBytes(bob): %v", err)
    59  	}
    60  
    61  	//  4. Complete the handshake, generating the shared secret.
    62  	ssAlice := KeyExchangeAlice(aBobPk, aliceSk, aRecData)
    63  
    64  	// Both Alice and Bob should have the same shared secret.  The
    65  	// reconciliation process will fail with a probability less than
    66  	// 2^-(2^-17).
    67  	if !bytes.Equal(ssAlice, ssBob) {
    68  		t.Fatalf("alice/bob shared secrets mismatch!")
    69  	}
    70  }