-
Notifications
You must be signed in to change notification settings - Fork 13
/
ffi_example.go
112 lines (100 loc) · 5.21 KB
/
ffi_example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
// ===================
// For dynamic linking
// ===================
// cgo LDFLAGS: -L./target/release/ -lcorset
// ==================
// For static linking
// ==================
//#cgo LDFLAGS: ./target/release/libcorset.a -lm
//#cgo CFLAGS: -I./target/
//#include <corset.h>
import "C"
import "fmt"
import "unsafe"
import _ "embed"
// +----------------------------------------------------------------------------------------+
// | |
// | ========== Exposed by libcorset ========== |
// | |
// | |
// | * Compute a trace & free it |
// | |
// | struct Trace *trace_compute(const char *zkevmfile, |
// | const char *tracefile, |
// | unsigned int threads, |
// | bool fail_on_missing); |
// | void trace_free(struct Trace *trace); |
// | |
// | |
// | * Get the number of columns in the trace |
// | |
// | unsigned int trace_column_count(const struct Trace *trace); |
// | |
// | |
// | * Get all column names; position in array == ID of the column |
// | |
// | char *const *trace_column_names(const struct Trace *trace); |
// | |
// | |
// | * Get column values, either by name or by ID (faster) |
// | |
// | struct ColumnData trace_column_by_name(const struct Trace *trace, const char *name); |
// | struct ColumnData trace_column_by_id(const struct Trace *trace, uint32_t i); |
// | |
// | * What's in a column |
// | |
// | typedef struct ColumnData { // all Fr are represented as [uint64; 4] |
// | uint64_t padding_value[4]; // a single Fr representing the padding value |
// | const uint64_t (*values)[4]; // the actual values of the column |
// | uint64_t values_len; // the length of the previous array |
// | } ColumnData; |
// +----------------------------------------------------------------------------------------+
func getColumnNames(trace *C.Trace) (r []string) {
ncols := C.trace_column_count(trace)
colnames_c := unsafe.Slice(C.trace_column_names(trace), ncols)
for _, name_c := range colnames_c {
name := C.GoString(name_c)
r = append(r, name)
fmt.Println(name)
}
fmt.Println(ncols, "columns found")
return
}
// go:embed zkevm.bin
var zkevm string
func main() {
// Load a .json(.gz), a constraint set, and return the computed & expanded trace
corset, err := C.corset_from_file(C.CString("/home/franklin/consensys/zkevm/zk-geth/zk-evm/zkevm.bin"))
// corset, err := C.corset_from_string(C.CString(zkevm))
trace, err := C.trace_compute(
corset,
C.CString("/home/franklin/traces/1846717126.json"), // trace file
4, // # threads
false, // crash on missing columns in the trace
)
if err != nil {
fmt.Println("Error while computing trace")
return
}
// Get all the column names
names := getColumnNames(trace)
fmt.Println("found columns:", names)
// Take a look at a column in particular
col, err := C.trace_column_by_name(trace, C.CString("rom__INV_rom_CODE_FRAGMENT_INDEX"))
if err != nil {
fmt.Println("while retrieving column")
return
}
// Show padding value, values, and values length
fmt.Println("col =", col)
// Convert the raw pointer to a slice
values := unsafe.Slice(col.values, col.values_len)
// See what's in it
fmt.Println(len(values), "values found")
for i, op := range values {
fmt.Println(i, op)
}
// Free memory
C.trace_free(trace)
}