Skip to content

Commit 309cb9d

Browse files
authored
Add fuzz tests (#19)
1 parent 5442047 commit 309cb9d

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
go-version: ['1.22.x', '1.23.x']
15+
go-version: ['1.22.x', '1.23.x', '1.24.x']
1616
steps:
1717
- uses: actions/checkout@v4
1818
with:
@@ -29,3 +29,7 @@ jobs:
2929
3030
- name: Run tests
3131
run: go test ./...
32+
33+
- name: Run fuzzy field tests
34+
run: |
35+
go test -tags=fuzz -fuzz=Fuzz -fuzztime=30s github.com/elliottech/poseidon_crypto/field

field/field_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,103 @@ func TestAddFDoubleWraparound(t *testing.T) {
302302
}
303303
}
304304

305+
func FuzzTestF(f *testing.F) {
306+
f.Add(uint64(0), uint64(0))
307+
f.Add(uint64(1), uint64(1))
308+
f.Add(uint64(math.MaxUint64), uint64(math.MaxUint64))
309+
f.Add(uint64(math.MaxUint32), uint64(math.MaxUint64))
310+
f.Add(uint64(g.ORDER-1), uint64(g.ORDER-1))
311+
f.Add(uint64(g.ORDER), uint64(g.ORDER))
312+
313+
f.Fuzz(func(t *testing.T, lhs, rhs uint64) {
314+
// AddF
315+
{
316+
addF := g.AddF(g.GoldilocksField(lhs), g.GoldilocksField(rhs)).ToCanonicalUint64()
317+
expected := SumMod(lhs, rhs)
318+
if addF != expected {
319+
t.Fatalf("AddF: Expected %d + %d = %d, but got %d", lhs, rhs, expected, addF)
320+
}
321+
}
322+
323+
// SubF
324+
{
325+
subF := g.SubF(g.GoldilocksField(lhs), g.GoldilocksField(rhs)).ToCanonicalUint64()
326+
expected := SubMod(lhs, rhs)
327+
if subF != expected {
328+
t.Fatalf("SubF: Expected %d - %d = %d, but got %d", lhs, rhs, expected, subF)
329+
}
330+
}
331+
332+
// MulF
333+
{
334+
mulF := g.MulF(g.GoldilocksField(lhs), g.GoldilocksField(rhs)).ToCanonicalUint64()
335+
expected := MulMod(lhs, rhs)
336+
if mulF != expected {
337+
t.Fatalf("MulF: Expected %d * %d = %d, but got %d", lhs, rhs, expected, mulF)
338+
}
339+
}
340+
341+
// NegF
342+
{
343+
negF := g.NegF(g.GoldilocksField(lhs)).ToCanonicalUint64()
344+
expected := NegMod(lhs)
345+
if negF != expected {
346+
t.Fatalf("NegF: Expected Neg(%d) = %d, but got %d", lhs, expected, negF)
347+
}
348+
}
349+
350+
// SquareF
351+
{
352+
sqrF := g.SquareF(g.GoldilocksField(lhs)).ToCanonicalUint64()
353+
expected := SquareMod(lhs)
354+
if sqrF != expected {
355+
t.Fatalf("SquareF: Expected (%d)^2 = %d, but got %d", lhs, expected, sqrF)
356+
}
357+
}
358+
359+
// AddCanonicalUint64
360+
{
361+
lhs := lhs & (g.ORDER - 1) // make sure lhs is in the field
362+
addCanonical := g.AddCanonicalUint64(g.GoldilocksField(lhs), rhs).ToCanonicalUint64()
363+
expected := SumMod(lhs, rhs)
364+
if addCanonical != expected {
365+
t.Fatalf("AddCanonicalUint64: Expected %d + %d = %d, but got %d", lhs, rhs, expected, addCanonical)
366+
}
367+
368+
}
369+
370+
// Reduce128Bit
371+
{
372+
val := g.UInt128{Hi: lhs, Lo: rhs}
373+
reduced := g.Reduce128Bit(val)
374+
bigVal := new(big.Int).SetUint64(val.Hi)
375+
bigVal.Lsh(bigVal, 64)
376+
bigVal.Add(bigVal, new(big.Int).SetUint64(val.Lo))
377+
bigOrder := new(big.Int).SetUint64(g.ORDER)
378+
bigVal.Mod(bigVal, bigOrder)
379+
expected := bigVal.Uint64()
380+
if reduced.ToCanonicalUint64() != expected {
381+
t.Fatalf("Reduce128Bit: Expected reduction of %v to be %d, but got %d", val, expected, reduced.ToCanonicalUint64())
382+
}
383+
}
384+
385+
// Reduce96Bit
386+
{
387+
val := g.UInt128{Hi: uint64(uint32(lhs)), Lo: rhs}
388+
reduced := g.Reduce96Bit(val)
389+
bigVal := new(big.Int).SetUint64(val.Hi)
390+
bigVal.Lsh(bigVal, 64)
391+
bigVal.Add(bigVal, new(big.Int).SetUint64(val.Lo))
392+
bigOrder := new(big.Int).SetUint64(g.ORDER)
393+
bigVal.Mod(bigVal, bigOrder)
394+
expected := bigVal.Uint64()
395+
if reduced.ToCanonicalUint64() != expected {
396+
t.Fatalf("Reduce96Bit: Expected reduction of %v to be %d, but got %d", val, expected, reduced.ToCanonicalUint64())
397+
}
398+
}
399+
})
400+
}
401+
305402
// Quintic extension tests
306403

307404
func TestQuinticExtensionAddSubMulSquare(t *testing.T) {

0 commit comments

Comments
 (0)