Skip to content

Commit 209cc34

Browse files
committed
f split computeamr
1 parent 4e3287d commit 209cc34

File tree

3 files changed

+66
-57
lines changed

3 files changed

+66
-57
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ test_fuzz_fuzz_SOURCES = \
312312
test/fuzz/secp256k1_ecdsa_signature_parse_der_lax.cpp \
313313
test/fuzz/signature_checker.cpp \
314314
test/fuzz/signet.cpp \
315+
test/fuzz/simplicity_compute_amr.c \
315316
test/fuzz/simplicity.cpp \
316317
test/fuzz/socks5.cpp \
317318
test/fuzz/span.cpp \

src/test/fuzz/simplicity.cpp

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@
66
#include <primitives/transaction.h>
77
extern "C" {
88
#include <simplicity/cmr.h>
9-
#define ReadBE64 simplicity_ReadBE64
10-
#define ReadBE32 simplicity_ReadBE32
11-
#define WriteBE64 simplicity_WriteBE64
12-
#include <simplicity/dag.h>
13-
#undef simplicity_ReadBE64
14-
#undef simplicity_ReadBE32
15-
#undef simplicity_WriteBE64
16-
#include <simplicity/deserialize.h> // simplicity_decodeMallocDag
17-
#include <simplicity/limitations.h> // DAG_LEN_MAX
18-
#include <simplicity/simplicity_alloc.h> // simplicity_free
19-
#include <simplicity/typeInference.h> // simplicity_mallocTypeInference
209
#include <simplicity/elements/env.h>
2110
#include <simplicity/elements/exec.h>
2211
}
@@ -40,53 +29,11 @@ std::vector<unsigned char> TAPROOT_CONTROL{};
4029
std::vector<unsigned char> TAPROOT_ANNEX{99, 0x50};
4130
//CMutableTransaction MTX_TEMPLATE{};
4231

43-
// Copy of computeCmr used for AMR
32+
// Defined in simplicity_compute_amr.c
33+
extern "C" {
4434
bool simplicity_computeAmr( simplicity_err* error, unsigned char* amr
4535
, const unsigned char* program, size_t program_len
46-
, const unsigned char* witness, size_t witness_len) {
47-
simplicity_assert(NULL != error);
48-
simplicity_assert(NULL != amr);
49-
simplicity_assert(NULL != program || 0 == program_len);
50-
simplicity_assert(NULL != witness || 0 == witness_len);
51-
52-
bitstream stream = initializeBitstream(program, program_len);
53-
dag_node* dag = NULL;
54-
combinator_counters census;
55-
int_fast32_t dag_len = simplicity_decodeMallocDag(&dag, &census, &stream);
56-
if (dag_len <= 0) {
57-
simplicity_assert(dag_len < 0);
58-
*error = (simplicity_err)dag_len;
59-
} else {
60-
simplicity_assert(NULL != dag);
61-
simplicity_assert((uint_fast32_t)dag_len <= DAG_LEN_MAX);
62-
*error = simplicity_closeBitstream(&stream);
63-
64-
type* type_dag = NULL;
65-
if (IS_OK(*error)) {
66-
*error = simplicity_mallocTypeInference(&type_dag, dag, (uint_fast32_t)dag_len, &census);
67-
}
68-
bitstream witness_stream;
69-
if (IS_OK(*error)) {
70-
witness_stream = initializeBitstream(witness, witness_len);
71-
*error = simplicity_fillWitnessData(dag, type_dag, (uint_fast32_t)dag_len, &witness_stream);
72-
}
73-
if (IS_OK(*error)) {
74-
*error = simplicity_closeBitstream(&witness_stream);
75-
if (SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES == *error) *error = SIMPLICITY_ERR_WITNESS_TRAILING_BYTES;
76-
if (SIMPLICITY_ERR_BITSTREAM_ILLEGAL_PADDING == *error) *error = SIMPLICITY_ERR_WITNESS_ILLEGAL_PADDING;
77-
}
78-
if (IS_OK(*error)) {
79-
analyses *analysis = (analyses*) simplicity_malloc((size_t)dag_len * sizeof(analyses));
80-
simplicity_assert(NULL != analysis);
81-
simplicity_computeAnnotatedMerkleRoot(analysis, dag, type_dag, (uint_fast32_t)dag_len);
82-
sha256_fromMidstate(amr, analysis[dag_len-1].annotatedMerkleRoot.s);
83-
simplicity_free(analysis);
84-
}
85-
simplicity_free(type_dag);
86-
}
87-
88-
simplicity_free(dag);
89-
return IS_PERMANENT(*error);
36+
, const unsigned char* witness, size_t witness_len);
9037
}
9138

