-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharaka_test.go
More file actions
242 lines (201 loc) · 5.53 KB
/
Copy pathharaka_test.go
File metadata and controls
242 lines (201 loc) · 5.53 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package aes
import (
"encoding/hex"
"testing"
)
func TestHaraka256(t *testing.T) {
// Test vector from Appendix B of the Haraka v2 paper
// Input: 0x00 0x01 0x02 ... 0x1f (32 bytes)
var input [32]byte
for i := range input {
input[i] = byte(i)
}
// Expected output from reference implementation
expected, _ := hex.DecodeString("8027ccb87949774b78d0545fb72bf70c695c2a0923cbd47bba1159efbf2b2c1c")
output := Haraka256(&input)
if string(output[:]) != string(expected) {
t.Errorf("Haraka256 mismatch\nInput: %x\nExpected: %x\nGot: %x", input, expected, output)
}
}
func TestHaraka512(t *testing.T) {
// Test vector from Appendix B of the Haraka v2 paper
// Input: 0x00 0x01 0x02 ... 0x3f (64 bytes)
var input [64]byte
for i := range input {
input[i] = byte(i)
}
// Expected output from reference implementation
expected, _ := hex.DecodeString("be7f723b4e80a99813b292287f306f625a6d57331cae5f34dd9277b0945be2aa")
output := Haraka512(&input)
if string(output[:]) != string(expected) {
t.Errorf("Haraka512 mismatch\nInput: %x\nExpected: %x\nGot: %x", input, expected, output)
}
}
func TestHaraka256ZeroInput(t *testing.T) {
var input [32]byte
output := Haraka256(&input)
// Just ensure it doesn't panic and returns a non-zero output
allZero := true
for _, b := range output {
if b != 0 {
allZero = false
break
}
}
if allZero {
t.Error("Haraka256 of zero input should not be zero")
}
}
func TestHaraka512ZeroInput(t *testing.T) {
var input [64]byte
output := Haraka512(&input)
// Just ensure it doesn't panic and returns a non-zero output
allZero := true
for _, b := range output {
if b != 0 {
allZero = false
break
}
}
if allZero {
t.Error("Haraka512 of zero input should not be zero")
}
}
func TestMix2Correctness(t *testing.T) {
// Test that mix2 performs the expected permutation
var s0, s1 Block
// Set up known state
for i := 0; i < 16; i++ {
s0[i] = byte(i)
s1[i] = byte(i + 16)
}
mix2(&s0, &s1)
// After mix2:
// s0 should be: [a0, b0, a1, b1] = [0-3, 16-19, 4-7, 20-23]
// s1 should be: [a2, b2, a3, b3] = [8-11, 24-27, 12-15, 28-31]
expected0 := []byte{0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23}
expected1 := []byte{8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31}
if string(s0[:]) != string(expected0) {
t.Errorf("mix2 s0 mismatch\nExpected: %x\nGot: %x", expected0, s0[:])
}
if string(s1[:]) != string(expected1) {
t.Errorf("mix2 s1 mismatch\nExpected: %x\nGot: %x", expected1, s1[:])
}
}
func TestHaraka256Deterministic(t *testing.T) {
// Test that the same input always produces the same output
var input [32]byte
for i := range input {
input[i] = byte(i * 7)
}
output1 := Haraka256(&input)
output2 := Haraka256(&input)
if output1 != output2 {
t.Errorf("Haraka256 not deterministic: %x != %x", output1, output2)
}
}
func TestHaraka512Deterministic(t *testing.T) {
// Test that the same input always produces the same output
var input [64]byte
for i := range input {
input[i] = byte(i * 11)
}
output1 := Haraka512(&input)
output2 := Haraka512(&input)
if output1 != output2 {
t.Errorf("Haraka512 not deterministic: %x != %x", output1, output2)
}
}
func BenchmarkHaraka256(b *testing.B) {
var input [32]byte
for i := range input {
input[i] = byte(i)
}
b.SetBytes(32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Haraka256(&input)
}
}
func BenchmarkHaraka512(b *testing.B) {
var input [64]byte
for i := range input {
input[i] = byte(i)
}
b.SetBytes(64)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Haraka512(&input)
}
}
func TestHaraka256HW(t *testing.T) {
// Test vector from Appendix B of the Haraka v2 paper
var input [32]byte
for i := range input {
input[i] = byte(i)
}
expected, _ := hex.DecodeString("8027ccb87949774b78d0545fb72bf70c695c2a0923cbd47bba1159efbf2b2c1c")
output := Haraka256HW(&input)
if string(output[:]) != string(expected) {
t.Errorf("Haraka256HW mismatch\nInput: %x\nExpected: %x\nGot: %x", input, expected, output)
}
}
func TestHaraka512HW(t *testing.T) {
// Test vector from Appendix B of the Haraka v2 paper
var input [64]byte
for i := range input {
input[i] = byte(i)
}
expected, _ := hex.DecodeString("be7f723b4e80a99813b292287f306f625a6d57331cae5f34dd9277b0945be2aa")
output := Haraka512HW(&input)
if string(output[:]) != string(expected) {
t.Errorf("Haraka512HW mismatch\nInput: %x\nExpected: %x\nGot: %x", input, expected, output)
}
}
func TestHarakaHWMatchesSoftware(t *testing.T) {
// Test that HW and software implementations produce identical results
for i := 0; i < 100; i++ {
// Test Haraka-256
var input256 [32]byte
for j := range input256 {
input256[j] = byte(i*7 + j*13)
}
sw256 := Haraka256(&input256)
hw256 := Haraka256HW(&input256)
if sw256 != hw256 {
t.Errorf("Haraka256 HW/SW mismatch for input %d:\nSW: %x\nHW: %x", i, sw256, hw256)
}
// Test Haraka-512
var input512 [64]byte
for j := range input512 {
input512[j] = byte(i*11 + j*17)
}
sw512 := Haraka512(&input512)
hw512 := Haraka512HW(&input512)
if sw512 != hw512 {
t.Errorf("Haraka512 HW/SW mismatch for input %d:\nSW: %x\nHW: %x", i, sw512, hw512)
}
}
}
func BenchmarkHaraka256HW(b *testing.B) {
var input [32]byte
for i := range input {
input[i] = byte(i)
}
b.SetBytes(32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Haraka256HW(&input)
}
}
func BenchmarkHaraka512HW(b *testing.B) {
var input [64]byte
for i := range input {
input[i] = byte(i)
}
b.SetBytes(64)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Haraka512HW(&input)
}
}