mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 12:16:20 +08:00
sm9/bn256: add test cases
This commit is contained in:
parent
ba6bd136f9
commit
a10e64f6f5
@ -16,22 +16,93 @@ var testdataP4 = gfP4{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_gfP12Square(t *testing.T) {
|
func TestGfp12BasicOperations(t *testing.T) {
|
||||||
x := &gfP12{
|
x := &gfP12{
|
||||||
testdataP4,
|
testdataP4,
|
||||||
testdataP4,
|
testdataP4,
|
||||||
*(&gfP4{}).SetOne(),
|
*(&gfP4{}).SetOne(),
|
||||||
}
|
}
|
||||||
xmulx := &gfP12{}
|
y := &gfP12{
|
||||||
xmulx.Mul(x, x)
|
testdataP4,
|
||||||
xmulx = gfP12Decode(xmulx)
|
testdataP4,
|
||||||
|
*(&gfP4{}).SetZero(),
|
||||||
|
}
|
||||||
|
|
||||||
x2 := &gfP12{}
|
t.Run("Add", func(t *testing.T) {
|
||||||
x2.Square(x)
|
expectedAdd := "(((6a6225e56e1acd7c5ae45b0f1b63733de799936987c8f38dd16bcddc6b500bcf, 0db9e03175ebe2b21be74db56d03e143dbd835729d7291fa6694b22536746fa1), (4d8891878c3113b4665011d7b24f278d4c1b54a22ec093840a00c030b5c239f9, 21092bc181ec5d9c488aac3c5feef9b725f7568b42b4794f807e271f22e38494)), ((6a6225e56e1acd7c5ae45b0f1b63733de799936987c8f38dd16bcddc6b500bcf, 0db9e03175ebe2b21be74db56d03e143dbd835729d7291fa6694b22536746fa1), (4d8891878c3113b4665011d7b24f278d4c1b54a22ec093840a00c030b5c239f9, 21092bc181ec5d9c488aac3c5feef9b725f7568b42b4794f807e271f22e38494)), ((0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000), (0000000000000000000000000000000000000000000000000000000000000000, 49bffffffd5c590e29fc54b00a7138bade0d6cb4e58511241a9064d81caeba83)))"
|
||||||
x2 = gfP12Decode(x2)
|
got := &gfP12{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Add(got, y)
|
||||||
|
|
||||||
if xmulx.x != x2.x || xmulx.y != x2.y || xmulx.z != x2.z {
|
if got.String() != expectedAdd {
|
||||||
t.Errorf("xmulx=%v, x2=%v", xmulx, x2)
|
t.Errorf("got %v, expected %v", got, expectedAdd)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Sub", func(t *testing.T) {
|
||||||
|
expectedSub := "(((0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000), (0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000)), ((0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000), (0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000)), ((0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000), (0000000000000000000000000000000000000000000000000000000000000000, 49bffffffd5c590e29fc54b00a7138bade0d6cb4e58511241a9064d81caeba83)))"
|
||||||
|
got := &gfP12{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Sub(got, y)
|
||||||
|
|
||||||
|
if got.String() != expectedSub {
|
||||||
|
t.Errorf("got %v, expected %v", got, expectedSub)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Mul", func(t *testing.T) {
|
||||||
|
expectedMul := "(((2302538ca37ab5cf8c253b56ece9734f92e31f026e5bea5f178828769a8e2322, 96fe2ddc0dda2779d93b9d8560eebd91bb61e659c81a9936dac9a2bcc3f8ab86), (2c371ba768b6f660eaba367b2c444295e529efc2a5ad95d8f80265235ad4c6c9, 4f24d79c798eb4d8c2005bf43cb955f5420baf40650c750f4f0f1c0a11882a1c)), ((3aa3e5d659abd344e5045b16e0ce686e32e90f265231c5dddbb7ebf9359bafb4, a09d31d474e04adc96b08258be0b2d5e2df568599fa8f8d2b4d22f210cf94261), (00a5868716591909286c54468e0599715190e2a67646ab1fc7100e9aa04e4b35, 34e214ace81b90ab66df3a7f7188097a54cf00aa4c6b5b77629a907ec7a587d5)), ((0ae5a5c7453cd90d6f245b1ea6395d9e7e388ae31c9a982de6040a15ffe75399, 7d40837771310c153b760bac1983b2335e2007f5876470cf1da010f5002ccfa4), (6a027b86a324c54fc08c42055f4ad29a78f903f5d847b197698ef82c6e2ba1ee, 48bafd984e4ac3ba8533c8c28321193d83a6aac956223d9f44b6f9de6c678b16)))"
|
||||||
|
got := &gfP12{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Mul(got, y)
|
||||||
|
|
||||||
|
if got.String() != expectedMul {
|
||||||
|
t.Errorf("got %v, expected %v", got, expectedMul)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Square", func(t *testing.T) {
|
||||||
|
got := &gfP12{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Square(got)
|
||||||
|
|
||||||
|
expected := (&gfP12{}).Mul(x, x)
|
||||||
|
|
||||||
|
if *expected != *got {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGfp12Order(t *testing.T) {
|
||||||
|
in := &gfP12{
|
||||||
|
testdataP4,
|
||||||
|
testdataP4,
|
||||||
|
*(&gfP4{}).SetOne(),
|
||||||
|
}
|
||||||
|
|
||||||
|
p6 := (&gfP12{}).FrobeniusP6(in)
|
||||||
|
p12 := (&gfP12{}).FrobeniusP6(p6)
|
||||||
|
if *p12 != *in {
|
||||||
|
t.Errorf("in^(p^12) not equal with in")
|
||||||
|
}
|
||||||
|
|
||||||
|
p2 := (&gfP12{}).FrobeniusP2(in)
|
||||||
|
p4 := (&gfP12{}).FrobeniusP2(p2)
|
||||||
|
p6_1 := (&gfP12{}).FrobeniusP2(p4)
|
||||||
|
p8 := (&gfP12{}).FrobeniusP2(p6_1)
|
||||||
|
p10 := (&gfP12{}).FrobeniusP2(p8)
|
||||||
|
p12_1 := (&gfP12{}).FrobeniusP2(p10)
|
||||||
|
if *p12_1 != *in {
|
||||||
|
t.Errorf("in^(p^12) not equal with in")
|
||||||
|
}
|
||||||
|
|
||||||
|
p3 := (&gfP12{}).FrobeniusP3(in)
|
||||||
|
p6_2 := (&gfP12{}).FrobeniusP3(p3)
|
||||||
|
p9 := (&gfP12{}).FrobeniusP3(p6_2)
|
||||||
|
p12_2 := (&gfP12{}).FrobeniusP3(p9)
|
||||||
|
if *p12_2 != *in {
|
||||||
|
t.Errorf("in^(p^12) not equal with in")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,23 +5,120 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_gfP2Square(t *testing.T) {
|
func TestGfp2BasicOperations(t *testing.T) {
|
||||||
x := &gfP2{
|
x := &gfP2{
|
||||||
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
||||||
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
||||||
}
|
}
|
||||||
|
y := &gfP2{
|
||||||
xmulx := &gfP2{}
|
*fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")),
|
||||||
xmulx.Mul(x, x)
|
*fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")),
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
expectedAdd := "(5bf55bb67d25f098609a367366d94d6599da7405db44c388edb64706908922e4, 728185f97d3df3a01d3ad2a0e140d12011e10fa47d50fd12e6413a361e549cd9)"
|
||||||
|
expectedSub := "(0e6cca2ef0f4dce3fa4a249bb48a25d84dbf1f63ac843004e3b586d5dac6e8eb, 51785a37fb519603d4b026648151d768ebe9b9193a9c83c365c31316fb711845)"
|
||||||
|
expectedMul := "(7f98a04cf83164be0fdc4763a7c6f24c2901191f2917eb71037cd5221cf002bb, 75a09ee1aa1b04ccdb24e629529a18492f378aa3034f63d3cd1b8b9f0d338b3a)"
|
||||||
|
expectedMulU := "(75a09ee1aa1b04ccdb24e629529a18492f378aa3034f63d3cd1b8b9f0d338b3a, 6d4ebf6614e484678c4ec7d89b8fa9f1f1e2f457e2c606d5c3e58c0b8cc28584)"
|
||||||
|
t.Run("Add", func(t *testing.T) {
|
||||||
|
ret := &gfP2{}
|
||||||
|
ret.Add(x, y)
|
||||||
|
if ret.String() != expectedAdd {
|
||||||
|
t.Errorf("expected %v, got %v", expectedAdd, ret)
|
||||||
|
}
|
||||||
|
ret.Set(x)
|
||||||
|
ret.Add(ret, y)
|
||||||
|
if ret.String() != expectedAdd {
|
||||||
|
t.Errorf("add self fail, expected %v, got %v", expectedAdd, ret)
|
||||||
|
}
|
||||||
|
ret.Set(y)
|
||||||
|
ret.Add(x, ret)
|
||||||
|
if ret.String() != expectedAdd {
|
||||||
|
t.Errorf("add self fail, expected %v, got %v", expectedAdd, ret)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Sub", func(t *testing.T) {
|
||||||
|
ret := &gfP2{}
|
||||||
|
ret.Sub(x, y)
|
||||||
|
if ret.String() != expectedSub {
|
||||||
|
t.Errorf("expected %v, got %v", expectedSub, ret)
|
||||||
|
}
|
||||||
|
ret.Set(x)
|
||||||
|
ret.Sub(ret, y)
|
||||||
|
if ret.String() != expectedSub {
|
||||||
|
t.Errorf("sub self fail, expected %v, got %v", expectedSub, ret)
|
||||||
|
}
|
||||||
|
ret.Set(y)
|
||||||
|
ret.Sub(x, ret)
|
||||||
|
if ret.String() != expectedSub {
|
||||||
|
t.Errorf("sub self fail, expected %v, got %v", expectedSub, ret)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Double", func(t *testing.T) {
|
||||||
|
expected := &gfP2{}
|
||||||
|
expected.Set(x)
|
||||||
|
expected.Add(expected, expected)
|
||||||
|
|
||||||
|
got := &gfP2{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Double(got)
|
||||||
|
if got.Equal(expected) != 1 {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Triple", func(t *testing.T) {
|
||||||
|
expected := &gfP2{}
|
||||||
|
expected.Set(x)
|
||||||
|
expected.Add(expected, expected)
|
||||||
|
expected.Add(expected, x)
|
||||||
|
got := &gfP2{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Triple(got)
|
||||||
|
if got.Equal(expected) != 1 {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Mul", func(t *testing.T) {
|
||||||
|
got := &gfP2{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Mul(got, y)
|
||||||
|
if got.String() != expectedMul {
|
||||||
|
t.Errorf("got %v, expected %v", got, expectedMul)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("MulU", func(t *testing.T) {
|
||||||
|
got := &gfP2{}
|
||||||
|
got.Set(x)
|
||||||
|
got.MulU(got, y)
|
||||||
|
if got.String() != expectedMulU {
|
||||||
|
t.Errorf("got %v, expected %v", got, expectedMulU)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Square", func(t *testing.T) {
|
||||||
|
expected := &gfP2{}
|
||||||
|
expected.Mul(x, x)
|
||||||
|
got := &gfP2{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Square(got)
|
||||||
|
if got.Equal(expected) != 1 {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("SquareU", func(t *testing.T) {
|
||||||
|
expected := &gfP2{}
|
||||||
|
expected.MulU(x, x)
|
||||||
|
got := &gfP2{}
|
||||||
|
got.Set(x)
|
||||||
|
got.SquareU(got)
|
||||||
|
if got.Equal(expected) != 1 {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_gfP2Invert(t *testing.T) {
|
func Test_gfP2Invert(t *testing.T) {
|
||||||
|
@ -5,6 +5,112 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestGfp4BasicOperations(t *testing.T) {
|
||||||
|
x := &gfP4{
|
||||||
|
gfP2{
|
||||||
|
*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
|
||||||
|
*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
|
||||||
|
},
|
||||||
|
gfP2{
|
||||||
|
*fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")),
|
||||||
|
*fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
y := &gfP4{}
|
||||||
|
y.x.Set(&x.y)
|
||||||
|
y.y.Set(&x.x)
|
||||||
|
|
||||||
|
expectedAdd := "((5bf55bb67d25f098609a367366d94d6599da7405db44c388edb64706908922e4, 728185f97d3df3a01d3ad2a0e140d12011e10fa47d50fd12e6413a361e549cd9), (5bf55bb67d25f098609a367366d94d6599da7405db44c388edb64706908922e4, 728185f97d3df3a01d3ad2a0e140d12011e10fa47d50fd12e6413a361e549cd9))"
|
||||||
|
expectedSub := "((0e6cca2ef0f4dce3fa4a249bb48a25d84dbf1f63ac843004e3b586d5dac6e8eb, 51785a37fb519603d4b026648151d768ebe9b9193a9c83c365c31316fb711845), (a7d335d111aeca0ddbb986b44104a16cd43373e76df6bed701ba1452088a5c92, 64c7a5c8075210ee015384eb743cefdc3608da31dfde6b187fac8810e7e02d38))"
|
||||||
|
expectedMul := "((5f318c234b817377df2179ff82a0759c6b926330853e5abd919e45a6a93e658e, 3c9db0f3bbdb89a9a407dfec4f8f4d6b8ef35b2a3f05e7bcc9bb6a956876faf7), (3ef93f2e9fa8c29914fd823d04d243503646107711ec6068eb28c59946d24878, 2caf5e47bc5be242917002b1f89afaf5ff27ebafcb9a7bcdab917c82b6a4cb41))"
|
||||||
|
expectedMulV := "((3ef93f2e9fa8c29914fd823d04d243503646107711ec6068eb28c59946d24878, 2caf5e47bc5be242917002b1f89afaf5ff27ebafcb9a7bcdab917c82b6a4cb41), (3c9db0f3bbdb89a9a407dfec4f8f4d6b8ef35b2a3f05e7bcc9bb6a956876faf7, ae1ce7b96e4466f3edc462a0e5dca3516cc060352a79283ca7a2ab027425bfde))"
|
||||||
|
|
||||||
|
t.Run("Add", func(t *testing.T) {
|
||||||
|
got := &gfP4{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Add(got, y)
|
||||||
|
if got.String() != expectedAdd {
|
||||||
|
t.Errorf("got %v, expected %v", got, expectedAdd)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Sub", func(t *testing.T) {
|
||||||
|
got := &gfP4{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Sub(got, y)
|
||||||
|
if got.String() != expectedSub {
|
||||||
|
t.Errorf("got %v, expected %v", got, expectedSub)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Mul", func(t *testing.T) {
|
||||||
|
got := &gfP4{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Mul(got, y)
|
||||||
|
if got.String() != expectedMul {
|
||||||
|
t.Errorf("got %v, expected %v", got, expectedMul)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("MulV", func(t *testing.T) {
|
||||||
|
got := &gfP4{}
|
||||||
|
got.Set(x)
|
||||||
|
got.MulV(got, y)
|
||||||
|
if got.String() != expectedMulV {
|
||||||
|
t.Errorf("got %v, expected %v", got, expectedMulV)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Double", func(t *testing.T) {
|
||||||
|
got := &gfP4{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Double(got)
|
||||||
|
|
||||||
|
expected := &gfP4{}
|
||||||
|
expected.Add(x, x)
|
||||||
|
if got.x.Equal(&expected.x) != 1 || got.y.Equal(&expected.y) != 1 {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Triple", func(t *testing.T) {
|
||||||
|
got := &gfP4{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Triple(got)
|
||||||
|
|
||||||
|
expected := &gfP4{}
|
||||||
|
expected.Add(x, x)
|
||||||
|
expected.Add(expected, x)
|
||||||
|
if got.x.Equal(&expected.x) != 1 || got.y.Equal(&expected.y) != 1 {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Square", func(t *testing.T) {
|
||||||
|
got := &gfP4{}
|
||||||
|
got.Set(x)
|
||||||
|
got.Square(got)
|
||||||
|
|
||||||
|
expected := &gfP4{}
|
||||||
|
expected.Mul(x, x)
|
||||||
|
if got.x.Equal(&expected.x) != 1 || got.y.Equal(&expected.y) != 1 {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("SquareV", func(t *testing.T) {
|
||||||
|
got := &gfP4{}
|
||||||
|
got.Set(x)
|
||||||
|
got.SquareV(got)
|
||||||
|
|
||||||
|
expected := &gfP4{}
|
||||||
|
expected.MulV(x, x)
|
||||||
|
if got.x.Equal(&expected.x) != 1 || got.y.Equal(&expected.y) != 1 {
|
||||||
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func Test_gfP4Square(t *testing.T) {
|
func Test_gfP4Square(t *testing.T) {
|
||||||
x := &gfP4{
|
x := &gfP4{
|
||||||
gfP2{
|
gfP2{
|
||||||
|
@ -6,69 +6,138 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_gfpBasicOperations(t *testing.T) {
|
func TestGfpBasicOperations(t *testing.T) {
|
||||||
x := fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141"))
|
x := fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141"))
|
||||||
y := fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B"))
|
y := fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B"))
|
||||||
expectedAdd := fromBigInt(bigFromHex("0691692307d370af56226e57920199fbbe10f216c67fbc9468c7f225a4b1f21f"))
|
expectedAdd := fromBigInt(bigFromHex("0691692307d370af56226e57920199fbbe10f216c67fbc9468c7f225a4b1f21f"))
|
||||||
|
expectedDouble := fromBigInt(bigFromHex("551de7a0ee24723edcf314ff72f478fac1c7c4e7044238acc3913cfbcdaf7d05"))
|
||||||
expectedSub := fromBigInt(bigFromHex("67b381821c52a5624f3304a8149be8461e3bc07adcb872c38aa65051ba53ba97"))
|
expectedSub := fromBigInt(bigFromHex("67b381821c52a5624f3304a8149be8461e3bc07adcb872c38aa65051ba53ba97"))
|
||||||
expectedNeg := fromBigInt(bigFromHex("7f1d8aad70909be90358f1d02240062433cc3a0248ded72febb879ec33ce6f22"))
|
expectedNeg := fromBigInt(bigFromHex("7f1d8aad70909be90358f1d02240062433cc3a0248ded72febb879ec33ce6f22"))
|
||||||
expectedMul := fromBigInt(bigFromHex("3d08bbad376584e4f74bd31f78f716372b96ba8c3f939c12b8d54e79b6489e76"))
|
expectedMul := fromBigInt(bigFromHex("3d08bbad376584e4f74bd31f78f716372b96ba8c3f939c12b8d54e79b6489e76"))
|
||||||
expectedMul2 := fromBigInt(bigFromHex("1df94a9e05a559ff38e0ab50cece734dc058d33738ceacaa15986a67cbff1ef6"))
|
expectedMul2 := fromBigInt(bigFromHex("1df94a9e05a559ff38e0ab50cece734dc058d33738ceacaa15986a67cbff1ef6"))
|
||||||
|
|
||||||
ret := &gfP{}
|
t.Run("add", func(t *testing.T) {
|
||||||
gfpAdd(ret, x, y)
|
ret := &gfP{}
|
||||||
if *expectedAdd != *ret {
|
gfpAdd(ret, x, y)
|
||||||
t.Errorf("add not same")
|
if *expectedAdd != *ret {
|
||||||
}
|
t.Errorf("add not same")
|
||||||
|
}
|
||||||
|
x1 := &gfP{}
|
||||||
|
x1.Set(x)
|
||||||
|
gfpAdd(x1, x1, y)
|
||||||
|
if *expectedAdd != *x1 {
|
||||||
|
t.Errorf("add not same when add self")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
gfpSub(ret, y, x)
|
t.Run("double", func(t *testing.T) {
|
||||||
if *expectedSub != *ret {
|
ret := &gfP{}
|
||||||
t.Errorf("sub not same")
|
gfpDouble(ret, x)
|
||||||
}
|
if ret.Equal(expectedDouble) != 1 {
|
||||||
|
t.Errorf("double not same, got %v, expected %v", ret, expectedDouble)
|
||||||
|
}
|
||||||
|
ret.Set(x)
|
||||||
|
gfpDouble(ret, ret)
|
||||||
|
if ret.Equal(expectedDouble) != 1 {
|
||||||
|
t.Errorf("double not same, got %v, expected %v", ret, expectedDouble)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
gfpNeg(ret, y)
|
t.Run("triple", func(t *testing.T) {
|
||||||
if *expectedNeg != *ret {
|
expected := &gfP{}
|
||||||
t.Errorf("neg not same")
|
gfpAdd(expected, x, expectedDouble)
|
||||||
}
|
ret := &gfP{}
|
||||||
|
ret.Set(x)
|
||||||
|
gfpTriple(ret, ret)
|
||||||
|
if ret.Equal(expected) != 1 {
|
||||||
|
t.Errorf("expected %v, got %v", expected, ret)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
gfpMul(ret, x, y)
|
t.Run("sub", func(t *testing.T) {
|
||||||
if *expectedMul != *ret {
|
ret := &gfP{}
|
||||||
t.Errorf("mul not same")
|
gfpSub(ret, y, x)
|
||||||
}
|
if *expectedSub != *ret {
|
||||||
|
t.Errorf("sub not same")
|
||||||
|
}
|
||||||
|
x1 := &gfP{}
|
||||||
|
x1.Set(x)
|
||||||
|
gfpSub(x1, y, x1)
|
||||||
|
if *expectedSub != *x1 {
|
||||||
|
t.Errorf("sub not same when sub self")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
ret1, ret2 := &gfP{}, &gfP{}
|
t.Run("neg", func(t *testing.T) {
|
||||||
gfpMul(ret1, ret, ret)
|
ret := &gfP{}
|
||||||
if *ret1 != *expectedMul2 {
|
gfpNeg(ret, y)
|
||||||
t.Errorf("mul not same")
|
if *expectedNeg != *ret {
|
||||||
}
|
t.Errorf("neg not same")
|
||||||
gfpMul(ret1, ret1, ret1)
|
}
|
||||||
gfpSqr(ret2, ret, 2)
|
ret.Set(y)
|
||||||
if *ret1 != *ret2 {
|
gfpNeg(ret, ret)
|
||||||
t.Errorf("mul/sqr not same")
|
if *expectedNeg != *ret {
|
||||||
}
|
t.Errorf("neg not same when neg self")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("mul", func(t *testing.T) {
|
||||||
|
ret := &gfP{}
|
||||||
|
gfpMul(ret, x, y)
|
||||||
|
if *expectedMul != *ret {
|
||||||
|
t.Errorf("mul not same")
|
||||||
|
}
|
||||||
|
ret.Set(x)
|
||||||
|
gfpMul(ret, ret, y)
|
||||||
|
if *expectedMul != *ret {
|
||||||
|
t.Errorf("mul not same when mul self")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("square", func(t *testing.T) {
|
||||||
|
ret, ret1, ret2 := &gfP{}, &gfP{}, &gfP{}
|
||||||
|
gfpMul(ret, x, y)
|
||||||
|
gfpMul(ret1, ret, ret)
|
||||||
|
if *ret1 != *expectedMul2 {
|
||||||
|
t.Errorf("mul not same")
|
||||||
|
}
|
||||||
|
gfpMul(ret1, ret1, ret1)
|
||||||
|
gfpSqr(ret2, ret, 2)
|
||||||
|
if *ret1 != *ret2 {
|
||||||
|
t.Errorf("mul/sqr not same")
|
||||||
|
}
|
||||||
|
ret2.Set(ret)
|
||||||
|
gfpSqr(ret2, ret2, 2)
|
||||||
|
if *ret1 != *ret2 {
|
||||||
|
t.Errorf("mul/sqr not same when square self")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_gfpSqr(t *testing.T) {
|
func TestGfpSqr(t *testing.T) {
|
||||||
// p - 1
|
t.Run("p-1", func(t *testing.T) {
|
||||||
pMinusOne := new(big.Int).Sub(p, big.NewInt(1))
|
pMinusOne := new(big.Int).Sub(p, big.NewInt(1))
|
||||||
x := fromBigInt(pMinusOne)
|
x := fromBigInt(pMinusOne)
|
||||||
ret := &gfP{}
|
ret := &gfP{}
|
||||||
gfpSqr(ret, x, 1)
|
gfpSqr(ret, x, 1)
|
||||||
pMinusOne.Mul(pMinusOne, pMinusOne)
|
pMinusOne.Mul(pMinusOne, pMinusOne)
|
||||||
pMinusOne.Mod(pMinusOne, p)
|
pMinusOne.Mod(pMinusOne, p)
|
||||||
expected := fromBigInt(pMinusOne)
|
expected := fromBigInt(pMinusOne)
|
||||||
if *ret != *expected {
|
if *ret != *expected {
|
||||||
t.Errorf("bad sqr")
|
t.Errorf("bad sqr")
|
||||||
}
|
}
|
||||||
// p + 1
|
})
|
||||||
pPlusOne := new(big.Int).Add(p, big.NewInt(1))
|
t.Run("p+1", func(t *testing.T) {
|
||||||
x = fromBigInt(pPlusOne)
|
pPlusOne := new(big.Int).Add(p, big.NewInt(1))
|
||||||
gfpSqr(ret, x, 1)
|
x := fromBigInt(pPlusOne)
|
||||||
pPlusOne.Mul(pPlusOne, pPlusOne)
|
ret := &gfP{}
|
||||||
pPlusOne.Mod(pPlusOne, p)
|
gfpSqr(ret, x, 1)
|
||||||
if *ret != *fromBigInt(pPlusOne) {
|
pPlusOne.Mul(pPlusOne, pPlusOne)
|
||||||
t.Errorf("bad sqr")
|
pPlusOne.Mod(pPlusOne, p)
|
||||||
}
|
if *ret != *fromBigInt(pPlusOne) {
|
||||||
|
t.Errorf("bad sqr")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromMont(t *testing.T) {
|
func TestFromMont(t *testing.T) {
|
||||||
@ -184,7 +253,7 @@ func TestGfpNeg(t *testing.T) {
|
|||||||
x := fromBigInt(bigFromHex("9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596"))
|
x := fromBigInt(bigFromHex("9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596"))
|
||||||
got := &gfP{}
|
got := &gfP{}
|
||||||
gfpSub(got, zero, x)
|
gfpSub(got, zero, x)
|
||||||
expected := &gfP{}
|
expected := &gfP{}
|
||||||
gfpNeg(expected, x)
|
gfpNeg(expected, x)
|
||||||
if *expected != *got {
|
if *expected != *got {
|
||||||
t.Errorf("got %v, expected %v", got, expected)
|
t.Errorf("got %v, expected %v", got, expected)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user