Skip to content

Commit bafe257

Browse files
committed
generator: massively speed up serialization
`secp256k1_pedersen_commit_serialize` would call `_load` (which does a sqrt to fully decompress the key, then a conditional negation based on the flag), then check the Jacobian symbol of the resulting y-coordinate, then re-serialize based on this. Instead, don't do any of this stuff. Copy the flag directly out of the internal representation and copy the x-coordinate directly out of the internal representation. Checked that none of the other _serialize methods in the modules do this. Fixes #293
1 parent d661a93 commit bafe257

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/modules/generator/main_impl.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,12 @@ int secp256k1_pedersen_commitment_parse(const secp256k1_context* ctx, secp256k1_
296296
}
297297

298298
int secp256k1_pedersen_commitment_serialize(const secp256k1_context* ctx, unsigned char *output, const secp256k1_pedersen_commitment* commit) {
299-
secp256k1_ge ge;
300-
301299
VERIFY_CHECK(ctx != NULL);
302300
ARG_CHECK(output != NULL);
303301
ARG_CHECK(commit != NULL);
304302

305-
secp256k1_pedersen_commitment_load(&ge, commit);
306-
307-
output[0] = 9 ^ secp256k1_fe_is_square_var(&ge.y);
308-
secp256k1_fe_normalize_var(&ge.x);
309-
secp256k1_fe_get_b32(&output[1], &ge.x);
303+
output[0] = 8 ^ (commit->data[0] & 1);
304+
memcpy(&output[1], &commit->data[1], 32);
310305
return 1;
311306
}
312307

src/modules/generator/tests_impl.h

+6
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,13 @@ static void test_pedersen(void) {
264264
}
265265
CHECK(secp256k1_pedersen_blind_sum(CTX, &blinds[(total - 1) * 32], bptr, total - 1, inputs));
266266
for (i = 0; i < total; i++) {
267+
unsigned char result[33];
268+
secp256k1_pedersen_commitment parse;
269+
267270
CHECK(secp256k1_pedersen_commit(CTX, &commits[i], &blinds[i * 32], values[i], secp256k1_generator_h));
271+
CHECK(secp256k1_pedersen_commitment_serialize(CTX, result, &commits[i]));
272+
CHECK(secp256k1_pedersen_commitment_parse(CTX, &parse, result));
273+
CHECK(secp256k1_memcmp_var(&commits[i], result, 33) == 0);
268274
}
269275
CHECK(secp256k1_pedersen_verify_tally(CTX, cptr, inputs, &cptr[inputs], outputs));
270276
CHECK(secp256k1_pedersen_verify_tally(CTX, &cptr[inputs], outputs, cptr, inputs));

0 commit comments

Comments
 (0)