Skip to content

Commit bb56c0c

Browse files
authored
Updates fuzz tests (#10)
1 parent d489140 commit bb56c0c

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test:
2929
test-race:
3030
go test -race ./...
3131

32-
FUZZTIME ?= 10s
32+
FUZZTIME ?= 2s
3333

3434
.PHONY: test-fuzz
3535
test-fuzz:

crypto/blinding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func DeriveBlindingVector(sharedSecrets []SharedKey, round uint32, nEls int32, f
2323
}
2424

2525
roundKeyBuf := make([]byte, 4+len(sharedSecrets[0]))
26-
binary.BigEndian.PutUint32(roundKeyBuf[:4], round)
26+
binary.BigEndian.PutUint32(roundKeyBuf, round)
2727

2828
workingEl := big.NewInt(0)
2929
words := make([]big.Word, bytesPerElement*8/bits.UintSize)

crypto/blinding_fuzz_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ func FuzzDeriveXorBlindingVector(f *testing.F) {
9393
t.Errorf("non-deterministic output")
9494
}
9595

96-
// Invariant 3: Different round produces different output
97-
if nBytes > 0 && round < ^uint32(0) {
96+
// Invariant 3: Different round produces different output (with high probability)
97+
// Skip this check for small nBytes since collisions are statistically expected
98+
// (e.g., 1/256 chance for nBytes=1)
99+
if nBytes >= 16 && round < ^uint32(0) {
98100
result3 := DeriveXorBlindingVector(sharedSecrets, round+1, nBytes)
99101
if bytes.Equal(result, result3) {
100102
t.Errorf("different rounds produced identical output")

crypto/fields.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func init() {
1515
// The result is stored in l and also returned.
1616
func FieldAddInplace(l *big.Int, r *big.Int, fieldOrder *big.Int) *big.Int {
1717
l.Add(l, r)
18-
if l.Cmp(fieldOrder) > 0 {
18+
if l.Cmp(fieldOrder) >= 0 {
1919
l.Sub(l, fieldOrder)
2020
}
2121

@@ -39,7 +39,7 @@ func FieldAddInplace(l *big.Int, r *big.Int, fieldOrder *big.Int) *big.Int {
3939
// The result is stored in l and also returned.
4040
func FieldSubInplace(l *big.Int, r *big.Int, fieldOrder *big.Int) *big.Int {
4141
l.Sub(l, r)
42-
if l.Cmp(fieldOrder) > 0 {
42+
if l.Cmp(fieldOrder) >= 0 {
4343
l.Sub(l, fieldOrder)
4444
}
4545
if l.Sign() < 0 {

crypto/types_fuzz_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package crypto
22

33
import (
44
"bytes"
5+
"strings"
56
"testing"
67
)
78

@@ -103,7 +104,8 @@ func FuzzXorInplace(f *testing.F) {
103104
}
104105

105106
// Invariant 3: Self-inverse property: XOR(XOR(a, b), b) = a
106-
XorInplace(a, b)
107+
// Use bCopy since a and b might share backing array (fuzzer can alias them)
108+
XorInplace(a, bCopy)
107109
if !bytes.Equal(a, aCopy) {
108110
t.Error("XOR is not self-inverse")
109111
}
@@ -197,8 +199,8 @@ func FuzzNewPublicKeyFromString(f *testing.F) {
197199
return
198200
}
199201

200-
// Invariant: String representation round-trips
201-
if pubKey.String() != input {
202+
// Invariant: String representation round-trips (case-insensitive, since hex is case-insensitive)
203+
if !strings.EqualFold(pubKey.String(), input) {
202204
t.Errorf("string round trip failed: got %s, want %s", pubKey.String(), input)
203205
}
204206

0 commit comments

Comments
 (0)