Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace secp256k1 code patching with C code. #50

Open
1 of 2 tasks
sergey-shandar opened this issue Mar 29, 2023 · 0 comments
Open
1 of 2 tasks

Replace secp256k1 code patching with C code. #50

sergey-shandar opened this issue Mar 29, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@sergey-shandar
Copy link
Contributor

sergey-shandar commented Mar 29, 2023

Currently, we patch the secp256k1 code immediately after downloading it (see source).

To avoid patching the secp256k1 code directly, we can create new C files for our modifications. This way, we can maintain our changes separately from the original library. Here's how to achieve this:

Tasks

Case 1: Remove static to make the code visible

For example, replace this code:

SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a) {

with this:

static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a) {

Alternative: Generate a new function from a list of signatures

// secp256k1_extension.h
#include <secp256k1.h>
SECP256K1_API void p256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a);
// secp256k1_extension.c
#include "secp256k1_extension.h"
void p256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a) {
    secp256k1_fe_add(r, a);
}

Case 2: Replace a function

For example, the original function:

void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a) {
    secp256k1_fe z2, z3;
    r->infinity = a->infinity;
    secp256k1_fe_inv(&a->z, &a->z);
    secp256k1_fe_sqr(&z2, &a->z);
    secp256k1_fe_mul(&z3, &a->z, &z2);
    secp256k1_fe_mul(&a->x, &a->x, &z2);
    secp256k1_fe_mul(&a->y, &a->y, &z3);
    secp256k1_fe_set_int(&a->z, 1);
    r->x = a->x;
    r->y = a->y;
}

was replaced with:

void secp256k1_ge_set_gej(secp256k1_ge *r, const secp256k1_gej *a) {
    secp256k1_fe z2, z3, az, ax, ay;
    r->infinity = a->infinity;
    secp256k1_fe_inv(&az, &a->z);
    secp256k1_fe_sqr(&z2, &az);
    secp256k1_fe_mul(&z3, &az, &z2);
    secp256k1_fe_mul(&ax, &a->x, &z2);
    secp256k1_fe_mul(&ay, &a->y, &z3);
    secp256k1_fe_set_int(&az, 1);
    r->x = ax;
    r->y = ay;
}

Alternative: Write a new function

void p256k1_ge_set_gej(secp256k1_ge *r, const secp256k1_gej *a) {
    secp256k1_fe z2, z3, az, ax, ay;
    r->infinity = a->infinity;
    secp256k1_fe_inv(&az, &a->z);
    secp256k1_fe_sqr(&z2, &az);
    secp256k1_fe_mul(&z3, &az, &z2);
    secp256k1_fe_mul(&ax, &a->x, &z2);
    secp256k1_fe_mul(&ay, &a->y, &z3);
    secp256k1_fe_set_int(&az, 1);
    r->x = ax;
    r->y = ay;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: ⚙️ WIP - Design
Development

No branches or pull requests

1 participant