Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
2f689e0e
Commit
2f689e0e
authored
Nov 22, 2018
by
sanghg
Committed by
vipwzw
Nov 22, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
issues #55
补充crypto的测试用例
parent
76169510
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
151 additions
and
179 deletions
+151
-179
privacy_test.go
plugin/dapp/privacy/crypto/privacy_test.go
+104
-179
ring_signature_test.go
plugin/dapp/privacy/crypto/ring_signature_test.go
+47
-0
No files found.
plugin/dapp/privacy/crypto/privacy_test.go
View file @
2f689e0e
...
@@ -4,182 +4,107 @@
...
@@ -4,182 +4,107 @@
package
privacy
package
privacy
/*
import
(
func TestPrivacyOnetimeKey(t *testing.T) {
"testing"
t.Logf("Begin to do TestPrivacyOnetimeKey\n")
"github.com/33cn/chain33/common/crypto"
priKey := "BC7621CE84D3D67851326C360B518DB5"
"github.com/33cn/chain33/common/log"
pribyte, _ := common.Hex2Bytes(priKey)
priaddr32 := (*[32]byte)(unsafe.Pointer(&pribyte[0]))
"github.com/stretchr/testify/assert"
privacyInfo, err := NewPrivacyWithPrivKey(priaddr32)
)
if err != nil {
t.Errorf("Failed to NewPrivacyWithPrivKey", "err info is", err)
type
pubKeyMock
struct
{
return
}
}
func
(
*
pubKeyMock
)
Bytes
()
[]
byte
{
t.Logf("viewprikey:%X, viewpubkey:%X\n", privacyInfo.ViewPrivKey, privacyInfo.ViewPubkey)
return
[]
byte
(
"pubKeyMock"
)
t.Logf("spendprikey:%X, spendpubkey:%X\n", privacyInfo.SpendPrivKey, privacyInfo.SpendPubkey)
}
viewPublic := (*[32]byte)(unsafe.Pointer(&privacyInfo.ViewPubkey[0]))
func
(
*
pubKeyMock
)
KeyString
()
string
{
spendPublic := (*[32]byte)(unsafe.Pointer(&privacyInfo.SpendPubkey[0]))
return
"pubKeyMock"
pubkeyOnetime, txPublicKey, err := GenerateOneTimeAddr(viewPublic, spendPublic)
}
if err != nil {
t.Errorf("Failed to GenerateOneTimeAddr")
func
(
*
pubKeyMock
)
VerifyBytes
(
msg
[]
byte
,
sig
crypto
.
Signature
)
bool
{
return
return
true
}
}
t.Logf("The generated pubkeyOnetime: %X \n", pubkeyOnetime[:])
t.Logf("The generated txPublicKey: %X \n", txPublicKey[:])
func
(
*
pubKeyMock
)
Equals
(
crypto
.
PubKey
)
bool
{
return
true
onetimePriKey, err := RecoverOnetimePriKey(txPublicKey[:], privacyInfo.ViewPrivKey, privacyInfo.SpendPrivKey)
}
if err != nil {
t.Errorf("Failed to RecoverOnetimePriKey")
type
signatureMock
struct
{
return
}
}
t.Logf("The recovered one time privicy key is:%X", onetimePriKey.Bytes())
func
(
*
signatureMock
)
Bytes
()
[]
byte
{
return
[]
byte
(
"signatureMock"
)
recoverPub := onetimePriKey.PubKey().Bytes()[:]
}
originPub := pubkeyOnetime[:]
t.Logf("****¥¥¥*****The recoverPub key is:%X", recoverPub)
func
(
*
signatureMock
)
IsZero
()
bool
{
t.Logf("****¥¥¥*****The originPub key is:%X", originPub)
return
true
}
if !bytes.Equal(recoverPub, originPub) {
func
(
*
signatureMock
)
String
()
string
{
t.Failed()
return
"signatureMock"
t.Errorf("recoverPub is not equal to originPub")
}
return
func
(
*
signatureMock
)
Equals
(
crypto
.
Signature
)
bool
{
} else {
return
true
t.Logf("Yea!!! Succeed to do the TestPrivacyOnetimeKey.")
}
}
type
privKeyMock
struct
{
t.Logf("End to do TestPrivacyOnetimeKey\n")
}
}
*/
func
(
mock
*
privKeyMock
)
Bytes
()
[]
byte
{
return
[]
byte
(
"1234"
)
/*
}
// TODO: 需要增加隐私签名的UT
func TestPrivacySignWithFixInput(t *testing.T) {
func
(
mock
*
privKeyMock
)
Sign
(
msg
[]
byte
)
crypto
.
Signature
{
prislice, _ := common.Hex2Bytes("9E0ED368F3DDAA9F472FE7F319F866227A74A2EF16B43410CEB3CE7C1BAAEB09")
return
&
signatureMock
{}
var onetimePriKey PrivKeyPrivacy
}
copy(onetimePriKey[:], prislice)
func
(
mock
*
privKeyMock
)
PubKey
()
crypto
.
PubKey
{
recoverPub := onetimePriKey.PubKey().Bytes()[:]
return
&
pubKeyMock
{}
}
data := []byte("Yea!!! Succeed to do the TestPrivacyOnetimeKey")
sig := onetimePriKey.Sign(data)
func
(
mock
*
privKeyMock
)
Equals
(
crypto
.
PrivKey
)
bool
{
sign := &types.Signature{
return
true
Ty: 4,
}
Pubkey: recoverPub,
Signature: sig.Bytes(),
func
init
()
{
}
log
.
SetLogLevel
(
"crit"
)
}
c := &oneTimeEd25519{}
func
TestNewPrivacy
(
t
*
testing
.
T
)
{
pub, err := c.PubKeyFromBytes(sign.Pubkey)
test_NewPrivacy
(
t
)
if err != nil {
test_NewPrivacyWithPrivKey
(
t
)
t.Failed()
test_GenerateOneTimeAddr
(
t
)
t.Errorf("Failed to PubKeyFromBytes")
test_RecoverOnetimePriKey
(
t
)
return
}
}
signbytes, err := c.SignatureFromBytes(sign.Signature)
func
test_RecoverOnetimePriKey
(
t
*
testing
.
T
)
{
if err != nil {
R
:=
[]
byte
(
"1234"
)
t.Failed()
pkm
:=
privKeyMock
{}
t.Errorf("Failed to SignatureFromBytes")
privKey
,
err
:=
RecoverOnetimePriKey
(
R
,
&
pkm
,
&
pkm
,
0
)
return
assert
.
Nil
(
t
,
err
)
}
assert
.
NotNil
(
t
,
privKey
)
}
if pub.VerifyBytes(data, signbytes) {
t.Logf("Yea!!! Succeed to pass CheckSign.")
func
test_GenerateOneTimeAddr
(
t
*
testing
.
T
)
{
bytes1
:=
[
32
]
byte
{}
} else {
pkot
,
err
:=
GenerateOneTimeAddr
(
&
bytes1
,
&
bytes1
,
&
bytes1
,
0
)
t.Failed()
assert
.
Nil
(
t
,
err
)
t.Errorf("Fail the CheckSign")
assert
.
NotNil
(
t
,
pkot
)
return
}
}
func
test_NewPrivacy
(
t
*
testing
.
T
)
{
p
:=
NewPrivacy
()
t.Logf("End to do TestPrivacyOnetimeKey\n")
assert
.
NotNil
(
t
,
p
)
}
}
*/
func
test_NewPrivacyWithPrivKey
(
t
*
testing
.
T
)
{
//func TestPrivacySign(t *testing.T) {
bytes1
:=
[
KeyLen32
]
byte
{}
// t.Logf("Begin to do TestPrivacyOnetimeKey\n")
p
,
err
:=
NewPrivacyWithPrivKey
(
&
bytes1
)
//
assert
.
Nil
(
t
,
err
)
// priKey := "BC7621CE84D3D67851326C360B518DB5"
assert
.
NotNil
(
t
,
p
)
// pribyte, _ := common.Hex2Bytes(priKey)
// priaddr32 := (*[32]byte)(unsafe.Pointer(&pribyte[0]))
}
// privacyInfo, err := NewPrivacyWithPrivKey(priaddr32)
// if err != nil {
// t.Errorf("Failed to NewPrivacyWithPrivKey", "err info is", err)
// return
// }
//
// t.Logf("viewprikey:%X, viewpubkey:%X\n", privacyInfo.ViewPrivKey, privacyInfo.ViewPubkey)
// t.Logf("spendprikey:%X, spendpubkey:%X\n", privacyInfo.SpendPrivKey, privacyInfo.SpendPubkey)
//
// viewPublic := (*[32]byte)(unsafe.Pointer(&privacyInfo.ViewPubkey[0]))
// spendPublic := (*[32]byte)(unsafe.Pointer(&privacyInfo.SpendPubkey[0]))
// pubkeyOnetime, txPublicKey, err := privacyInfo.GenerateOneTimeAddr(viewPublic, spendPublic)
// if err != nil {
// t.Errorf("Failed to GenerateOneTimeAddr")
// return
// }
// t.Logf("The generated pubkeyOnetime: %X \n", pubkeyOnetime[:])
// t.Logf("The generated txPublicKey: %X \n", txPublicKey[:])
//
// onetimePriKey, err := privacyInfo.RecoverOnetimePriKey(txPublicKey[:], privacyInfo.ViewPrivKey, privacyInfo.SpendPrivKey)
// if err != nil {
// t.Errorf("Failed to RecoverOnetimePriKey")
// return
// }
// t.Logf("The recovered one time privicy key is:%X", onetimePriKey.Bytes())
//
// recoverPub := onetimePriKey.PubKey().Bytes()[:]
// originPub := pubkeyOnetime[:]
// t.Logf("****¥¥¥*****The recoverPub key is:%X", recoverPub)
// t.Logf("****¥¥¥*****The originPub key is:%X", originPub)
//
//
// if !bytes.Equal(recoverPub, originPub) {
// t.Failed()
// t.Errorf("recoverPub is not equal to originPub")
// return
//
// } else {
// t.Logf("Yea!!! Succeed to do the TestPrivacyOnetimeKey.")
// }
// data := []byte("Yea!!! Succeed to do the TestPrivacyOnetimeKey")
// sig := onetimePriKey.Sign(data)
// sign := &types.Signature{
// Ty: 4,
// Pubkey: recoverPub,
// Signature:sig.Bytes(),
// }
//
// c := &oneTimeEd25519{}
//
// pub, err := c.PubKeyFromBytes(sign.Pubkey)
// if err != nil {
// t.Failed()
// t.Errorf("Failed to PubKeyFromBytes")
// return
// }
// signbytes, err := c.SignatureFromBytes(sign.Signature)
// if err != nil {
// t.Failed()
// t.Errorf("Failed to SignatureFromBytes")
// return
// }
//
// if pub.VerifyBytes(data, signbytes) {
// t.Logf("Yea!!! Succeed to pass CheckSign.")
//
// } else {
// t.Failed()
// t.Errorf("Fail the CheckSign")
// return
//
// }
//
// t.Logf("End to do TestPrivacyOnetimeKey\n")
//}
plugin/dapp/privacy/crypto/ring_signature_test.go
View file @
2f689e0e
...
@@ -6,8 +6,11 @@ package privacy
...
@@ -6,8 +6,11 @@ package privacy
import
(
import
(
"bytes"
"bytes"
"encoding/hex"
"testing"
"testing"
"github.com/stretchr/testify/assert"
"math/rand"
"math/rand"
"time"
"time"
...
@@ -458,3 +461,47 @@ func Benchmark_RingSignatureAllStep(b *testing.B) {
...
@@ -458,3 +461,47 @@ func Benchmark_RingSignatureAllStep(b *testing.B) {
}
}
}
}
func
TestRingSignatureCrypto
(
t
*
testing
.
T
)
{
{
sig
:=
&
RingSignature
{}
bytes
:=
hex
.
EncodeToString
(
sig
.
Bytes
())
assert
.
Equal
(
t
,
bytes
,
""
)
assert
.
Equal
(
t
,
true
,
sig
.
IsZero
())
assert
.
Equal
(
t
,
sig
.
String
(),
""
)
assert
.
Equal
(
t
,
sig
.
Equals
(
sig
),
true
)
}
{
key
:=
&
RingSignPrivateKey
{}
bytes
:=
hex
.
EncodeToString
(
key
.
Bytes
())
assert
.
Equal
(
t
,
bytes
,
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
)
assert
.
NotNil
(
t
,
key
.
PubKey
())
assert
.
Equal
(
t
,
key
.
Equals
(
key
),
true
)
sig
:=
key
.
Sign
([]
byte
(
"Messages"
))
assert
.
NotNil
(
t
,
sig
)
}
{
ringsig
:=
&
RingSignature
{}
key
:=
&
RingSignPublicKey
{}
bytes
:=
hex
.
EncodeToString
(
key
.
Bytes
())
assert
.
Equal
(
t
,
bytes
,
"0000000000000000000000000000000000000000000000000000000000000000"
)
assert
.
Equal
(
t
,
key
.
KeyString
(),
"0000000000000000000000000000000000000000000000000000000000000000"
)
assert
.
Equal
(
t
,
key
.
Equals
(
key
),
true
)
assert
.
Equal
(
t
,
key
.
VerifyBytes
([]
byte
(
"Message"
),
ringsig
),
false
)
}
{
ring
:=
&
RingSignED25519
{}
privKey
,
err
:=
ring
.
GenKey
()
assert
.
NoError
(
t
,
err
)
assert
.
NotNil
(
t
,
privKey
)
privKey
,
err
=
ring
.
PrivKeyFromBytes
([]
byte
(
"00000000000000000000000000000000"
))
assert
.
NoError
(
t
,
err
)
assert
.
NotNil
(
t
,
privKey
)
pubKey
,
err
:=
ring
.
PubKeyFromBytes
([]
byte
(
"00000000000000000000000000000000"
))
assert
.
NoError
(
t
,
err
)
assert
.
NotNil
(
t
,
pubKey
)
sig
,
err
:=
ring
.
SignatureFromBytes
([]
byte
(
"00000000000000000000000000000000"
))
assert
.
NoError
(
t
,
err
)
assert
.
NotNil
(
t
,
sig
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment