// Copyright (c) blackshirt. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// vtest build: has_modern_openssl?
module slhdsa

import encoding.hex

fn test_basic_slhdsa_signing_verifying() ! {
	mut pv := PrivateKey.new()!
	msg := 'msg'.bytes()

	sig := pv.sign(msg)!

	mut pb := pv.public_key()!
	verified := pb.verify(sig, msg)!
	assert verified

	pv.free()
	pb.free()
}

// Test for SLH-DSA signature generation, for SLH_DSA_SHA2_128s key
fn test_sha2_128_signature_generation() ! {
	for idx, item in samples_slh128s_generated_signatures {
		priv := hex.decode(item.priv)!
		entropy := hex.decode(item.entropy)!
		msg := hex.decode(item.msg)!
		sig := hex.decode(item.sig)!

		// key options
		k_opt := KeyOpts{
			kind: .sha2_128s
			flag: 2
			priv: priv
		}
		mut pv := PrivateKey.new(k_opt)!

		// signing (verifying) options
		s_opt := SignerOpts{
			encoding: 0
			entropy:  entropy
		}
		out_sig := pv.sign(msg, s_opt)!
		assert out_sig == sig

		mut pb := pv.public_key()!
		verified := pb.verify(out_sig, msg, s_opt)!
		assert verified

		pv.free()
		pb.free()
	}
}

struct TestSignatureGenerationSLHSHA2128s {
	priv    string
	entropy string
	msg     string
	sig     string
}

// This samples of data was copied and adapted into V opaque from
// https://boringssl.googlesource.com/boringssl.git/+/chromium-stable/crypto/slhdsa/slhdsa_siggen.txt
//
const samples_slh128s_generated_signatures = [
	TestSignatureGenerationSLHSHA2128s{
		priv:    '4329f96b412b5d5ca9c730c9d54e52595265f6fc3ea67c183aa7043d90e949b8fd320c8fc0fa6a23462483535f131b9ea265848e7bb5f4d3cfd99de2595382b3'
		entropy: '65ac888a4cbd14aa5533e8685b0cc0e1'
		msg:     '707951a4c5040af79e4dbb1d5614ea77ee58a1b16d8c17a40abb01499d1b65f5c55b517b603711e5db67f758cf6717aa10cee53b4417b4c1f4b1dcd6e9e65e96a68b856ebefb45d394e6c9214d0c55e692eb2ffbb10b804bc91076a50aa5cba4884564254ea99540c99647c4637ed26b57e77cb58be6db0ab0d418543b0f69db'
		sig:     '[encoded data, 15712 characters]'
	},
	TestSignatureGenerationSLHSHA2128s{
		priv:    '5c34d15e5931c27c976f7044d18f24e11a268267de70e8c86e1e90f888dd692fb534c67bf12f66252e772e66675c46de3aecaf7da2a98e7971b1a455eb4e1300'
		entropy: 'f93091ff5b082105513400726dfbd17b'
		msg:     '2d884fd72a2daf869220adb4abfd959ebbfff58b3e74676afdad958ef77b'
		sig:     '[encoded data, 15712 characters]'
	},
	TestSignatureGenerationSLHSHA2128s{
		priv:    '4f0b94c0e1d8abec0d22ad3f180e514115272ce1ab8252b0608da02659d84732a4bf46d8c3329e2e48594ab7970d1ff0e644e8ac35994f9308049ca1e108ff1e'
		entropy: 'c02b762bed67adc3a058487df6fabf42'
		msg:     '7ea1563d2d0e4d398028b06f1fab51f69962c6e84f9e916b655aeff9bcd2d811fcbfd1aacfe09515f4e45f73441046fc1b6912f1204d01504c251dc323421edf9bf57f65a86d60ab53e73650efdd91821d133bdab274067c257a182f8be9fc2b354e51'
		sig:     '[encoded data, 15712 characters]'
	},
	TestSignatureGenerationSLHSHA2128s{
		priv:    'f99d60c4ba723d4a81277aa8bdeec37b6fc27624faafef99f6e82256f351fae23d422fcf462f0f4a0df974a7ebcf5b85217fc357d9db5e5fa864d848636b5511'
		entropy: 'd45c2dcbb823f8db583a4038ca7af794'
		msg:     '90'
		sig:     '[encoded data, 15712 characters]'
	},
	TestSignatureGenerationSLHSHA2128s{
		priv:    '2174e8f49dab2ab631e641a5406fd710f772a575d2746c9c9b7a2b4217441f88bf830dd8d578bd710316bf443cca3f48e4e056030eaad73bdef022c2a0291fd3'
		entropy: '10bc13cc39a3d35de53c309a6ad1902c'
		msg:     '7f47df895ab1a0a777ab84002eef5844bcffb413dabe8fd6fc0027721be348207a59316f89dc88d587c3b22d8227b659595d898890516ab0b66966e387286a2ad90627e448b1a246884465456f463f63e3ae9b860e2127a23e7a202b221019ed082173c28a9907f4e398a9b7f4a82ad79488f4a116734c4cc9b2'
		sig:     '[encoded data, 15712 characters]'
	},
	TestSignatureGenerationSLHSHA2128s{
		priv:    'be4bfeaaf8e4fbe9bdc93b44d621380a48090ab3599d40d3bcbcfb50c0564d7b48f8846d7ba5fa5ef49151563e84bce614b947b2be7565f4fcf7ad7a5ee899f9'
		entropy: 'df096f9d0302657c0f9b6974b80c59eb'
		msg:     'eb29cb0687183747a27d51778eb7282fd52778778ea810131092cc90e4e3500d348908e88622a237c28d5309f70c7b895a2027262f6d94f4125129294ff9d4f6'
		sig:     '[encoded data, 15712 characters]'
	},
	TestSignatureGenerationSLHSHA2128s{
		priv:    '03d3b1c80c61d8f13ffa8ab1f2515d2071c4afd7d2b91d979cf8f4b6dd907ef2a0985f04c61e90ef3af41d6fbba798d070b5f35f53599f764ad53121ae24da8d'
		entropy: '426827d1935bb4642b404799bafc62d6'
		msg:     'f724'
		sig:     '[encoded data, 15712 characters]'
	},
]
