Skip to content

Commit 7038b41

Browse files
committed
Merge BlockstreamResearch#246: update simplicity to 472b2105eacf00eedda0d9cc7c97c9c5edd9e9da
ec10a51 update simplicity to 472b2105eacf00eedda0d9cc7c97c9c5edd9e9da (Andrew Poelstra) Pull request description: . ACKs for top commit: uncomputable: ACK ec10a51 Tree-SHA512: aaf8e9f3410c92dd5329e620604d97536b6614f49900c0803bb1e9eb62dbd4593b7037a5e342844fcb1a48882cd234c3714786fde071df07176e26f50e18f743
2 parents 7138429 + ec10a51 commit 7038b41

34 files changed

+526
-3076
lines changed

simplicity-sys/depend/jets_wrapper.c

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ WRAP_(asset_amount_hash)
2222
WRAP_(bip_0340_verify)
2323
WRAP_(build_tapbranch)
2424
WRAP_(build_tapleaf_simplicity)
25+
WRAP_(build_taptweak)
2526
WRAP_(calculate_asset)
2627
WRAP_(calculate_confidential_token)
2728
WRAP_(calculate_explicit_token)
@@ -449,6 +450,7 @@ WRAP_(subtract_64)
449450
WRAP_(subtract_8)
450451
WRAP_(swu)
451452
WRAP_(tap_env_hash)
453+
WRAP_(tapdata_init)
452454
WRAP_(tapleaf_hash)
453455
WRAP_(tapleaf_version)
454456
WRAP_(tappath)
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# This file has been automatically generated.
2-
3daad68dc9e995fefcd5287fa5b15a7bfb13462a
2+
472b2105eacf00eedda0d9cc7c97c9c5edd9e9da

simplicity-sys/depend/simplicity/jets-secp256k1.c

+37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "jets.h"
2+
#include "taptweak.h"
23

34
#include "precomputed.h"
45
#include "prefix.h"
@@ -713,3 +714,39 @@ bool simplicity_hash_to_curve(frameItem* dst, frameItem src, const txEnv* env) {
713714
write_ge(dst, &ge);
714715
return true;
715716
}
717+
718+
/* THIS IS NOT A JET. It doesn't have the type signatue of a jet
719+
* This is a generic taptweak jet implementation parameterized by the tag used in the hash.
720+
* It is designed to be specialized to implement slightly different taptweak operations for Bitcoin and Elements.
721+
*
722+
* PUBKEY * TWO^256 |- PUBKEY
723+
*
724+
* Precondition: unsigned char[tagLen] tag
725+
*/
726+
bool simplicity_generic_taptweak(frameItem* dst, frameItem *src, const unsigned char *tagName, size_t tagLen) {
727+
unsigned char buf[32];
728+
secp256k1_xonly_pubkey input_pubkey, output_pubkey;
729+
secp256k1_pubkey pubkey;
730+
sha256_midstate taptweakTag, input_hash, output_hash;
731+
{
732+
sha256_context ctx = sha256_init(taptweakTag.s);
733+
sha256_uchars(&ctx, tagName, tagLen);
734+
sha256_finalize(&ctx);
735+
}
736+
sha256_context ctx = sha256_init(output_hash.s);
737+
sha256_hash(&ctx, &taptweakTag);
738+
sha256_hash(&ctx, &taptweakTag);
739+
740+
read8s(buf, 32, src);
741+
sha256_uchars(&ctx, buf, 32);
742+
if (!secp256k1_xonly_pubkey_parse(&input_pubkey, buf)) return false;
743+
read32s(input_hash.s, 8, src);
744+
sha256_hash(&ctx, &input_hash);
745+
sha256_finalize(&ctx);
746+
sha256_fromMidstate(buf, output_hash.s);
747+
if (!secp256k1_xonly_pubkey_tweak_add(&pubkey, &input_pubkey, buf)) return false;
748+
if (!secp256k1_xonly_pubkey_from_pubkey(&output_pubkey, NULL, &pubkey)) return false;
749+
if (!secp256k1_xonly_pubkey_serialize(buf,&output_pubkey)) return false;
750+
write8s(dst, buf, 32);
751+
return true;
752+
}