9239
void initialize_simplicity()
@@ -163,7 +110,7 @@ FUZZ_TARGET_INIT(simplicity, initialize_simplicity)
163110

164111
//printf("OK going\n");
165112

166-
// 2. Parse the transaction (the program and witness are just raw bytes)
113+
// 2. Parse the transaction (the program and witness are just raw bytes)
167114
CMutableTransaction mtx;
168115
CDataStream txds{Span{tx_data, tx_data_len}, SER_NETWORK, INIT_PROTO_VERSION};
169116
try {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <simplicity/cmr.h>
6+
#include <simplicity/dag.h>
7+
#include <simplicity/deserialize.h> // simplicity_decodeMallocDag
8+
#include <simplicity/limitations.h> // DAG_LEN_MAX
9+
#include <simplicity/simplicity_alloc.h> // simplicity_free
10+
#include <simplicity/typeInference.h> // simplicity_mallocTypeInference
11+
#include <simplicity/elements/env.h>
12+
#include <simplicity/elements/exec.h>
13+
14+
// Copy of computeCmr used for AMR
15+
bool simplicity_computeAmr( simplicity_err* error, unsigned char* amr
16+
, const unsigned char* program, size_t program_len
17+
, const unsigned char* witness, size_t witness_len) {
18+
simplicity_assert(NULL != error);
19+
simplicity_assert(NULL != amr);
20+
simplicity_assert(NULL != program || 0 == program_len);
21+
simplicity_assert(NULL != witness || 0 == witness_len);
22+
23+
bitstream stream = initializeBitstream(program, program_len);
24+
dag_node* dag = NULL;
25+
combinator_counters census;
26+
int_fast32_t dag_len = simplicity_decodeMallocDag(&dag, &census, &stream);
27+
if (dag_len <= 0) {
28+
simplicity_assert(dag_len < 0);
29+
*error = (simplicity_err)dag_len;
30+
} else {
31+
simplicity_assert(NULL != dag);
32+
simplicity_assert((uint_fast32_t)dag_len <= DAG_LEN_MAX);
33+
*error = simplicity_closeBitstream(&stream);
34+
35+
type* type_dag = NULL;
36+
if (IS_OK(*error)) {
37+
*error = simplicity_mallocTypeInference(&type_dag, dag, (uint_fast32_t)dag_len, &census);
38+
}
39+
bitstream witness_stream;
40+
if (IS_OK(*error)) {
41+
witness_stream = initializeBitstream(witness, witness_len);
42+
*error = simplicity_fillWitnessData(dag, type_dag, (uint_fast32_t)dag_len, &witness_stream);
43+
}
44+
if (IS_OK(*error)) {
45+
*error = simplicity_closeBitstream(&witness_stream);
46+
if (SIMPLICITY_ERR_BITSTREAM_TRAILING_BYTES == *error) *error = SIMPLICITY_ERR_WITNESS_TRAILING_BYTES;
47+
if (SIMPLICITY_ERR_BITSTREAM_ILLEGAL_PADDING == *error) *error = SIMPLICITY_ERR_WITNESS_ILLEGAL_PADDING;
48+
}
49+
if (IS_OK(*error)) {
50+
analyses *analysis = (analyses*) simplicity_malloc((size_t)dag_len * sizeof(analyses));
51+
simplicity_assert(NULL != analysis);
52+
simplicity_computeAnnotatedMerkleRoot(analysis, dag, type_dag, (uint_fast32_t)dag_len);
53+
sha256_fromMidstate(amr, analysis[dag_len-1].annotatedMerkleRoot.s);
54+
simplicity_free(analysis);
55+
}
56+
simplicity_free(type_dag);
57+
}
58+
59+
simplicity_free(dag);
60+
return IS_PERMANENT(*error);
61+
}

0 commit comments

Comments
 (0)