-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.go
132 lines (119 loc) · 2.45 KB
/
binary.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
package serpent
type uint32le [4]byte
type uint128le [16]byte
type uint256le [32]byte
type bitsliceNew []uint32le
const (
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
)
var shifts = [len(digits) + 1]uint{
1 << 1: 1,
1 << 2: 2,
1 << 3: 3,
1 << 4: 4,
1 << 5: 5,
}
// Function newFromUint64 creates a uint128le from uint64 'num'.
func newFromUint64(num uint64) (result uint128le) {
for i := 0; i < 8; i++ {
result[i] = byte(num & 0xff)
num >>= 8
}
return result
}
// Function newUint32le takes a []byte and returns a uint32le.
func newUint32le(num []byte) uint32le {
var result uint32le
for i, _ := range num {
result[i] = num[i]
}
return result
}
// Function xor performs a sequential Xor operation on the elements
// in 'args'.
func xorNEW(args []uint128le) *uint128le {
result := &args[0]
for i := 1; i < len(args); i++ {
result.binaryXorNEW(args[i])
}
return result
}
// Method quadSplit splits a 128-bit bitstring into a list of 4 32-bit
// bitstrings.
func (num uint128le) quadSplitNEW() []uint32le {
result := bitsliceNew{newUint32le(num[0:3]),
newUint32le(num[4:7]),
newUint32le(num[8:11]),
newUint32le(num[12:15])}
return result
}
// Method binaryXor performs a Xor operation between 'num' and 'other'.
func (num *uint128le) binaryXorNEW(other uint128le) {
for i := 0; i < 16; i++ {
num[i] ^= other[i]
}
}
// Method String returns 'num' as a string using shifts and a mask
func (num uint128le) String() string {
s := shifts[2]
var a [128]byte
i := 0
m := uintptr(1)
for j := 0; j < len(num); j++ {
for l := 0; l < 8; l++ {
for num[j] > 1 {
a[i] = digits[uintptr(num[j])&m]
num[j] >>= s
i++
l++
}
if l < 7 && num[j] == 1 {
a[i] = digits[uintptr(num[j])]
num[j]--
} else {
a[i] = digits[uintptr(num[j])]
}
i++
}
}
result := string(a[0:])
return result
}
func (bstr *Bitstring) pad(l int) {
b := make([]byte, l)
for i := 0; i < l; i++ {
b[i] = 48
}
*bstr = Bitstring(b) + *bstr
}
func fromBitstring(b Bitstring) uint128le {
if len(b) < 128 {
b.pad(128 - len(b))
}
var result uint128le
var j int = 0
sl := make([]byte, 8)
var tempNum byte = 0
pt := &tempNum
for i := 0; i < 128; {
for p, t := range b[i : i+8] {
if t == 49 {
sl[p] = 1
} else {
sl[p] = 0
}
}
*pt = 0
for q := 7; q >= 0; q-- {
if q == 7 && sl[q] == 0 {
continue
} else if sl[q] == 1 {
*pt += 1 << uint(q)
}
}
result[j] = tempNum
j++
i += 8
}
return result
}