-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcompiler.go
290 lines (249 loc) · 6.14 KB
/
compiler.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// Copyright (c) 2019-2022 Markku Rossi
//
// All rights reserved.
//
package compiler
import (
"fmt"
"io"
"math/big"
"os"
"path"
"strings"
"github.com/markkurossi/mpc/circuit"
"github.com/markkurossi/mpc/compiler/ast"
"github.com/markkurossi/mpc/compiler/utils"
"github.com/markkurossi/mpc/p2p"
)
// Compiler implements MPCL compiler.
type Compiler struct {
params *utils.Params
packages map[string]*ast.Package
pkgPath string
}
type pkgPath struct {
precond string
env string
prefix string
}
// New creates a new compiler instance.
func New(params *utils.Params) *Compiler {
return &Compiler{
params: params,
packages: make(map[string]*ast.Package),
}
}
// Compile compiles the input program.
func (c *Compiler) Compile(data string) (*circuit.Circuit, ast.Annotations,
error) {
return c.compile("{data}", strings.NewReader(data))
}
// CompileFile compiles the input file.
func (c *Compiler) CompileFile(file string) (*circuit.Circuit, ast.Annotations,
error) {
f, err := os.Open(file)
if err != nil {
return nil, nil, err
}
defer f.Close()
return c.compile(file, f)
}
// ParseFile parses the input file.
func (c *Compiler) ParseFile(file string) (*ast.Package, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
logger := utils.NewLogger(os.Stdout)
return c.parse(file, f, logger, nil)
}
func (c *Compiler) compile(source string, in io.Reader) (
*circuit.Circuit, ast.Annotations, error) {
logger := utils.NewLogger(os.Stdout)
pkg, err := c.parse(source, in, logger, ast.NewPackage("main", source, nil))
if err != nil {
return nil, nil, err
}
ctx := ast.NewCodegen(logger, pkg, c.packages, c.params)
program, annotation, err := pkg.Compile(ctx)
if err != nil {
return nil, nil, err
}
if c.params.NoCircCompile {
return nil, annotation, nil
}
circ, err := program.CompileCircuit(c.params)
if err != nil {
return nil, nil, err
}
return circ, annotation, nil
}
// StreamFile compiles the input program and uses the streaming mode
// to garble and stream the circuit to the evaluator node.
func (c *Compiler) StreamFile(conn *p2p.Conn, file string,
input []string) (circuit.IO, []*big.Int, error) {
f, err := os.Open(file)
if err != nil {
return nil, nil, err
}
defer f.Close()
return c.stream(conn, file, f, input)
}
func (c *Compiler) stream(conn *p2p.Conn, source string, in io.Reader,
inputFlag []string) (circuit.IO, []*big.Int, error) {
timing := circuit.NewTiming()
logger := utils.NewLogger(os.Stdout)
pkg, err := c.parse(source, in, logger, ast.NewPackage("main", source, nil))
if err != nil {
return nil, nil, err
}
ctx := ast.NewCodegen(logger, pkg, c.packages, c.params)
program, _, err := pkg.Compile(ctx)
if err != nil {
return nil, nil, err
}
timing.Sample("Compile", nil)
if len(program.Inputs) != 2 {
return nil, nil,
fmt.Errorf("invalid program for 2-party computation: %d parties",
len(program.Inputs))
}
input, err := program.Inputs[0].Parse(inputFlag)
if err != nil {
return nil, nil, err
}
fmt.Printf(" + In1: %s\n", program.Inputs[0])
fmt.Printf(" - In2: %s\n", program.Inputs[1])
fmt.Printf(" - Out: %s\n", program.Outputs)
fmt.Printf(" - In: %s\n", inputFlag)
return program.StreamCircuit(conn, c.params, input, timing)
}
func (c *Compiler) parse(source string, in io.Reader, logger *utils.Logger,
pkg *ast.Package) (*ast.Package, error) {
parser := NewParser(source, c, logger, in)
pkg, err := parser.Parse(pkg)
if err != nil {
return nil, err
}
c.packages[pkg.Name] = pkg
for alias, name := range pkg.Imports {
_, err := c.parsePkg(alias, name, source)
if err != nil {
return nil, err
}
}
return pkg, nil
}
func (c *Compiler) resolvePkgPath() error {
if len(c.pkgPath) != 0 {
return nil
}
for _, pkgPath := range pkgPaths {
// Check path precondition.
if len(pkgPath.precond) > 0 {
_, ok := os.LookupEnv(pkgPath.precond)
if !ok {
continue
}
}
dir := path.Join(os.Getenv(pkgPath.env), pkgPath.prefix)
df, err := os.Open(dir)
if err != nil {
continue
}
defer df.Close()
fi, err := df.Stat()
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("pkg root is not directory: %s", dir)
}
c.pkgPath = dir
break
}
if len(c.pkgPath) == 0 {
fmt.Printf("could not resolve pkg root directory, tried:\n")
for _, pkgPath := range pkgPaths {
if len(pkgPath.precond) > 0 {
fmt.Printf(" - $(%s):\n", pkgPath.precond)
} else {
fmt.Printf(" - *:\n")
}
fmt.Printf(" - $(%s)/%s\n", pkgPath.env, pkgPath.prefix)
}
return fmt.Errorf("could not find pkg root directory")
}
if c.params.Verbose {
fmt.Printf("found PkgRoot from '%s'\n", c.pkgPath)
}
return nil
}
var pkgPaths = []*pkgPath{
{
precond: "MPCLDIR",
env: "MPCLDIR",
prefix: "pkg",
},
{
precond: "GITHUB_WORKFLOW",
env: "GITHUB_WORKSPACE",
prefix: "pkg",
},
{
env: "HOME",
prefix: "go/src/github.com/markkurossi/mpc/pkg",
},
}
func (c *Compiler) parsePkg(alias, name, source string) (*ast.Package, error) {
pkg, ok := c.packages[alias]
if ok {
return pkg, nil
}
pkg = ast.NewPackage(alias, source, nil)
err := c.resolvePkgPath()
if err != nil {
return nil, err
}
if c.params.Verbose {
fmt.Printf("looking for package %s (%s)\n", alias, name)
}
dir := path.Join(c.pkgPath, name)
df, err := os.Open(dir)
if err != nil {
return nil, fmt.Errorf("package %s not found", name)
}
defer df.Close()
files, err := df.Readdirnames(-1)
if err != nil {
return nil, fmt.Errorf("package %s not found: %s", name, err)
}
var mpcls []string
for _, f := range files {
if strings.HasSuffix(f, ".mpcl") {
mpcls = append(mpcls, f)
}
}
if len(mpcls) == 0 {
return nil, fmt.Errorf("package %s is empty", name)
}
for _, mpcl := range mpcls {
fp := path.Join(dir, mpcl)
if c.params.Verbose {
fmt.Printf(" - parsing @%v\n", fp[len(c.pkgPath):])
}
f, err := os.Open(fp)
if err != nil {
fmt.Printf("pkg not found: %s\n", err)
return nil, fmt.Errorf("error reading package %s: %s", name, err)
}
defer f.Close()
pkg, err = c.parse(fp, f, utils.NewLogger(os.Stdout), pkg)
if err != nil {
return nil, err
}
}
return pkg, nil
}