|
| 1 | +package miniscript |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// maxFuzzInput bounds the size of a fuzz input. Parse's threshold passes are |
| 14 | +// exponential in the number of thresh sub expressions, so an unbounded input |
| 15 | +// could construct a pathologically expensive (though still bounded) parse; this |
| 16 | +// keeps individual executions fast while leaving plenty of room to reach every |
| 17 | +// fragment. Active fuzzing may still surface the known exponential-thresh cost |
| 18 | +// for a compact but large threshold. |
| 19 | +const maxFuzzInput = 4096 |
| 20 | + |
| 21 | +// miniscriptFuzzSeedFiles are the committed corpora fed in as fuzz seeds. Their |
| 22 | +// first whitespace-separated token is the expression (the remaining columns are |
| 23 | +// the expected type and op count in some files). |
| 24 | +var miniscriptFuzzSeedFiles = []string{ |
| 25 | + "valid_from_alloy.txt", |
| 26 | + "valid_8f1e8_from_alloy.txt", |
| 27 | + "malleable_from_alloy.txt", |
| 28 | + "conflict_from_alloy.txt", |
| 29 | + "invalid_from_alloy.txt", |
| 30 | + "edge_cases.txt", |
| 31 | + "opcodes.txt", |
| 32 | + "invalid.txt", |
| 33 | +} |
| 34 | + |
| 35 | +// hardcodedMiniscriptSeeds covers every fragment kind with single-letter keys, |
| 36 | +// keeping the seed set representative even without the corpus files. |
| 37 | +var hardcodedMiniscriptSeeds = []string{ |
| 38 | + "0", |
| 39 | + "1", |
| 40 | + "pk(A)", |
| 41 | + "pkh(A)", |
| 42 | + "pk_k(A)", |
| 43 | + "pk_h(A)", |
| 44 | + "older(144)", |
| 45 | + "after(500000000)", |
| 46 | + "sha256(" + strings.Repeat("0", 64) + ")", |
| 47 | + "and_v(v:pk(A),pk(B))", |
| 48 | + "and_b(pk(A),s:pk(B))", |
| 49 | + "or_b(pk(A),s:pk(B))", |
| 50 | + "or_c(pk(A),v:pk(B))", |
| 51 | + "or_d(pk(A),pk(B))", |
| 52 | + "or_i(pk(A),pk(B))", |
| 53 | + "andor(pk(A),pk(B),pk(C))", |
| 54 | + "thresh(2,pk(A),s:pk(B),s:pk(C))", |
| 55 | + "multi(2,A,B,C)", |
| 56 | + "multi_a(2,A,B,C)", |
| 57 | + "and_v(v:pk(A),or_d(pk(B),older(65535)))", |
| 58 | +} |
| 59 | + |
| 60 | +// FuzzParse fuzzes miniscript parsing and, on every successful parse, exercises |
| 61 | +// the deep downstream passes (lifting, satisfaction sizing, key extraction, |
| 62 | +// script generation and satisfaction) in both script contexts. It checks the |
| 63 | +// no-crash property throughout, plus the invariant that the statically computed |
| 64 | +// script length matches the length of the actually generated script. |
| 65 | +func FuzzParse(f *testing.F) { |
| 66 | + for _, dir := range []string{"testdata"} { |
| 67 | + for _, name := range miniscriptFuzzSeedFiles { |
| 68 | + addMiniscriptSeeds(f, filepath.Join(dir, name)) |
| 69 | + } |
| 70 | + } |
| 71 | + for _, seed := range hardcodedMiniscriptSeeds { |
| 72 | + f.Add(seed) |
| 73 | + } |
| 74 | + |
| 75 | + f.Fuzz(func(t *testing.T, expr string) { |
| 76 | + if len(expr) > maxFuzzInput { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // A miniscript is valid (or not) largely independent of the |
| 81 | + // context, but the allowed fragments differ (multi vs multi_a), |
| 82 | + // so both are worth exercising. |
| 83 | + for _, ctx := range []Context{P2WSH, P2TR} { |
| 84 | + node, err := Parse(expr, ctx) |
| 85 | + if err != nil { |
| 86 | + continue |
| 87 | + } |
| 88 | + exerciseAST(t, node, ctx, expr) |
| 89 | + } |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +// exerciseAST drives the deep, key-independent and key-dependent operations of |
| 94 | +// a successfully parsed miniscript, asserting only invariants that hold for any |
| 95 | +// valid expression. |
| 96 | +func exerciseAST(t *testing.T, node *AST, ctx Context, expr string) { |
| 97 | + // Key-independent operations must never panic. |
| 98 | + _ = node.DrawTree() |
| 99 | + _ = node.Keys() |
| 100 | + _, _ = node.Lift() |
| 101 | + _, _ = node.MaxSatisfactionSize() |
| 102 | + _, _ = node.MaxSatisfactionWitnessElements() |
| 103 | + |
| 104 | + // Substitute a concrete key of the length the context expects, then |
| 105 | + // check that the compiled script has exactly the statically computed |
| 106 | + // length. This ties the size pass to the real script builder. |
| 107 | + keyLen := 33 |
| 108 | + if ctx == P2TR { |
| 109 | + keyLen = 32 |
| 110 | + } |
| 111 | + dummyKey := append( |
| 112 | + []byte{0x02}, bytes.Repeat([]byte{0x01}, keyLen-1)..., |
| 113 | + ) |
| 114 | + |
| 115 | + err := node.ApplyVars(func(string) ([]byte, error) { |
| 116 | + return dummyKey, nil |
| 117 | + }) |
| 118 | + if err != nil { |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + script, err := node.Script() |
| 123 | + if err == nil { |
| 124 | + require.Equalf(t, node.ScriptLen(), len(script), |
| 125 | + "ScriptLen mismatch for %q in %v", expr, ctx) |
| 126 | + } |
| 127 | + |
| 128 | + // A satisfier that provides everything drives the full satisfaction |
| 129 | + // machinery; it must not panic regardless of the shape of the script. |
| 130 | + _, _ = node.Satisfy(&Satisfier{ |
| 131 | + Sign: func([]byte) ([]byte, bool) { |
| 132 | + return make([]byte, 72), true |
| 133 | + }, |
| 134 | + Preimage: func(string, []byte) ([]byte, bool) { |
| 135 | + return make([]byte, 32), true |
| 136 | + }, |
| 137 | + CheckOlder: func(uint32) (bool, error) { |
| 138 | + return true, nil |
| 139 | + }, |
| 140 | + CheckAfter: func(uint32) (bool, error) { |
| 141 | + return true, nil |
| 142 | + }, |
| 143 | + }) |
| 144 | +} |
| 145 | + |
| 146 | +// addMiniscriptSeeds adds the first token of each non-empty, non-comment line |
| 147 | +// of the given file as a fuzz seed. A missing file is ignored so the fuzzer |
| 148 | +// stays usable without the full corpus. |
| 149 | +func addMiniscriptSeeds(f *testing.F, path string) { |
| 150 | + data, err := os.ReadFile(path) |
| 151 | + if err != nil { |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + for _, line := range strings.Split(string(data), "\n") { |
| 156 | + line = strings.TrimSpace(line) |
| 157 | + if line == "" || strings.HasPrefix(line, "#") { |
| 158 | + continue |
| 159 | + } |
| 160 | + f.Add(strings.Fields(line)[0]) |
| 161 | + } |
| 162 | +} |
0 commit comments