|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package csp |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + math "github.com/IBM/mathlib" |
| 13 | + bn254fr "github.com/consensys/gnark-crypto/ecc/bn254/fr" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// TestFieldArithmeticOptimization verifies that the polynomial expansion |
| 18 | +// (c-1)*(c-2) = c² - 3c + 2 holds in field arithmetic. |
| 19 | +func TestFieldArithmeticOptimization(t *testing.T) { |
| 20 | + curve := math.Curves[math.BN254] |
| 21 | + rand, err := curve.Rand() |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + // Generate a random field element c |
| 25 | + cZr := curve.NewRandomZr(rand) |
| 26 | + var c bn254fr.Element |
| 27 | + c.SetBigInt(cZr.BigInt()) |
| 28 | + |
| 29 | + // Compute (c-1) |
| 30 | + var cMinus1 bn254fr.Element |
| 31 | + var one bn254fr.Element |
| 32 | + one.SetOne() |
| 33 | + cMinus1.Sub(&c, &one) |
| 34 | + |
| 35 | + // Compute (c-2) |
| 36 | + var cMinus2 bn254fr.Element |
| 37 | + var two bn254fr.Element |
| 38 | + two.SetInt64(2) |
| 39 | + cMinus2.Sub(&c, &two) |
| 40 | + |
| 41 | + // Direct multiplication: (c-1)*(c-2) |
| 42 | + var directProduct bn254fr.Element |
| 43 | + directProduct.Mul(&cMinus1, &cMinus2) |
| 44 | + |
| 45 | + // Optimized computation: c² - 3c + 2 |
| 46 | + var cSquared bn254fr.Element |
| 47 | + cSquared.Mul(&c, &c) |
| 48 | + |
| 49 | + var three bn254fr.Element |
| 50 | + three.SetInt64(3) |
| 51 | + |
| 52 | + var threeC bn254fr.Element |
| 53 | + threeC.Mul(&three, &c) |
| 54 | + |
| 55 | + var optimized bn254fr.Element |
| 56 | + optimized.Sub(&cSquared, &threeC) |
| 57 | + optimized.Add(&optimized, &two) |
| 58 | + |
| 59 | + // Verify they are equal |
| 60 | + require.Equal(t, directProduct.Bytes(), optimized.Bytes(), |
| 61 | + "(c-1)*(c-2) should equal c² - 3c + 2 in field arithmetic") |
| 62 | + |
| 63 | + t.Logf("✓ Verified: (c-1)*(c-2) = c² - 3c + 2") |
| 64 | + t.Logf(" Direct product: %x", directProduct.Bytes()) |
| 65 | + t.Logf(" Optimized: %x", optimized.Bytes()) |
| 66 | +} |
| 67 | + |
| 68 | +// TestFieldArithmeticOptimizationPattern verifies the general pattern: |
| 69 | +// (c-i)*(c-j) = c² - c*(i+j) + i*j |
| 70 | +func TestFieldArithmeticOptimizationPattern(t *testing.T) { |
| 71 | + curve := math.Curves[math.BN254] |
| 72 | + rand, err := curve.Rand() |
| 73 | + require.NoError(t, err) |
| 74 | + |
| 75 | + // Generate a random field element c |
| 76 | + cZr := curve.NewRandomZr(rand) |
| 77 | + var c bn254fr.Element |
| 78 | + c.SetBigInt(cZr.BigInt()) |
| 79 | + |
| 80 | + // Test several (i, j) pairs |
| 81 | + testCases := []struct { |
| 82 | + i, j int64 |
| 83 | + }{ |
| 84 | + {0, 3}, // (c-0)*(c-3) = c² - 3c |
| 85 | + {1, 2}, // (c-1)*(c-2) = c² - 3c + 2 |
| 86 | + {0, 7}, // (c-0)*(c-7) = c² - 7c |
| 87 | + {2, 5}, // (c-2)*(c-5) = c² - 7c + 10 |
| 88 | + } |
| 89 | + |
| 90 | + for _, tc := range testCases { |
| 91 | + // Compute (c-i) |
| 92 | + var cMinusI bn254fr.Element |
| 93 | + var iElem bn254fr.Element |
| 94 | + iElem.SetInt64(tc.i) |
| 95 | + cMinusI.Sub(&c, &iElem) |
| 96 | + |
| 97 | + // Compute (c-j) |
| 98 | + var cMinusJ bn254fr.Element |
| 99 | + var jElem bn254fr.Element |
| 100 | + jElem.SetInt64(tc.j) |
| 101 | + cMinusJ.Sub(&c, &jElem) |
| 102 | + |
| 103 | + // Direct multiplication: (c-i)*(c-j) |
| 104 | + var directProduct bn254fr.Element |
| 105 | + directProduct.Mul(&cMinusI, &cMinusJ) |
| 106 | + |
| 107 | + // Optimized computation: c² - c*(i+j) + i*j |
| 108 | + var cSquared bn254fr.Element |
| 109 | + cSquared.Mul(&c, &c) |
| 110 | + |
| 111 | + var iPlusJ bn254fr.Element |
| 112 | + iPlusJ.SetInt64(tc.i + tc.j) |
| 113 | + |
| 114 | + var cTimesSumIJ bn254fr.Element |
| 115 | + cTimesSumIJ.Mul(&c, &iPlusJ) |
| 116 | + |
| 117 | + var iTimesJ bn254fr.Element |
| 118 | + iTimesJ.SetInt64(tc.i * tc.j) |
| 119 | + |
| 120 | + var optimized bn254fr.Element |
| 121 | + optimized.Sub(&cSquared, &cTimesSumIJ) |
| 122 | + optimized.Add(&optimized, &iTimesJ) |
| 123 | + |
| 124 | + // Verify they are equal |
| 125 | + require.Equal(t, directProduct.Bytes(), optimized.Bytes(), |
| 126 | + "(c-%d)*(c-%d) should equal c² - c*%d + %d in field arithmetic", |
| 127 | + tc.i, tc.j, tc.i+tc.j, tc.i*tc.j) |
| 128 | + |
| 129 | + t.Logf("✓ Verified: (c-%d)*(c-%d) = c² - c*%d + %d", tc.i, tc.j, tc.i+tc.j, tc.i*tc.j) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// Made with Bob |
0 commit comments