MCPcopy
hub / github.com/tjfoc/gmsm / sm2P256PointAdd

Function sm2P256PointAdd

sm2/p256.go:443–498  ·  view source on GitHub ↗

(x3, y3, z3) = (x1, y1, z1) + (x2, y2, z2)

(x1, y1, z1, x2, y2, z2, x3, y3, z3 *sm2P256FieldElement)

Source from the content-addressed store, hash-verified

441
442// (x3, y3, z3) = (x1, y1, z1) + (x2, y2, z2)
443func sm2P256PointAdd(x1, y1, z1, x2, y2, z2, x3, y3, z3 *sm2P256FieldElement) {
444 var u1, u2, z22, z12, z23, z13, s1, s2, h, h2, r, r2, tm sm2P256FieldElement
445
446 if sm2P256ToBig(z1).Sign() == 0 {
447 sm2P256Dup(x3, x2)
448 sm2P256Dup(y3, y2)
449 sm2P256Dup(z3, z2)
450 return
451 }
452
453 if sm2P256ToBig(z2).Sign() == 0 {
454 sm2P256Dup(x3, x1)
455 sm2P256Dup(y3, y1)
456 sm2P256Dup(z3, z1)
457 return
458 }
459
460 sm2P256Square(&z12, z1) // z12 = z1 ^ 2
461 sm2P256Square(&z22, z2) // z22 = z2 ^ 2
462
463 sm2P256Mul(&z13, &z12, z1) // z13 = z1 ^ 3
464 sm2P256Mul(&z23, &z22, z2) // z23 = z2 ^ 3
465
466 sm2P256Mul(&u1, x1, &z22) // u1 = x1 * z2 ^ 2
467 sm2P256Mul(&u2, x2, &z12) // u2 = x2 * z1 ^ 2
468
469 sm2P256Mul(&s1, y1, &z23) // s1 = y1 * z2 ^ 3
470 sm2P256Mul(&s2, y2, &z13) // s2 = y2 * z1 ^ 3
471
472 if sm2P256ToBig(&u1).Cmp(sm2P256ToBig(&u2)) == 0 &&
473 sm2P256ToBig(&s1).Cmp(sm2P256ToBig(&s2)) == 0 {
474 sm2P256PointDouble(x1, y1, z1, x1, y1, z1)
475 }
476
477 sm2P256Sub(&h, &u2, &u1) // h = u2 - u1
478 sm2P256Sub(&r, &s2, &s1) // r = s2 - s1
479
480 sm2P256Square(&r2, &r) // r2 = r ^ 2
481 sm2P256Square(&h2, &h) // h2 = h ^ 2
482
483 sm2P256Mul(&tm, &h2, &h) // tm = h ^ 3
484 sm2P256Sub(x3, &r2, &tm)
485 sm2P256Mul(&tm, &u1, &h2)
486 sm2P256Scalar(&tm, 2) // tm = 2 * (u1 * h ^ 2)
487 sm2P256Sub(x3, x3, &tm) // x3 = r ^ 2 - h ^ 3 - 2 * u1 * h ^ 2
488
489 sm2P256Mul(&tm, &u1, &h2) // tm = u1 * h ^ 2
490 sm2P256Sub(&tm, &tm, x3) // tm = u1 * h ^ 2 - x3
491 sm2P256Mul(y3, &r, &tm)
492 sm2P256Mul(&tm, &h2, &h) // tm = h ^ 3
493 sm2P256Mul(&tm, &tm, &s1) // tm = s1 * h ^ 3
494 sm2P256Sub(y3, y3, &tm) // y3 = r * (u1 * h ^ 2 - x3) - s1 * h ^ 3
495
496 sm2P256Mul(z3, z1, z2)
497 sm2P256Mul(z3, z3, &h) // z3 = z1 * z3 * h
498}
499
500// (x3, y3, z3) = (x1, y1, z1)- (x2, y2, z2)

Callers 2

AddMethod · 0.85
sm2P256ScalarMultFunction · 0.85

Calls 8

sm2P256ToBigFunction · 0.85
sm2P256DupFunction · 0.85
sm2P256SquareFunction · 0.85
sm2P256MulFunction · 0.85
sm2P256PointDoubleFunction · 0.85
sm2P256SubFunction · 0.85
sm2P256ScalarFunction · 0.85
SignMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…