Add _prefix and _bip324 ellswift_xdh hash functions
This commit is contained in:
parent
9695deb351
commit
df633cdeba
@ -8,7 +8,7 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* This module provides an implementation of ElligatorSwift as well as a
|
/* This module provides an implementation of ElligatorSwift as well as a
|
||||||
* version of x-only ECDH using it.
|
* version of x-only ECDH using it (including compatibility with BIP324).
|
||||||
*
|
*
|
||||||
* ElligatorSwift is described in https://eprint.iacr.org/2022/759 by
|
* ElligatorSwift is described in https://eprint.iacr.org/2022/759 by
|
||||||
* Chavez-Saab, Rodriguez-Henriquez, and Tibouchi. It permits encoding
|
* Chavez-Saab, Rodriguez-Henriquez, and Tibouchi. It permits encoding
|
||||||
@ -67,6 +67,19 @@ typedef int (*secp256k1_ellswift_xdh_hash_function)(
|
|||||||
void *data
|
void *data
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/** An implementation of an secp256k1_ellswift_xdh_hash_function which uses
|
||||||
|
* SHA256(prefix64 || ell_a64 || ell_b64 || x32), where prefix64 is the 64-byte
|
||||||
|
* array pointed to by data. */
|
||||||
|
SECP256K1_API_VAR const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_prefix;
|
||||||
|
|
||||||
|
/** An implementation of an secp256k1_ellswift_xdh_hash_function compatible with
|
||||||
|
* BIP324. It returns H_tag(ell_a64 || ell_b64 || x32), where H_tag is the
|
||||||
|
* BIP340 tagged hash function with tag "bip324_ellswift_xonly_ecdh". Equivalent
|
||||||
|
* to secp256k1_ellswift_xdh_hash_function_prefix with prefix64 set to
|
||||||
|
* SHA256("bip324_ellswift_xonly_ecdh")||SHA256("bip324_ellswift_xonly_ecdh").
|
||||||
|
* The data argument is ignored. */
|
||||||
|
SECP256K1_API_VAR const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_bip324;
|
||||||
|
|
||||||
/** Construct a 64-byte ElligatorSwift encoding of a given pubkey.
|
/** Construct a 64-byte ElligatorSwift encoding of a given pubkey.
|
||||||
*
|
*
|
||||||
* Returns: 1 always.
|
* Returns: 1 always.
|
||||||
|
@ -498,6 +498,51 @@ int secp256k1_ellswift_decode(const secp256k1_context *ctx, secp256k1_pubkey *pu
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int ellswift_xdh_hash_function_prefix(unsigned char *output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data) {
|
||||||
|
secp256k1_sha256 sha;
|
||||||
|
|
||||||
|
secp256k1_sha256_initialize(&sha);
|
||||||
|
secp256k1_sha256_write(&sha, data, 64);
|
||||||
|
secp256k1_sha256_write(&sha, ell_a64, 64);
|
||||||
|
secp256k1_sha256_write(&sha, ell_b64, 64);
|
||||||
|
secp256k1_sha256_write(&sha, x32, 32);
|
||||||
|
secp256k1_sha256_finalize(&sha, output);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set hash state to the BIP340 tagged hash midstate for "bip324_ellswift_xonly_ecdh". */
|
||||||
|
static void secp256k1_ellswift_sha256_init_bip324(secp256k1_sha256* hash) {
|
||||||
|
secp256k1_sha256_initialize(hash);
|
||||||
|
hash->s[0] = 0x8c12d730ul;
|
||||||
|
hash->s[1] = 0x827bd392ul;
|
||||||
|
hash->s[2] = 0x9e4fb2eeul;
|
||||||
|
hash->s[3] = 0x207b373eul;
|
||||||
|
hash->s[4] = 0x2292bd7aul;
|
||||||
|
hash->s[5] = 0xaa5441bcul;
|
||||||
|
hash->s[6] = 0x15c3779ful;
|
||||||
|
hash->s[7] = 0xcfb52549ul;
|
||||||
|
|
||||||
|
hash->bytes = 64;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ellswift_xdh_hash_function_bip324(unsigned char* output, const unsigned char *x32, const unsigned char *ell_a64, const unsigned char *ell_b64, void *data) {
|
||||||
|
secp256k1_sha256 sha;
|
||||||
|
|
||||||
|
(void)data;
|
||||||
|
|
||||||
|
secp256k1_ellswift_sha256_init_bip324(&sha);
|
||||||
|
secp256k1_sha256_write(&sha, ell_a64, 64);
|
||||||
|
secp256k1_sha256_write(&sha, ell_b64, 64);
|
||||||
|
secp256k1_sha256_write(&sha, x32, 32);
|
||||||
|
secp256k1_sha256_finalize(&sha, output);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_prefix = ellswift_xdh_hash_function_prefix;
|
||||||
|
const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_bip324 = ellswift_xdh_hash_function_bip324;
|
||||||
|
|
||||||
int secp256k1_ellswift_xdh(const secp256k1_context *ctx, unsigned char *output, const unsigned char *ell_a64, const unsigned char *ell_b64, const unsigned char *seckey32, int party, secp256k1_ellswift_xdh_hash_function hashfp, void *data) {
|
int secp256k1_ellswift_xdh(const secp256k1_context *ctx, unsigned char *output, const unsigned char *ell_a64, const unsigned char *ell_b64, const unsigned char *seckey32, int party, secp256k1_ellswift_xdh_hash_function hashfp, void *data) {
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int overflow;
|
int overflow;
|
||||||
|
@ -21,6 +21,14 @@ struct ellswift_decode_test {
|
|||||||
int odd_y;
|
int odd_y;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ellswift_xdh_test {
|
||||||
|
unsigned char priv_ours[32];
|
||||||
|
unsigned char ellswift_ours[64];
|
||||||
|
unsigned char ellswift_theirs[64];
|
||||||
|
int initiating;
|
||||||
|
unsigned char shared_secret[32];
|
||||||
|
};
|
||||||
|
|
||||||
/* Set of (point, encodings) test vectors, selected to maximize branch coverage, part of the BIP324
|
/* Set of (point, encodings) test vectors, selected to maximize branch coverage, part of the BIP324
|
||||||
* test vectors. Created using an independent implementation, and tested decoding against paper
|
* test vectors. Created using an independent implementation, and tested decoding against paper
|
||||||
* authors' code. */
|
* authors' code. */
|
||||||
@ -141,6 +149,19 @@ static const struct ellswift_decode_test ellswift_decode_tests[] = {
|
|||||||
{{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, 0xfe, 0xfb, 0xb9, 0x82, 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, 0xf6, 0xd6, 0xdb, 0x1f}, SECP256K1_FE_CONST(0x1c92ccdf, 0xcf4ac550, 0xc28db57c, 0xff0c8515, 0xcb26936c, 0x786584a7, 0x0114008d, 0x6c33a34b), 0},
|
{{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, 0xfe, 0xfb, 0xb9, 0x82, 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, 0xf6, 0xd6, 0xdb, 0x1f}, SECP256K1_FE_CONST(0x1c92ccdf, 0xcf4ac550, 0xc28db57c, 0xff0c8515, 0xcb26936c, 0x786584a7, 0x0114008d, 0x6c33a34b), 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Set of expected ellswift_xdh BIP324 shared secrets, given private key, encodings, initiating,
|
||||||
|
* taken from the BIP324 test vectors. Created using an independent implementation, and tested
|
||||||
|
* against the paper authors' decoding code. */
|
||||||
|
static const struct ellswift_xdh_test ellswift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, 0x1a, 0x62, 0xea, 0xee, 0x1a, 0x5a, 0xca, 0xa8, 0x45, 0x9a, 0x35, 0x92}},
|
||||||
|
{{0x1f, 0x9c, 0x58, 0x1b, 0x35, 0x23, 0x18, 0x38, 0xf0, 0xf1, 0x7c, 0xf0, 0xc9, 0x79, 0x83, 0x5b, 0xac, 0xcb, 0x7f, 0x3a, 0xbb, 0xbb, 0x96, 0xff, 0xcc, 0x31, 0x8a, 0xb7, 0x1e, 0x6e, 0x12, 0x6f}, {0xa1, 0x85, 0x5e, 0x10, 0xe9, 0x4e, 0x00, 0xba, 0xa2, 0x30, 0x41, 0xd9, 0x16, 0xe2, 0x59, 0xf7, 0x04, 0x4e, 0x49, 0x1d, 0xa6, 0x17, 0x12, 0x69, 0x69, 0x47, 0x63, 0xf0, 0x18, 0xc7, 0xe6, 0x36, 0x93, 0xd2, 0x95, 0x75, 0xdc, 0xb4, 0x64, 0xac, 0x81, 0x6b, 0xaa, 0x1b, 0xe3, 0x53, 0xba, 0x12, 0xe3, 0x87, 0x6c, 0xba, 0x76, 0x28, 0xbd, 0x0b, 0xd8, 0xe7, 0x55, 0xe7, 0x21, 0xeb, 0x01, 0x40}, {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, 0xfe, 0xff, 0xff, 0xfc, 0x2f, 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, 0x00}, 0, {0xa0, 0x13, 0x8f, 0x56, 0x4f, 0x74, 0xd0, 0xad, 0x70, 0xbc, 0x33, 0x7d, 0xac, 0xc9, 0xd0, 0xbf,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7a, 0xa9, 0xd8, 0x3a}, 0, {0x19, 0x18, 0xb7, 0x41, 0xef, 0x5f, 0x9d, 0x1d, 0x76, 0x70, 0xb0, 0x50, 0xc1, 0x52, 0xb4, 0xa4, 0xea, 0xd2, 0xc3, 0x1b, 0xe9, 0xae, 0xcb, 0x06, 0x81, 0xc0, 0xcd, 0x43, 0x24, 0x15, 0x08, 0x53}},
|
||||||
|
{{0xa6, 0xec, 0x25, 0x12, 0x7c, 0xa1, 0xaa, 0x4c, 0xf1, 0x6b, 0x20, 0x08, 0x4b, 0xa1, 0xe6, 0x51, 0x6b, 0xaa, 0xe4, 0xd3, 0x24, 0x22, 0x28, 0x8e, 0x9b, 0x36, 0xd8, 0xbd, 0xdd, 0x2d, 0xe3, 0x5a}, {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, 0x05, 0x3d, 0x7e, 0xcc, 0xa5, 0x3e, 0x33, 0xe1, 0x85, 0xa8, 0xb9, 0xbe, 0x4e, 0x76, 0x99, 0xa9, 0x7c, 0x6f, 0xf4, 0xc7, 0x95, 0x52, 0x2e, 0x59, 0x18, 0xab, 0x7c, 0xd6, 0xb6, 0x88, 0x4f, 0x67, 0xe6, 0x83, 0xf3, 0xdc}, {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, 0xa7, 0x73, 0x0b, 0xe3, 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, 0x00}, 1, {0xdd, 0x21, 0x0a, 0xa6, 0x62, 0x9f, 0x20, 0xbb, 0x32, 0x8e, 0x5d, 0x89, 0xda, 0xa6, 0xeb, 0x2a, 0xc3, 0xd1, 0xc6, 0x58, 0xa7, 0x25, 0x53, 0x6f, 0xf1, 0x54, 0xf3, 0x1b, 0x53, 0x6c, 0x23, 0xb2}},
|
||||||
|
{{0x0a, 0xf9, 0x52, 0x65, 0x9e, 0xd7, 0x6f, 0x80, 0xf5, 0x85, 0x96, 0x6b, 0x95, 0xab, 0x6e, 0x6f, 0xd6, 0x86, 0x54, 0x67, 0x28, 0x27, 0x87, 0x86, 0x84, 0xc8, 0xb5, 0x47, 0xb1, 0xb9, 0x4f, 0x5a}, {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, 0xc8, 0x10, 0x17, 0xfd, 0x92, 0xfd, 0x31, 0x63, 0x7c, 0x26, 0xc9, 0x06, 0xb4, 0x20, 0x92, 0xe1, 0x1c, 0xc0, 0xd3, 0xaf, 0xae, 0x8d, 0x90, 0x19, 0xd2, 0x57, 0x8a, 0xf2, 0x27, 0x35, 0xce, 0x7b, 0xc4, 0x69, 0xc7, 0x2d}, {0x96, 0x52, 0xd7, 0x8b, 0xae, 0xfc, 0x02, 0x8c, 0xd3, 0x7a, 0x6a, 0x92, 0x62, 0x5b, 0x8b, 0x8f, 0x85, 0xfd, 0xe1, 0xe4, 0xc9, 0x44, 0xad, 0x3f, 0x20, 0xe1, 0x98, 0xbe, 0xf8, 0xc0, 0x2f, 0x19, 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, 0xf2, 0xe9, 0x18, 0x70}, 0, {0x35, 0x68, 0xf2, 0xae, 0xa2, 0xe1, 0x4e, 0xf4, 0xee, 0x4a, 0x3c, 0x2a, 0x8b, 0x8d, 0x31, 0xbc, 0x5e, 0x31, 0x87, 0xba, 0x86, 0xdb, 0x10, 0x73, 0x9b, 0x4f, 0xf8, 0xec, 0x92, 0xff, 0x66, 0x55}},
|
||||||
|
{{0xf9, 0x0e, 0x08, 0x0c, 0x64, 0xb0, 0x58, 0x24, 0xc5, 0xa2, 0x4b, 0x25, 0x01, 0xd5, 0xae, 0xaf, 0x08, 0xaf, 0x38, 0x72, 0xee, 0x86, 0x0a, 0xa8, 0x0b, 0xdc, 0xd4, 0x30, 0xf7, 0xb6, 0x34, 0x94}, {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, 0x11, 0x51, 0x73, 0x76, 0x5d, 0xc2, 0x02, 0xcf, 0x02, 0x9a, 0xd3, 0xf1, 0x54, 0x79, 0x73, 0x5d, 0x57, 0x69, 0x7a, 0xf1, 0x2b, 0x01, 0x31, 0xdd, 0x21, 0x43, 0x0d, 0x57, 0x72, 0xe4, 0xef, 0x11, 0x47, 0x4d, 0x58, 0xb9}, {0x12, 0xa5, 0x0f, 0x3f, 0xaf, 0xea, 0x7c, 0x1e, 0xea, 0xda, 0x4c, 0xf8, 0xd3, 0x37, 0x77, 0x70, 0x4b, 0x77, 0x36, 0x14, 0x53, 0xaf, 0xc8, 0x3b, 0xda, 0x91, 0xee, 0xf3, 0x49, 0xae, 0x04, 0x4d, 0x20, 0x12, 0x6c, 0x62, 0x00, 0x54, 0x7e, 0xa5, 0xa6, 0x91, 0x17, 0x76, 0xc0, 0x5d, 0xee, 0x2a, 0x7f, 0x1a, 0x9b, 0xa7, 0xdf, 0xba, 0xbb, 0xbd, 0x27, 0x3c, 0x3e, 0xf2, 0x9e, 0xf4, 0x6e, 0x46}, 1, {0xe2, 0x54, 0x61, 0xfb, 0x0e, 0x4c, 0x16, 0x2e, 0x18, 0x12, 0x3e, 0xcd, 0xe8, 0x83, 0x42, 0xd5, 0x4d, 0x44, 0x96, 0x31, 0xe9, 0xb7, 0x5a, 0x26, 0x6f, 0xd9, 0x26, 0x0c, 0x2b, 0xb2, 0xf4, 0x1d}},
|
||||||
|
};
|
||||||
|
|
||||||
/** This is a hasher for ellswift_xdh which just returns the shared X coordinate.
|
/** This is a hasher for ellswift_xdh which just returns the shared X coordinate.
|
||||||
*
|
*
|
||||||
* This is generally a bad idea as it means changes to the encoding of the
|
* This is generally a bad idea as it means changes to the encoding of the
|
||||||
@ -185,6 +206,22 @@ void run_ellswift_tests(void) {
|
|||||||
CHECK(check_fe_equal(&testcase->x, &ge.x));
|
CHECK(check_fe_equal(&testcase->x, &ge.x));
|
||||||
CHECK(secp256k1_fe_is_odd(&ge.y) == testcase->odd_y);
|
CHECK(secp256k1_fe_is_odd(&ge.y) == testcase->odd_y);
|
||||||
}
|
}
|
||||||
|
for (i = 0; (unsigned)i < sizeof(ellswift_xdh_tests_bip324) / sizeof(ellswift_xdh_tests_bip324[0]); ++i) {
|
||||||
|
const struct ellswift_xdh_test *test = &ellswift_xdh_tests_bip324[i];
|
||||||
|
unsigned char shared_secret[32];
|
||||||
|
int ret;
|
||||||
|
int party = !test->initiating;
|
||||||
|
const unsigned char* ell_a64 = party ? test->ellswift_theirs : test->ellswift_ours;
|
||||||
|
const unsigned char* ell_b64 = party ? test->ellswift_ours : test->ellswift_theirs;
|
||||||
|
ret = secp256k1_ellswift_xdh(CTX, shared_secret,
|
||||||
|
ell_a64, ell_b64,
|
||||||
|
test->priv_ours,
|
||||||
|
party,
|
||||||
|
secp256k1_ellswift_xdh_hash_function_bip324,
|
||||||
|
NULL);
|
||||||
|
CHECK(ret);
|
||||||
|
CHECK(secp256k1_memcmp_var(shared_secret, test->shared_secret, 32) == 0);
|
||||||
|
}
|
||||||
/* Verify that secp256k1_ellswift_encode + decode roundtrips. */
|
/* Verify that secp256k1_ellswift_encode + decode roundtrips. */
|
||||||
for (i = 0; i < 1000 * COUNT; i++) {
|
for (i = 0; i < 1000 * COUNT; i++) {
|
||||||
unsigned char rnd32[32];
|
unsigned char rnd32[32];
|
||||||
@ -257,13 +294,30 @@ void run_ellswift_tests(void) {
|
|||||||
}
|
}
|
||||||
/* Verify the joint behavior of secp256k1_ellswift_xdh */
|
/* Verify the joint behavior of secp256k1_ellswift_xdh */
|
||||||
for (i = 0; i < 200 * COUNT; i++) {
|
for (i = 0; i < 200 * COUNT; i++) {
|
||||||
unsigned char auxrnd32a[32], auxrnd32b[32];
|
unsigned char auxrnd32a[32], auxrnd32b[32], auxrnd32a_bad[32], auxrnd32b_bad[32];
|
||||||
unsigned char sec32a[32], sec32b[32], sec32a_bad[32], sec32b_bad[32];
|
unsigned char sec32a[32], sec32b[32], sec32a_bad[32], sec32b_bad[32];
|
||||||
secp256k1_scalar seca, secb;
|
secp256k1_scalar seca, secb;
|
||||||
unsigned char ell64a[64], ell64b[64], ell64a_bad[64], ell64b_bad[64];
|
unsigned char ell64a[64], ell64b[64], ell64a_bad[64], ell64b_bad[64];
|
||||||
unsigned char share32a[32], share32b[32], share32_bad[32];
|
unsigned char share32a[32], share32b[32], share32_bad[32];
|
||||||
|
unsigned char prefix64[64];
|
||||||
|
secp256k1_ellswift_xdh_hash_function hash_function;
|
||||||
|
void* data;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
/* Pick hasher to use. */
|
||||||
|
if ((i % 3) == 0) {
|
||||||
|
hash_function = ellswift_xdh_hash_x32;
|
||||||
|
data = NULL;
|
||||||
|
} else if ((i % 3) == 1) {
|
||||||
|
hash_function = secp256k1_ellswift_xdh_hash_function_bip324;
|
||||||
|
data = NULL;
|
||||||
|
} else {
|
||||||
|
hash_function = secp256k1_ellswift_xdh_hash_function_prefix;
|
||||||
|
secp256k1_testrand256_test(prefix64);
|
||||||
|
secp256k1_testrand256_test(prefix64 + 32);
|
||||||
|
data = prefix64;
|
||||||
|
}
|
||||||
|
|
||||||
/* Generate random secret keys and random randomizers. */
|
/* Generate random secret keys and random randomizers. */
|
||||||
secp256k1_testrand256_test(auxrnd32a);
|
secp256k1_testrand256_test(auxrnd32a);
|
||||||
secp256k1_testrand256_test(auxrnd32b);
|
secp256k1_testrand256_test(auxrnd32b);
|
||||||
@ -282,10 +336,10 @@ void run_ellswift_tests(void) {
|
|||||||
|
|
||||||
/* Compute the shared secret both ways and compare with each other. */
|
/* Compute the shared secret both ways and compare with each other. */
|
||||||
/* For A: */
|
/* For A: */
|
||||||
ret = secp256k1_ellswift_xdh(CTX, share32a, ell64a, ell64b, sec32a, 0, &ellswift_xdh_hash_x32, NULL);
|
ret = secp256k1_ellswift_xdh(CTX, share32a, ell64a, ell64b, sec32a, 0, hash_function, data);
|
||||||
CHECK(ret);
|
CHECK(ret);
|
||||||
/* For B: */
|
/* For B: */
|
||||||
ret = secp256k1_ellswift_xdh(CTX, share32b, ell64a, ell64b, sec32b, 1, &ellswift_xdh_hash_x32, NULL);
|
ret = secp256k1_ellswift_xdh(CTX, share32b, ell64a, ell64b, sec32b, 1, hash_function, data);
|
||||||
CHECK(ret);
|
CHECK(ret);
|
||||||
/* And compare: */
|
/* And compare: */
|
||||||
CHECK(secp256k1_memcmp_var(share32a, share32b, 32) == 0);
|
CHECK(secp256k1_memcmp_var(share32a, share32b, 32) == 0);
|
||||||
@ -294,13 +348,13 @@ void run_ellswift_tests(void) {
|
|||||||
/* For A (using a bad public key for B): */
|
/* For A (using a bad public key for B): */
|
||||||
memcpy(ell64b_bad, ell64b, sizeof(ell64a_bad));
|
memcpy(ell64b_bad, ell64b, sizeof(ell64a_bad));
|
||||||
secp256k1_testrand_flip(ell64b_bad, sizeof(ell64b_bad));
|
secp256k1_testrand_flip(ell64b_bad, sizeof(ell64b_bad));
|
||||||
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, &ellswift_xdh_hash_x32, NULL);
|
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, hash_function, data);
|
||||||
CHECK(ret); /* Mismatching encodings don't get detected by secp256k1_ellswift_xdh. */
|
CHECK(ret); /* Mismatching encodings don't get detected by secp256k1_ellswift_xdh. */
|
||||||
CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
|
CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
|
||||||
/* For B (using a bad public key for A): */
|
/* For B (using a bad public key for A): */
|
||||||
memcpy(ell64a_bad, ell64a, sizeof(ell64a_bad));
|
memcpy(ell64a_bad, ell64a, sizeof(ell64a_bad));
|
||||||
secp256k1_testrand_flip(ell64a_bad, sizeof(ell64a_bad));
|
secp256k1_testrand_flip(ell64a_bad, sizeof(ell64a_bad));
|
||||||
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, &ellswift_xdh_hash_x32, NULL);
|
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, hash_function, data);
|
||||||
CHECK(ret);
|
CHECK(ret);
|
||||||
CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
|
CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
|
||||||
|
|
||||||
@ -308,13 +362,43 @@ void run_ellswift_tests(void) {
|
|||||||
/* For A: */
|
/* For A: */
|
||||||
memcpy(sec32a_bad, sec32a, sizeof(sec32a_bad));
|
memcpy(sec32a_bad, sec32a, sizeof(sec32a_bad));
|
||||||
secp256k1_testrand_flip(sec32a_bad, sizeof(sec32a_bad));
|
secp256k1_testrand_flip(sec32a_bad, sizeof(sec32a_bad));
|
||||||
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32a_bad, 0, &ellswift_xdh_hash_x32, NULL);
|
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32a_bad, 0, hash_function, data);
|
||||||
CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
|
CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
|
||||||
/* For B: */
|
/* For B: */
|
||||||
memcpy(sec32b_bad, sec32b, sizeof(sec32b_bad));
|
memcpy(sec32b_bad, sec32b, sizeof(sec32b_bad));
|
||||||
secp256k1_testrand_flip(sec32b_bad, sizeof(sec32b_bad));
|
secp256k1_testrand_flip(sec32b_bad, sizeof(sec32b_bad));
|
||||||
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32b_bad, 1, &ellswift_xdh_hash_x32, NULL);
|
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32b_bad, 1, hash_function, data);
|
||||||
CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
|
CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
|
||||||
|
|
||||||
|
if (hash_function != ellswift_xdh_hash_x32) {
|
||||||
|
/* Verify that the shared secret doesn't match when a different encoding of the same public key is used. */
|
||||||
|
/* For A (changing B's public key): */
|
||||||
|
memcpy(auxrnd32b_bad, auxrnd32b, sizeof(auxrnd32b_bad));
|
||||||
|
secp256k1_testrand_flip(auxrnd32b_bad, sizeof(auxrnd32b_bad));
|
||||||
|
ret = secp256k1_ellswift_create(CTX, ell64b_bad, sec32b, auxrnd32b_bad);
|
||||||
|
CHECK(ret);
|
||||||
|
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, hash_function, data);
|
||||||
|
CHECK(ret);
|
||||||
|
CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
|
||||||
|
/* For B (changing A's public key): */
|
||||||
|
memcpy(auxrnd32a_bad, auxrnd32a, sizeof(auxrnd32a_bad));
|
||||||
|
secp256k1_testrand_flip(auxrnd32a_bad, sizeof(auxrnd32a_bad));
|
||||||
|
ret = secp256k1_ellswift_create(CTX, ell64a_bad, sec32a, auxrnd32a_bad);
|
||||||
|
CHECK(ret);
|
||||||
|
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, hash_function, data);
|
||||||
|
CHECK(ret);
|
||||||
|
CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
|
||||||
|
|
||||||
|
/* Verify that swapping sides changes the shared secret. */
|
||||||
|
/* For A (claiming to be B): */
|
||||||
|
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32a, 1, hash_function, data);
|
||||||
|
CHECK(ret);
|
||||||
|
CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
|
||||||
|
/* For B (claiming to be A): */
|
||||||
|
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32b, 0, hash_function, data);
|
||||||
|
CHECK(ret);
|
||||||
|
CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Test hash initializers. */
|
/* Test hash initializers. */
|
||||||
@ -322,6 +406,7 @@ void run_ellswift_tests(void) {
|
|||||||
secp256k1_sha256 sha, sha_optimized;
|
secp256k1_sha256 sha, sha_optimized;
|
||||||
static const unsigned char encode_tag[25] = "secp256k1_ellswift_encode";
|
static const unsigned char encode_tag[25] = "secp256k1_ellswift_encode";
|
||||||
static const unsigned char create_tag[25] = "secp256k1_ellswift_create";
|
static const unsigned char create_tag[25] = "secp256k1_ellswift_create";
|
||||||
|
static const unsigned char bip324_tag[26] = "bip324_ellswift_xonly_ecdh";
|
||||||
|
|
||||||
/* Check that hash initialized by
|
/* Check that hash initialized by
|
||||||
* secp256k1_ellswift_sha256_init_encode has the expected
|
* secp256k1_ellswift_sha256_init_encode has the expected
|
||||||
@ -336,6 +421,13 @@ void run_ellswift_tests(void) {
|
|||||||
secp256k1_sha256_initialize_tagged(&sha, create_tag, sizeof(create_tag));
|
secp256k1_sha256_initialize_tagged(&sha, create_tag, sizeof(create_tag));
|
||||||
secp256k1_ellswift_sha256_init_create(&sha_optimized);
|
secp256k1_ellswift_sha256_init_create(&sha_optimized);
|
||||||
test_sha256_eq(&sha, &sha_optimized);
|
test_sha256_eq(&sha, &sha_optimized);
|
||||||
|
|
||||||
|
/* Check that hash initialized by
|
||||||
|
* secp256k1_ellswift_sha256_init_bip324 has the expected
|
||||||
|
* state. */
|
||||||
|
secp256k1_sha256_initialize_tagged(&sha, bip324_tag, sizeof(bip324_tag));
|
||||||
|
secp256k1_ellswift_sha256_init_bip324(&sha_optimized);
|
||||||
|
test_sha256_eq(&sha, &sha_optimized);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user