simplicity-sys/depend/simplicity/jets.c

+20
Original file line numberDiff line numberDiff line change
@@ -1403,3 +1403,23 @@ bool simplicity_parse_sequence(frameItem* dst, frameItem src, const txEnv* env)
14031403
}
14041404
return true;
14051405
}
1406+
1407+
/* simplicity_tapdata_init : ONE |- CTX8 */
1408+
bool simplicity_tapdata_init(frameItem* dst, frameItem src, const txEnv* env) {
1409+
(void) env; /* env is unused. */
1410+
(void) src; /* env is unused. */
1411+
1412+
sha256_midstate tapleafTag;
1413+
{
1414+
static const unsigned char tagName[] = "TapData";
1415+
sha256_context ctx = sha256_init(tapleafTag.s);
1416+
sha256_uchars(&ctx, tagName, sizeof(tagName) - 1);
1417+
sha256_finalize(&ctx);
1418+
}
1419+
uint32_t iv[8];
1420+
sha256_context ctx = sha256_init(iv);
1421+
sha256_hash(&ctx, &tapleafTag);
1422+
sha256_hash(&ctx, &tapleafTag);
1423+
1424+
return simplicity_write_sha256_context(dst, &ctx);
1425+
}

simplicity-sys/depend/simplicity/jets.h

+1
Original file line numberDiff line numberDiff line change
@@ -395,5 +395,6 @@ bool simplicity_bip_0340_verify(frameItem* dst, frameItem src, const txEnv* env)
395395

396396
bool simplicity_parse_lock(frameItem* dst, frameItem src, const txEnv* env);
397397
bool simplicity_parse_sequence(frameItem* dst, frameItem src, const txEnv* env);
398+
bool simplicity_tapdata_init(frameItem* dst, frameItem src, const txEnv* env);
398399

399400
#endif

simplicity-sys/depend/simplicity/primitive/elements/jets.c

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "ops.h"
44
#include "primitive.h"
5+
#include "../../taptweak.h"
56
#include "../../simplicity_assert.h"
67

