Skip to content

Commit 1a4006c

Browse files
Make primitive.h functions indirect
The original plan here was to build different Simplicity primitives (e.g. Elements and Bitcoin), which are selected at link time. However, we would like to link multiple sets of primitives into Haskell for testing purposes. This means we will need different names for the Bitcoin and Elements to bind them. To support this we turn the primitive.h functions into callbacks for the various specific primitive implementations. Various functions are rearranged to support the removal of primitive.h and make the pairing of .c file implementing .h header files more consistent. It should now be the case that each .c files implement exactly the declairations of one .h file.
1 parent 0fceefe commit 1a4006c

File tree

20 files changed

+400
-368
lines changed

20 files changed

+400
-368
lines changed

C/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
OBJS := bitstream.o dag.o deserialize.o eval.o frame.o jets.o jets-secp256k1.o rsort.o sha256.o type.o typeInference.o primitive/elements/env.o primitive/elements/exec.o primitive/elements/ops.o primitive/elements/jets.o primitive/elements/primitive.o primitive/elements/cmr.o
1+
OBJS := bitstream.o dag.o deserialize.o eval.o frame.o jets.o jets-secp256k1.o rsort.o sha256.o type.o typeInference.o primitive/elements/env.o primitive/elements/exec.o primitive/elements/ops.o primitive/elements/jets.o primitive/elements/primitive.o primitive/elements/cmr.o primitive/elements/txEnv.o
22
TEST_OBJS := test.o ctx8Pruned.o ctx8Unpruned.o hashBlock.o regression4.o schnorr0.o schnorr6.o typeSkipTest.o primitive/elements/checkSigHashAllTx1.o
33

44
# From https://fastcompression.blogspot.com/2019/01/compiler-warnings.html

C/deserialize.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <limits.h>
44
#include "limitations.h"
5-
#include "primitive.h"
65
#include "simplicity_alloc.h"
76
#include "simplicity_assert.h"
87

