//go:build amd64
// +build amd64

package sm4ni

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"testing"
	"time"

	"golang.org/x/sys/cpu"
	"xdx.jelly/xgcl/grand"
	"xdx.jelly/xgcl/sm/sm4"
)

func TestSm4ni(t *testing.T) {
	var encKey = make([]uint32, 32)
	var decKey = make([]uint32, 32)

	key, _ := hex.DecodeString("0123456789abcdeffedcba9876543210")
	msg := grand.GetRandom(4 * 16)
	cipher4 := make([]byte, len(msg))
	cipher := make([]byte, len(msg))
	sm4.ExpandKey(key, encKey, decKey)

	EncryptBlocks(encKey, msg, cipher4)
	sm4.EncryptECB(cipher, key, msg)

	if bytes.Compare(cipher, cipher4) != 0 {
		t.Fatal("Error")
	}
}

func TestSm4niSpeed(t *testing.T) {
	var encKey = make([]uint32, 32)
	var decKey = make([]uint32, 32)

	key, _ := hex.DecodeString("0123456789abcdeffedcba9876543210")
	msg := grand.GetRandom(64 * 1000)
	cipher := make([]byte, len(msg))
	sm4.ExpandKey(key, encKey, decKey)

	for i := 0; i < 10; i++ {
		EncryptBlocks(encKey, msg, cipher)
	}

	start := time.Now()
	times := 100000
	for i := 0; i < times; i++ {
		EncryptBlocks(encKey, msg, cipher)
	}
	end := time.Now()
	elapsed := end.Sub(start)
	t.Log("SM4 Encrypt: ", int(float64(times*len(msg))/float64(1024*1024)/elapsed.Seconds()), "MBps")
}

func TestHasAes(t *testing.T) {
	fmt.Println(cpu.X86.HasAES)
	fmt.Println(cpu.ARM64.HasAES)
	fmt.Println(cpu.X86.HasPCLMULQDQ)
	fmt.Println(cpu.ARM64.HasPMULL)
}
