Skip to content

Commit 0ba6730

Browse files
Merge commit from fork
* fix: assert s < l eddsa * fix: assert r, s < l in ecdsa
1 parent fe973b9 commit 0ba6730

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

std/signature/ecdsa/ecdsa.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (pk PublicKey[T, S]) prepareVerification(api frontend.API, params sw_emulat
5959
if err != nil {
6060
panic(err)
6161
}
62+
63+
scalarApi.AssertIsLessOrEqual(&sig.S, scalarApi.Modulus())
64+
scalarApi.AssertIsLessOrEqual(&sig.R, scalarApi.Modulus())
65+
6266
pkpt := sw_emulated.AffinePoint[T](pk)
6367
msInv := scalarApi.Div(msg, &sig.S)
6468
rsInv := scalarApi.Div(&sig.R, &sig.S)

std/signature/eddsa/eddsa.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func Verify(curve twistededwards.Curve, sig Signature, msg frontend.Variable, pu
5353
Y: curve.Params().Base[1],
5454
}
5555

56+
// Assert S < GroupSize (see https://datatracker.ietf.org/doc/html/rfc8032#section-3.4)
57+
curve.API().AssertIsLessOrEqual(sig.S, curve.Params().Order)
58+
5659
//[S]G-[H(R,A,M)]*A
5760
_A := curve.Neg(pubKey.A)
5861
Q := curve.DoubleBaseScalarMul(base, _A, sig.S, hRAM)

std/signature/eddsa/eddsa_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,47 @@ func (circuit *eddsaCircuit) Define(api frontend.API) error {
4242
return Verify(curve, circuit.Signature, circuit.Message, circuit.PublicKey, &mimc)
4343
}
4444

45+
// Forge signature: S → S + order
46+
func forge(id tedwards.ID, sig []byte) ([]byte, error) {
47+
48+
forged := make([]byte, len(sig))
49+
copy(forged, sig)
50+
51+
var offset int
52+
switch id {
53+
case tedwards.BN254:
54+
offset = 32
55+
case tedwards.BLS12_381:
56+
offset = 32
57+
case tedwards.BLS12_377:
58+
offset = 32
59+
case tedwards.BW6_761:
60+
offset = 48
61+
case tedwards.BLS24_317:
62+
offset = 32
63+
case tedwards.BLS24_315:
64+
offset = 32
65+
case tedwards.BW6_633:
66+
offset = 40
67+
default:
68+
panic("not implemented")
69+
}
70+
71+
s := new(big.Int).SetBytes(sig[offset:])
72+
params, err := twistededwards.GetCurveParams(id)
73+
if err != nil {
74+
return nil, err
75+
}
76+
s.Add(s, params.Order)
77+
78+
sizeS := len(sig) - offset
79+
buf := make([]byte, sizeS)
80+
copy(buf[sizeS-len(s.Bytes()):], s.Bytes())
81+
82+
copy(forged[offset:], buf)
83+
return forged, nil
84+
}
85+
4586
func TestEddsa(t *testing.T) {
4687

4788
assert := test.NewAssert(t)
@@ -110,9 +151,17 @@ func TestEddsa(t *testing.T) {
110151
invalidWitness.PublicKey.Assign(conf.curve, pubKey.Bytes())
111152
invalidWitness.Signature.Assign(conf.curve, signature)
112153

154+
var invalidWitnessOverflow eddsaCircuit
155+
invalidWitnessOverflow.Message = msg
156+
invalidWitnessOverflow.PublicKey.Assign(conf.curve, pubKey.Bytes())
157+
forgedSig, err := forge(conf.curve, signature)
158+
assert.NoError(err, "forging signature")
159+
invalidWitnessOverflow.Signature.Assign(conf.curve, forgedSig)
160+
113161
assert.CheckCircuit(&circuit,
114162
test.WithValidAssignment(&validWitness),
115163
test.WithInvalidAssignment(&invalidWitness),
164+
test.WithInvalidAssignment(&invalidWitnessOverflow),
116165
test.WithCurves(snarkCurve))
117166

118167
}

0 commit comments

Comments
 (0)