test: reduce fmt.Printf

This commit is contained in:
Sun Yimin 2022-06-17 08:49:50 +08:00 committed by GitHub
parent aa9e546638
commit 823cf4a470
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 52 deletions

View File

@ -6,7 +6,6 @@ package sm2
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt"
"io" "io"
"math/big" "math/big"
"testing" "testing"
@ -82,14 +81,12 @@ func Test_p256Sqr(t *testing.T) {
gx := []uint64{0x61328990f418029e, 0x3e7981eddca6c050, 0xd6a1ed99ac24c3c3, 0x91167a5ee1c13b05} gx := []uint64{0x61328990f418029e, 0x3e7981eddca6c050, 0xd6a1ed99ac24c3c3, 0x91167a5ee1c13b05}
p256Sqr(res, gx, 2) p256Sqr(res, gx, 2)
resInt := toBigInt(res) resInt := toBigInt(res)
fmt.Printf("1=%s\n", hex.EncodeToString(resInt.Bytes()))
gxsqr := new(big.Int).Mul(x, x) gxsqr := new(big.Int).Mul(x, x)
gxsqr = new(big.Int).Mod(gxsqr, p) gxsqr = new(big.Int).Mod(gxsqr, p)
gxsqr = new(big.Int).Mul(gxsqr, gxsqr) gxsqr = new(big.Int).Mul(gxsqr, gxsqr)
gxsqr = new(big.Int).Mod(gxsqr, p) gxsqr = new(big.Int).Mod(gxsqr, p)
gxsqr = new(big.Int).Mul(gxsqr, r) gxsqr = new(big.Int).Mul(gxsqr, r)
gxsqr = new(big.Int).Mod(gxsqr, p) gxsqr = new(big.Int).Mod(gxsqr, p)
fmt.Printf("2=%s\n", hex.EncodeToString(gxsqr.Bytes()))
if resInt.Cmp(gxsqr) != 0 { if resInt.Cmp(gxsqr) != 0 {
t.FailNow() t.FailNow()
} }
@ -106,12 +103,10 @@ func Test_p256Mul(t *testing.T) {
p256Mul(res, gx, gy) p256Mul(res, gx, gy)
resInt := toBigInt(res) resInt := toBigInt(res)
fmt.Printf("1=%s\n", hex.EncodeToString(resInt.Bytes()))
xmy := new(big.Int).Mul(x, y) xmy := new(big.Int).Mul(x, y)
xmy = new(big.Int).Mod(xmy, p) xmy = new(big.Int).Mod(xmy, p)
xmy = new(big.Int).Mul(xmy, r) xmy = new(big.Int).Mul(xmy, r)
xmy = new(big.Int).Mod(xmy, p) xmy = new(big.Int).Mod(xmy, p)
fmt.Printf("2=%s\n", hex.EncodeToString(xmy.Bytes()))
if resInt.Cmp(xmy) != 0 { if resInt.Cmp(xmy) != 0 {
t.FailNow() t.FailNow()
} }
@ -215,14 +210,12 @@ func Test_p256MulSqr(t *testing.T) {
p256Sqr(res, gx, 32) p256Sqr(res, gx, 32)
resInt := toBigInt(res) resInt := toBigInt(res)
fmt.Printf("0=%s\n", hex.EncodeToString(resInt.Bytes()))
p256Mul(res, gx, gx) p256Mul(res, gx, gx)
for i := 0; i < 31; i++ { for i := 0; i < 31; i++ {
p256Mul(res, res, res) p256Mul(res, res, res)
} }
resInt1 := toBigInt(res) resInt1 := toBigInt(res)
fmt.Printf("1=%s\n", hex.EncodeToString(resInt1.Bytes()))
resInt2 := new(big.Int).Mod(x, p) resInt2 := new(big.Int).Mod(x, p)
@ -232,7 +225,6 @@ func Test_p256MulSqr(t *testing.T) {
} }
resInt2 = new(big.Int).Mul(resInt2, r) resInt2 = new(big.Int).Mul(resInt2, r)
resInt2 = new(big.Int).Mod(resInt2, p) resInt2 = new(big.Int).Mod(resInt2, p)
fmt.Printf("2=%s\n", hex.EncodeToString(resInt2.Bytes()))
if resInt.Cmp(resInt2) != 0 || resInt1.Cmp(resInt2) != 0 { if resInt.Cmp(resInt2) != 0 || resInt1.Cmp(resInt2) != 0 {
t.FailNow() t.FailNow()
@ -250,18 +242,15 @@ func Test_p256OrdSqr(t *testing.T) {
p256BigToLittle(gx, xm.Bytes()) p256BigToLittle(gx, xm.Bytes())
p256OrdMul(res, gx, gx) p256OrdMul(res, gx, gx)
resInt := toBigInt(res) resInt := toBigInt(res)
fmt.Printf("p256OrdMul=%s\n", hex.EncodeToString(resInt.Bytes()))
gxsqr := new(big.Int).Mul(x, x) gxsqr := new(big.Int).Mul(x, x)
gxsqr = new(big.Int).Mod(gxsqr, n) gxsqr = new(big.Int).Mod(gxsqr, n)
gxsqr = new(big.Int).Mul(gxsqr, r) gxsqr = new(big.Int).Mul(gxsqr, r)
gxsqr = new(big.Int).Mod(gxsqr, n) gxsqr = new(big.Int).Mod(gxsqr, n)
fmt.Printf("2=%s\n", hex.EncodeToString(gxsqr.Bytes()))
if resInt.Cmp(gxsqr) != 0 { if resInt.Cmp(gxsqr) != 0 {
t.FailNow() t.FailNow()
} }
p256OrdSqr(res, gx, 1) p256OrdSqr(res, gx, 1)
resInt = toBigInt(res) resInt = toBigInt(res)
fmt.Printf("p256OrdSqr=%s\n", hex.EncodeToString(resInt.Bytes()))
if resInt.Cmp(gxsqr) != 0 { if resInt.Cmp(gxsqr) != 0 {
t.FailNow() t.FailNow()
} }
@ -275,11 +264,9 @@ func Test_p256Inverse(t *testing.T) {
res := make([]uint64, 4) res := make([]uint64, 4)
p256Inverse(res, gx) p256Inverse(res, gx)
resInt := toBigInt(res) resInt := toBigInt(res)
fmt.Printf("p256Inverse=%s\n", hex.EncodeToString(resInt.Bytes()))
xInv := new(big.Int).ModInverse(x, p) xInv := new(big.Int).ModInverse(x, p)
xInv = new(big.Int).Mul(xInv, r) xInv = new(big.Int).Mul(xInv, r)
xInv = new(big.Int).Mod(xInv, p) xInv = new(big.Int).Mod(xInv, p)
fmt.Printf("expected=%s\n", hex.EncodeToString(xInv.Bytes()))
if resInt.Cmp(xInv) != 0 { if resInt.Cmp(xInv) != 0 {
t.FailNow() t.FailNow()
} }
@ -297,16 +284,12 @@ func Test_p256PointAddAsm_basepoint(t *testing.T) {
res := make([]uint64, 12) res := make([]uint64, 12)
copy(in, basePoint) copy(in, basePoint)
p256PointDoubleAsm(res, in) p256PointDoubleAsm(res, in)
n := p256PointAddAsm(res, res, in) p256PointAddAsm(res, res, in)
fmt.Printf("n=%d\n", n)
var r p256Point var r p256Point
copy(r.xyz[:], res) copy(r.xyz[:], res)
x1, y1 := r.p256PointToAffine() x1, y1 := r.p256PointToAffine()
fmt.Printf("x1=%s, y1=%s\n", hex.EncodeToString(x1.Bytes()), hex.EncodeToString(y1.Bytes()))
x2, y2 := params.Double(params.Gx, params.Gy) x2, y2 := params.Double(params.Gx, params.Gy)
x2, y2 = params.Add(params.Gx, params.Gy, x2, y2) x2, y2 = params.Add(params.Gx, params.Gy, x2, y2)
fmt.Printf("x2=%s, y2=%s\n", hex.EncodeToString(x2.Bytes()), hex.EncodeToString(y2.Bytes()))
if x1.Cmp(x2) != 0 || y1.Cmp(y2) != 0 { if x1.Cmp(x2) != 0 || y1.Cmp(y2) != 0 {
t.FailNow() t.FailNow()
} }
@ -326,14 +309,12 @@ func Test_p256PointDoubleAsm(t *testing.T) {
var r p256Point var r p256Point
copy(r.xyz[:], t1) copy(r.xyz[:], t1)
x1, y1 := r.p256PointToAffine() x1, y1 := r.p256PointToAffine()
fmt.Printf("x1=%s, y1=%s\n", hex.EncodeToString(x1.Bytes()), hex.EncodeToString(y1.Bytes()))
curve1 := P256() curve1 := P256()
params := curve1.Params() params := curve1.Params()
x2, y2 := params.Double(params.Gx, params.Gy) x2, y2 := params.Double(params.Gx, params.Gy)
for i := 0; i < 15; i++ { for i := 0; i < 15; i++ {
x2, y2 = params.Double(x2, y2) x2, y2 = params.Double(x2, y2)
} }
fmt.Printf("x2=%s, y2=%s\n", hex.EncodeToString(x2.Bytes()), hex.EncodeToString(y2.Bytes()))
if x1.Cmp(x2) != 0 || y1.Cmp(y2) != 0 { if x1.Cmp(x2) != 0 || y1.Cmp(y2) != 0 {
t.FailNow() t.FailNow()
} }
@ -343,10 +324,8 @@ func Test_ScalarBaseMult(t *testing.T) {
scalar := big.NewInt(0xffffffff) scalar := big.NewInt(0xffffffff)
curve1 := P256() curve1 := P256()
x1, y1 := curve1.ScalarBaseMult(scalar.Bytes()) x1, y1 := curve1.ScalarBaseMult(scalar.Bytes())
fmt.Printf("x1=%s, y1=%s\n", hex.EncodeToString(x1.Bytes()), hex.EncodeToString(y1.Bytes()))
params := curve1.Params() params := curve1.Params()
x2, y2 := params.ScalarBaseMult(scalar.Bytes()) x2, y2 := params.ScalarBaseMult(scalar.Bytes())
fmt.Printf("x2=%s, y2=%s\n", hex.EncodeToString(x2.Bytes()), hex.EncodeToString(y2.Bytes()))
if x1.Cmp(x2) != 0 || y1.Cmp(y2) != 0 { if x1.Cmp(x2) != 0 || y1.Cmp(y2) != 0 {
t.FailNow() t.FailNow()
} }
@ -360,7 +339,6 @@ func Test_p256PointAddAsm(t *testing.T) {
k2, _ := randFieldElement(params, rand.Reader) k2, _ := randFieldElement(params, rand.Reader)
x2, y2 := params.ScalarBaseMult(k2.Bytes()) x2, y2 := params.ScalarBaseMult(k2.Bytes())
x3, y3 := params.Add(x1, y1, x2, y2) x3, y3 := params.Add(x1, y1, x2, y2)
fmt.Printf("x1=%s, y1=%s\n", hex.EncodeToString(x3.Bytes()), hex.EncodeToString(y3.Bytes()))
var in1, in2, rp p256Point var in1, in2, rp p256Point
fromBig(in1.xyz[0:4], maybeReduceModP(x1)) fromBig(in1.xyz[0:4], maybeReduceModP(x1))
fromBig(in1.xyz[4:8], maybeReduceModP(y1)) fromBig(in1.xyz[4:8], maybeReduceModP(y1))
@ -380,10 +358,8 @@ func Test_p256PointAddAsm(t *testing.T) {
p256Mul(in2.xyz[4:8], in2.xyz[4:8], rr[:]) p256Mul(in2.xyz[4:8], in2.xyz[4:8], rr[:])
res := make([]uint64, 12) res := make([]uint64, 12)
n := p256PointAddAsm(res, in1.xyz[:], in2.xyz[:]) n := p256PointAddAsm(res, in1.xyz[:], in2.xyz[:])
fmt.Printf("n=%d\n", n)
copy(rp.xyz[:], res) copy(rp.xyz[:], res)
x4, y4 := rp.p256PointToAffine() x4, y4 := rp.p256PointToAffine()
fmt.Printf("x1=%s, y1=%s\n", hex.EncodeToString(x4.Bytes()), hex.EncodeToString(y4.Bytes()))
if n == 0 && (x3.Cmp(x4) != 0 || y3.Cmp(y4) != 0) { if n == 0 && (x3.Cmp(x4) != 0 || y3.Cmp(y4) != 0) {
t.FailNow() t.FailNow()
} }
@ -393,10 +369,8 @@ func Test_ScalarMult_basepoint(t *testing.T) {
scalar := big.NewInt(0xffffffff) scalar := big.NewInt(0xffffffff)
curve1 := P256() curve1 := P256()
x1, y1 := curve1.ScalarMult(curve1.Params().Gx, curve1.Params().Gy, scalar.Bytes()) x1, y1 := curve1.ScalarMult(curve1.Params().Gx, curve1.Params().Gy, scalar.Bytes())
fmt.Printf("x1=%s, y1=%s\n", hex.EncodeToString(x1.Bytes()), hex.EncodeToString(y1.Bytes()))
params := curve1.Params() params := curve1.Params()
x2, y2 := params.ScalarMult(curve1.Params().Gx, curve1.Params().Gy, scalar.Bytes()) x2, y2 := params.ScalarMult(curve1.Params().Gx, curve1.Params().Gy, scalar.Bytes())
fmt.Printf("x2=%s, y2=%s\n", hex.EncodeToString(x2.Bytes()), hex.EncodeToString(y2.Bytes()))
if x1.Cmp(x2) != 0 || y1.Cmp(y2) != 0 { if x1.Cmp(x2) != 0 || y1.Cmp(y2) != 0 {
t.FailNow() t.FailNow()
} }
@ -408,12 +382,9 @@ func Test_Inverse(t *testing.T) {
nm2 := new(big.Int).Sub(n, big.NewInt(2)) nm2 := new(big.Int).Sub(n, big.NewInt(2))
nm2a := make([]uint64, 4) nm2a := make([]uint64, 4)
fromBig(nm2a, nm2) fromBig(nm2a, nm2)
fmt.Printf("%0b, %0b, %b, %b\n", nm2a[0], nm2a[1], nm2a[2], nm2a[3])
xInv1 := fermatInverse(x, n) xInv1 := fermatInverse(x, n)
fmt.Printf("expect=%s\n", hex.EncodeToString(xInv1.Bytes()))
_ = P256() _ = P256()
xInv2 := p256.Inverse(x) xInv2 := p256.Inverse(x)
fmt.Printf("result=%s\n", hex.EncodeToString(xInv2.Bytes()))
if xInv1.Cmp(xInv2) != 0 { if xInv1.Cmp(xInv2) != 0 {
t.FailNow() t.FailNow()

View File

@ -3,7 +3,6 @@ package sm9
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt"
"math/big" "math/big"
"testing" "testing"
@ -95,6 +94,7 @@ func TestSignSM9Sample(t *testing.T) {
hid := byte(0x01) hid := byte(0x01)
uid := []byte("Alice") uid := []byte("Alice")
r := bigFromHex("033c8616b06704813203dfd00965022ed15975c662337aed648835dc4b1cbe") r := bigFromHex("033c8616b06704813203dfd00965022ed15975c662337aed648835dc4b1cbe")
masterKey := new(SignMasterPrivateKey) masterKey := new(SignMasterPrivateKey)
masterKey.D = bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4") masterKey.D = bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4")
masterKey.MasterPublicKey = new(bn256.G2).ScalarBaseMult(masterKey.D) masterKey.MasterPublicKey = new(bn256.G2).ScalarBaseMult(masterKey.D)
@ -129,13 +129,18 @@ func TestSignSM9Sample(t *testing.T) {
// SM9 Appendix B // SM9 Appendix B
func TestKeyExchangeSample(t *testing.T) { func TestKeyExchangeSample(t *testing.T) {
hid := byte(0x02) hid := byte(0x02)
expectedPube := "9174542668e8f14ab273c0945c3690c66e5dd09678b86f734c4350567ed0628354e598c6bf749a3dacc9fffedd9db6866c50457cfc7aa2a4ad65c3168ff74210"
expectedKey := "c5c13a8f59a97cdeae64f16a2272a9e7" expectedKey := "c5c13a8f59a97cdeae64f16a2272a9e7"
expectedSignatureB := "3bb4bcee8139c960b4d6566db1e0d5f0b2767680e5e1bf934103e6c66e40ffee" expectedSignatureB := "3bb4bcee8139c960b4d6566db1e0d5f0b2767680e5e1bf934103e6c66e40ffee"
expectedSignatureA := "195d1b7256ba7e0e67c71202a25f8c94ff8241702c2f55d613ae1c6b98215172" expectedSignatureA := "195d1b7256ba7e0e67c71202a25f8c94ff8241702c2f55d613ae1c6b98215172"
masterKey := new(EncryptMasterPrivateKey) masterKey := new(EncryptMasterPrivateKey)
masterKey.D = bigFromHex("02E65B0762D042F51F0D23542B13ED8CFA2E9A0E7206361E013A283905E31F") masterKey.D = bigFromHex("02E65B0762D042F51F0D23542B13ED8CFA2E9A0E7206361E013A283905E31F")
masterKey.MasterPublicKey = new(bn256.G1).ScalarBaseMult(masterKey.D) masterKey.MasterPublicKey = new(bn256.G1).ScalarBaseMult(masterKey.D)
fmt.Printf("Pub-e=%v\n", hex.EncodeToString(masterKey.MasterPublicKey.Marshal()))
if hex.EncodeToString(masterKey.MasterPublicKey.Marshal()) != expectedPube {
t.Errorf("not expected master public key")
}
userA := []byte("Alice") userA := []byte("Alice")
userB := []byte("Bob") userB := []byte("Bob")
@ -150,7 +155,7 @@ func TestKeyExchangeSample(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
responder:=NewKeyExchange(userKey, userB, userA, 16, true) responder := NewKeyExchange(userKey, userB, userA, 16, true)
// A1-A4 // A1-A4
initKeyExchange(initiator, hid, bigFromHex("5879DD1D51E175946F23B1B41E93BA31C584AE59A426EC1046A4D03B06C8")) initKeyExchange(initiator, hid, bigFromHex("5879DD1D51E175946F23B1B41E93BA31C584AE59A426EC1046A4D03B06C8"))
@ -274,11 +279,18 @@ func TestUnmarshalSM9KeyPackage(t *testing.T) {
// SM9 Appendix C // SM9 Appendix C
func TestWrapKeySM9Sample(t *testing.T) { func TestWrapKeySM9Sample(t *testing.T) {
expectedMasterPublicKey := "787ed7b8a51f3ab84e0a66003f32da5c720b17eca7137d39abc66e3c80a892ff769de61791e5adc4b9ff85a31354900b202871279a8c49dc3f220f644c57a7b1"
expectedUserPrivateKey := "94736acd2c8c8796cc4785e938301a139a059d3537b6414140b2d31eecf41683115bae85f5d8bc6c3dbd9e5342979acccf3c2f4f28420b1cb4f8c0b59a19b1587aa5e47570da7600cd760a0cf7beaf71c447f3844753fe74fa7ba92ca7d3b55f27538a62e7f7bfb51dce08704796d94c9d56734f119ea44732b50e31cdeb75c1"
expectedUserPublicKey := "709d165808b0a43e2574e203fa885abcbab16a240c4c1916552e7c43d09763b8693269a6be2456f43333758274786b6051ff87b7f198da4ba1a2c6e336f51fcc"
expectedCipher := "1edee2c3f465914491de44cefb2cb434ab02c308d9dc5e2067b4fed5aaac8a0f1c9b4c435eca35ab83bb734174c0f78fde81a53374aff3b3602bbc5e37be9a4c"
expectedKey := "4ff5cf86d2ad40c8f4bac98d76abdbde0c0e2f0a829d3f911ef5b2bce0695480" expectedKey := "4ff5cf86d2ad40c8f4bac98d76abdbde0c0e2f0a829d3f911ef5b2bce0695480"
masterKey := new(EncryptMasterPrivateKey) masterKey := new(EncryptMasterPrivateKey)
masterKey.D = bigFromHex("01EDEE3778F441F8DEA3D9FA0ACC4E07EE36C93F9A08618AF4AD85CEDE1C22") masterKey.D = bigFromHex("01EDEE3778F441F8DEA3D9FA0ACC4E07EE36C93F9A08618AF4AD85CEDE1C22")
masterKey.MasterPublicKey = new(bn256.G1).ScalarBaseMult(masterKey.D) masterKey.MasterPublicKey = new(bn256.G1).ScalarBaseMult(masterKey.D)
fmt.Printf("Pub-e=%v\n", hex.EncodeToString(masterKey.MasterPublicKey.Marshal())) if hex.EncodeToString(masterKey.MasterPublicKey.Marshal()) != expectedMasterPublicKey {
t.Errorf("not expected master public key")
}
uid := []byte("Bob") uid := []byte("Bob")
hid := byte(0x03) hid := byte(0x03)
@ -287,14 +299,20 @@ func TestWrapKeySM9Sample(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fmt.Printf("UserPrivKey=%v\n", hex.EncodeToString(userKey.PrivateKey.Marshal())) if hex.EncodeToString(userKey.PrivateKey.Marshal()) != expectedUserPrivateKey {
t.Errorf("not expected user private key")
}
q := masterKey.Public().GenerateUserPublicKey(uid, hid) q := masterKey.Public().GenerateUserPublicKey(uid, hid)
fmt.Printf("Qb=%v\n", hex.EncodeToString(q.Marshal())) if hex.EncodeToString(q.Marshal()) != expectedUserPublicKey {
var r *big.Int = bigFromHex("74015F8489C01EF4270456F9E6475BFB602BDE7F33FD482AB4E3684A6722") t.Errorf("not expected user public key")
}
var r *big.Int = bigFromHex("74015F8489C01EF4270456F9E6475BFB602BDE7F33FD482AB4E3684A6722")
cipher := new(bn256.G1).ScalarMult(q, r) cipher := new(bn256.G1).ScalarMult(q, r)
fmt.Printf("C=%v\n", hex.EncodeToString(cipher.Marshal())) if hex.EncodeToString(cipher.Marshal()) != expectedCipher {
t.Errorf("not expected cipher")
}
g := bn256.Pair(masterKey.Public().MasterPublicKey, bn256.Gen2) g := bn256.Pair(masterKey.Public().MasterPublicKey, bn256.Gen2)
w := new(bn256.GT).ScalarMult(g, r) w := new(bn256.GT).ScalarMult(g, r)
@ -324,11 +342,19 @@ func TestWrapKeySM9Sample(t *testing.T) {
// SM9 Appendix D // SM9 Appendix D
func TestEncryptSM9Sample(t *testing.T) { func TestEncryptSM9Sample(t *testing.T) {
plaintext := []byte("Chinese IBE standard") plaintext := []byte("Chinese IBE standard")
expectedMasterPublicKey := "787ed7b8a51f3ab84e0a66003f32da5c720b17eca7137d39abc66e3c80a892ff769de61791e5adc4b9ff85a31354900b202871279a8c49dc3f220f644c57a7b1"
expectedUserPrivateKey := "94736acd2c8c8796cc4785e938301a139a059d3537b6414140b2d31eecf41683115bae85f5d8bc6c3dbd9e5342979acccf3c2f4f28420b1cb4f8c0b59a19b1587aa5e47570da7600cd760a0cf7beaf71c447f3844753fe74fa7ba92ca7d3b55f27538a62e7f7bfb51dce08704796d94c9d56734f119ea44732b50e31cdeb75c1"
expectedUserPublicKey := "709d165808b0a43e2574e203fa885abcbab16a240c4c1916552e7c43d09763b8693269a6be2456f43333758274786b6051ff87b7f198da4ba1a2c6e336f51fcc"
expectedCipher := "2445471164490618e1ee20528ff1d545b0f14c8bcaa44544f03dab5dac07d8ff42ffca97d57cddc05ea405f2e586feb3a6930715532b8000759f13059ed59ac0"
expectedKey := "58373260f067ec48667c21c144f8bc33cd3049788651ffd5f738003e51df31174d0e4e402fd87f4581b612f74259db574f67ece6"
expectedCiphertext := "2445471164490618e1ee20528ff1d545b0f14c8bcaa44544f03dab5dac07d8ff42ffca97d57cddc05ea405f2e586feb3a6930715532b8000759f13059ed59ac0ba672387bcd6de5016a158a52bb2e7fc429197bcab70b25afee37a2b9db9f3671b5f5b0e951489682f3e64e1378cdd5da9513b1c" expectedCiphertext := "2445471164490618e1ee20528ff1d545b0f14c8bcaa44544f03dab5dac07d8ff42ffca97d57cddc05ea405f2e586feb3a6930715532b8000759f13059ed59ac0ba672387bcd6de5016a158a52bb2e7fc429197bcab70b25afee37a2b9db9f3671b5f5b0e951489682f3e64e1378cdd5da9513b1c"
masterKey := new(EncryptMasterPrivateKey) masterKey := new(EncryptMasterPrivateKey)
masterKey.D = bigFromHex("01EDEE3778F441F8DEA3D9FA0ACC4E07EE36C93F9A08618AF4AD85CEDE1C22") masterKey.D = bigFromHex("01EDEE3778F441F8DEA3D9FA0ACC4E07EE36C93F9A08618AF4AD85CEDE1C22")
masterKey.MasterPublicKey = new(bn256.G1).ScalarBaseMult(masterKey.D) masterKey.MasterPublicKey = new(bn256.G1).ScalarBaseMult(masterKey.D)
fmt.Printf("Pub-e=%v\n", hex.EncodeToString(masterKey.MasterPublicKey.Marshal())) if hex.EncodeToString(masterKey.MasterPublicKey.Marshal()) != expectedMasterPublicKey {
t.Errorf("not expected master public key")
}
uid := []byte("Bob") uid := []byte("Bob")
hid := byte(0x03) hid := byte(0x03)
@ -337,14 +363,20 @@ func TestEncryptSM9Sample(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fmt.Printf("UserPrivKey=%v\n", hex.EncodeToString(userKey.PrivateKey.Marshal())) if hex.EncodeToString(userKey.PrivateKey.Marshal()) != expectedUserPrivateKey {
t.Errorf("not expected user private key")
}
q := masterKey.Public().GenerateUserPublicKey(uid, hid) q := masterKey.Public().GenerateUserPublicKey(uid, hid)
fmt.Printf("Qb=%v\n", hex.EncodeToString(q.Marshal())) if hex.EncodeToString(q.Marshal()) != expectedUserPublicKey {
var r *big.Int = bigFromHex("AAC0541779C8FC45E3E2CB25C12B5D2576B2129AE8BB5EE2CBE5EC9E785C") t.Errorf("not expected user public key")
}
var r *big.Int = bigFromHex("AAC0541779C8FC45E3E2CB25C12B5D2576B2129AE8BB5EE2CBE5EC9E785C")
cipher := new(bn256.G1).ScalarMult(q, r) cipher := new(bn256.G1).ScalarMult(q, r)
fmt.Printf("C=%v\n", hex.EncodeToString(cipher.Marshal())) if hex.EncodeToString(cipher.Marshal()) != expectedCipher {
t.Errorf("not expected cipher")
}
g := bn256.Pair(masterKey.Public().MasterPublicKey, bn256.Gen2) g := bn256.Pair(masterKey.Public().MasterPublicKey, bn256.Gen2)
w := new(bn256.GT).ScalarMult(g, r) w := new(bn256.GT).ScalarMult(g, r)
@ -358,8 +390,9 @@ func TestEncryptSM9Sample(t *testing.T) {
if !ok { if !ok {
t.Failed() t.Failed()
} }
if hex.EncodeToString(key) != expectedKey {
fmt.Printf("key=%v\n", hex.EncodeToString(key)) t.Errorf("not expected key")
}
xor.XorBytes(key, key[:len(plaintext)], plaintext) xor.XorBytes(key, key[:len(plaintext)], plaintext)
hash := sm3.New() hash := sm3.New()

View File

@ -16,7 +16,6 @@ import (
"encoding/json" "encoding/json"
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt"
"io" "io"
"math/big" "math/big"
"net" "net"
@ -236,10 +235,12 @@ func parseAndCheckCsr(csrPem []byte) error {
func Test_ParseCertificate(t *testing.T) { func Test_ParseCertificate(t *testing.T) {
cert, err := ParseCertificatePEM([]byte(sm2Certificate)) cert, err := ParseCertificatePEM([]byte(sm2Certificate))
if err != nil { if err != nil {
t.Fatalf("%v\n", err) t.Fatal(err)
}
_, err = json.Marshal(cert)
if err != nil {
t.Fatal(err)
} }
jsonContent, err := json.Marshal(cert)
fmt.Printf("%s\n", jsonContent)
} }
func TestParseAliCertificateRequest(t *testing.T) { func TestParseAliCertificateRequest(t *testing.T) {
@ -260,7 +261,6 @@ func TestCreateSM2CertificateRequest(t *testing.T) {
} }
block := &pem.Block{Bytes: csrblock, Type: "CERTIFICATE REQUEST"} block := &pem.Block{Bytes: csrblock, Type: "CERTIFICATE REQUEST"}
pemContent := string(pem.EncodeToMemory(block)) pemContent := string(pem.EncodeToMemory(block))
fmt.Printf("%s\n", pemContent)
err = parseAndCheckCsr([]byte(pemContent)) err = parseAndCheckCsr([]byte(pemContent))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -327,11 +327,10 @@ func TestParsePKIXPublicKeyFromExternal(t *testing.T) {
t.Fatalf("%s failed to get public key %v", test.name, err) t.Fatalf("%s failed to get public key %v", test.name, err)
} }
pub1 := pub.(*ecdsa.PublicKey) pub1 := pub.(*ecdsa.PublicKey)
encrypted, err := sm2.Encrypt(rand.Reader, pub1, []byte("encryption standard"), sm2.ASN1EncrypterOpts) _, err = sm2.Encrypt(rand.Reader, pub1, []byte("encryption standard"), sm2.ASN1EncrypterOpts)
if err != nil { if err != nil {
t.Fatalf("%s failed to encrypt %v", test.name, err) t.Fatalf("%s failed to encrypt %v", test.name, err)
} }
fmt.Printf("encrypted=%s\n", base64.RawURLEncoding.EncodeToString(encrypted))
} }
} }