MCPcopy
hub / github.com/OpenNHP/opennhp / VerifySignature

Function VerifySignature

nhp/core/verifier/csv/csv.go:213–241  ·  view source on GitHub ↗

refer to 7.1 in GB/T32918.2—2016

(pub *ecdsa.PublicKey, hash []byte, r, s *big.Int)

Source from the content-addressed store, hash-verified

211
212// refer to 7.1 in GB/T32918.2—2016
213func VerifySignature(pub *ecdsa.PublicKey, hash []byte, r, s *big.Int) bool {
214 n := pub.Curve.Params().N
215
216 if r.Sign() <= 0 || s.Sign() <= 0 || r.Cmp(n) >= 0 || s.Cmp(n) >= 0 {
217 return false
218 }
219
220 sm2_N := pub.Params().N
221
222 // t = (r + s) % n
223 t := new(big.Int).Mod(new(big.Int).Add(r, s), sm2_N)
224
225 e := new(big.Int).SetBytes(hash)
226
227 // x1, y1 = [r]G
228 // x2, y2 = [s]PubKey
229 x1, y1 := pub.Curve.ScalarBaseMult(s.Bytes())
230 x2, y2 := pub.Curve.ScalarMult(pub.X, pub.Y, t.Bytes())
231
232 if x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0 {
233 // x1, y1 = Double(x1, y1)
234 x1, y1 = pub.Curve.Double(x1, y1)
235 } else {
236 // x1, y1 = x1 + x2, y1 + y2
237 x1, y1 = pub.Curve.Add(x1, y1, x2, y2)
238 }
239
240 return r.Cmp(new(big.Int).Mod(new(big.Int).Add(x1, e), sm2_N)) == 0
241}
242
243func (a *Attestation) verifySm2SignatureWithId(qx, qy, r, s []byte, id []byte, msg []byte) error {
244 if len(qx) != 32 || len(qy) != 32 {

Callers 1

Calls 6

SignMethod · 0.80
ParamsMethod · 0.65
AddMethod · 0.65
ScalarBaseMultMethod · 0.65
BytesMethod · 0.65
ScalarMultMethod · 0.65

Tested by

no test coverage detected