mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-22 10:16:18 +08:00
check arm64 gfp
This commit is contained in:
parent
554621915d
commit
4f2585ddbc
@ -81,29 +81,26 @@ func Test_gfpBasicOperations(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGfpSqrt(t *testing.T) {
|
||||
tests := []string{
|
||||
"9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596",
|
||||
"92fe90b700fbd4d8cc177d300ed16e4e15471a681b2c9e3728c1b82c885e49c2",
|
||||
func TestGfpExp(t *testing.T) {
|
||||
xI := bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")
|
||||
x := fromBigInt(xI)
|
||||
ret := &gfP{}
|
||||
ret.exp(x, pMinus2)
|
||||
|
||||
ret1 := &gfP{}
|
||||
ret1.exp2(x, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457b"))
|
||||
if ret1.String() == ret.String() {
|
||||
t.Errorf("exp not same")
|
||||
}
|
||||
for i, test := range tests {
|
||||
y2 := bigFromHex(test)
|
||||
y21 := new(big.Int).ModSqrt(y2, p)
|
||||
|
||||
y3 := new(big.Int).Mul(y21, y21)
|
||||
y3.Mod(y3, p)
|
||||
if y2.Cmp(y3) != 0 {
|
||||
t.Error("Invalid sqrt")
|
||||
}
|
||||
ret2 := new(big.Int).Exp(xI, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457b"), p)
|
||||
if hex.EncodeToString(ret2.Bytes()) == ret.String() {
|
||||
t.Errorf("exp not same")
|
||||
}
|
||||
|
||||
tmp := fromBigInt(y2)
|
||||
tmp.Sqrt(tmp)
|
||||
montDecode(tmp, tmp)
|
||||
var res [32]byte
|
||||
tmp.Marshal(res[:])
|
||||
if hex.EncodeToString(res[:]) != hex.EncodeToString(y21.Bytes()) {
|
||||
t.Errorf("case %v, got %v, expected %v\n", i, hex.EncodeToString(res[:]), hex.EncodeToString(y21.Bytes()))
|
||||
}
|
||||
xInv := new(big.Int).ModInverse(xI, p)
|
||||
if hex.EncodeToString(ret2.Bytes()) != hex.EncodeToString(xInv.Bytes()) {
|
||||
t.Errorf("exp not same, got %v, expected %v\n", hex.EncodeToString(ret2.Bytes()), hex.EncodeToString(xInv.Bytes()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,124 +125,29 @@ func TestGfpDiv(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_gfp12Gen(t *testing.T) {
|
||||
ret := pairing(twistGen, curveGen)
|
||||
if ret.x != gfP12Gen.x || ret.y != gfP12Gen.y || ret.z != gfP12Gen.z {
|
||||
t.Errorf("not expected")
|
||||
func TestGfpSqrt(t *testing.T) {
|
||||
tests := []string{
|
||||
"9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596",
|
||||
"92fe90b700fbd4d8cc177d300ed16e4e15471a681b2c9e3728c1b82c885e49c2",
|
||||
}
|
||||
}
|
||||
for i, test := range tests {
|
||||
y2 := bigFromHex(test)
|
||||
y21 := new(big.Int).ModSqrt(y2, p)
|
||||
|
||||
func Test_gfP2Square(t *testing.T) {
|
||||
x := &gfP2{
|
||||
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
||||
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
||||
}
|
||||
y3 := new(big.Int).Mul(y21, y21)
|
||||
y3.Mod(y3, p)
|
||||
if y2.Cmp(y3) != 0 {
|
||||
t.Error("Invalid sqrt")
|
||||
}
|
||||
|
||||
xmulx := &gfP2{}
|
||||
xmulx.Mul(x, x)
|
||||
xmulx = gfP2Decode(xmulx)
|
||||
|
||||
x2 := &gfP2{}
|
||||
x2.Square(x)
|
||||
x2 = gfP2Decode(x2)
|
||||
|
||||
if xmulx.x != x2.x || xmulx.y != x2.y {
|
||||
t.Errorf("xmulx=%v, x2=%v", xmulx, x2)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_gfP2Invert(t *testing.T) {
|
||||
x := &gfP2{
|
||||
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
||||
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
||||
}
|
||||
|
||||
xInv := &gfP2{}
|
||||
xInv.Invert(x)
|
||||
|
||||
y := &gfP2{}
|
||||
y.Mul(x, xInv)
|
||||
expected := (&gfP2{}).SetOne()
|
||||
|
||||
if y.x != expected.x || y.y != expected.y {
|
||||
t.Errorf("got %v, expected %v", y, expected)
|
||||
}
|
||||
|
||||
x = &gfP2{
|
||||
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
||||
*zero,
|
||||
}
|
||||
|
||||
xInv.Invert(x)
|
||||
|
||||
y.Mul(x, xInv)
|
||||
|
||||
if y.x != expected.x || y.y != expected.y {
|
||||
t.Errorf("got %v, expected %v", y, expected)
|
||||
}
|
||||
|
||||
x = &gfP2{
|
||||
*zero,
|
||||
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
||||
}
|
||||
|
||||
xInv.Invert(x)
|
||||
|
||||
y.Mul(x, xInv)
|
||||
|
||||
if y.x != expected.x || y.y != expected.y {
|
||||
t.Errorf("got %v, expected %v", y, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_gfP2Exp(t *testing.T) {
|
||||
x := &gfP2{
|
||||
*fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")),
|
||||
*fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")),
|
||||
}
|
||||
got := &gfP2{}
|
||||
got.Exp(x, big.NewInt(1))
|
||||
if x.x != got.x || x.y != got.y {
|
||||
t.Errorf("got %v, expected %v", got, x)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_gfP2Frobenius(t *testing.T) {
|
||||
x := &gfP2{
|
||||
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
||||
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
||||
}
|
||||
expected := &gfP2{}
|
||||
expected.Exp(x, p)
|
||||
got := &gfP2{}
|
||||
got.Frobenius(x)
|
||||
if expected.x != got.x || expected.y != got.y {
|
||||
t.Errorf("got %v, expected %v", got, x)
|
||||
}
|
||||
|
||||
// make sure i^(p-1) = -1
|
||||
i := &gfP2{}
|
||||
i.SetU()
|
||||
i.Exp(i, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457c"))
|
||||
i = gfP2Decode(i)
|
||||
expected.y.Set(newGFp(-1))
|
||||
expected.x.Set(zero)
|
||||
expected = gfP2Decode(expected)
|
||||
if expected.x != i.x || expected.y != i.y {
|
||||
t.Errorf("got %v, expected %v", i, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_gfP2Div2(t *testing.T) {
|
||||
x := &gfP2{
|
||||
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
||||
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
||||
}
|
||||
ret := &gfP2{}
|
||||
ret.Div2(x)
|
||||
ret.Add(ret, ret)
|
||||
if *ret != *x {
|
||||
t.Errorf("got %v, expected %v", ret, x)
|
||||
tmp := fromBigInt(y2)
|
||||
tmp.Sqrt(tmp)
|
||||
montDecode(tmp, tmp)
|
||||
var res [32]byte
|
||||
tmp.Marshal(res[:])
|
||||
if hex.EncodeToString(res[:]) != hex.EncodeToString(y21.Bytes()) {
|
||||
t.Errorf("case %v, got %v, expected %v\n", i, hex.EncodeToString(res[:]), hex.EncodeToString(y21.Bytes()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
18
sm9/gfp.go
18
sm9/gfp.go
@ -103,6 +103,24 @@ func (e *gfP) exp(f *gfP, bits [4]uint64) {
|
||||
e.Set(sum)
|
||||
}
|
||||
|
||||
func (e *gfP) exp2(f *gfP, power *big.Int) *gfP {
|
||||
sum := &gfP{}
|
||||
sum.Set(one)
|
||||
t := &gfP{}
|
||||
|
||||
for i := power.BitLen() - 1; i >= 0; i-- {
|
||||
gfpMul(t, sum, sum)
|
||||
if power.Bit(i) != 0 {
|
||||
gfpMul(sum, f, f)
|
||||
} else {
|
||||
sum.Set(t)
|
||||
}
|
||||
}
|
||||
|
||||
e.Set(sum)
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *gfP) Invert(f *gfP) {
|
||||
e.exp(f, pMinus2)
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package sm9
|
||||
|
||||
/*
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
@ -119,4 +118,3 @@ func Test_gfP2Div2(t *testing.T) {
|
||||
t.Errorf("got %v, expected %v", ret, x)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user