mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 04:06: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) {
|
func TestGfpExp(t *testing.T) {
|
||||||
tests := []string{
|
xI := bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")
|
||||||
"9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596",
|
x := fromBigInt(xI)
|
||||||
"92fe90b700fbd4d8cc177d300ed16e4e15471a681b2c9e3728c1b82c885e49c2",
|
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)
|
ret2 := new(big.Int).Exp(xI, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457b"), p)
|
||||||
y3.Mod(y3, p)
|
if hex.EncodeToString(ret2.Bytes()) == ret.String() {
|
||||||
if y2.Cmp(y3) != 0 {
|
t.Errorf("exp not same")
|
||||||
t.Error("Invalid sqrt")
|
}
|
||||||
}
|
|
||||||
|
|
||||||
tmp := fromBigInt(y2)
|
xInv := new(big.Int).ModInverse(xI, p)
|
||||||
tmp.Sqrt(tmp)
|
if hex.EncodeToString(ret2.Bytes()) != hex.EncodeToString(xInv.Bytes()) {
|
||||||
montDecode(tmp, tmp)
|
t.Errorf("exp not same, got %v, expected %v\n", hex.EncodeToString(ret2.Bytes()), hex.EncodeToString(xInv.Bytes()))
|
||||||
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()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,124 +125,29 @@ func TestGfpDiv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_gfp12Gen(t *testing.T) {
|
func TestGfpSqrt(t *testing.T) {
|
||||||
ret := pairing(twistGen, curveGen)
|
tests := []string{
|
||||||
if ret.x != gfP12Gen.x || ret.y != gfP12Gen.y || ret.z != gfP12Gen.z {
|
"9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596",
|
||||||
t.Errorf("not expected")
|
"92fe90b700fbd4d8cc177d300ed16e4e15471a681b2c9e3728c1b82c885e49c2",
|
||||||
}
|
}
|
||||||
}
|
for i, test := range tests {
|
||||||
|
y2 := bigFromHex(test)
|
||||||
|
y21 := new(big.Int).ModSqrt(y2, p)
|
||||||
|
|
||||||
func Test_gfP2Square(t *testing.T) {
|
y3 := new(big.Int).Mul(y21, y21)
|
||||||
x := &gfP2{
|
y3.Mod(y3, p)
|
||||||
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
if y2.Cmp(y3) != 0 {
|
||||||
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
t.Error("Invalid sqrt")
|
||||||
}
|
}
|
||||||
|
|
||||||
xmulx := &gfP2{}
|
tmp := fromBigInt(y2)
|
||||||
xmulx.Mul(x, x)
|
tmp.Sqrt(tmp)
|
||||||
xmulx = gfP2Decode(xmulx)
|
montDecode(tmp, tmp)
|
||||||
|
var res [32]byte
|
||||||
x2 := &gfP2{}
|
tmp.Marshal(res[:])
|
||||||
x2.Square(x)
|
if hex.EncodeToString(res[:]) != hex.EncodeToString(y21.Bytes()) {
|
||||||
x2 = gfP2Decode(x2)
|
t.Errorf("case %v, got %v, expected %v\n", i, hex.EncodeToString(res[:]), hex.EncodeToString(y21.Bytes()))
|
||||||
|
}
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
sm9/gfp.go
18
sm9/gfp.go
@ -103,6 +103,24 @@ func (e *gfP) exp(f *gfP, bits [4]uint64) {
|
|||||||
e.Set(sum)
|
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) {
|
func (e *gfP) Invert(f *gfP) {
|
||||||
e.exp(f, pMinus2)
|
e.exp(f, pMinus2)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package sm9
|
package sm9
|
||||||
|
|
||||||
/*
|
|
||||||
import (
|
import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
@ -119,4 +118,3 @@ func Test_gfP2Div2(t *testing.T) {
|
|||||||
t.Errorf("got %v, expected %v", ret, x)
|
t.Errorf("got %v, expected %v", ret, x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user