78
/* Read a 256-bit hash value from the 'src' frame, advancing the cursor 256 cells.
@@ -862,6 +863,13 @@ bool simplicity_build_tapbranch(frameItem* dst, frameItem src, const txEnv* env)
862863
return true;
863864
}
864865

866+
/* build_taptweak : PUBKEY * TWO^256 |- PUBKEY */
867+
bool simplicity_build_taptweak(frameItem* dst, frameItem src, const txEnv* env) {
868+
(void) env; // env is unused.
869+
static unsigned char taptweak[] = "TapTweak/elements";
870+
return simplicity_generic_taptweak(dst, &src, taptweak, sizeof(taptweak)-1);
871+
}
872+
865873
/* outpoint_hash : CTX8 * S TWO^256 * TWO^256 * TWO^32 |- CTX8 */
866874
bool simplicity_outpoint_hash(frameItem* dst, frameItem src, const txEnv* env) {
867875
(void) env; // env is unused.

simplicity-sys/depend/simplicity/primitive/elements/jets.h

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bool simplicity_calculate_confidential_token(frameItem* dst, frameItem src, cons
7272
bool simplicity_lbtc_asset(frameItem* dst, frameItem src, const txEnv* env);
7373
bool simplicity_build_tapleaf_simplicity(frameItem* dst, frameItem src, const txEnv* env);
7474
bool simplicity_build_tapbranch(frameItem* dst, frameItem src, const txEnv* env);
75+
bool simplicity_build_taptweak(frameItem* dst, frameItem src, const txEnv* env);
7576
bool simplicity_outpoint_hash(frameItem* dst, frameItem src, const txEnv* env);
7677
bool simplicity_asset_amount_hash(frameItem* dst, frameItem src, const txEnv* env);
7778
bool simplicity_nonce_hash(frameItem* dst, frameItem src, const txEnv* env);

simplicity-sys/depend/simplicity/primitive/elements/primitive.c

+2
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ static simplicity_err decodePrimitive(jetName* result, bitstream* stream) {
10321032
switch (code) {
10331033
case 1: *result = PARSE_LOCK; return SIMPLICITY_NO_ERROR;
10341034
case 2: *result = PARSE_SEQUENCE; return SIMPLICITY_NO_ERROR;
1035+
case 3: *result = TAPDATA_INIT; return SIMPLICITY_NO_ERROR;
10351036
}
10361037
break;
10371038
}
@@ -1079,6 +1080,7 @@ static simplicity_err decodePrimitive(jetName* result, bitstream* stream) {
10791080
case 32: *result = ANNEX_HASH; return SIMPLICITY_NO_ERROR;
10801081
case 33: *result = BUILD_TAPLEAF_SIMPLICITY; return SIMPLICITY_NO_ERROR;
10811082
case 34: *result = BUILD_TAPBRANCH; return SIMPLICITY_NO_ERROR;
1083+
case 35: *result = BUILD_TAPTWEAK; return SIMPLICITY_NO_ERROR;
10821084
}
10831085
break;
10841086
case 2: /* Timelock jets chapter */

simplicity-sys/depend/simplicity/primitive/elements/primitiveEnumJet.inc

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ASSET_AMOUNT_HASH,
1717
BIP_0340_VERIFY,
1818
BUILD_TAPBRANCH,
1919
BUILD_TAPLEAF_SIMPLICITY,
20+
BUILD_TAPTWEAK,
2021
CALCULATE_ASSET,
2122
CALCULATE_CONFIDENTIAL_TOKEN,
2223
CALCULATE_EXPLICIT_TOKEN,
@@ -444,6 +445,7 @@ SUBTRACT_64,
444445
SUBTRACT_8,
445446
SWU,
446447
TAP_ENV_HASH,
448+
TAPDATA_INIT,
447449
TAPLEAF_HASH,
448450
TAPLEAF_VERSION,
449451
TAPPATH,

simplicity-sys/depend/simplicity/primitive/elements/primitiveJetNode.inc

+16
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@
143143
, .targetIx = ty_w256
144144
, .cost = 1843 /* milli weight units */
145145
}
146+
,[BUILD_TAPTWEAK] =
147+
{ .tag = JET
148+
, .jet = simplicity_build_taptweak
149+
, .cmr = {{0xa3234771u, 0xe7acebbfu, 0x7005eb3fu, 0xab4cb27au, 0x8ac253aau, 0x08918e75u, 0x1ec74c2eu, 0xf2a4354fu}}
150+
, .sourceIx = ty_w512
151+
, .targetIx = ty_w256
152+
, .cost = 92813 /* milli weight units */
153+
}
146154
,[CALCULATE_ASSET] =
147155
{ .tag = JET
148156
, .jet = simplicity_calculate_asset
@@ -3559,6 +3567,14 @@
35593567
, .targetIx = ty_w256
35603568
, .cost = 162 /* milli weight units */
35613569
}
3570+
,[TAPDATA_INIT] =
3571+
{ .tag = JET
3572+
, .jet = simplicity_tapdata_init
3573+
, .cmr = {{0x1c17e3ecu, 0x888848f9u, 0xcc86fed1u, 0xa90714f0u, 0x5c7395a2u, 0x2764f8adu, 0x619729eeu, 0x52a6db05u}}
3574+
, .sourceIx = ty_u
3575+
, .targetIx = ty_ppmw256pmw128pmw64pmw32pmw16mw8pw64w256
3576+
, .cost = 1178 /* milli weight units */
3577+
}
35623578
,[TAPLEAF_HASH] =
35633579
{ .tag = JET
35643580
, .jet = simplicity_tapleaf_hash
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/***********************************************************************
2+
* Copyright (c) 2013, 2014 Pieter Wuille *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
***********************************************************************/
6+
7+
#ifndef SECP256K1_ECKEY_H
8+
#define SECP256K1_ECKEY_H
9+
10+
#include <stddef.h>
11+
12+
#include "group.h"
13+
#include "scalar.h"
14+
#if 0
15+
#include "ecmult.h"
16+
#include "ecmult_gen.h"
17+
18+
static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size);
19+
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed);
20+
21+
static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak);
22+
#endif
23+
static int secp256k1_eckey_pubkey_tweak_add(secp256k1_ge *key, const secp256k1_scalar *tweak);
24+
#if 0
25+
static int secp256k1_eckey_privkey_tweak_mul(secp256k1_scalar *key, const secp256k1_scalar *tweak);
26+
static int secp256k1_eckey_pubkey_tweak_mul(secp256k1_ge *key, const secp256k1_scalar *tweak);
27+
#endif
28+
29+
#endif /* SECP256K1_ECKEY_H */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/***********************************************************************
2+
* Copyright (c) 2013, 2014 Pieter Wuille *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
***********************************************************************/
6+
7+
#ifndef SECP256K1_ECKEY_IMPL_H
8+
#define SECP256K1_ECKEY_IMPL_H
9+
10+
#include "eckey.h"
11+
12+
#include "scalar.h"
13+
#include "field.h"
14+
#include "group.h"
15+
#if 0
16+
#include "ecmult_gen.h"
17+
18+
static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size) {
19+
if (size == 33 && (pub[0] == SECP256K1_TAG_PUBKEY_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_ODD)) {
20+
secp256k1_fe x;
21+
return secp256k1_fe_set_b32(&x, pub+1) && secp256k1_ge_set_xo_var(elem, &x, pub[0] == SECP256K1_TAG_PUBKEY_ODD);
22+
} else if (size == 65 && (pub[0] == SECP256K1_TAG_PUBKEY_UNCOMPRESSED || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
23+
secp256k1_fe x, y;
24+
if (!secp256k1_fe_set_b32(&x, pub+1) || !secp256k1_fe_set_b32(&y, pub+33)) {
25+
return 0;
26+
}
27+
secp256k1_ge_set_xy(elem, &x, &y);
28+
if ((pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD) &&
29+
secp256k1_fe_is_odd(&y) != (pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
30+
return 0;
31+
}
32+
return secp256k1_ge_is_valid_var(elem);
33+
} else {
34+
return 0;
35+
}
36+
}
37+
38+
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed) {
39+
if (secp256k1_ge_is_infinity(elem)) {
40+
return 0;
41+
}
42+
secp256k1_fe_normalize_var(&elem->x);
43+
secp256k1_fe_normalize_var(&elem->y);
44+
secp256k1_fe_get_b32(&pub[1], &elem->x);
45+
if (compressed) {
46+
*size = 33;
47+
pub[0] = secp256k1_fe_is_odd(&elem->y) ? SECP256K1_TAG_PUBKEY_ODD : SECP256K1_TAG_PUBKEY_EVEN;
48+
} else {
49+
*size = 65;
50+
pub[0] = SECP256K1_TAG_PUBKEY_UNCOMPRESSED;
51+
secp256k1_fe_get_b32(&pub[33], &elem->y);
52+
}
53+
return 1;
54+
}
55+
56+
static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak) {
57+
secp256k1_scalar_add(key, key, tweak);
58+
return !secp256k1_scalar_is_zero(key);
59+
}
60+
#endif
61+
62+
static int secp256k1_eckey_pubkey_tweak_add(secp256k1_ge *key, const secp256k1_scalar *tweak) {
63+
secp256k1_gej pt;
64+
secp256k1_scalar one;
65+
secp256k1_gej_set_ge(&pt, key);
66+
secp256k1_scalar_set_int(&one, 1);
67+
secp256k1_ecmult(&pt, &pt, &one, tweak);
68+
69+
if (secp256k1_gej_is_infinity(&pt)) {
70+
return 0;
71+
}
72+
secp256k1_ge_set_gej_var(key, &pt);
73+
return 1;
74+
}
75+
76+
#if 0
77+
static int secp256k1_eckey_privkey_tweak_mul(secp256k1_scalar *key, const secp256k1_scalar *tweak) {
78+
int ret;
79+
ret = !secp256k1_scalar_is_zero(tweak);
80+
81+
secp256k1_scalar_mul(key, key, tweak);
82+
return ret;
83+
}
84+
85+
static int secp256k1_eckey_pubkey_tweak_mul(secp256k1_ge *key, const secp256k1_scalar *tweak) {
86+
secp256k1_scalar zero;
87+
secp256k1_gej pt;
88+
if (secp256k1_scalar_is_zero(tweak)) {
89+
return 0;
90+
}
91+
92+
secp256k1_scalar_set_int(&zero, 0);
93+
secp256k1_gej_set_ge(&pt, key);
94+
secp256k1_ecmult(&pt, &pt, tweak, &zero);
95+
secp256k1_ge_set_gej(key, &pt);
96+
return 1;
97+
}
98+
#endif
99+
100+
#endif /* SECP256K1_ECKEY_IMPL_H */

