// 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

struct TestSignatureVerifySLHSHA2128s {
	pbk   string
	msg   string
	sig   string
	valid bool
}

fn test_slh128s_signature_verification_from_samples() ! {
	for idx, item in samples_slh128s_signature_verified {
		pbk := hex.decode(item.pbk)!
		msg := hex.decode(item.msg)!
		sig := hex.decode(item.sig)!

		// create public key
		mut pbkey := PublicKey.from_bytes(pbk, .sha2_128s)!
		// for testing, set encoding option into 0
		opt := SignerOpts{
			encoding: 0
		}
		valid := pbkey.verify(sig, msg, opt)!
		assert valid == item.valid

		pbkey.free()
	}
}

// This samples of data was copied and adapted into V from BoringSSL signature verification test located at
// https://boringssl.googlesource.com/boringssl.git/+/chromium-stable/crypto/slhdsa/slhdsa_sigver.txt
// Its only availables for sha2_128s
const samples_slh128s_signature_verified = [
	TestSignatureVerifySLHSHA2128s{
		pbk:   '9c0b487a467b79234031eed3569055ffea884d68408b0f7a960ecdde04f6aa2a'
		msg:   '1b9dcd809ca93f5192af4f8ca9e2ae0b3221325279709d9d24169a2ef98a604a54ba8b006e272bd32a2ff703736b4503b9c48b93a94fa3a79d3ca8c77c69818ab2c2f8'
		sig:   '[encoded data, 15712 characters]'
		valid: false
	}, /*
	TestSignatureVerifySLHSHA2128s{
		pbk:   '507d6635a4c00f4bc736631a2c2b2188a0fe22e75800707d68b70d6fc1b2fafd'
		msg:   'aebbbfb373acd2a4b775e1c6f19c600e5585694684d043dcc0c23bb4924514'
		sig:   '[encoded data, 15712 characters]'
		valid: true
	},
	*/
	TestSignatureVerifySLHSHA2128s{
		pbk:   'b31826a9c03c1d1e9da67ece57879e0e4ef1ec125fd5f581fa28f01044d0eed9'
		msg:   '8a4e07dbdc77b8053cc949f987e6960ab79c606d9e285d6123b72730aa625b1920e3d7dd1e04601ccaf14cff025c03f0a8177278ab36fb612b26e03c9482f688c94b568bd63f2129150ca49a8a888804f4bcb2a1eb2565766fae1c3f2e75286f8bd7888a61f8ac5bb85d9e10ad6a1048660eb2bdf1826052d5b783839ada87b2'
		sig:   '[encoded data, 15712 characters]'
		valid: false
	},
	TestSignatureVerifySLHSHA2128s{
		pbk:   'de4dac64aebce7d48ba91cd715515d0fcc208da8d4e41c7a7f9d5bffb66a43c5'
		msg:   '8d00'
		sig:   '[encoded data, 15712 characters]'
		valid: true
	},
	TestSignatureVerifySLHSHA2128s{
		pbk:   'a21207c0f63475fdc0e4bb74236803352e3799b7915d3bd9d290dd1d2b2c330d'
		msg:   '04c1567025442898d1cff952156e13eb4d124f6e0107f6aeeacf2dd6056d0e388554c0173a291b0fbef8c7bee746b026ec30d66513d0edc5d991994053e791988ee199c594ff1c2378623eefcdd03f841080be97059c1a1e195517564d9371fde7948e27cbc0a6'
		sig:   '[encoded data, 15710 characters]'
		valid: false
	},
	TestSignatureVerifySLHSHA2128s{
		pbk:   '3dd6ad6dc886e370b5cb3909cded99e67292f34028295071c0b47a5bf92fd7ee'
		msg:   '3f'
		sig:   '[encoded data, 15712 characters]'
		valid: true
	},
	TestSignatureVerifySLHSHA2128s{
		pbk:   'ac376ce9c7700fd27039a75bb74622dc6ae1f7f99100f80bf4eba17614d9e3cd'
		msg:   '21e9a27edb1f76d2ad5a00b0623cc9e46b8d03d5ad44c58d962163d249ef166ef1c4a2abae6d0fbb4fd3b9b2a4801490c9bd21fc160f2bc49450ecebdb'
		sig:   '[encoded data, 15712 characters]'
		valid: false
	},
	TestSignatureVerifySLHSHA2128s{
		pbk:   '90c17eea2ee4ecf9e9158339844171fce6891d26c11de869a81fbb3622fdf9cf'
		msg:   'b897f15adc12154cee4751cf79acb6f3d31768a21a2a9414b9176a9a5bfb4855c06f707f65'
		sig:   '[encoded data, 15712 characters]'
		valid: false
	},
	TestSignatureVerifySLHSHA2128s{
		pbk:   '95ab9f808b34e34796c4f7b09c554bed61e8bb8800a12dda2fc80d0c45420c3b'
		msg:   '42f78db178ee85a9ad1131e87e4a0d'
		sig:   '[encoded data, 15714 characters]'
		valid: false
	},
]
