-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathencoding_test.go
More file actions
97 lines (76 loc) · 2.28 KB
/
encoding_test.go
File metadata and controls
97 lines (76 loc) · 2.28 KB
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
package kyber
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.dedis.ch/kyber/v4/internal/protobuf"
)
type intWrapper struct {
Value int
}
type int64Wrapper struct {
Value int64
}
type int32Wrapper struct {
Value int32
}
type uint32Wrapper struct {
Value uint32
}
func TestIntAndInt64Encoding(t *testing.T) {
a := intWrapper{Value: 10}
b := int64Wrapper{Value: 10}
aEncoded, err := protobuf.Encode(&a)
require.NoError(t, err)
bEncoded, err := protobuf.Encode(&b)
require.NoError(t, err)
assert.Equal(t, aEncoded, bEncoded)
}
func TestUInt32AndInt32Encoding(t *testing.T) {
u := int32Wrapper{Value: 1}
s := uint32Wrapper{Value: 1}
uEncoded, err := protobuf.Encode(&s)
require.NoError(t, err)
sEncoded, err := protobuf.Encode(&u)
require.NoError(t, err)
assert.NotEqual(t, uEncoded, sEncoded)
}
func TestInt32AndIntEncoding(t *testing.T) {
a := int32Wrapper{Value: 2}
b := intWrapper{Value: 2}
aEncoded, err := protobuf.Encode(&a)
require.NoError(t, err)
bEncoded, err := protobuf.Encode(&b)
require.NoError(t, err)
assert.Equal(t, aEncoded, bEncoded)
}
func TestInt32AndInt64Encoding(t *testing.T) {
a := int32Wrapper{Value: math.MaxInt32}
b := int64Wrapper{Value: math.MaxInt32}
aEncoded, err := protobuf.Encode(&a)
require.NoError(t, err)
bEncoded, err := protobuf.Encode(&b)
require.NoError(t, err)
assert.Equal(t, aEncoded, bEncoded)
}
func TestInt64AndInt32Decoding(t *testing.T) {
a := int64Wrapper{Value: 2}
b := int32Wrapper{Value: 2}
aEncoded, err := protobuf.Encode(&a)
require.NoError(t, err)
bEncoded, err := protobuf.Encode(&b)
require.NoError(t, err)
assert.Equal(t, aEncoded, bEncoded)
var aDecoded int32Wrapper
err = protobuf.Decode(aEncoded, &aDecoded)
require.NoError(t, err)
var bDecoded int32Wrapper
err = protobuf.Decode(bEncoded, &bDecoded)
require.NoError(t, err)
assert.Equal(t, aDecoded.Value, bDecoded.Value)
}
// zig-zag encoding for signed integers, does it also happen with Kyber structs?
// varint encoding, is it a problem for constant-time, how is the data processed before being sent?
// we should not leak information about the numbers' size at transmission time
// NOTE: a lot of parts of the code (e.g. poly.go) use encoding/binary and encoding/hex instead of protobuf