manu
manu

Reputation: 29

c++ libsecp256k1 multiplying a point by a scalar

need help writing code. Actually, the code below adds two points on an elliptic curve and multiplies the point by a scalar. I use the library secp256k1bitcoin-core/secp256k1 It adds correctly, but the multiplication does not. I can't figure out what the reason is. https://gist.github.com/manyunya/3ea69f7fcbdbf5d4118c741a098f309c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "secp256k1.h"

// Initializing the context 

  secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);


// Function to parse public key from hex string
int parse_pubkey(const char* hex, secp256k1_pubkey* pubkey) {
size_t len = strlen(hex) / 2; // Length in bytes
unsigned char* buffer = (unsigned char*)malloc(len);
if (buffer == NULL) {
    fprintf(stderr, "Memory allocation failed\n");
    return 0; // Memory allocation error
}

// Convert a hexadecimal string to a byte array
for (size_t i = 0; i < len; i++) {
    sscanf(hex + 2 * i, "%2hhx", &buffer[i]);
}

// Parsing the public key
int result = secp256k1_ec_pubkey_parse(ctx, pubkey, buffer, len);

free(buffer); // Freeing up memory
return result; // Return the parsing result 
}

// Function to output public key
void printPubKey(const secp256k1_pubkey * pubkey) {
    unsigned char output[65];
    size_t output_len = sizeof(output);
    if (secp256k1_ec_pubkey_serialize(ctx, output, &output_len, pubkey, SECP256K1_EC_UNCOMPRESSED)) {
        printf("Public Key: ");
        for (size_t i = 0; i < output_len; i++) {
            printf("%02x", output[i]);
        }
        printf("\n");
    }
    else {
        printf("Failed to serialize public key\n");
    }
}

// Function to add two public keys 
int add_pubkeys(secp256k1_pubkey * result, const secp256k1_pubkey * pub1,
    const secp256k1_pubkey * pub2) {
    const secp256k1_pubkey* pubkeys[2] = { pub1, pub2 };
    return secp256k1_ec_pubkey_combine(ctx, result, pubkeys, 2);
}

// Function to multiply a point on an elliptic curve by a scalar 
void multiplyScalar(secp256k1_pubkey* pub, const unsigned char* scalar, secp256k1_pubkey* result) {
    secp256k1_ec_pubkey_tweak_mul(ctx, result, scalar);
}

// Example of using functions 
int main() {
    secp256k1_pubkey pub1, pub2, pub_sum, result;

    const char* pubkey_hex1 = "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8";
    const char* pubkey_hex2 = "04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a";
    const unsigned char scalar[32] = { 0x01 };

    // Parsing public keys from hexadecimal format
    assert(parse_pubkey(pubkey_hex1, &pub1));
    assert(parse_pubkey(pubkey_hex2, &pub2));

    // We output the original public keys
    printf("Original Public Keys:\n");
    printPubKey(&pub1);
    printPubKey(&pub2);

    // Add two public keys together
    if (add_pubkeys(&pub_sum, &pub1, &pub2)) {
        printf("Sum of Public Keys:\n");
        printPubKey(&pub_sum);
    }
    else {
        printf("Failed to add public keys\n");
    }


    // Multiplication by a scalar
    multiplyScalar(&pub1, scalar, &result);
    printf("Resulting Public Key after scalar multiplication:\n");
    printPubKey(&result);

    // Resource release
    secp256k1_context_destroy(ctx);

    return 0;
}

Upvotes: 0

Views: 160

Answers (0)

Related Questions