commit 9f9f7eabcda0cf46c35e4394f6c15c39fd78f3ce
Author: nicholasfarrow <nicholas.w.farrow@gmail.com>
Date: Wed, 11 Dec 2019 03:40:58 +1100
first commit
Diffstat:
7 files changed, 215 insertions(+), 0 deletions(-)
diff --git a/create_pubkey b/create_pubkey
Binary files differ.
diff --git a/create_pubkey.h b/create_pubkey.h
@@ -0,0 +1,79 @@
+/* https://rosettacode.org/wiki/Bitcoin/public_point_to_address
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <openssl/sha.h>
+#include <openssl/ripemd.h>
+
+#define COIN_VER 0
+const char *coin_err;
+
+typedef unsigned char byte;
+
+int is_hex(const char *s) {
+ int i;
+ for (i = 0; i < 64; i++)
+ if (!isxdigit(s[i])) return 0;
+ return 1;
+}
+
+void str_to_byte(const char *src, byte *dst, int n) {
+ while (n--) sscanf(src + n * 2, "%2hhx", dst + n);
+}
+
+char* base58(byte *s, char *out) {
+ static const char *tmpl = "123456789"
+ "ABCDEFGHJKLMNPQRSTUVWXYZ"
+ "abcdefghijkmnopqrstuvwxyz";
+ static char buf[40];
+
+ int c, i, n;
+ if (!out) out = buf;
+
+ out[n = 34] = 0;
+ while (n--) {
+ for (c = i = 0; i < 25; i++) {
+ c = c * 256 + s[i];
+ s[i] = c / 58;
+ c %= 58;
+ }
+ out[n] = tmpl[c];
+ }
+
+ for (n = 0; out[n] == '1'; n++);
+ memmove(out, out + n, 34 - n);
+
+ return out;
+}
+
+char *coin_encode(const char *x, const char *y, char *out) {
+ byte s[65];
+ byte rmd[5 + RIPEMD160_DIGEST_LENGTH];
+
+ if (!is_hex(x) || !(is_hex(y))) {
+ coin_err = "bad public point string";
+ return 0;
+ }
+
+ s[0] = 4;
+ str_to_byte(x, s + 1, 32);
+ str_to_byte(y, s + 33, 32);
+
+ rmd[0] = COIN_VER;
+ RIPEMD160(SHA256(s, 65, 0), SHA256_DIGEST_LENGTH, rmd + 1);
+
+ memcpy(rmd + 21, SHA256(SHA256(rmd, 21, 0), SHA256_DIGEST_LENGTH, 0), 4);
+
+ return base58(rmd, out);
+}
+/*
+int main(void) {
+ puts(coin_encode(
+ "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352",
+ "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6",
+ 0));
+ return 0;
+}
+*/
diff --git a/ecdsa b/ecdsa
Binary files differ.
diff --git a/ecdsa.c b/ecdsa.c
@@ -0,0 +1,101 @@
+#include <stdio.h>
+#include <secp256k1.h>
+#include <create_pubkey.h>
+
+/* y^2 = x^3 + 7 */
+
+
+/* we want to return an array of unsigned bytes */
+char unsigned * gen_private_key() {
+ int byte_count = 32;
+ static char unsigned data[32];
+ FILE *random_file;
+
+ random_file = fopen("/dev/urandom", "r");
+ fread(&data, 1, byte_count, random_file);
+ fclose(random_file);
+
+ printf("Private Key : ");
+ for(int i=0; i<byte_count; i++) {
+ printf("%02X", data[i]);
+ }
+
+ return data;
+}
+
+
+int main() {
+ /*
+ pointer to a char byte
+ char unsigned *priv_key;
+
+ priv_key = gen_private_key(32);
+
+ printf("\n\nPrivate Key : ");
+ for(int i=0; i<32; i++) {
+ printf("%02X", *(i + priv_key));
+ }
+ */
+
+ char unsigned priv_key[32];
+ FILE *random_file;
+
+ random_file = fopen("/dev/urandom", "r");
+ fread(&priv_key, 1, 32, random_file);
+ fclose(random_file);
+
+ printf("\n\nPrivate Key : ");
+ for(int i=0; i<32; i++) {
+ printf("%02X", priv_key[i]);
+ }
+ int verify_ret;
+
+ secp256k1_context* ctx;
+ ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
+
+ verify_ret = secp256k1_ec_seckey_verify(ctx, priv_key);
+ printf("\nverify priv_key: %d\n", verify_ret);
+
+
+ secp256k1_pubkey pubkey;
+ int ret;
+ ret = secp256k1_ec_pubkey_create(ctx, &pubkey, priv_key);
+ printf("%d", ret);
+
+ char unsigned public_key64[65];
+ size_t pk_len = sizeof(public_key64);
+
+ secp256k1_ec_pubkey_serialize(ctx, public_key64, &pk_len, &pubkey, SECP256K1_EC_UNCOMPRESSED);
+
+ printf("\n\nLong Public Key : ");
+ for(int i=0; i<65; i++) {
+ printf("%02X", public_key64[i]);
+ }
+
+ char unsigned public_key32[33];
+ size_t pk32_len = sizeof(public_key32);
+
+ secp256k1_ec_pubkey_serialize(ctx, public_key32, &pk32_len, &pubkey, SECP256K1_EC_COMPRESSED);
+
+ printf("\n\nShort Public Key : ");
+ for(int i=0; i<33; i++) {
+ printf("%02X", public_key32[i]);
+ }
+
+ /*
+ &pubkeys[i]
+
+ struct secp256k1_context_struct secp256k1_context;
+
+ struct secp256k1_context_struct secp256k1_context;
+
+ int op;
+ op = secp256k1_ec_pubkey_create(ctx, priv_key, &pubkey);
+
+ printf("%d", op);
+ */
+ printf("\n\n\n");
+}
+
+
+
diff --git a/old_ecdsa.c b/old_ecdsa.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+
+/* y^2 = x^3 + 7 */
+
+char unsigned gen_private_key(int byte_count) {
+ char unsigned data[byte_count];
+ FILE *random_file;
+
+ random_file = fopen("/dev/urandom", "r");
+ fread(&data, 1, byte_count, random_file);
+ fclose(random_file);
+
+ printf("Private Key : ");
+ for(int i=0; i<byte_count; i++) {
+ printf("%02X", data[i]);
+ }
+
+ return data;
+}
+
+
+int main() {
+ char unsigned priv_key[32];
+ priv_key[32] = gen_private_key(32);
+
+ printf("\n\nPrivate Key : ");
+ for(int i=0; i<32; i++) {
+ printf("%02X", priv_key[i]);
+ }
+}
diff --git a/secp256k1 b/secp256k1
@@ -0,0 +1 @@
+Subproject commit 96cd94e385f64c1936abf0d1e303d12d0f5da980
diff --git a/v3.c b/v3.c
@@ -0,0 +1,4 @@
+#include <stdio.h>
+#include <secp256k1.h>
+
+