|
| 1 | +package crypto |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func FuzzDeriveBlindingVector(f *testing.F) { |
| 9 | + // Add seed corpus with various parameters |
| 10 | + f.Add([]byte("shared-secret-1"), uint32(1), int32(10)) |
| 11 | + f.Add([]byte("shared-secret-2"), uint32(0), int32(1)) |
| 12 | + f.Add([]byte("shared-secret-3"), uint32(100), int32(50)) |
| 13 | + |
| 14 | + f.Fuzz(func(t *testing.T, secret []byte, round uint32, nEls int32) { |
| 15 | + // Skip invalid inputs |
| 16 | + if len(secret) == 0 || nEls <= 0 || nEls > 1000 { |
| 17 | + t.Skip() |
| 18 | + } |
| 19 | + |
| 20 | + sharedSecrets := []SharedKey{secret} |
| 21 | + |
| 22 | + result := DeriveBlindingVector(sharedSecrets, round, nEls, AuctionFieldOrder) |
| 23 | + |
| 24 | + // Invariant 1: Output length matches nEls |
| 25 | + if len(result) != int(nEls) { |
| 26 | + t.Errorf("output length mismatch: got %d, want %d", len(result), nEls) |
| 27 | + } |
| 28 | + |
| 29 | + // Invariant 2: All elements are in valid field range |
| 30 | + for i, el := range result { |
| 31 | + if el == nil { |
| 32 | + t.Errorf("element %d is nil", i) |
| 33 | + continue |
| 34 | + } |
| 35 | + if el.Sign() < 0 { |
| 36 | + t.Errorf("element %d is negative: %v", i, el) |
| 37 | + } |
| 38 | + if el.Cmp(AuctionFieldOrder) >= 0 { |
| 39 | + t.Errorf("element %d >= fieldOrder: %v", i, el) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Invariant 3: Determinism - same inputs produce same outputs |
| 44 | + result2 := DeriveBlindingVector(sharedSecrets, round, nEls, AuctionFieldOrder) |
| 45 | + for i := range result { |
| 46 | + if result[i].Cmp(result2[i]) != 0 { |
| 47 | + t.Errorf("non-deterministic: element %d differs on second call", i) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Invariant 4: Different round produces different output (with high probability) |
| 52 | + if round < ^uint32(0) { |
| 53 | + result3 := DeriveBlindingVector(sharedSecrets, round+1, nEls, AuctionFieldOrder) |
| 54 | + allSame := true |
| 55 | + for i := range result { |
| 56 | + if result[i].Cmp(result3[i]) != 0 { |
| 57 | + allSame = false |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + if allSame && nEls > 0 { |
| 62 | + t.Errorf("different rounds produced identical output") |
| 63 | + } |
| 64 | + } |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func FuzzDeriveXorBlindingVector(f *testing.F) { |
| 69 | + // Add seed corpus |
| 70 | + f.Add([]byte("shared-secret-1"), uint32(1), 100) |
| 71 | + f.Add([]byte("shared-secret-2"), uint32(0), 1) |
| 72 | + f.Add([]byte("shared-secret-3"), uint32(100), 16) // AES block size |
| 73 | + f.Add([]byte("shared-secret-4"), uint32(50), 17) // Non-aligned |
| 74 | + |
| 75 | + f.Fuzz(func(t *testing.T, secret []byte, round uint32, nBytes int) { |
| 76 | + // Skip invalid inputs |
| 77 | + if len(secret) == 0 || nBytes < 0 || nBytes > 10000 { |
| 78 | + t.Skip() |
| 79 | + } |
| 80 | + |
| 81 | + sharedSecrets := []SharedKey{secret} |
| 82 | + |
| 83 | + result := DeriveXorBlindingVector(sharedSecrets, round, nBytes) |
| 84 | + |
| 85 | + // Invariant 1: Output length matches nBytes |
| 86 | + if len(result) != nBytes { |
| 87 | + t.Errorf("output length mismatch: got %d, want %d", len(result), nBytes) |
| 88 | + } |
| 89 | + |
| 90 | + // Invariant 2: Determinism |
| 91 | + result2 := DeriveXorBlindingVector(sharedSecrets, round, nBytes) |
| 92 | + if !bytes.Equal(result, result2) { |
| 93 | + t.Errorf("non-deterministic output") |
| 94 | + } |
| 95 | + |
| 96 | + // Invariant 3: Different round produces different output |
| 97 | + if nBytes > 0 && round < ^uint32(0) { |
| 98 | + result3 := DeriveXorBlindingVector(sharedSecrets, round+1, nBytes) |
| 99 | + if bytes.Equal(result, result3) { |
| 100 | + t.Errorf("different rounds produced identical output") |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Invariant 4: Zero bytes returns empty slice |
| 105 | + if nBytes == 0 && len(result) != 0 { |
| 106 | + t.Errorf("zero nBytes should return empty slice") |
| 107 | + } |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +func FuzzXorBlindingRoundTrip(f *testing.F) { |
| 112 | + f.Add([]byte("secret1"), []byte("secret2"), uint32(5), 100) |
| 113 | + |
| 114 | + f.Fuzz(func(t *testing.T, secret1, secret2 []byte, round uint32, nBytes int) { |
| 115 | + if len(secret1) == 0 || len(secret2) == 0 || nBytes <= 0 || nBytes > 1000 { |
| 116 | + t.Skip() |
| 117 | + } |
| 118 | + |
| 119 | + secrets := []SharedKey{secret1, secret2} |
| 120 | + |
| 121 | + // XOR blinding should be self-inverse when applied twice |
| 122 | + blinding := DeriveXorBlindingVector(secrets, round, nBytes) |
| 123 | + data := make([]byte, nBytes) |
| 124 | + for i := range data { |
| 125 | + data[i] = byte(i % 256) |
| 126 | + } |
| 127 | + original := make([]byte, nBytes) |
| 128 | + copy(original, data) |
| 129 | + |
| 130 | + // Apply blinding |
| 131 | + XorInplace(data, blinding) |
| 132 | + |
| 133 | + // Apply again (should restore original) |
| 134 | + XorInplace(data, blinding) |
| 135 | + |
| 136 | + if !bytes.Equal(data, original) { |
| 137 | + t.Errorf("XOR round trip failed") |
| 138 | + } |
| 139 | + }) |
| 140 | +} |
0 commit comments