Skip to content

Make primitive.h functions indirect #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion C/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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
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
TEST_OBJS := test.o ctx8Pruned.o ctx8Unpruned.o hashBlock.o regression4.o schnorr0.o schnorr6.o typeSkipTest.o primitive/elements/checkSigHashAllTx1.o

# From https://fastcompression.blogspot.com/2019/01/compiler-warnings.html
Expand Down
13 changes: 6 additions & 7 deletions C/deserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <limits.h>
#include "limitations.h"
#include "primitive.h"
#include "simplicity_alloc.h"
#include "simplicity_assert.h"

Expand Down Expand Up @@ -55,15 +54,15 @@ static simplicity_err getHash(sha256_midstate* result, bitstream* stream) {
* i < 2^31 - 1
* NULL != stream
*/
static simplicity_err decodeNode(dag_node* dag, uint_fast32_t i, bitstream* stream) {
static simplicity_err decodeNode(dag_node* dag, simplicity_callback_decodeJet decodeJet, uint_fast32_t i, bitstream* stream) {
int32_t bit = read1Bit(stream);
if (bit < 0) return (simplicity_err)bit;
dag[i] = (dag_node){0};
if (bit) {
bit = read1Bit(stream);
if (bit < 0) return (simplicity_err)bit;
if (bit) {
return simplicity_decodeJet(&dag[i], stream);
return decodeJet(&dag[i], stream);
} else {
/* Decode WORD. */
int32_t depth = simplicity_decodeUptoMaxInt(stream);
Expand Down Expand Up @@ -153,9 +152,9 @@ static simplicity_err decodeNode(dag_node* dag, uint_fast32_t i, bitstream* stre
* len < 2^31
* NULL != stream
*/
static simplicity_err decodeDag(dag_node* dag, const uint_fast32_t len, combinator_counters* census, bitstream* stream) {
static simplicity_err decodeDag(dag_node* dag, simplicity_callback_decodeJet decodeJet, const uint_fast32_t len, combinator_counters* census, bitstream* stream) {
for (uint_fast32_t i = 0; i < len; ++i) {
simplicity_err error = decodeNode(dag, i, stream);
simplicity_err error = decodeNode(dag, decodeJet, i, stream);
if (!IS_OK(error)) return error;

enumerator(census, dag[i].tag);
Expand Down Expand Up @@ -186,7 +185,7 @@ static simplicity_err decodeDag(dag_node* dag, const uint_fast32_t len, combinat
* of the function is positive and when NULL != census;
* NULL == *dag when the return value is negative.
*/
int_fast32_t simplicity_decodeMallocDag(dag_node** dag, combinator_counters* census, bitstream* stream) {
int_fast32_t simplicity_decodeMallocDag(dag_node** dag, simplicity_callback_decodeJet decodeJet, combinator_counters* census, bitstream* stream) {
*dag = NULL;
int32_t dagLen = simplicity_decodeUptoMaxInt(stream);
if (dagLen <= 0) return dagLen;
Expand All @@ -199,7 +198,7 @@ int_fast32_t simplicity_decodeMallocDag(dag_node** dag, combinator_counters* cen
if (!*dag) return SIMPLICITY_ERR_MALLOC;

if (census) *census = (combinator_counters){0};
simplicity_err error = decodeDag(*dag, (uint_fast32_t)dagLen, census, stream);
simplicity_err error = decodeDag(*dag, decodeJet, (uint_fast32_t)dagLen, census, stream);

if (IS_OK(error)) {
error = HIDDEN == (*dag)[dagLen - 1].tag
Expand Down
14 changes: 13 additions & 1 deletion C/deserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
#include "bitstream.h"
#include "dag.h"

/* Decode an application specific jet from 'stream' into 'node'.
* All jets begin with a bit prefix of '1' which needs to have already been consumed from the 'stream'.
* Returns 'SIMPLICITY_ERR_DATA_OUT_OF_RANGE' if the stream's prefix doesn't match any valid code for a jet.
* Returns 'SIMPLICITY_ERR_BITSTRING_EOF' if not enough bits are available in the 'stream'.
* In the above error cases, 'dag' may be modified.
* Returns 'SIMPLICITY_NO_ERROR' if successful.
*
* Precondition: NULL != node
* NULL != stream
*/
typedef simplicity_err (*simplicity_callback_decodeJet)(dag_node* node, bitstream* stream);

/* Decode a length-prefixed Simplicity DAG from 'stream'.
* Returns 'SIMPLICITY_ERR_DATA_OUT_OF_RANGE' the length prefix's value is too large.
* Returns 'SIMPLICITY_ERR_DATA_OUT_OF_RANGE' if some node's child isn't a reference to one of the preceding nodes.
Expand All @@ -28,6 +40,6 @@
* of the function is positive and when NULL != census;
* NULL == *dag when the return value is negative.
*/
int_fast32_t simplicity_decodeMallocDag(dag_node** dag, combinator_counters* census, bitstream* stream);
int_fast32_t simplicity_decodeMallocDag(dag_node** dag, simplicity_callback_decodeJet decodeJet, combinator_counters* census, bitstream* stream);

#endif
3 changes: 2 additions & 1 deletion C/elements-sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/exec.c
ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/jets.c
ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/ops.c
ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/primitive.c
ELEMENTS_SIMPLICITY_LIB_SOURCES_INT += %reldir%/primitive/elements/txEnv.c

ELEMENTS_SIMPLICITY_LIB_HEADERS_INT =
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/bitstream.h
Expand All @@ -42,7 +43,6 @@ ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/frame.h
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/jets.h
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/limitations.h
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/precomputed.h
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive.h
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/rsort.h
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/sha256.h
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/sha256_x86.inc
Expand Down Expand Up @@ -97,3 +97,4 @@ ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/primitiveEnum
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/primitiveEnumTy.inc
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/primitiveInitTy.inc
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/primitiveJetNode.inc
ELEMENTS_SIMPLICITY_LIB_HEADERS_INT += %reldir%/primitive/elements/txEnv.h
41 changes: 0 additions & 41 deletions C/primitive.h

This file was deleted.

3 changes: 2 additions & 1 deletion C/primitive/elements/cmr.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../../limitations.h"
#include "../../simplicity_alloc.h"
#include "../../simplicity_assert.h"
#include "primitive.h"

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

bitstream stream = initializeBitstream(program, program_len);
dag_node* dag = NULL;
int_fast32_t dag_len = simplicity_decodeMallocDag(&dag, NULL, &stream);
int_fast32_t dag_len = simplicity_decodeMallocDag(&dag, simplicity_elements_decodeJet, NULL, &stream);
if (dag_len <= 0) {
simplicity_assert(dag_len < 0);
*error = (simplicity_err)dag_len;
Expand Down
27 changes: 1 addition & 26 deletions C/primitive/elements/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdalign.h>
#include <stddef.h>
#include <string.h>
#include "primitive.h"
#include "txEnv.h"
#include "ops.h"
#include "../../rsort.h"
#include "../../sha256.h"
Expand Down Expand Up @@ -651,28 +651,3 @@ extern tapEnv* simplicity_elements_mallocTapEnv(const rawTapEnv* rawEnv) {
extern void simplicity_elements_freeTapEnv(tapEnv* env) {
simplicity_free(env);
}

/* Construct a txEnv structure from its components.
* This function will precompute any cached values.
*
* Precondition: NULL != tx
* NULL != taproot
* NULL != genesisHash
* ix < tx->numInputs
*/
txEnv simplicity_build_txEnv(const transaction* tx, const tapEnv* taproot, const sha256_midstate* genesisHash, uint_fast32_t ix) {
txEnv result = { .tx = tx
, .taproot = taproot
, .genesisHash = *genesisHash
, .ix = ix
};
sha256_context ctx = sha256_init(result.sigAllHash.s);
sha256_hash(&ctx, genesisHash);
sha256_hash(&ctx, genesisHash);
sha256_hash(&ctx, &tx->txHash);
sha256_hash(&ctx, &taproot->tapEnvHash);
sha256_u32be(&ctx, ix);
sha256_finalize(&ctx);

return result;
}
5 changes: 3 additions & 2 deletions C/primitive/elements/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdalign.h>
#include <string.h>
#include "primitive.h"
#include "txEnv.h"
#include "../../deserialize.h"
#include "../../eval.h"
#include "../../limitations.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ extern bool simplicity_elements_execSimplicity( simplicity_err* error, unsigned

{
bitstream stream = initializeBitstream(program, program_len);
dag_len = simplicity_decodeMallocDag(&dag, &census, &stream);
dag_len = simplicity_decodeMallocDag(&dag, simplicity_elements_decodeJet, &census, &stream);
if (dag_len <= 0) {
simplicity_assert(dag_len < 0);
*error = (simplicity_err)dag_len;
Expand All @@ -79,7 +80,7 @@ extern bool simplicity_elements_execSimplicity( simplicity_err* error, unsigned

if (IS_OK(*error)) {
type* type_dag = NULL;
*error = simplicity_mallocTypeInference(&type_dag, dag, (uint_fast32_t)dag_len, &census);
*error = simplicity_mallocTypeInference(&type_dag, simplicity_elements_mallocBoundVars, dag, (uint_fast32_t)dag_len, &census);
if (IS_OK(*error)) {
simplicity_assert(NULL != type_dag);
if (0 != dag[dag_len-1].sourceType || 0 != dag[dag_len-1].targetType) {
Expand Down
2 changes: 1 addition & 1 deletion C/primitive/elements/jets.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "jets.h"

#include "ops.h"
#include "primitive.h"
#include "txEnv.h"
#include "../../taptweak.h"
#include "../../simplicity_assert.h"

Expand Down
2 changes: 1 addition & 1 deletion C/primitive/elements/ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define SIMPLICITY_PRIMITIVE_ELEMENTS_OPS_H

#include "../../sha256.h"
#include "primitive.h"
#include "txEnv.h"

/* Add an 'confidential' value to be consumed by an ongoing SHA-256 evaluation.
* If the 'confidential' value is blinded, then the 'evenPrefix' used if the y coordinate is even,
Expand Down
9 changes: 3 additions & 6 deletions C/primitive/elements/primitive.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* This module implements the 'primitive.h' interface for the Elements application of Simplicity.
*/
#include "primitive.h"

#include "jets.h"
#include "../../limitations.h"
#include "../../primitive.h"
#include "../../simplicity_alloc.h"
#include "../../simplicity_assert.h"

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