Skip to content

Commit

Permalink
Merge #294: generator: massively speed up serialization
Browse files Browse the repository at this point in the history
6361266 generator: speed up parsing (Andrew Poelstra)
5e7c2c1 generator: massively speed up serialization (Andrew Poelstra)

Pull request description:

ACKs for top commit:
  real-or-random:
    utACK 6361266
  jonasnick:
    ACK 6361266

Tree-SHA512: 9f35467ac9d39d23b68a3f830c920f61ae39d99974d6a864df4a3c19860dc8fc447609d0480e45234c66250878d34da03bfcf0056eaa83d3c78babb254962bf4
  • Loading branch information
jonasnick committed May 22, 2024
2 parents d661a93 + 6361266 commit 1683772
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/modules/generator/main_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ static void secp256k1_pedersen_commitment_save(secp256k1_pedersen_commitment* co

int secp256k1_pedersen_commitment_parse(const secp256k1_context* ctx, secp256k1_pedersen_commitment* commit, const unsigned char *input) {
secp256k1_fe x;
secp256k1_ge ge;

VERIFY_CHECK(ctx != NULL);
ARG_CHECK(commit != NULL);
Expand All @@ -285,28 +284,20 @@ int secp256k1_pedersen_commitment_parse(const secp256k1_context* ctx, secp256k1_

if ((input[0] & 0xFE) != 8 ||
!secp256k1_fe_set_b32_limit(&x, &input[1]) ||
!secp256k1_ge_set_xquad(&ge, &x)) {
!secp256k1_ge_x_on_curve_var(&x)) {
return 0;
}
if (input[0] & 1) {
secp256k1_ge_neg(&ge, &ge);
}
secp256k1_pedersen_commitment_save(commit, &ge);

memcpy(commit->data, input, 33);
return 1;
}

int secp256k1_pedersen_commitment_serialize(const secp256k1_context* ctx, unsigned char *output, const secp256k1_pedersen_commitment* commit) {
secp256k1_ge ge;

VERIFY_CHECK(ctx != NULL);
ARG_CHECK(output != NULL);
ARG_CHECK(commit != NULL);

secp256k1_pedersen_commitment_load(&ge, commit);

output[0] = 9 ^ secp256k1_fe_is_square_var(&ge.y);
secp256k1_fe_normalize_var(&ge.x);
secp256k1_fe_get_b32(&output[1], &ge.x);
memcpy(output, commit->data, 33);
return 1;
}

Expand Down
6 changes: 6 additions & 0 deletions src/modules/generator/tests_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ static void test_pedersen(void) {
}
CHECK(secp256k1_pedersen_blind_sum(CTX, &blinds[(total - 1) * 32], bptr, total - 1, inputs));
for (i = 0; i < total; i++) {
unsigned char result[33];
secp256k1_pedersen_commitment parse;

CHECK(secp256k1_pedersen_commit(CTX, &commits[i], &blinds[i * 32], values[i], secp256k1_generator_h));
CHECK(secp256k1_pedersen_commitment_serialize(CTX, result, &commits[i]));
CHECK(secp256k1_pedersen_commitment_parse(CTX, &parse, result));
CHECK(secp256k1_memcmp_var(&commits[i], &parse, 33) == 0);
}
CHECK(secp256k1_pedersen_verify_tally(CTX, cptr, inputs, &cptr[inputs], outputs));
CHECK(secp256k1_pedersen_verify_tally(CTX, &cptr[inputs], outputs, cptr, inputs));
Expand Down

0 comments on commit 1683772

Please sign in to comment.