Skip to content

Commit 978dde4

Browse files
cyclomancerbonbud-macryg
authored andcommitted
add tweak to privkeys and compressed pubkeys, change pub-from-priv interface to use compressed pubkeys
1 parent cd6e1af commit 978dde4

2 files changed

Lines changed: 66 additions & 12 deletions

File tree

urcrypt/secp256k1.c

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,30 +245,76 @@ urcrypt_secp_schnorr_veri(urcrypt_secp_context* context,
245245
}
246246

247247
int
248-
urcrypt_secp_point_from_scalar(urcrypt_secp_context* context,
249-
const uint8_t scalar[32],
250-
uint8_t point[65]) {
248+
urcrypt_secp_cmp_point_from_scalar(urcrypt_secp_context* context,
249+
const uint8_t scalar[32],
250+
uint8_t cmp_point[33]) {
251251
urcrypt__reverse(32, scalar);
252+
252253
secp256k1_keypair keypair;
253254
secp256k1_pubkey pubkey;
254255

255256
secp256k1_keypair_create(context->secp, &keypair, scalar);
256-
257257
secp256k1_keypair_pub(context->secp, &pubkey, &keypair);
258258

259-
size_t output_len = 65;
259+
size_t output_len = 33;
260260
if (1 != secp256k1_ec_pubkey_serialize(
261261
context->secp,
262-
point,
262+
cmp_point,
263263
&output_len,
264264
&pubkey,
265-
SECP256K1_FLAGS_TYPE_COMPRESSION)) {
265+
SECP256K1_EC_COMPRESSED)) {
266266
return -1;
267267
}
268268

269-
urcrypt__reverse(32, point + 1);
270-
urcrypt__reverse(32, point + 33);
269+
urcrypt__reverse(33, cmp_point);
270+
271+
return 0;
272+
}
273+
274+
int
275+
urcrypt_secp_scalar_tweak_add(urcrypt_secp_context* context,
276+
uint8_t scalar[32],
277+
const uint8_t tweak[32]) {
278+
urcrypt__reverse(32, scalar);
279+
urcrypt__reverse(32, tweak);
280+
281+
if (1 != secp256k1_ec_seckey_tweak_add(context, scalar, tweak)) {
282+
return -1;
283+
}
284+
285+
urcrypt__reverse(32, scalar);
271286

272287
return 0;
273288
}
274289

290+
int
291+
urcrypt_secp_cmp_point_tweak_add(urcrypt_secp_context* context,
292+
uint8_t cmp_point[33],
293+
const uint8_t tweak[32]) {
294+
urcrypt__reverse(33, cmp_point);
295+
urcrypt__reverse(32, tweak);
296+
297+
secp256k1_pubkey point;
298+
size_t cmp_len = 33;
299+
300+
if (1 != secp256k1_ec_pubkey_parse(context->secp, &point, cmp_point, cmp_len)) {
301+
return -1; //invalid compressed point
302+
}
303+
304+
if (1 != secp256k1_ec_pubkey_tweak_add(context->secp, &point, tweak)) {
305+
return -2; //invalid tweak
306+
}
307+
308+
if (1 != secp256k1_ec_pubkey_serialize(
309+
context->secp,
310+
cmp_point,
311+
&cmp_len,
312+
&point,
313+
SECP256K1_EC_COMPRESSED)) {
314+
return -3; //something is very wrong
315+
}
316+
317+
urcrypt__reverse(33, cmp_point);
318+
319+
return 0;
320+
}

urcrypt/urcrypt.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,17 @@ void urcrypt_scrypt_pbkdf_sha256(const uint8_t *passwd,
284284
size_t outlen, // must be at most 32*(2^32-1)
285285
uint8_t *out);
286286

287-
int urcrypt_secp_point_from_scalar(urcrypt_secp_context* context,
288-
const uint8_t scalar[32],
289-
uint8_t point[65]);
287+
int urcrypt_secp_cmp_point_from_scalar(urcrypt_secp_context* context,
288+
const uint8_t scalar[32],
289+
uint8_t cmp_point[33]);
290+
291+
int urcrypt_secp_scalar_tweak_add(urcrypt_secp_context* context,
292+
uint8_t scalar[32],
293+
const uint8_t tweak[32]);
294+
295+
int urcrypt_secp_cmp_point_tweak_add(urcrypt_secp_context* context,
296+
uint8_t cmp_point[33],
297+
const uint8_t tweak[32]);
290298

291299
int urcrypt_scrypt(const uint8_t *passwd,
292300
size_t passwdlen,

0 commit comments

Comments
 (0)