Skip to content

Commit 73be556

Browse files
Implementation of cmr.c for bitcoin
1 parent acbc6fc commit 73be556

File tree

6 files changed

+114
-25
lines changed

6 files changed

+114
-25
lines changed

C/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CORE_OBJS := bitstream.o dag.o deserialize.o eval.o frame.o jets.o jets-secp256k1.o rsort.o sha256.o type.o typeInference.o
2-
BITCOIN_OBJS := bitcoin/env.o bitcoin/exec.o bitcoin/ops.o bitcoin/bitcoinJets.o bitcoin/primitive.o bitcoin/txEnv.o
1+
CORE_OBJS := bitstream.o cmr.o dag.o deserialize.o eval.o frame.o jets.o jets-secp256k1.o rsort.o sha256.o type.o typeInference.o
2+
BITCOIN_OBJS := bitcoin/env.o bitcoin/exec.o bitcoin/ops.o bitcoin/bitcoinJets.o bitcoin/primitive.o bitcoin/cmr.o bitcoin/txEnv.o
33
ELEMENTS_OBJS := elements/env.o elements/exec.o elements/ops.o elements/elementsJets.o elements/primitive.o elements/cmr.o elements/txEnv.o
44
TEST_OBJS := test.o ctx8Pruned.o ctx8Unpruned.o hashBlock.o regression4.o schnorr0.o schnorr6.o typeSkipTest.o elements/checkSigHashAllTx1.o
55

C/bitcoin/cmr.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <simplicity/bitcoin/cmr.h>
2+
3+
#include "../cmr.h"
4+
#include "primitive.h"
5+
6+
/* Deserialize a Simplicity 'program' and compute its CMR.
7+
*
8+
* Caution: no typechecking is performed, only a well-formedness check.
9+
*
10+
* If at any time malloc fails then '*error' is set to 'SIMPLICITY_ERR_MALLOC' and 'false' is returned,
11+
* Otherwise, 'true' is returned indicating that the result was successfully computed and returned in the '*error' value.
12+
*
13+
* If the operation completes successfully then '*error' is set to 'SIMPLICITY_NO_ERROR', and the 'cmr' array is filled in with the program's computed CMR.
14+
*
15+
* Precondition: NULL != error;
16+
* unsigned char cmr[32]
17+
* unsigned char program[program_len]
18+
*/
19+
bool simplicity_bitcoin_computeCmr( simplicity_err* error, unsigned char* cmr
20+
, const unsigned char* program, size_t program_len) {
21+
return simplicity_computeCmr(error, cmr, simplicity_bitcoin_decodeJet, program, program_len);
22+
}

C/cmr.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "cmr.h"
2+
3+
#include "limitations.h"
4+
#include "simplicity_alloc.h"
5+
#include "simplicity_assert.h"
6+
7+
/* Deserialize a Simplicity 'program' and compute its CMR.
8+
*
9+
* Caution: no typechecking is performed, only a well-formedness check.
10+
*
11+
* If at any time malloc fails then '*error' is set to 'SIMPLICITY_ERR_MALLOC' and 'false' is returned,
12+
* Otherwise, 'true' is returned indicating that the result was successfully computed and returned in the '*error' value.
13+
*
14+
* If the operation completes successfully then '*error' is set to 'SIMPLICITY_NO_ERROR', and the 'cmr' array is filled in with the program's computed CMR.
15+
*
16+
* Precondition: NULL != error;
17+
* unsigned char cmr[32]
18+
* unsigned char program[program_len]
19+
*/
20+
bool simplicity_computeCmr( simplicity_err* error, unsigned char* cmr, simplicity_callback_decodeJet decodeJet
21+
, const unsigned char* program, size_t program_len) {
22+
simplicity_assert(NULL != error);
23+
simplicity_assert(NULL != cmr);
24+
simplicity_assert(NULL != program || 0 == program_len);
25+
26+
bitstream stream = initializeBitstream(program, program_len);
27+
dag_node* dag = NULL;
28+
int_fast32_t dag_len = simplicity_decodeMallocDag(&dag, decodeJet, NULL, &stream);
29+
if (dag_len <= 0) {
30+
simplicity_assert(dag_len < 0);
31+
*error = (simplicity_err)dag_len;
32+
} else {
33+
simplicity_assert(NULL != dag);
34+
simplicity_assert((uint_fast32_t)dag_len <= DAG_LEN_MAX);
35+
*error = simplicity_closeBitstream(&stream);
36+
sha256_fromMidstate(cmr, dag[dag_len-1].cmr.s);
37+
}
38+
39+
simplicity_free(dag);
40+
return IS_PERMANENT(*error);
41+
}

