<commit_before>package noise

import (
	"context"
	"crypto/rand"
	"fmt"
	"net"

	"golang.org/x/crypto/curve25519"

	"github.com/libp2p/go-libp2p-core/crypto"
	"github.com/libp2p/go-libp2p-core/peer"
	"github.com/libp2p/go-libp2p-core/sec"
)

// ID is the protocol ID for noise
const ID = "/noise"

var _ sec.SecureTransport = &Transport{}

// Keypair is a noise ed25519 public-private keypair
type Keypair struct {
	public_key  [32]byte
	private_key [32]byte
}

// GenerateKeypair creates a new ed25519 keypair
func GenerateKeypair() (*Keypair, error) {
	var public_key [32]byte
	var private_key [32]byte
	c, err := rand.Read(private_key[:])
	if err != nil {
		return nil, err
	}
	if c != 32 {
		return nil, fmt.Errorf("unable to generate 32 bytes of random data, got %d", c)
	}
	curve25519.ScalarBaseMult(&public_key, &private_key)
	return &Keypair{public_key, private_key}, nil
}

// Transport implements the interface sec.SecureTransport
// https://godoc.org/github.com/libp2p/go-libp2p-core/sec#SecureConn
type Transport struct {
	LocalID             peer.ID
	PrivateKey          crypto.PrivKey
	NoisePipesSupport   bool
	NoiseStaticKeyCache *KeyCache
	NoiseKeypair        *Keypair
}

type transportConstructor func(crypto.PrivKey) (*Transport, error)

// Maker returns a function that will construct a new Noise transport
// using the given Options. The returned function may be provided as a libp2p.Security
// option when configuring a libp2p Host using libp2p.New, and is compatible with the
// "reflection magic" that libp2p.New uses to inject the private identity key:
//
//    host := libp2p.New(
//      libp2p.Security(noise.ID, noise.Maker()))<commit_msg>rm redundant check for num bytes read<commit_after>package noise

import (
	"context"
	"crypto/rand"
	"net"

	"golang.org/x/crypto/curve25519"

	"github.com/libp2p/go-libp2p-core/crypto"
	"github.com/libp2p/go-libp2p-core/peer"
	"github.com/libp2p/go-libp2p-core/sec"
)

// ID is the protocol ID for noise
const ID = "/noise"

var _ sec.SecureTransport = &Transport{}

// Keypair is a noise ed25519 public-private keypair
type Keypair struct {
	public_key  [32]byte
	private_key [32]byte
}

// GenerateKeypair creates a new ed25519 keypair
func GenerateKeypair() (*Keypair, error) {
	var public_key [32]byte
	var private_key [32]byte
	_, err := rand.Read(private_key[:])
	if err != nil {
		return nil, err
	}
	curve25519.ScalarBaseMult(&public_key, &private_key)
	return &Keypair{public_key, private_key}, nil
}

// Transport implements the interface sec.SecureTransport
// https://godoc.org/github.com/libp2p/go-libp2p-core/sec#SecureConn
type Transport struct {
	LocalID             peer.ID
	PrivateKey          crypto.PrivKey
	NoisePipesSupport   bool
	NoiseStaticKeyCache *KeyCache
	NoiseKeypair        *Keypair
}

type transportConstructor func(crypto.PrivKey) (*Transport, error)

// Maker returns a function that will construct a new Noise transport
// using the given Options. The returned function may be provided as a libp2p.Security
// option when configuring a libp2p Host using libp2p.New, and is compatible with the
// "reflection magic" that libp2p.New uses to inject the private identity key:
//
//    host := libp2p.New(
//      libp2p.Security(noise.ID, noise.Maker()))
{'fraction_non_alphanumeric': 0.10657248157248157, 'fraction_numerical': 0.02702702702702703, 'mean_word_length': 3.8830584707646176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3080098', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1115, 'n_words': 309}