[SM2] optimize inverse

This commit is contained in:
Emman 2022-01-27 14:18:23 +08:00
parent 841c9c4fef
commit 81e9a06398
4 changed files with 14 additions and 19 deletions

View File

@ -2,8 +2,8 @@ language: go
jobs: jobs:
include: include:
# - arch: ppc64le - arch: ppc64le
# go: 1.17.x go: 1.17.x
- arch: arm64-graviton2 - arch: arm64-graviton2
virt: vm virt: vm
os: linux os: linux

View File

@ -1,7 +1,10 @@
# GM-Standards SM2/SM3/SM4 for Go # GM-Standards SM2/SM3/SM4 for Go
[![Build Status](https://app.travis-ci.com/emmansun/gmsm.svg?branch=main)](https://app.travis-ci.com/emmansun/gmsm) <a href="https://github.com/emmansun/gmsm/actions?query=workflow%3Aci+branch%3Amain"><img src="https://github.com/emmansun/gmsm/workflows/ci/badge.svg" alt="CI" style="max-width: 100%;"></a> [![Documentation](https://godoc.org/github.com/emmansun/gmsm?status.svg)](https://godoc.org/github.com/emmansun/gmsm) [![Release](https://img.shields.io/github/release/emmansun/gmsm/all.svg)](https://github.com/emmansun/gmsm/releases) [![Travis CI](https://app.travis-ci.com/emmansun/gmsm.svg?branch=main)](https://app.travis-ci.com/emmansun/gmsm)
[![Github CI](https://github.com/emmansun/gmsm/actions/workflows/ci.yml/badge.svg)](https://github.com/emmansun/gmsm/actions/workflows/ci.yml)
[![Documentation](https://godoc.org/github.com/emmansun/gmsm?status.svg)](https://godoc.org/github.com/emmansun/gmsm)
[![Release](https://img.shields.io/github/release/emmansun/gmsm/all.svg)](https://github.com/emmansun/gmsm/releases)
This is a **SM2 sm2p256v1** implementation whose performance is similar like golang native NIST P256 under **amd64** and **arm64**, for implementation detail, please refer [SM2实现细节](https://github.com/emmansun/gmsm/wiki/SM2%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96). This is a **SM2 sm2p256v1** implementation whose performance is similar like golang native NIST P256 under **amd64** and **arm64**, for implementation detail, please refer [SM2实现细节](https://github.com/emmansun/gmsm/wiki/SM2%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96).

View File

@ -896,20 +896,14 @@ func p256Invert(out, in *[p256Limbs]uint32) {
} // 2^128 - 2^64 } // 2^128 - 2^64
p256Mul(&ftmp, &ftmp, &e64) // 2^128 - 1 p256Mul(&ftmp, &ftmp, &e64) // 2^128 - 1
for i := 0; i < 32; i++ { for i := 0; i < 96; i++ {
p256Square(&ftmp, &ftmp) p256Square(&ftmp, &ftmp)
} // 2^160 - 2^32 } // 2^224 - 2^96
p256Mul(&ftmp, &ftmp, &e32m2) // 2^160 - 2
for i := 0; i < 95; i++ {
p256Square(&ftmp, &ftmp)
} // 2^255 - 2^96
p256Assign(&ftmp2, &e32m2) p256Assign(&ftmp2, &e32m2)
for i := 0; i < 223; i++ { for i := 0; i < 224; i++ {
p256Square(&ftmp2, &ftmp2) p256Square(&ftmp2, &ftmp2)
} // 2^255 - 2^224 } // 2^256 - 2^225
p256Mul(&ftmp, &ftmp, &ftmp2) // 2^256 - 2^224 - 2^96 p256Mul(&ftmp, &ftmp, &ftmp2) // 2^256 - 2^224 - 2^96

View File

@ -389,13 +389,11 @@ func p256Inverse(out, in []uint64) {
p256Sqr(out, p32, 32) //2^64 - 2^32 p256Sqr(out, p32, 32) //2^64 - 2^32
p256Mul(p64, out, p32) // 2^64 - 2^0 p256Mul(p64, out, p32) // 2^64 - 2^0
p256Sqr(out, p64, 64) //2^128 - 2^64 p256Sqr(out, p64, 64) //2^128 - 2^64
p256Mul(out, out, p64) // 2^128 - 2^0 p256Mul(out, out, p64) // 2^128 - 2^0
p256Sqr(out, out, 32) //2^160 - 2^32 p256Sqr(ptmp, out, 96) // 2^224 - 2^96
p256Mul(out, out, p32m2) //2^160 - 2^1
p256Sqr(ptmp, out, 95) //2^255 - 2^96
p256Sqr(out, p32m2, 223) //2^255 - 2^224 p256Sqr(out, p32m2, 224) //2^256 - 2^225
p256Mul(ptmp, ptmp, out) //2^256 - 2^224 - 2^96 p256Mul(ptmp, ptmp, out) //2^256 - 2^224 - 2^96
p256Sqr(out, p32, 16) // 2^48 - 2^16 p256Sqr(out, p32, 16) // 2^48 - 2^16