|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +// This is licensed under the Cryptographic Open Software License 1.0 |
| 3 | +pragma solidity ^0.8.28; |
| 4 | + |
| 5 | +import {Test} from "forge-std/Test.sol"; |
| 6 | +import "../../src/base/Constants.sol"; |
| 7 | +import {Errors} from "../../src/base/Errors.sol"; |
| 8 | +import {VerificationBuilder} from "../../src/builder/VerificationBuilder.pre.sol"; |
| 9 | +import {SignExpr} from "../../src/proof_gadgets/SignExpr.pre.sol"; |
| 10 | +import {F} from "../base/FieldUtil.sol"; |
| 11 | + |
| 12 | +contract SignExprTest is Test { |
| 13 | + function testSimpleAndExpr() public pure { |
| 14 | + VerificationBuilder.Builder memory builder; |
| 15 | + uint256[] memory bitDistribution = new uint256[](2); |
| 16 | + bitDistribution[0] = 0x800000000000000000000000000000000000000000000000000000000000007D; |
| 17 | + bitDistribution[1] = 0x8000000000000000000000000000000000000000000000000000000000000002; |
| 18 | + VerificationBuilder.__setBitDistributions(builder, bitDistribution); |
| 19 | + builder.maxDegree = 3; |
| 20 | + builder.constraintMultipliers = new uint256[](7); |
| 21 | + builder.constraintMultipliers[0] = 5; |
| 22 | + builder.constraintMultipliers[1] = 5; |
| 23 | + builder.constraintMultipliers[2] = 5; |
| 24 | + builder.constraintMultipliers[3] = 5; |
| 25 | + builder.constraintMultipliers[4] = 5; |
| 26 | + builder.constraintMultipliers[5] = 5; |
| 27 | + builder.constraintMultipliers[6] = 5; |
| 28 | + builder.aggregateEvaluation = 0; |
| 29 | + builder.rowMultipliersEvaluation = addmod(MODULUS, mulmod(MODULUS_MINUS_ONE, 2, MODULUS), MODULUS); |
| 30 | + |
| 31 | + int64[4] memory evaluationVector = [int64(700), -6, 3007, 134562844]; |
| 32 | + |
| 33 | + int64[4][10] memory vectorsToEvaluate = [ |
| 34 | + [int64(106), 23, -60, -76], |
| 35 | + [int64(1), 1, 1, 1], |
| 36 | + [int64(0), 1, 0, 0], |
| 37 | + [int64(0), 1, 1, 1], |
| 38 | + [int64(1), 0, 0, 0], |
| 39 | + [int64(0), 1, 0, 1], |
| 40 | + [int64(1), 0, 0, 1], |
| 41 | + [int64(1), 0, 1, 0], |
| 42 | + [int64(1), 1, 0, 0], |
| 43 | + [int64(1), 1, 0, 0] |
| 44 | + ]; |
| 45 | + |
| 46 | + uint256[] memory evaluations = new uint256[](10); |
| 47 | + |
| 48 | + for (uint8 i = 0; i < 10; ++i) { |
| 49 | + int64 evaluation = 0; |
| 50 | + for (uint8 j = 0; j < 4; ++j) { |
| 51 | + evaluation += evaluationVector[j] * vectorsToEvaluate[i][j]; |
| 52 | + } |
| 53 | + evaluations[i] = F.from(evaluation).into(); |
| 54 | + } |
| 55 | + |
| 56 | + uint256[] memory finalRoundMles = new uint256[](7); |
| 57 | + for (uint8 i = 2; i < 9; ++i) { |
| 58 | + finalRoundMles[i - 2] = evaluations[i]; |
| 59 | + } |
| 60 | + |
| 61 | + VerificationBuilder.__setFinalRoundMLEs(builder, finalRoundMles); |
| 62 | + |
| 63 | + uint256 signEval; |
| 64 | + (builder, signEval) = SignExpr.__signExprEvaluate(evaluations[0], builder, evaluations[1]); |
| 65 | + assert(signEval == evaluations[9]); |
| 66 | + } |
| 67 | + |
| 68 | + /// forge-config: default.allow_internal_expect_revert = true |
| 69 | + function testIncorrectBitDecomposition() public { |
| 70 | + VerificationBuilder.Builder memory builder; |
| 71 | + uint256[] memory bitDistribution = new uint256[](2); |
| 72 | + bitDistribution[0] = 0x800000000000000000000000000000000000000000000000000000000000007D; |
| 73 | + bitDistribution[1] = 0x8000000000000000000000000000000000000000000000000000000000000002; |
| 74 | + VerificationBuilder.__setBitDistributions(builder, bitDistribution); |
| 75 | + builder.maxDegree = 3; |
| 76 | + builder.constraintMultipliers = new uint256[](7); |
| 77 | + builder.constraintMultipliers[0] = 5; |
| 78 | + builder.constraintMultipliers[1] = 5; |
| 79 | + builder.constraintMultipliers[2] = 5; |
| 80 | + builder.constraintMultipliers[3] = 5; |
| 81 | + builder.constraintMultipliers[4] = 5; |
| 82 | + builder.constraintMultipliers[5] = 5; |
| 83 | + builder.constraintMultipliers[6] = 5; |
| 84 | + builder.aggregateEvaluation = 0; |
| 85 | + builder.rowMultipliersEvaluation = addmod(MODULUS, mulmod(MODULUS_MINUS_ONE, 2, MODULUS), MODULUS); |
| 86 | + |
| 87 | + int64[4] memory evaluationVector = [int64(700), -6, 3007, 134562844]; |
| 88 | + |
| 89 | + int64[4][10] memory vectorsToEvaluate = [ |
| 90 | + [int64(106), 23, -60, -76], |
| 91 | + [int64(1), 1, 1, 1], |
| 92 | + [int64(0), 2, 0, 0], |
| 93 | + [int64(0), 1, 1, 1], |
| 94 | + [int64(1), 0, 0, 0], |
| 95 | + [int64(0), 1, 0, 1], |
| 96 | + [int64(1), 0, 0, 1], |
| 97 | + [int64(1), 0, 1, 0], |
| 98 | + [int64(1), 1, 0, 0], |
| 99 | + [int64(1), 1, 0, 0] |
| 100 | + ]; |
| 101 | + |
| 102 | + uint256[] memory evaluations = new uint256[](10); |
| 103 | + |
| 104 | + for (uint8 i = 0; i < 10; ++i) { |
| 105 | + int64 evaluation = 0; |
| 106 | + for (uint8 j = 0; j < 4; ++j) { |
| 107 | + evaluation += evaluationVector[j] * vectorsToEvaluate[i][j]; |
| 108 | + } |
| 109 | + evaluations[i] = F.from(evaluation).into(); |
| 110 | + } |
| 111 | + |
| 112 | + uint256[] memory finalRoundMles = new uint256[](7); |
| 113 | + for (uint8 i = 2; i < 9; ++i) { |
| 114 | + finalRoundMles[i - 2] = evaluations[i]; |
| 115 | + } |
| 116 | + |
| 117 | + VerificationBuilder.__setFinalRoundMLEs(builder, finalRoundMles); |
| 118 | + |
| 119 | + vm.expectRevert(Errors.BitDecompositionInvalid.selector); |
| 120 | + SignExpr.__signExprEvaluate(evaluations[0], builder, evaluations[1]); |
| 121 | + } |
| 122 | + |
| 123 | + /// forge-config: default.allow_internal_expect_revert = true |
| 124 | + function testBitDecompositionWithInvalidBits() public { |
| 125 | + VerificationBuilder.Builder memory builder; |
| 126 | + uint256[] memory bitDistribution = new uint256[](2); |
| 127 | + bitDistribution[0] = 0xFF00000000000000000000000000000000000000000000000000000000000000; |
| 128 | + bitDistribution[1] = 0x8000000000000000000000000000000000000000000000000000000000000002; |
| 129 | + VerificationBuilder.__setBitDistributions(builder, bitDistribution); |
| 130 | + builder.maxDegree = 3; |
| 131 | + builder.constraintMultipliers = new uint256[](7); |
| 132 | + builder.constraintMultipliers[0] = 5; |
| 133 | + builder.constraintMultipliers[1] = 5; |
| 134 | + builder.constraintMultipliers[2] = 5; |
| 135 | + builder.constraintMultipliers[3] = 5; |
| 136 | + builder.constraintMultipliers[4] = 5; |
| 137 | + builder.constraintMultipliers[5] = 5; |
| 138 | + builder.constraintMultipliers[6] = 5; |
| 139 | + builder.aggregateEvaluation = 0; |
| 140 | + builder.rowMultipliersEvaluation = addmod(MODULUS, mulmod(MODULUS_MINUS_ONE, 2, MODULUS), MODULUS); |
| 141 | + |
| 142 | + int64[4] memory evaluationVector = [int64(700), -6, 3007, 134562844]; |
| 143 | + |
| 144 | + int64[4][10] memory vectorsToEvaluate = [ |
| 145 | + [int64(106), 23, -60, -76], |
| 146 | + [int64(1), 1, 1, 1], |
| 147 | + [int64(0), 2, 0, 0], |
| 148 | + [int64(0), 1, 1, 1], |
| 149 | + [int64(1), 0, 0, 0], |
| 150 | + [int64(0), 1, 0, 1], |
| 151 | + [int64(1), 0, 0, 1], |
| 152 | + [int64(1), 0, 1, 0], |
| 153 | + [int64(1), 1, 0, 0], |
| 154 | + [int64(1), 1, 0, 0] |
| 155 | + ]; |
| 156 | + |
| 157 | + uint256[] memory evaluations = new uint256[](10); |
| 158 | + |
| 159 | + for (uint8 i = 0; i < 10; ++i) { |
| 160 | + int64 evaluation = 0; |
| 161 | + for (uint8 j = 0; j < 4; ++j) { |
| 162 | + evaluation += evaluationVector[j] * vectorsToEvaluate[i][j]; |
| 163 | + } |
| 164 | + evaluations[i] = F.from(evaluation).into(); |
| 165 | + } |
| 166 | + |
| 167 | + uint256[] memory finalRoundMles = new uint256[](7); |
| 168 | + for (uint8 i = 2; i < 9; ++i) { |
| 169 | + finalRoundMles[i - 2] = evaluations[i]; |
| 170 | + } |
| 171 | + |
| 172 | + VerificationBuilder.__setFinalRoundMLEs(builder, finalRoundMles); |
| 173 | + |
| 174 | + vm.expectRevert(Errors.InvalidVaryingBits.selector); |
| 175 | + SignExpr.__signExprEvaluate(evaluations[0], builder, evaluations[1]); |
| 176 | + } |
| 177 | +} |
0 commit comments