Last active
November 13, 2024 12:16
-
-
Save randombit/fd2ad31e5de9211820dd248b73fb27ba to your computer and use it in GitHub Desktop.
Generate numsp512d1 test vectors using OpenSSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define OPENSSL_SUPPRESS_DEPRECATED | |
#include <openssl/bn.h> | |
#include <openssl/ec.h> | |
#include <stdio.h> | |
void check_rc(const char* where, int rc, int expected_rc = 1) { | |
if(rc != expected_rc) { | |
printf("%s returned %d\n", where, rc); | |
exit(1); | |
} | |
} | |
BIGNUM* hex2bn(const char* s) { | |
auto bn = BN_new(); | |
int rc = BN_hex2bn(&bn, s); | |
if(rc == 0) { printf("hex2bn failed"); exit(1); } | |
return bn; | |
} | |
void dump(const char* what, BIGNUM* n) { | |
printf("%s = %s\n", what, BN_bn2hex(n)); | |
} | |
void dump(const char* what, EC_GROUP* group, EC_POINT* p, BN_CTX* ctx) { | |
uint8_t buf[256] = { 0 }; | |
size_t written = EC_POINT_point2oct(group, p, POINT_CONVERSION_UNCOMPRESSED, | |
buf, sizeof(buf), ctx); | |
printf("%s = ", what); | |
for(size_t i = 0; i != written; ++i) { | |
printf("%02X", buf[i]); | |
} | |
printf("\n"); | |
} | |
int main() { | |
auto meth = EC_GFp_mont_method(); | |
auto group = EC_GROUP_new(meth); | |
auto bn_ctx = BN_CTX_new(); | |
auto p = hex2bn("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\ | |
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7"); | |
auto a = hex2bn("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\ | |
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC4"); | |
auto b = hex2bn("1D99B"); | |
auto gx = hex2bn("2"); | |
auto gy = hex2bn("1C282EB23327F9711952C250EA61AD53FCC13031CF6DD336E0B9328433AFBDD8CC5A1C1F0C716FDC724DDE537\ | |
C2B0ADB00BB3D08DC83755B205CC30D7F83CF28"); | |
auto order = hex2bn("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5B3CA4FB94E7831B4FC258\ | |
ED97D0BDC63B568B36607CD243CE153F390433555D"); | |
auto cofactor = hex2bn("1"); | |
const size_t bits = BN_num_bits(order); | |
check_rc("EC_GROUP_set_curve", EC_GROUP_set_curve(group, p, a, b, bn_ctx)); | |
auto generator = EC_POINT_new(group); | |
check_rc("EC_POINT_set_affine_coordinates", | |
EC_POINT_set_affine_coordinates(group, generator, gx, gy, bn_ctx), | |
1); | |
check_rc("EC_GROUP_set_generator", EC_GROUP_set_generator(group, generator, order, cofactor)); | |
check_rc("EC_GROUP_precompute_mult", EC_GROUP_precompute_mult(group, bn_ctx)); | |
for(size_t i = 1; i != 2; ++i) { | |
EC_POINT* r = EC_POINT_new(group); | |
BIGNUM* n = BN_new(); | |
BN_lshift1(n, order); | |
//BN_set_word(n, i); | |
//BN_rand(n, bits, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY); | |
EC_POINTs_mul(group, r, n, 0, nullptr, nullptr, bn_ctx); | |
dump("k", n); | |
dump("P", group, r, bn_ctx); | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment