-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathresult.go
175 lines (151 loc) · 3.86 KB
/
result.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
//
// Copyright (c) 2019-2024 Markku Rossi
//
// All rights reserved.
//
package mpc
import (
"fmt"
"math/big"
"reflect"
"unicode"
"github.com/markkurossi/mpc/circuit"
"github.com/markkurossi/mpc/types"
)
// PrintResults prints the result values.
func PrintResults(results []*big.Int, outputs circuit.IO) {
for idx, value := range Results(results, outputs) {
fmt.Printf("Result[%d]: ", idx)
switch v := value.(type) {
case []byte:
fmt.Printf("%x\n", v)
default:
fmt.Printf("%v\n", v)
}
}
}
// Results return the result values as an array of Go values.
func Results(results []*big.Int, outputs circuit.IO) []interface{} {
var ret []interface{}
for idx, result := range results {
var r interface{}
if outputs == nil {
r = Result(result, circuit.IOArg{
Type: types.Info{
Type: types.TUint,
IsConcrete: true,
Bits: 1024, // Anything >64 returns big.Int
},
})
} else {
r = Result(result, outputs[idx])
}
ret = append(ret, r)
}
return ret
}
// Result converts the result to Go value.
func Result(result *big.Int, output circuit.IOArg) interface{} {
switch output.Type.Type {
case types.TString:
mask := big.NewInt(0xff)
var str string
for i := 0; i < int(output.Type.Bits)/8; i++ {
tmp := new(big.Int).Rsh(result, uint(i*8))
r := rune(tmp.And(tmp, mask).Uint64())
if unicode.IsPrint(r) {
str += string(r)
} else {
str += fmt.Sprintf("\\u%04x", r)
}
}
return str
case types.TUint:
if output.Type.Bits <= 8 {
return uint8(result.Uint64())
} else if output.Type.Bits <= 16 {
return uint16(result.Uint64())
} else if output.Type.Bits <= 32 {
return uint32(result.Uint64())
} else if output.Type.Bits <= 64 {
return result.Uint64()
} else {
return result
}
case types.TInt:
bits := int(output.Type.Bits)
if result.Bit(bits-1) == 1 {
// Negative number.
tmp := new(big.Int)
tmp.SetBit(tmp, bits, 1)
result.Sub(tmp, result)
result.Neg(result)
}
if output.Type.Bits <= 8 {
return int8(result.Int64())
} else if output.Type.Bits <= 16 {
return int16(result.Int64())
} else if output.Type.Bits <= 32 {
return int32(result.Int64())
} else if output.Type.Bits <= 64 {
return result.Int64()
} else {
return result
}
case types.TBool:
return result.Uint64() != 0
case types.TArray, types.TSlice:
count := int(output.Type.ArraySize)
elSize := int(output.Type.ElementType.Bits)
mask := new(big.Int)
for i := 0; i < elSize; i++ {
mask.SetBit(mask, i, 1)
}
var slice reflect.Value
var elementType reflect.Type
switch output.Type.ElementType.Type {
case types.TString:
elementType = reflect.TypeOf("")
case types.TUint:
if elSize <= 8 {
elementType = reflect.TypeOf(uint8(0))
} else if elSize <= 16 {
elementType = reflect.TypeOf(uint16(0))
} else if elSize <= 32 {
elementType = reflect.TypeOf(uint32(0))
} else if elSize <= 64 {
elementType = reflect.TypeOf(uint64(0))
} else {
elementType = reflect.TypeOf((*big.Int)(nil))
}
case types.TInt:
if elSize <= 8 {
elementType = reflect.TypeOf(int8(0))
} else if elSize <= 16 {
elementType = reflect.TypeOf(int16(0))
} else if elSize <= 32 {
elementType = reflect.TypeOf(int32(0))
} else if elSize <= 64 {
elementType = reflect.TypeOf(int64(0))
} else {
elementType = reflect.TypeOf((*big.Int)(nil))
}
case types.TBool:
elementType = reflect.TypeOf(true)
default:
elementType = reflect.TypeOf(nil)
}
slice = reflect.MakeSlice(reflect.SliceOf(elementType), 0, count)
for i := 0; i < count; i++ {
r := new(big.Int).Rsh(result, uint(i*elSize))
r = r.And(r, mask)
v := reflect.ValueOf(Result(r, circuit.IOArg{
Type: *output.Type.ElementType,
}))
slice = reflect.Append(slice, v)
}
return slice.Interface()
default:
return fmt.Sprintf("%v (%s)", result, output.Type)
}
}