Create branch go 1.16

This commit is contained in:
Emman 2022-03-31 08:19:58 +08:00
parent 6450e27784
commit f15ccb066b
9 changed files with 85 additions and 1474 deletions

39
.github/workflows/go1_16.ci.yml vendored Normal file
View File

@ -0,0 +1,39 @@
name: ci
on:
push:
branches: [ 'go_1.16' ]
pull_request:
branches: [ 'go_1.16' ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
goVer: ['1.16', '1.17']
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.goVer }}
- name: Setup Environment
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Module cache
uses: actions/cache@v2.1.7
env:
cache-name: go-mod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
- name: Test
run: go test -v ./...

View File

@ -8,7 +8,7 @@ jobs:
virt: vm virt: vm
os: linux os: linux
dist: focal dist: focal
go: 1.15.x go: 1.16.x
group: edge group: edge
install: install:

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/emmansun/gmsm module github.com/emmansun/gmsm
go 1.15 go 1.16
require ( require (
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064

13
sm2/export_generate.go Normal file
View File

@ -0,0 +1,13 @@
//go:build tablegen
// +build tablegen
package sm2
// This block exports p256-related internals for the p256 table generator in internal/gen.
var (
P256PointDoubleAsm = p256PointDoubleAsm
P256PointAddAsm = p256PointAddAsm
P256Inverse = p256Inverse
P256Sqr = p256Sqr
P256Mul = p256Mul
)

View File

@ -2,27 +2,19 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
//go:build amd64 //go:build ignore
// +build amd64 // +build ignore
package sm2 package main
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"fmt" "log"
"go/format" "os"
"github.com/emmansun/gmsm/sm2"
) )
func GenTables() { func main() {
buf := new(bytes.Buffer)
fmt.Fprint(buf, `
// Generated by gen_p256_table.go. DO NOT EDIT.
//go:build amd64
// +build amd64
package sm2
`[1:])
// Generate precomputed p256 tables. // Generate precomputed p256 tables.
var pre [43][32 * 8]uint64 var pre [43][32 * 8]uint64
@ -42,56 +34,43 @@ package sm2
// The window size is 6 so we need to double 6 times. // The window size is 6 so we need to double 6 times.
if i != 0 { if i != 0 {
for k := 0; k < 6; k++ { for k := 0; k < 6; k++ {
p256PointDoubleAsm(t1, t1) sm2.P256PointDoubleAsm(t1, t1)
} }
} }
// Convert the point to affine form. (Its values are // Convert the point to affine form. (Its values are
// still in Montgomery form however.) // still in Montgomery form however.)
p256Inverse(zInv, t1[8:12]) sm2.P256Inverse(zInv, t1[8:12])
p256Sqr(zInvSq, zInv, 1) sm2.P256Sqr(zInvSq, zInv, 1)
p256Mul(zInv, zInv, zInvSq) sm2.P256Mul(zInv, zInv, zInvSq)
p256Mul(t1[:4], t1[:4], zInvSq) sm2.P256Mul(t1[:4], t1[:4], zInvSq)
p256Mul(t1[4:8], t1[4:8], zInv) sm2.P256Mul(t1[4:8], t1[4:8], zInv)
copy(t1[8:12], basePoint[8:12]) copy(t1[8:12], basePoint[8:12])
// Update the table entry // Update the table entry
copy(pre[i][j*8:], t1[:8]) copy(pre[i][j*8:], t1[:8])
} }
if j == 0 { if j == 0 {
p256PointDoubleAsm(t2, basePoint) sm2.P256PointDoubleAsm(t2, basePoint)
} else { } else {
p256PointAddAsm(t2, t2, basePoint) sm2.P256PointAddAsm(t2, t2, basePoint)
} }
} }
fmt.Fprint(buf, "const p256Precomputed = \"\" +\n\n") var bin []byte
// Dump the precomputed tables, flattened, little-endian. // Dump the precomputed tables, flattened, little-endian.
// These tables are used directly by assembly on little-endian platforms. // These tables are used directly by assembly on little-endian platforms.
// Putting the data in a const string lets it be stored readonly. // go:embedding the data into a string lets it be stored readonly.
for i := range &pre { for i := range &pre {
for j, v := range &pre[i] { for _, v := range &pre[i] {
fmt.Fprintf(buf, "\"")
var u8 [8]byte var u8 [8]byte
binary.LittleEndian.PutUint64(u8[:], v) binary.LittleEndian.PutUint64(u8[:], v)
for _, b := range &u8 { bin = append(bin, u8[:]...)
fmt.Fprintf(buf, "\\x%02x", b)
}
fmt.Fprintf(buf, "\"")
if i < len(pre)-1 || j < len(pre[i])-1 {
fmt.Fprint(buf, "+")
}
if j%8 == 7 {
fmt.Fprint(buf, "\n")
}
} }
fmt.Fprint(buf, "\n")
} }
src := buf.Bytes() err := os.WriteFile("p256_asm_table.bin", bin, 0644)
fmtsrc, fmterr := format.Source(src) if err != nil {
// If formatting failed, keep the original source for debugging. log.Fatal(err)
if fmterr == nil {
src = fmtsrc
} }
fmt.Println(string(src))
} }

View File

@ -14,9 +14,15 @@ package sm2
import ( import (
"crypto/elliptic" "crypto/elliptic"
_ "embed"
"math/big" "math/big"
) )
//go:generate go run -tags=tablegen gen_p256_table.go
//go:embed p256_asm_table.bin
var p256Precomputed string
type ( type (
p256Curve struct { p256Curve struct {
*elliptic.CurveParams *elliptic.CurveParams

BIN
sm2/p256_asm_table.bin Normal file

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -26,7 +26,7 @@ func Test_p256ordk0(t *testing.T) {
n = n.ModInverse(n, p) n = n.ModInverse(n, p)
n = n.Neg(n) n = n.Neg(n)
n = n.Mod(n, p) n = n.Mod(n, p)
if "327f9e8872350975" != hex.EncodeToString(n.Bytes()) { if hex.EncodeToString(n.Bytes()) != "327f9e8872350975" {
t.Failed() t.Failed()
} }
} }