|
| 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 | +} |
0 commit comments