C/cmr.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef SIMPLICITY_CMR_H
2+
#define SIMPLICITY_CMR_H
3+
4+
#include <stdbool.h>
5+
#include <stddef.h>
6+
#include <simplicity/errorCodes.h>
7+
#include "deserialize.h"
8+
9+
/* Deserialize a Simplicity 'program' and compute its CMR.
10+
*
11+
* Caution: no typechecking is performed, only a well-formedness check.
12+
*
13+
* If at any time malloc fails then '*error' is set to 'SIMPLICITY_ERR_MALLOC' and 'false' is returned,
14+
* Otherwise, 'true' is returned indicating that the result was successfully computed and returned in the '*error' value.
15+
*
16+
* If the operation completes successfully then '*error' is set to 'SIMPLICITY_NO_ERROR', and the 'cmr' array is filled in with the program's computed CMR.
17+
*
18+
* Precondition: NULL != error;
19+
* unsigned char cmr[32]
20+
* unsigned char program[program_len]
21+
*/
22+
extern bool simplicity_computeCmr( simplicity_err* error, unsigned char* cmr, simplicity_callback_decodeJet decodeJet
23+
, const unsigned char* program, size_t program_len);
24+
#endif

C/elements/cmr.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include <simplicity/elements/cmr.h>
22

3-
#include "../deserialize.h"
4-
#include "../limitations.h"
5-
#include "../simplicity_alloc.h"
6-
#include "../simplicity_assert.h"
3+
#include "../cmr.h"
74
#include "primitive.h"
85

96
/* Deserialize a Simplicity 'program' and compute its CMR.
@@ -21,23 +18,5 @@
2118
*/
2219
bool simplicity_elements_computeCmr( simplicity_err* error, unsigned char* cmr
2320
, const unsigned char* program, size_t program_len) {
24-
simplicity_assert(NULL != error);
25-
simplicity_assert(NULL != cmr);
26-
simplicity_assert(NULL != program || 0 == program_len);
27-
28-
bitstream stream = initializeBitstream(program, program_len);
29-
dag_node* dag = NULL;
30-
int_fast32_t dag_len = simplicity_decodeMallocDag(&dag, simplicity_elements_decodeJet, NULL, &stream);
31-
if (dag_len <= 0) {
32-
simplicity_assert(dag_len < 0);
33-
*error = (simplicity_err)dag_len;
34-
} else {
35-
simplicity_assert(NULL != dag);
36-
simplicity_assert((uint_fast32_t)dag_len <= DAG_LEN_MAX);
37-
*error = simplicity_closeBitstream(&stream);
38-
sha256_fromMidstate(cmr, dag[dag_len-1].cmr.s);
39-
}
40-
41-
simplicity_free(dag);
42-
return IS_PERMANENT(*error);
21+
return simplicity_computeCmr(error, cmr, simplicity_elements_decodeJet, program, program_len);
4322
}

C/include/simplicity/bitcoin/cmr.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef SIMPLICITY_BITCOIN_CMR_H
2+
#define SIMPLICITY_BITCOIN_CMR_H
3+
4+
#include <stdbool.h>
5+
#include <stddef.h>
6+
#include <simplicity/errorCodes.h>
7+
8+
/* Deserialize a Simplicity 'program' and compute its CMR.
9+
*
10+
* Caution: no typechecking is performed, only a well-formedness check.
11+
*
12+
* If at any time malloc fails then '*error' is set to 'SIMPLICITY_ERR_MALLOC' and 'false' is returned,
13+
* Otherwise, 'true' is returned indicating that the result was successfully computed and returned in the '*error' value.
14+
*
15+
* If the operation completes successfully then '*error' is set to 'SIMPLICITY_NO_ERROR', and the 'cmr' array is filled in with the program's computed CMR.
16+
*
17+
* Precondition: NULL != error;
18+
* unsigned char cmr[32]
19+
* unsigned char program[program_len]
20+
*/
21+
extern bool simplicity_bitcoin_computeCmr( simplicity_err* error, unsigned char* cmr
22+
, const unsigned char* program, size_t program_len);
23+
#endif

0 commit comments

Comments
 (0)