@@ -60,7 +60,7 @@ func (c *ecrecoverCircuit) Define(api frontend.API) error {
6060 return nil
6161}
6262
63- func testRoutineECRecover (t * testing.T , wantStrict bool ) (circ , wit * ecrecoverCircuit , largeS bool ) {
63+ func testRoutineECRecover (t * testing.T , wantStrict bool , forceLargeS bool ) (circ , wit * ecrecoverCircuit , largeS bool ) {
6464 halfFr := new (big.Int ).Sub (fr .Modulus (), big .NewInt (1 ))
6565 halfFr .Div (halfFr , big .NewInt (2 ))
6666
@@ -77,6 +77,12 @@ func testRoutineECRecover(t *testing.T, wantStrict bool) (circ, wit *ecrecoverCi
7777 if err != nil {
7878 t .Fatal ("sign" , err )
7979 }
80+ // SignForRecover always returns s < r_mod/2. But in the tests we want
81+ // to check that the circuit fails when s > r_mod/2 in strict mode.
82+ if forceLargeS && s .Cmp (halfFr ) <= 0 {
83+ s .Sub (fr .Modulus (), s )
84+ }
85+
8086 if ! wantStrict || halfFr .Cmp (s ) > 0 {
8187 break
8288 }
@@ -103,14 +109,14 @@ func testRoutineECRecover(t *testing.T, wantStrict bool) (circ, wit *ecrecoverCi
103109
104110func TestECRecoverCircuitShortStrict (t * testing.T ) {
105111 assert := test .NewAssert (t )
106- circuit , witness , _ := testRoutineECRecover (t , true )
112+ circuit , witness , _ := testRoutineECRecover (t , true , false )
107113 err := test .IsSolved (circuit , witness , ecc .BN254 .ScalarField ())
108114 assert .NoError (err )
109115}
110116
111117func TestECRecoverCircuitShortLax (t * testing.T ) {
112118 assert := test .NewAssert (t )
113- circuit , witness , _ := testRoutineECRecover (t , false )
119+ circuit , witness , _ := testRoutineECRecover (t , false , false )
114120 err := test .IsSolved (circuit , witness , ecc .BN254 .ScalarField ())
115121 assert .NoError (err )
116122}
@@ -122,7 +128,7 @@ func TestECRecoverCircuitShortMismatch(t *testing.T) {
122128 var circuit , witness * ecrecoverCircuit
123129 var largeS bool
124130 for {
125- circuit , witness , largeS = testRoutineECRecover (t , false )
131+ circuit , witness , largeS = testRoutineECRecover (t , false , true )
126132 if largeS {
127133 witness .Strict = 1
128134 break
@@ -134,7 +140,7 @@ func TestECRecoverCircuitShortMismatch(t *testing.T) {
134140
135141func TestECRecoverCircuitFull (t * testing.T ) {
136142 assert := test .NewAssert (t )
137- circuit , witness , _ := testRoutineECRecover (t , false )
143+ circuit , witness , _ := testRoutineECRecover (t , false , false )
138144
139145 assert .CheckCircuit (
140146 circuit ,
@@ -256,7 +262,7 @@ func TestECRecoverInfinityWoFailure(t *testing.T) {
256262
257263func TestInvalidFailureTag (t * testing.T ) {
258264 assert := test .NewAssert (t )
259- circuit , witness , _ := testRoutineECRecover (t , false )
265+ circuit , witness , _ := testRoutineECRecover (t , false , false )
260266 witness .IsFailure = 1
261267 err := test .IsSolved (circuit , witness , ecc .BN254 .ScalarField ())
262268 assert .Error (err )
0 commit comments