Skip to content

Commit

Permalink
Added -objdump argument to print circuit statistics. Refactored
Browse files Browse the repository at this point in the history
circuit file name suffix handling.
  • Loading branch information
markkurossi committed Oct 2, 2022
1 parent 94d05e5 commit 37f95a2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 21 deletions.
21 changes: 12 additions & 9 deletions apps/garbled/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
bmr := flag.Int("bmr", -1, "semi-honest secure BMR protocol player number")
doc := flag.String("doc", "",
"generate documentation about files to the argument directory")
analyze := flag.Bool("analyze", false, "analyze circuits")
objdump := flag.Bool("objdump", false, "print information about objects")
flag.Parse()

log.SetFlags(0)
Expand Down Expand Up @@ -104,7 +104,16 @@ func main() {
if *ssa && !*compile {
params.NoCircCompile = true
}
params.CircAnalyze = *analyze
if *objdump {
if len(flag.Args()) == 0 {
fmt.Printf("no files specified\n")
os.Exit(1)
}
if err := dumpObjects(flag.Args()); err != nil {
log.Fatal(err)
}
return
}

if len(*doc) > 0 {
if len(flag.Args()) == 0 {
Expand Down Expand Up @@ -163,9 +172,7 @@ func main() {
}
}
}
if strings.HasSuffix(arg, ".circ") ||
strings.HasSuffix(arg, ".bristol") ||
strings.HasSuffix(arg, ".mpclc") {
if circuit.IsFilename(arg) {
circ, err = circuit.Parse(arg)
if err != nil {
fmt.Printf("Failed to parse circuit file '%s': %s\n", arg, err)
Expand Down Expand Up @@ -209,10 +216,6 @@ func main() {

if circ != nil {
circ.AssignLevels()

if params.CircAnalyze {
circ.Analyze()
}
if verbose {
fmt.Printf("Circuit: %v\n", circ)
}
Expand Down
61 changes: 61 additions & 0 deletions apps/garbled/objdump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// main.go
//
// Copyright (c) 2019-2022 Markku Rossi
//
// All rights reserved.
//

package main

import (
"os"

"github.com/markkurossi/mpc/circuit"
"github.com/markkurossi/tabulate"
)

func dumpObjects(files []string) error {
type oCircuit struct {
name string
circuit *circuit.Circuit
}
var circuits []oCircuit

for _, file := range files {
if circuit.IsFilename(file) {
c, err := circuit.Parse(file)
if err != nil {
return err
}
circuits = append(circuits, oCircuit{
name: file,
circuit: c,
})
}
}

if len(circuits) > 0 {
tab := tabulate.New(tabulate.Github)
tab.Header("File")
tab.Header("XOR").SetAlign(tabulate.MR)
tab.Header("XNOR").SetAlign(tabulate.MR)
tab.Header("AND").SetAlign(tabulate.MR)
tab.Header("OR").SetAlign(tabulate.MR)
tab.Header("INV").SetAlign(tabulate.MR)
tab.Header("Gates").SetAlign(tabulate.MR)
tab.Header("xor").SetAlign(tabulate.MR)
tab.Header("!xor").SetAlign(tabulate.MR)
tab.Header("Wires").SetAlign(tabulate.MR)

for _, c := range circuits {
row := tab.Row()
row.Column(c.name)
c.circuit.TabulateRow(row)
}

tab.Print(os.Stdout)
}

return nil
}
8 changes: 4 additions & 4 deletions benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ Optimized circuits from [pkg/math/](pkg/math/):
| sub64.circ | 313 | 63 | |
| mul64.circ | 9707 | 4033 | |
| div64.circ | 24817 | 4664 | |
| MPCL a+b | 316 | 63 | 100 |
| MPCL a-b | 317 | 63 | 100 |
| MPCL a*b | 8294 | 4005 | 99.3 |
| MPCL a/b | 24581 | 8192 | 175.6 |
| MPCL a+b | 251 | 63 | 83.5 |
| MPCL a-b | 252 | 65 | 72.2 |
| MPCL a*b | 8261 | 4007 | 89.7 |
| MPCL a/b | 24515 | 8194 | 100.1 |
9 changes: 6 additions & 3 deletions circuit/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,13 @@ func (c *Circuit) TabulateStats(out io.Writer) {
tab.Header("!XOR").SetAlign(tabulate.MR)
tab.Header("Wires").SetAlign(tabulate.MR)

row := tab.Row()
c.TabulateRow(tab.Row())
tab.Print(out)
}

// TabulateRow tabulates circuit statistics to the argument tabulation
// row.
func (c *Circuit) TabulateRow(row *tabulate.Row) {
var sumGates uint64
for op := XOR; op < Count; op++ {
row.Column(fmt.Sprintf("%v", c.Stats[op]))
Expand All @@ -330,8 +335,6 @@ func (c *Circuit) TabulateStats(out io.Writer) {
row.Column(fmt.Sprintf("%v", c.Stats[XOR]+c.Stats[XNOR]))
row.Column(fmt.Sprintf("%v", c.Stats[AND]+c.Stats[OR]+c.Stats[INV]))
row.Column(fmt.Sprintf("%v", c.NumWires))

tab.Print(out)
}

// Cost computes the relative computational cost of the circuit.
Expand Down
10 changes: 9 additions & 1 deletion circuit/parser.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2019-2021 Markku Rossi
// Copyright (c) 2019-2022 Markku Rossi
//
// All rights reserved.
//
Expand Down Expand Up @@ -41,6 +41,14 @@ func (s Seen) Set(index Wire) error {
return nil
}

// IsFilename tests if the argument file is a potential circuit
// filename.
func IsFilename(file string) bool {
return strings.HasSuffix(file, ".circ") ||
strings.HasSuffix(file, ".bristol") ||
strings.HasSuffix(file, ".mpclc")
}

// Parse parses the circuit file.
func Parse(file string) (*Circuit, error) {
f, err := os.Open(file)
Expand Down
4 changes: 1 addition & 3 deletions compiler/ast/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package ast
import (
"fmt"
"path"
"strings"

"github.com/markkurossi/mpc/circuit"
"github.com/markkurossi/mpc/compiler/circuits"
Expand Down Expand Up @@ -332,8 +331,7 @@ func nativeSSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator,
return block, []ssa.Value{v}, nil

default:
if strings.HasSuffix(name, ".circ") ||
strings.HasSuffix(name, ".mpclc") {
if circuit.IsFilename(name) {
return nativeCircuit(name, block, ctx, gen, args, loc)
}
return nil, nil, ctx.Errorf(loc, "unknown native '%s'", name)
Expand Down
1 change: 0 additions & 1 deletion compiler/utils/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type Params struct {
CircDotOut io.WriteCloser
CircSvgOut io.WriteCloser
CircFormat string
CircAnalyze bool

CircMultArrayTreshold int

Expand Down

0 comments on commit 37f95a2

Please sign in to comment.