internal/sm2ec: enable ppc64le step 1

This commit is contained in:
Sun Yimin 2024-08-27 15:14:15 +08:00 committed by GitHub
parent e1e485c32d
commit 187a4f7b80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 1497 additions and 135 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@
// Code generated by generate.go. DO NOT EDIT.
//go:build purego || !(amd64 || arm64 || s390x)
//go:build purego || !(amd64 || arm64 || s390x || ppc64le)
package sm2ec

View File

@ -7,7 +7,7 @@
// 256-bit primes"
// https://link.springer.com/article/10.1007%2Fs13389-014-0090-x
// https://eprint.iacr.org/2013/816.pdf
//go:build (amd64 || arm64 || s390x) && !purego
//go:build (amd64 || arm64 || s390x || ppc64le) && !purego
package sm2ec

View File

@ -1,44 +0,0 @@
//go:build !purego
package sm2ec
// p256Element is a P-256 base field element in [0, P-1] in the Montgomery
// domain (with R 2²⁵⁶) as four limbs in little-endian order value.
type p256Element [4]uint64
// p256OrdElement is a P-256 scalar field element in [0, ord(G)-1] in the
// Montgomery domain (with R 2²⁵⁶) as four uint64 limbs in little-endian order.
type p256OrdElement [4]uint64
// Montgomery multiplication by R⁻¹, or 1 outside the domain.
// Sets res = in * R⁻¹, bringing res out of the Montgomery domain.
//
//go:noescape
func p256FromMont(res, in *p256Element)
// If cond is not 0, sets val = -val mod p.
//
//go:noescape
func p256NegCond(val *p256Element, cond int)
// If cond is 0, sets res = b, otherwise sets res = a.
//
//go:noescape
func p256MovCond(res, a, b *SM2P256Point, cond int)
//go:noescape
func p256BigToLittle(res *p256Element, in *[32]byte)
//go:noescape
func p256LittleToBig(res *[32]byte, in *p256Element)
//go:noescape
func p256OrdBigToLittle(res *p256OrdElement, in *[32]byte)
//go:noescape
func p256OrdLittleToBig(res *[32]byte, in *p256OrdElement)
// p256OrdReduce ensures s is in the range [0, ord(G)-1].
//
//go:noescape
func p256OrdReduce(s *p256OrdElement)

View File

@ -1,88 +0,0 @@
//go:build ppc64le && !purego
package sm2ec
import (
"math/big"
"testing"
)
var bigOne = big.NewInt(1)
// fromBig converts a *big.Int into a format used by this code.
func fromBig(out *[4]uint64, big *big.Int) {
for i := range out {
out[i] = 0
}
for i, v := range big.Bits() {
out[i] = uint64(v)
}
}
func montFromBig(out *[4]uint64, n *big.Int) {
p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16)
r := new(big.Int).Lsh(bigOne, 256)
// out = big * R mod P
outBig := new(big.Int).Mul(n, r)
outBig.Mod(outBig, p)
fromBig(out, outBig)
}
func toBigInt(in *p256Element) *big.Int {
var valBytes [32]byte
p256LittleToBig(&valBytes, in)
return new(big.Int).SetBytes(valBytes[:])
}
func ordElmToBigInt(in *p256OrdElement) *big.Int {
var valBytes [32]byte
p256OrdLittleToBig(&valBytes, in)
return new(big.Int).SetBytes(valBytes[:])
}
func testP256FromMont(v *big.Int, t *testing.T) {
val := new(p256Element)
montFromBig((*[4]uint64)(val), v)
res := new(p256Element)
p256FromMont(res, val)
if toBigInt(res).Cmp(v) != 0 {
t.Errorf("p256FromMont failed for %x", v.Bytes())
}
}
func TestP256FromMont(t *testing.T) {
p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16)
for i := 0; i < 20; i++ {
bigVal := big.NewInt(int64(i))
testP256FromMont(bigVal, t)
if i != 0 {
bigVal = new(big.Int).Sub(p, big.NewInt(int64(i)))
testP256FromMont(bigVal, t)
}
}
}
func testP256OrderReduce(v, expected *big.Int, t *testing.T) {
val := new(p256OrdElement)
fromBig((*[4]uint64)(val), v)
p256OrdReduce(val)
if ordElmToBigInt(val).Cmp(expected) != 0 {
t.Errorf("p256OrdReduce failed for %x, expected %x", v.Bytes(), expected.Bytes())
}
}
func TestP256OrderReduce(t *testing.T) {
p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16)
for i := 0; i < 20; i++ {
bigVal := big.NewInt(int64(i))
testP256OrderReduce(bigVal, bigVal, t)
bigVal = new(big.Int).Add(p, big.NewInt(int64(i)))
testP256OrderReduce(bigVal, big.NewInt(int64(i)), t)
}
testP256OrderReduce(p, big.NewInt(0), t)
for i := 1; i < 20; i++ {
bigVal := new(big.Int).Sub(p, big.NewInt(int64(i)))
testP256OrderReduce(bigVal, bigVal, t)
}
}

View File

@ -1,4 +1,4 @@
//go:build (amd64 || arm64 || s390x) && !purego
//go:build (amd64 || arm64 || s390x || ppc64le) && !purego
package sm2ec