package simd

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

	"xdx.jelly/xgcl/sm/sm4"
)

func TestEncrypt4(t *testing.T) {
	key, _ := hex.DecodeString("AF07B5BDDF77A3727E9E5FEC48DA1D9E")
	// test 4 blocks for aesni
	src, _ := hex.DecodeString(
		"B358A63B7587FCCB46CD41FFE778D5C1" +
			"B358A63B7587FCCB46CD41FFE778D5C1" +
			"B358A63B7587FCCB46CD41FFE778D5C1" +
			"B358A63B7587FCCB46CD41FFE778D5C1",
	)

	dst := make([]byte, len(src))
	encKey := make([]uint32, 32)
	decKey := make([]uint32, 32)
	sm4.ExpandKey(key, encKey, decKey)
	encrypt4(dst, src, encKey)
	// dst = 6845268B 91394C00 6648E71A 2D6D68C1 * 4
	for i := 0; i < 64; i++ {
		fmt.Printf("%02x ", dst[i])
		if i > 0 && (i+1)%16 == 0 {
			fmt.Println()
		}
	}

}

func TestMMSet(t *testing.T) {
	// src := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
	src := []uint32{0x01234567, 0x89abcdef, 0xfedcba98, 0x76543210}
	dst := make([]byte, 16)
	mmSet(dst, src)
	fmt.Printf("%x\n", dst)
}

// 67452301 efcdab89 98badcfe 10325476

func TestMMExtLoad32(t *testing.T) {
	dst := make([]byte, 16)
	mmExtLoad32(dst)
	fmt.Printf("%x\n", dst)
}

func printBlock4(s []byte) {
	for i := 0; i < 64; i++ {
		fmt.Printf("%02x", s[i])
		if i > 0 && (i+1)%4 == 0 {
			fmt.Print(" ")
		}
		if i > 0 && (i+1)%16 == 0 {
			fmt.Println()
		}
	}
}
func TestTranspose(t *testing.T) {
	var src = make([]byte, 64)
	for i := 0; i < 64; i++ {
		src[i] = byte(i)
	}

	dst := make([]byte, 64)
	mmTranspose(dst, src)

	printBlock4(src)
	fmt.Println("----------")
	printBlock4(dst)
}
