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
os: linux
dist: focal
go: 1.15.x
go: 1.16.x
group: edge
install:

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/emmansun/gmsm
go 1.15
go 1.16
require (
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
// license that can be found in the LICENSE file.
//go:build amd64
// +build amd64
//go:build ignore
// +build ignore
package sm2
package main
import (
"bytes"
"encoding/binary"
"fmt"
"go/format"
"log"
"os"
"github.com/emmansun/gmsm/sm2"
)
func GenTables() {
buf := new(bytes.Buffer)
fmt.Fprint(buf, `
// Generated by gen_p256_table.go. DO NOT EDIT.
//go:build amd64
// +build amd64
package sm2
`[1:])
func main() {
// Generate precomputed p256 tables.
var pre [43][32 * 8]uint64
@ -42,56 +34,43 @@ package sm2
// The window size is 6 so we need to double 6 times.
if i != 0 {
for k := 0; k < 6; k++ {
p256PointDoubleAsm(t1, t1)
sm2.P256PointDoubleAsm(t1, t1)
}
}
// Convert the point to affine form. (Its values are
// still in Montgomery form however.)
p256Inverse(zInv, t1[8:12])
p256Sqr(zInvSq, zInv, 1)
p256Mul(zInv, zInv, zInvSq)
p256Mul(t1[:4], t1[:4], zInvSq)
p256Mul(t1[4:8], t1[4:8], zInv)
sm2.P256Inverse(zInv, t1[8:12])
sm2.P256Sqr(zInvSq, zInv, 1)
sm2.P256Mul(zInv, zInv, zInvSq)
sm2.P256Mul(t1[:4], t1[:4], zInvSq)
sm2.P256Mul(t1[4:8], t1[4:8], zInv)
copy(t1[8:12], basePoint[8:12])
// Update the table entry
copy(pre[i][j*8:], t1[:8])
}
if j == 0 {
p256PointDoubleAsm(t2, basePoint)
sm2.P256PointDoubleAsm(t2, basePoint)
} 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.
// 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 j, v := range &pre[i] {
fmt.Fprintf(buf, "\"")
for _, v := range &pre[i] {
var u8 [8]byte
binary.LittleEndian.PutUint64(u8[:], v)
for _, b := range &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")
}
bin = append(bin, u8[:]...)
}
fmt.Fprint(buf, "\n")
}
src := buf.Bytes()
fmtsrc, fmterr := format.Source(src)
// If formatting failed, keep the original source for debugging.
if fmterr == nil {
src = fmtsrc
err := os.WriteFile("p256_asm_table.bin", bin, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(src))
}

View File

@ -14,9 +14,15 @@ package sm2
import (
"crypto/elliptic"
_ "embed"
"math/big"
)
//go:generate go run -tags=tablegen gen_p256_table.go
//go:embed p256_asm_table.bin
var p256Precomputed string
type (
p256Curve struct {
*elliptic.CurveParams
@ -425,7 +431,7 @@ func (p *p256Point) p256StorePoint(r *[16 * 4 * 3]uint64, index int) {
// to compute the least significant recoded digit, given that there's no bit
// b_-1, has to be b_4 b_3 b_2 b_1 b_0 0.
//
// Reference:
// Reference:
// https://github.com/openssl/openssl/blob/master/crypto/ec/ecp_nistputil.c
// https://github.com/google/boringssl/blob/master/crypto/fipsmodule/ec/util.c
func boothW5(in uint) (int, int) {

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.Neg(n)
n = n.Mod(n, p)
if "327f9e8872350975" != hex.EncodeToString(n.Bytes()) {
if hex.EncodeToString(n.Bytes()) != "327f9e8872350975" {
t.Failed()
}
}