simplicity-sys/depend/simplicity/secp256k1/extrakeys.h

+51
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,55 @@ static SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_parse(
3333
const unsigned char *input32
3434
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
3535

36+
/** Serialize an xonly_pubkey object into a 32-byte sequence.
37+
*
38+
* Returns: 1 always.
39+
*
40+
* Out: output32: a pointer to a 32-byte array to place the serialized key in.
41+
* In: pubkey: a pointer to a secp256k1_xonly_pubkey containing an initialized public key.
42+
*/
43+
static int secp256k1_xonly_pubkey_serialize(
44+
unsigned char *output32,
45+
const secp256k1_xonly_pubkey* pubkey
46+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
47+
/** Converts a secp256k1_pubkey into a secp256k1_xonly_pubkey.
48+
*
49+
* Returns: 1 always.
50+
*
51+
* Out: xonly_pubkey: pointer to an x-only public key object for placing the converted public key.
52+
* pk_parity: Ignored if NULL. Otherwise, pointer to an integer that
53+
* will be set to 1 if the point encoded by xonly_pubkey is
54+
* the negation of the pubkey and set to 0 otherwise.
55+
* In: pubkey: pointer to a public key that is converted.
56+
*/
57+
static SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_from_pubkey(
58+
secp256k1_xonly_pubkey *xonly_pubkey,
59+
int *pk_parity,
60+
const secp256k1_pubkey *pubkey
61+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
62+
63+
/** Tweak an x-only public key by adding the generator multiplied with tweak32
64+
* to it.
65+
*
66+
* Note that the resulting point can not in general be represented by an x-only
67+
* pubkey because it may have an odd Y coordinate. Instead, the output_pubkey
68+
* is a normal secp256k1_pubkey.
69+
*
70+
* Returns: 0 if the arguments are invalid or the resulting public key would be
71+
* invalid (only when the tweak is the negation of the corresponding
72+
* secret key). 1 otherwise.
73+
*
74+
* Out: output_pubkey: pointer to a public key to store the result. Will be set
75+
* to an invalid value if this function returns 0.
76+
* In: internal_pubkey: pointer to an x-only pubkey to apply the tweak to.
77+
* tweak32: pointer to a 32-byte tweak. If the tweak is invalid
78+
* according to secp256k1_ec_seckey_verify, this function
79+
* returns 0. For uniformly random 32-byte arrays the
80+
* chance of being invalid is negligible (around 1 in 2^128).
81+
*/
82+
static SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add(
83+
secp256k1_pubkey *output_pubkey,
84+
const secp256k1_xonly_pubkey *internal_pubkey,
85+
const unsigned char *tweak32
86+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
3687
#endif /* SECP256K1_EXTRAKEYS_H */

0 commit comments

Comments
 (0)