@@ -55,15 +54,15 @@ static simplicity_err getHash(sha256_midstate* result, bitstream* stream) {
5554
* i < 2^31 - 1
5655
* NULL != stream
5756
*/
58-
static simplicity_err decodeNode(dag_node* dag, uint_fast32_t i, bitstream* stream) {
57+
static simplicity_err decodeNode(dag_node* dag, simplicity_callback_decodeJet decodeJet, uint_fast32_t i, bitstream* stream) {
5958
int32_t bit = read1Bit(stream);
6059
if (bit < 0) return (simplicity_err)bit;
6160
dag[i] = (dag_node){0};
6261
if (bit) {
6362
bit = read1Bit(stream);
6463
if (bit < 0) return (simplicity_err)bit;
6564
if (bit) {
66-
return simplicity_decodeJet(&dag[i], stream);
65+
return decodeJet(&dag[i], stream);
6766
} else {
6867
/* Decode WORD. */
6968
int32_t depth = simplicity_decodeUptoMaxInt(stream);
@@ -153,9 +152,9 @@ static simplicity_err decodeNode(dag_node* dag, uint_fast32_t i, bitstream* stre
153152
* len < 2^31
154153
* NULL != stream
155154
*/
156-
static simplicity_err decodeDag(dag_node* dag, const uint_fast32_t len, combinator_counters* census, bitstream* stream) {
155+
static simplicity_err decodeDag(dag_node* dag, simplicity_callback_decodeJet decodeJet, const uint_fast32_t len, combinator_counters* census, bitstream* stream) {
157156
for (uint_fast32_t i = 0; i < len; ++i) {
158-
simplicity_err error = decodeNode(dag, i, stream);
157+
simplicity_err error = decodeNode(dag, decodeJet, i, stream);
159158
if (!IS_OK(error)) return error;
160159

161160
enumerator(census, dag[i].tag);
@@ -186,7 +185,7 @@ static simplicity_err decodeDag(dag_node* dag, const uint_fast32_t len, combinat
186185
* of the function is positive and when NULL != census;
187186
* NULL == *dag when the return value is negative.
188187
*/
189-
int_fast32_t simplicity_decodeMallocDag(dag_node** dag, combinator_counters* census, bitstream* stream) {
188+
int_fast32_t simplicity_decodeMallocDag(dag_node** dag, simplicity_callback_decodeJet decodeJet, combinator_counters* census, bitstream* stream) {
190189
*dag = NULL;
191190
int32_t dagLen = simplicity_decodeUptoMaxInt(stream);
192191
if (dagLen <= 0) return dagLen;
@@ -199,7 +198,7 @@ int_fast32_t simplicity_decodeMallocDag(dag_node** dag, combinator_counters* cen
199198
if (!*dag) return SIMPLICITY_ERR_MALLOC;
200199

201200
if (census) *census = (combinator_counters){0};
202-
simplicity_err error = decodeDag(*dag, (uint_fast32_t)dagLen, census, stream);
201+
simplicity_err error = decodeDag(*dag, decodeJet, (uint_fast32_t)dagLen, census, stream);
203202

204203
if (IS_OK(error)) {
205204
error = HIDDEN == (*dag)[dagLen - 1].tag

C/deserialize.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
#include "bitstream.h"
77
#include "dag.h"
88

9+
/* Decode an application specific jet from 'stream' into 'node'.
10+
* All jets begin with a bit prefix of '1' which needs to have already been consumed from the 'stream'.
11+
* Returns 'SIMPLICITY_ERR_DATA_OUT_OF_RANGE' if the stream's prefix doesn't match any valid code for a jet.
12+
* Returns 'SIMPLICITY_ERR_BITSTRING_EOF' if not enough bits are available in the 'stream'.
13+
* In the above error cases, 'dag' may be modified.
14+
* Returns 'SIMPLICITY_NO_ERROR' if successful.
15+
*
16+
* Precondition: NULL != node
17+
* NULL != stream
18+
*/
19+
typedef simplicity_err (*simplicity_callback_decodeJet)(dag_node* node, bitstream* stream);
20+
921
/* Decode a length-prefixed Simplicity DAG from 'stream'.
1022
* Returns 'SIMPLICITY_ERR_DATA_OUT_OF_RANGE' the length prefix's value is too large.
1123
* Returns 'SIMPLICITY_ERR_DATA_OUT_OF_RANGE' if some node's child isn't a reference to one of the preceding nodes.
@@ -28,6 +40,6 @@
2840
* of the function is positive and when NULL != census;
2941
* NULL == *dag when the return value is negative.
3042
*/
31-
int_fast32_t simplicity_decodeMallocDag(dag_node** dag, combinator_counters* census, bitstream* stream);
43+
int_fast32_t simplicity_decodeMallocDag(dag_node** dag, simplicity_callback_decodeJet decodeJet, combinator_counters* census, bitstream* stream);
3244

3345
#endif

C/elements-sources.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/exec.c
2929
ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/jets.c
3030
ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/ops.c
3131
ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/primitive.c
32+
ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/txEnv.c
3233

3334
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT =
3435
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/bitstream.h
@@ -42,7 +43,6 @@ ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/frame.h
4243
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/jets.h
4344
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/limitations.h
4445
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/precomputed.h
45-
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive.h
4646
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/rsort.h
4747
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/sha256.h
4848
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/sha256_x86.inc
@@ -97,3 +97,4 @@ ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/primitiveEnum
9797
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/primitiveEnumTy.inc
9898
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/primitiveInitTy.inc
9999
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/primitiveJetNode.inc
100+
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/txEnv.h

C/primitive.h

Lines changed: 0 additions & 41 deletions
This file was deleted.

C/primitive/elements/cmr.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../../limitations.h"
55
#include "../../simplicity_alloc.h"
66
#include "../../simplicity_assert.h"
7+
#include "primitive.h"
78

89
/* Deserialize a Simplicity 'program' and compute its CMR.
910
*
@@ -26,7 +27,7 @@ bool simplicity_elements_computeCmr( simplicity_err* error, unsigned char* cmr
2627

2728
bitstream stream = initializeBitstream(program, program_len);
2829
dag_node* dag = NULL;
29-
int_fast32_t dag_len = simplicity_decodeMallocDag(&dag, NULL, &stream);
30+
int_fast32_t dag_len = simplicity_decodeMallocDag(&dag, simplicity_elements_decodeJet, NULL, &stream);
3031
if (dag_len <= 0) {
3132
simplicity_assert(dag_len < 0);
3233
*error = (simplicity_err)dag_len;

C/primitive/elements/env.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <stdalign.h>
44
#include <stddef.h>
55
#include <string.h>
6-
#include "primitive.h"
6+
#include "txEnv.h"
77
#include "ops.h"
88
#include "../../rsort.h"
99
#include "../../sha256.h"
@@ -651,28 +651,3 @@ extern tapEnv* simplicity_elements_mallocTapEnv(const rawTapEnv* rawEnv) {
651651
extern void simplicity_elements_freeTapEnv(tapEnv* env) {
652652
simplicity_free(env);
653653
}
654-
655-
/* Construct a txEnv structure from its components.
656-
* This function will precompute any cached values.
657-
*
658-
* Precondition: NULL != tx
659-
* NULL != taproot
660-
* NULL != genesisHash
661-
* ix < tx->numInputs
662-
*/
663-
txEnv simplicity_build_txEnv(const transaction* tx, const tapEnv* taproot, const sha256_midstate* genesisHash, uint_fast32_t ix) {
664-
txEnv result = { .tx = tx
665-
, .taproot = taproot
666-
, .genesisHash = *genesisHash
667-
, .ix = ix
668-
};
669-
sha256_context ctx = sha256_init(result.sigAllHash.s);
670-
sha256_hash(&ctx, genesisHash);
671-
sha256_hash(&ctx, genesisHash);
672-
sha256_hash(&ctx, &tx->txHash);
673-
sha256_hash(&ctx, &taproot->tapEnvHash);
674-
sha256_u32be(&ctx, ix);
675-
sha256_finalize(&ctx);
676-
677-
return result;
678-
}

C/primitive/elements/exec.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdalign.h>
44
#include <string.h>
55
#include "primitive.h"
6+
#include "txEnv.h"
67
#include "../../deserialize.h"
78
#include "../../eval.h"
89
#include "../../limitations.h"
@@ -60,7 +61,7 @@ extern bool simplicity_elements_execSimplicity( simplicity_err* error, unsigned
6061

6162
{
6263
bitstream stream = initializeBitstream(program, program_len);
63-
dag_len = simplicity_decodeMallocDag(&dag, &census, &stream);
64+
dag_len = simplicity_decodeMallocDag(&dag, simplicity_elements_decodeJet, &census, &stream);
6465
if (dag_len <= 0) {
6566
simplicity_assert(dag_len < 0);
6667
*error = (simplicity_err)dag_len;
@@ -79,7 +80,7 @@ extern bool simplicity_elements_execSimplicity( simplicity_err* error, unsigned
7980

8081
if (IS_OK(*error)) {
8182
type* type_dag = NULL;
82-
*error = simplicity_mallocTypeInference(&type_dag, dag, (uint_fast32_t)dag_len, &census);
83+
*error = simplicity_mallocTypeInference(&type_dag, simplicity_elements_mallocBoundVars, dag, (uint_fast32_t)dag_len, &census);
8384
if (IS_OK(*error)) {
8485
simplicity_assert(NULL != type_dag);
8586
if (0 != dag[dag_len-1].sourceType || 0 != dag[dag_len-1].targetType) {

C/primitive/elements/jets.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "jets.h"
22

33
#include "ops.h"
4-
#include "primitive.h"
4+
#include "txEnv.h"
55
#include "../../taptweak.h"
66
#include "../../simplicity_assert.h"
77

C/primitive/elements/ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#define SIMPLICITY_PRIMITIVE_ELEMENTS_OPS_H
55

66
#include "../../sha256.h"
7-
#include "primitive.h"
7+
#include "txEnv.h"
88

99
/* Add an 'confidential' value to be consumed by an ongoing SHA-256 evaluation.
1010
* If the 'confidential' value is blinded, then the 'evenPrefix' used if the y coordinate is even,

C/primitive/elements/primitive.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
/* This module implements the 'primitive.h' interface for the Elements application of Simplicity.
2-
*/
31
#include "primitive.h"
42

53
#include "jets.h"
64
#include "../../limitations.h"
7-
#include "../../primitive.h"
85
#include "../../simplicity_alloc.h"
96
#include "../../simplicity_assert.h"
107

@@ -32,7 +29,7 @@ enum TypeNamesForJets {
3229
* '(*bound_var)[i]' is bound to 'A' and '(*bound_var)[j]' is bound to 'B'
3330
* and, '*word256_ix < *extra_var_start' and '(*bound_var)[*word256_ix]' is bound the type 'TWO^256'
3431
*/
35-
size_t simplicity_mallocBoundVars(unification_var** bound_var, size_t* word256_ix, size_t* extra_var_start, size_t extra_var_len) {
32+
size_t simplicity_elements_mallocBoundVars(unification_var** bound_var, size_t* word256_ix, size_t* extra_var_start, size_t extra_var_len) {
3633
static_assert(1 <= NumberOfTypeNames, "Missing TypeNamesForJets.");
3734
static_assert(NumberOfTypeNames <= NUMBER_OF_TYPENAMES_MAX, "Too many TypeNamesForJets.");
3835
static_assert(DAG_LEN_MAX <= (SIZE_MAX - NumberOfTypeNames) / 6, "NumberOfTypeNames + 6*DAG_LEN_MAX doesn't fit in size_t");
@@ -94,12 +91,12 @@ static dag_node jetNode(jetName name) {
9491
* Returns 'SIMPLICITY_ERR_DATA_OUT_OF_RANGE' if the stream's prefix doesn't match any valid code for a jet.
9592
* Returns 'SIMPLICITY_ERR_BITSTRING_EOF' if not enough bits are available in the 'stream'.
9693
* In the above error cases, 'dag' may be modified.
97-
* Returns 'SIMPLICITY_NO_ERR' if successful.
94+
* Returns 'SIMPLICITY_NO_ERROR' if successful.
9895
*
9996
* Precondition: NULL != node
10097
* NULL != stream
10198
*/
102-
simplicity_err simplicity_decodeJet(dag_node* node, bitstream* stream) {
99+
simplicity_err simplicity_elements_decodeJet(dag_node* node, bitstream* stream) {
103100
jetName name;
104101
simplicity_err error = decodePrimitive(&name, stream);
105102
if (!IS_OK(error)) return error;

0 commit comments

Comments
 (0)