refer to 7.1 in GB/T32918.2—2016
(pub *ecdsa.PublicKey, hash []byte, r, s *big.Int)
| 211 | |
| 212 | // refer to 7.1 in GB/T32918.2—2016 |
| 213 | func 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 | |
| 243 | func (a *Attestation) verifySm2SignatureWithId(qx, qy, r, s []byte, id []byte, msg []byte) error { |
| 244 | if len(qx) != 32 || len(qy) != 32 { |
no test coverage detected