-
Notifications
You must be signed in to change notification settings - Fork 22
/
ioarg.go
213 lines (182 loc) · 4.32 KB
/
ioarg.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
//
// Copyright (c) 2019-2024 Markku Rossi
//
// All rights reserved.
//
package circuit
import (
"fmt"
"math/big"
"strings"
"github.com/markkurossi/mpc/types"
)
// IO specifies circuit input and output arguments.
type IO []IOArg
// Size computes the size of the circuit input and output arguments in
// bits.
func (io IO) Size() int {
var sum int
for _, a := range io {
sum += int(a.Type.Bits)
}
return sum
}
func (io IO) String() string {
var str = ""
for i, a := range io {
if i > 0 {
str += ", "
}
if len(a.Name) > 0 {
str += a.Name + ":"
}
str += a.Type.String()
}
return str
}
// Split splits the value into separate I/O arguments.
func (io IO) Split(in *big.Int) []*big.Int {
var result []*big.Int
var bit int
for _, arg := range io {
r := big.NewInt(0)
for i := 0; i < int(arg.Type.Bits); i++ {
if in.Bit(bit) == 1 {
r = big.NewInt(0).SetBit(r, i, 1)
}
bit++
}
result = append(result, r)
}
return result
}
// IOArg describes circuit input argument.
type IOArg struct {
Name string
Type types.Info
Compound IO
}
func (io IOArg) String() string {
if len(io.Compound) > 0 {
return io.Compound.String()
}
if len(io.Name) > 0 {
return io.Name + ":" + io.Type.String()
}
return io.Type.String()
}
// Parse parses the I/O argument from the input string values.
func (io IOArg) Parse(inputs []string) (*big.Int, error) {
result := new(big.Int)
if len(io.Compound) == 0 {
if len(inputs) != 1 {
return nil,
fmt.Errorf("invalid amount of arguments, got %d, expected 1",
len(inputs))
}
switch io.Type.Type {
case types.TInt, types.TUint:
_, ok := result.SetString(inputs[0], 0)
if !ok {
return nil, fmt.Errorf("invalid input '%s' for %s",
inputs[0], io.Type)
}
case types.TBool:
switch inputs[0] {
case "0", "f", "false":
case "1", "t", "true":
result.SetInt64(1)
default:
return nil, fmt.Errorf("invalid bool constant: %s", inputs[0])
}
case types.TArray, types.TSlice:
count := int(io.Type.ArraySize)
elSize := int(io.Type.ElementType.Bits)
if io.Type.Type == types.TArray && count == 0 {
// Handle empty types.TArray arguments.
break
}
val := new(big.Int)
_, ok := val.SetString(inputs[0], 0)
if !ok {
return nil, fmt.Errorf("invalid input '%s' for %s",
inputs[0], io.Type)
}
var bitLen int
if strings.HasPrefix(inputs[0], "0x") {
bitLen = (len(inputs[0]) - 2) * 4
} else {
bitLen = val.BitLen()
}
valElCount := bitLen / elSize
if bitLen%elSize != 0 {
valElCount++
}
if io.Type.Type == types.TSlice {
// Set the count=valElCount for types.TSlice arguments.
count = valElCount
}
if valElCount > count {
return nil, fmt.Errorf("too many values for input: %s",
inputs[0])
}
pad := count - valElCount
val.Lsh(val, uint(pad*elSize))
mask := new(big.Int)
for i := 0; i < elSize; i++ {
mask.SetBit(mask, i, 1)
}
for i := 0; i < count; i++ {
next := new(big.Int).Rsh(val, uint((count-i-1)*elSize))
next = next.And(next, mask)
next.Lsh(next, uint(i*elSize))
result.Or(result, next)
}
default:
return nil, fmt.Errorf("unsupported input type: %s", io.Type)
}
return result, nil
}
if len(inputs) != len(io.Compound) {
return nil,
fmt.Errorf("invalid amount of arguments, got %d, expected %d",
len(inputs), len(io.Compound))
}
var offset int
for idx, arg := range io.Compound {
input, err := arg.Parse(inputs[idx : idx+1])
if err != nil {
return nil, err
}
input.Lsh(input, uint(offset))
result.Or(result, input)
offset += int(arg.Type.Bits)
}
return result, nil
}
// InputSizes computes the bit sizes of the input arguments. This is
// used for parametrized main() when the program is instantiated based
// on input sizes.
func InputSizes(inputs []string) ([]int, error) {
var result []int
for _, input := range inputs {
switch input {
case "_":
result = append(result, 0)
case "0", "f", "false", "1", "t", "true":
result = append(result, 1)
default:
if strings.HasPrefix(input, "0x") {
result = append(result, (len(input)-2)*4)
} else {
val := new(big.Int)
_, ok := val.SetString(input, 0)
if !ok {
return nil, fmt.Errorf("invalid input: %s", input)
}
result = append(result, val.BitLen())
}
}
}
return result, nil
}