MCPcopy Create free account
hub / github.com/ElementsProject/elements / test_random_pubkeys

Function test_random_pubkeys

src/secp256k1/src/tests.c:6808–6866  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6806}
6807
6808static void test_random_pubkeys(void) {
6809 secp256k1_ge elem;
6810 secp256k1_ge elem2;
6811 unsigned char in[65];
6812 /* Generate some randomly sized pubkeys. */
6813 size_t len = secp256k1_testrand_bits(2) == 0 ? 65 : 33;
6814 if (secp256k1_testrand_bits(2) == 0) {
6815 len = secp256k1_testrand_bits(6);
6816 }
6817 if (len == 65) {
6818 in[0] = secp256k1_testrand_bits(1) ? 4 : (secp256k1_testrand_bits(1) ? 6 : 7);
6819 } else {
6820 in[0] = secp256k1_testrand_bits(1) ? 2 : 3;
6821 }
6822 if (secp256k1_testrand_bits(3) == 0) {
6823 in[0] = secp256k1_testrand_bits(8);
6824 }
6825 if (len > 1) {
6826 secp256k1_testrand256(&in[1]);
6827 }
6828 if (len > 33) {
6829 secp256k1_testrand256(&in[33]);
6830 }
6831 if (secp256k1_eckey_pubkey_parse(&elem, in, len)) {
6832 unsigned char out[65];
6833 unsigned char firstb;
6834 int res;
6835 size_t size = len;
6836 firstb = in[0];
6837 /* If the pubkey can be parsed, it should round-trip... */
6838 CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, len == 33));
6839 CHECK(size == len);
6840 CHECK(secp256k1_memcmp_var(&in[1], &out[1], len-1) == 0);
6841 /* ... except for the type of hybrid inputs. */
6842 if ((in[0] != 6) && (in[0] != 7)) {
6843 CHECK(in[0] == out[0]);
6844 }
6845 size = 65;
6846 CHECK(secp256k1_eckey_pubkey_serialize(&elem, in, &size, 0));
6847 CHECK(size == 65);
6848 CHECK(secp256k1_eckey_pubkey_parse(&elem2, in, size));
6849 ge_equals_ge(&elem,&elem2);
6850 /* Check that the X9.62 hybrid type is checked. */
6851 in[0] = secp256k1_testrand_bits(1) ? 6 : 7;
6852 res = secp256k1_eckey_pubkey_parse(&elem2, in, size);
6853 if (firstb == 2 || firstb == 3) {
6854 if (in[0] == firstb + 4) {
6855 CHECK(res);
6856 } else {
6857 CHECK(!res);
6858 }
6859 }
6860 if (res) {
6861 ge_equals_ge(&elem,&elem2);
6862 CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, 0));
6863 CHECK(secp256k1_memcmp_var(&in[1], &out[1], 64) == 0);
6864 }
6865 }

Callers 1

run_random_pubkeysFunction · 0.85

Calls 6

secp256k1_testrand_bitsFunction · 0.85
secp256k1_testrand256Function · 0.85
secp256k1_memcmp_varFunction · 0.70
ge_equals_geFunction · 0.70

Tested by

no test coverage detected