// KEKS/CM -- KEKS-encoded cryptographic messages
// Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program.  If not, see <http://www.gnu.org/licenses/>.

package sntrup761x25519

import (
	"crypto/ecdh"
	"crypto/rand"

	"go.cypherpunks.su/keks/cm/enc/sntrup761-x25519/sntrup761/kem"
	"go.cypherpunks.su/keks/cm/enc/sntrup761-x25519/sntrup761/kem/ntruprime/sntrup761"
)

func NewKeypair() (prv, pub []byte, err error) {
	s := sntrup761.Scheme()
	var pubSNTRUP kem.PublicKey
	var prvSNTRUP kem.PrivateKey
	pubSNTRUP, prvSNTRUP, err = s.GenerateKeyPair()
	if err != nil {
		return
	}
	var prvX25519 *ecdh.PrivateKey
	prvX25519, err = ecdh.X25519().GenerateKey(rand.Reader)
	if err != nil {
		return
	}
	prv, err = prvSNTRUP.MarshalBinary()
	if err != nil {
		return
	}
	pub, err = pubSNTRUP.MarshalBinary()
	if err != nil {
		return
	}
	pubX25519 := prvX25519.PublicKey()
	prv = append(prv, prvX25519.Bytes()...)
	pub = append(pub, pubX25519.Bytes()...)
	return
}
