@@ -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
307404func TestQuinticExtensionAddSubMulSquare (t * testing.T ) {
0 commit comments