Path: blob/main/crypto/libecc/src/tests/bip0340_test_vectors.h
34878 views
/*1* Copyright (C) 2022 - This file is part of libecc project2*3* Authors:4* Arnaud EBALARD <[email protected]>5* Ryad BENADJILA <[email protected]>6*7* This software is licensed under a dual BSD and GPL v2 license.8* See LICENSE file at the root folder of the project.9*/10#ifndef __BIP0340_TEST_VECTORS_H__11#define __BIP0340_TEST_VECTORS_H__1213#if defined(WITH_HASH_SHA256) && defined(WITH_CURVE_SECP256K1)14/************************************************/15static const u8 bip0340_1_test_vectors_priv_key[] = {160x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,17};18static const u8 bip0340_1_test_vectors_expected_sig[] = {190xE9, 0x07, 0x83, 0x1F, 0x80, 0x84, 0x8D, 0x10, 0x69, 0xA5, 0x37, 0x1B, 0x40, 0x24, 0x10, 0x36, 0x4B, 0xDF, 0x1C, 0x5F, 0x83, 0x07, 0xB0, 0x08, 0x4C, 0x55, 0xF1, 0xCE, 0x2D, 0xCA, 0x82, 0x15, 0x25, 0xF6, 0x6A, 0x4A, 0x85, 0xEA, 0x8B, 0x71, 0xE4, 0x82, 0xA7, 0x4F, 0x38, 0x2D, 0x2C, 0xE5, 0xEB, 0xEE, 0xE8, 0xFD, 0xB2, 0x17, 0x2F, 0x47, 0x7D, 0xF4, 0x90, 0x0D, 0x31, 0x05, 0x36, 0xC0,20};21static int bip0340_1_nn_random_test_vector(nn_t out, nn_src_t q)22{23int ret, cmp;2425/*26* Fixed auxiliary random for BIP034027* Test vectors from:28* https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv29*/30const u8 k_buf[] = {310x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,32};3334ret = nn_init_from_buf(out, k_buf, sizeof(k_buf)); EG(ret, err);35ret = nn_cmp(out, q, &cmp); EG(ret, err);3637ret = (cmp >= 0) ? -1 : 0;3839err:40return ret;41}4243static const ec_test_case bip0340_1_test_case = {44.name = "BIP0340-SHA256/secp256k1 1",45.ec_str_p = &secp256k1_str_params,46.priv_key = bip0340_1_test_vectors_priv_key,47.priv_key_len = sizeof(bip0340_1_test_vectors_priv_key),48.nn_random = bip0340_1_nn_random_test_vector,49.hash_type = SHA256,50.msg = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",51.msglen = 32,52.sig_type = BIP0340,53.exp_sig = bip0340_1_test_vectors_expected_sig,54.exp_siglen = sizeof(bip0340_1_test_vectors_expected_sig),55.adata = NULL,56.adata_len = 057};5859/************************************************/60static const u8 bip0340_2_test_vectors_priv_key[] = {610xB7, 0xE1, 0x51, 0x62, 0x8A, 0xED, 0x2A, 0x6A, 0xBF, 0x71, 0x58, 0x80, 0x9C, 0xF4, 0xF3, 0xC7, 0x62, 0xE7, 0x16, 0x0F, 0x38, 0xB4, 0xDA, 0x56, 0xA7, 0x84, 0xD9, 0x04, 0x51, 0x90, 0xCF, 0xEF,62};63static const u8 bip0340_2_test_vectors_expected_sig[] = {640x68, 0x96, 0xBD, 0x60, 0xEE, 0xAE, 0x29, 0x6D, 0xB4, 0x8A, 0x22, 0x9F, 0xF7, 0x1D, 0xFE, 0x07, 0x1B, 0xDE, 0x41, 0x3E, 0x6D, 0x43, 0xF9, 0x17, 0xDC, 0x8D, 0xCF, 0x8C, 0x78, 0xDE, 0x33, 0x41, 0x89, 0x06, 0xD1, 0x1A, 0xC9, 0x76, 0xAB, 0xCC, 0xB2, 0x0B, 0x09, 0x12, 0x92, 0xBF, 0xF4, 0xEA, 0x89, 0x7E, 0xFC, 0xB6, 0x39, 0xEA, 0x87, 0x1C, 0xFA, 0x95, 0xF6, 0xDE, 0x33, 0x9E, 0x4B, 0x0A,65};66static int bip0340_2_nn_random_test_vector(nn_t out, nn_src_t q)67{68int ret, cmp;6970/*71* Fixed auxiliary random for BIP034072* Test vectors from:73* https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv74*/75const u8 k_buf[] = {760x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,77};7879ret = nn_init_from_buf(out, k_buf, sizeof(k_buf)); EG(ret, err);80ret = nn_cmp(out, q, &cmp); EG(ret, err);8182ret = (cmp >= 0) ? -1 : 0;8384err:85return ret;86}8788static const ec_test_case bip0340_2_test_case = {89.name = "BIP0340-SHA256/secp256k1 2",90.ec_str_p = &secp256k1_str_params,91.priv_key = bip0340_2_test_vectors_priv_key,92.priv_key_len = sizeof(bip0340_2_test_vectors_priv_key),93.nn_random = bip0340_2_nn_random_test_vector,94.hash_type = SHA256,95.msg = "\x24\x3F\x6A\x88\x85\xA3\x08\xD3\x13\x19\x8A\x2E\x03\x70\x73\x44\xA4\x09\x38\x22\x29\x9F\x31\xD0\x08\x2E\xFA\x98\xEC\x4E\x6C\x89",96.msglen = 32,97.sig_type = BIP0340,98.exp_sig = bip0340_2_test_vectors_expected_sig,99.exp_siglen = sizeof(bip0340_2_test_vectors_expected_sig),100.adata = NULL,101.adata_len = 0102};103104/************************************************/105static const u8 bip0340_3_test_vectors_priv_key[] = {1060xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x14, 0xE5, 0xC9,107};108static const u8 bip0340_3_test_vectors_expected_sig[] = {1090x58, 0x31, 0xAA, 0xEE, 0xD7, 0xB4, 0x4B, 0xB7, 0x4E, 0x5E, 0xAB, 0x94, 0xBA, 0x9D, 0x42, 0x94, 0xC4, 0x9B, 0xCF, 0x2A, 0x60, 0x72, 0x8D, 0x8B, 0x4C, 0x20, 0x0F, 0x50, 0xDD, 0x31, 0x3C, 0x1B, 0xAB, 0x74, 0x58, 0x79, 0xA5, 0xAD, 0x95, 0x4A, 0x72, 0xC4, 0x5A, 0x91, 0xC3, 0xA5, 0x1D, 0x3C, 0x7A, 0xDE, 0xA9, 0x8D, 0x82, 0xF8, 0x48, 0x1E, 0x0E, 0x1E, 0x03, 0x67, 0x4A, 0x6F, 0x3F, 0xB7,110};111static int bip0340_3_nn_random_test_vector(nn_t out, nn_src_t q)112{113int ret, cmp;114115/*116* Fixed auxiliary random for BIP0340117* Test vectors from:118* https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv119*/120const u8 k_buf[] = {1210xC8, 0x7A, 0xA5, 0x38, 0x24, 0xB4, 0xD7, 0xAE, 0x2E, 0xB0, 0x35, 0xA2, 0xB5, 0xBB, 0xBC, 0xCC, 0x08, 0x0E, 0x76, 0xCD, 0xC6, 0xD1, 0x69, 0x2C, 0x4B, 0x0B, 0x62, 0xD7, 0x98, 0xE6, 0xD9, 0x06,122};123124ret = nn_init_from_buf(out, k_buf, sizeof(k_buf)); EG(ret, err);125ret = nn_cmp(out, q, &cmp); EG(ret, err);126127ret = (cmp >= 0) ? -1 : 0;128129err:130return ret;131}132133static const ec_test_case bip0340_3_test_case = {134.name = "BIP0340-SHA256/secp256k1 3",135.ec_str_p = &secp256k1_str_params,136.priv_key = bip0340_3_test_vectors_priv_key,137.priv_key_len = sizeof(bip0340_3_test_vectors_priv_key),138.nn_random = bip0340_3_nn_random_test_vector,139.hash_type = SHA256,140.msg = "\x7E\x2D\x58\xD8\xB3\xBC\xDF\x1A\xBA\xDE\xC7\x82\x90\x54\xF9\x0D\xDA\x98\x05\xAA\xB5\x6C\x77\x33\x30\x24\xB9\xD0\xA5\x08\xB7\x5C",141.msglen = 32,142.sig_type = BIP0340,143.exp_sig = bip0340_3_test_vectors_expected_sig,144.exp_siglen = sizeof(bip0340_3_test_vectors_expected_sig),145.adata = NULL,146.adata_len = 0147};148149/************************************************/150static const u8 bip0340_4_test_vectors_priv_key[] = {1510x0B, 0x43, 0x2B, 0x26, 0x77, 0x93, 0x73, 0x81, 0xAE, 0xF0, 0x5B, 0xB0, 0x2A, 0x66, 0xEC, 0xD0, 0x12, 0x77, 0x30, 0x62, 0xCF, 0x3F, 0xA2, 0x54, 0x9E, 0x44, 0xF5, 0x8E, 0xD2, 0x40, 0x17, 0x10,152};153static const u8 bip0340_4_test_vectors_expected_sig[] = {1540x7E, 0xB0, 0x50, 0x97, 0x57, 0xE2, 0x46, 0xF1, 0x94, 0x49, 0x88, 0x56, 0x51, 0x61, 0x1C, 0xB9, 0x65, 0xEC, 0xC1, 0xA1, 0x87, 0xDD, 0x51, 0xB6, 0x4F, 0xDA, 0x1E, 0xDC, 0x96, 0x37, 0xD5, 0xEC, 0x97, 0x58, 0x2B, 0x9C, 0xB1, 0x3D, 0xB3, 0x93, 0x37, 0x05, 0xB3, 0x2B, 0xA9, 0x82, 0xAF, 0x5A, 0xF2, 0x5F, 0xD7, 0x88, 0x81, 0xEB, 0xB3, 0x27, 0x71, 0xFC, 0x59, 0x22, 0xEF, 0xC6, 0x6E, 0xA3,155};156static int bip0340_4_nn_random_test_vector(nn_t out, nn_src_t q)157{158int ret, cmp;159160/*161* Fixed auxiliary random for BIP0340162* Test vectors from:163* https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv164*/165const u8 k_buf[] = {1660xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,167};168169ret = nn_init_from_buf(out, k_buf, sizeof(k_buf)); EG(ret, err);170ret = nn_cmp(out, q, &cmp); EG(ret, err);171172ret = (cmp >= 0) ? -1 : 0;173174err:175return ret;176}177178static const ec_test_case bip0340_4_test_case = {179.name = "BIP0340-SHA256/secp256k1 4",180.ec_str_p = &secp256k1_str_params,181.priv_key = bip0340_4_test_vectors_priv_key,182.priv_key_len = sizeof(bip0340_4_test_vectors_priv_key),183.nn_random = bip0340_4_nn_random_test_vector,184.hash_type = SHA256,185.msg = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",186.msglen = 32,187.sig_type = BIP0340,188.exp_sig = bip0340_4_test_vectors_expected_sig,189.exp_siglen = sizeof(bip0340_4_test_vectors_expected_sig),190.adata = NULL,191.adata_len = 0192};193194#endif195196/************************************************/197#define BIP0340_ALL_TESTS() \198&bip0340_1_test_case, \199&bip0340_2_test_case, \200&bip0340_3_test_case, \201&bip0340_4_test_case,202203#endif /* __BIP0340_TEST_VECTORS_H__ */204205206