bugs fixed

This commit is contained in:
www-git-cn 2024-05-01 16:51:09 +08:00
parent b4180f75f9
commit 448df64874
30 changed files with 1765 additions and 72 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/sample_chain.iml" filepath="$PROJECT_DIR$/.idea/sample_chain.iml" />
</modules>
</component>
</project>

9
.idea/sample_chain.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

Binary file not shown.

BIN
blockDB_1408/000454.ldb Normal file

Binary file not shown.

BIN
blockDB_1408/000455.ldb Normal file

Binary file not shown.

BIN
blockDB_1408/000458.ldb Normal file

Binary file not shown.

BIN
blockDB_1408/000461.ldb Normal file

Binary file not shown.

BIN
blockDB_1408/000462.log Normal file

Binary file not shown.

View File

@ -1 +1 @@
MANIFEST-000175
MANIFEST-000463

View File

@ -1 +1 @@
MANIFEST-000172
MANIFEST-000460

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
000001
6d0770e05a3ce79a32e2aea78fcda3bcb4054b86df8369e50a5ab4b7e26f1d38

View File

@ -1 +1 @@
000001
226383fbed07e3841affd83f7681f90602ad08e57bd27d6cad9a666d81b8b7ef17672b52cbc215c51d308299a1b640d6c0229bf761364cca3eae31d16c13da29

View File

@ -1 +1 @@
000002
6d0770e05a3ce79a32e2aea78fcda3bcb4054b86df8369e50a5ab4b7e26f1d38

View File

@ -1 +1 @@
000002
226383fbed07e3841affd83f7681f90602ad08e57bd27d6cad9a666d81b8b7ef17672b52cbc215c51d308299a1b640d6c0229bf761364cca3eae31d16c13da29

View File

@ -1 +1 @@
000003
6d0770e05a3ce79a32e2aea78fcda3bcb4054b86df8369e50a5ab4b7e26f1d38

View File

@ -1 +1 @@
000003
226383fbed07e3841affd83f7681f90602ad08e57bd27d6cad9a666d81b8b7ef17672b52cbc215c51d308299a1b640d6c0229bf761364cca3eae31d16c13da29

230
common/kgc/gofunc.go Normal file
View File

@ -0,0 +1,230 @@
package kgc
import (
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"math/big"
)
func hash1(curve elliptic.Curve, ID []byte, Qx, Qy, PKx, PKy *big.Int) *big.Int {
// 创建 SHA-256 散列器
h := sha256.New()
// 更新散列器以包含字符串 ID 的内容
h.Write(ID)
// 更新散列器以包含点 Q 的坐标信息
h.Write(Qx.Bytes())
h.Write(Qy.Bytes())
// 更新散列器以包含公钥 PK_pub 的坐标信息
h.Write(PKx.Bytes())
h.Write(PKy.Bytes())
// 计算 SHA-256 散列值
hash := h.Sum(nil)
// 将散列值转换为大整数
hBig := new(big.Int).SetBytes(hash)
// 对大整数 hBig 进行幂运算,幂为 1模数为 n
hBig.Mod(hBig, curve.Params().N)
return hBig
}
func hash2(curve elliptic.Curve, ID []byte, Xx, Xy *big.Int) *big.Int {
// 计算 hash 值 H_2(ID, X)
hash := sha256.New()
hash.Write(ID)
hash.Write(Xx.Bytes())
hash.Write(Xy.Bytes())
// 计算 SHA-256 散列值
h2 := hash.Sum(nil)
// 转换为大整数
h2Big := new(big.Int).SetBytes(h2)
// 对大整数 h2Big 进行幂运算,幂为 1模数为 n
h2Big.Mod(h2Big, curve.Params().N)
return h2Big
}
func hash3(curve elliptic.Curve, ID, msg []byte, Qx, Qy, Ux, Uy, PKx, PKy *big.Int) *big.Int {
// 创建 SHA-256 散列器
h := sha256.New()
// 更新散列器以包含字符串 ID 的内容
h.Write(ID)
// 更新散列器以包含字符串 msg 的内容
h.Write(msg)
// 更新散列器以包含点 Q 的坐标信息
h.Write(Qx.Bytes())
h.Write(Qy.Bytes())
// 更新散列器以包含点 U 的坐标信息
h.Write(Ux.Bytes())
h.Write(Uy.Bytes())
// 更新散列器以包含公钥 PK_pub 的坐标信息
h.Write(PKx.Bytes())
h.Write(PKy.Bytes())
// 计算 SHA-256 散列值
hash := h.Sum(nil)
// 将散列值转换为大整数
hBig := new(big.Int).SetBytes(hash)
hBig.Mod(hBig, curve.Params().N)
return hBig
}
func equal(lx, ly, rx, ry *big.Int) error {
if lx.Cmp(rx) == 0 && ly.Cmp(ry) == 0 {
return nil
}
return errors.New("point compare failed ")
}
func genKGCKey(curve elliptic.Curve) (*big.Int, *big.Int, *big.Int) {
// 随机生成 msk
msk, _ := rand.Int(rand.Reader, curve.Params().N)
PKx, PKy := curve.ScalarBaseMult(msk.Bytes())
return PKx, PKy, msk
}
func genSecret(curve elliptic.Curve) (*big.Int, *big.Int, *big.Int) {
// 随机生成 x
x, _ := rand.Int(rand.Reader, curve.Params().N)
Xx, Xy := curve.ScalarBaseMult(x.Bytes())
return Xx, Xy, x
}
func genPartialKey(curve elliptic.Curve, ID []byte, msk, Xx, Xy, PKx, PKy *big.Int) (*big.Int, *big.Int, *big.Int) {
// 随机生成 r
r, _ := rand.Int(rand.Reader, curve.Params().N)
Rx, Ry := curve.ScalarBaseMult(r.Bytes())
//计算h2的hash值
h2Big := hash2(curve, ID, Xx, Xy)
//计算 h2Big * X
tx, ty := curve.ScalarMult(Xx, Xy, h2Big.Bytes())
//计算 Q = R + h2 * X
Qx, Qy := curve.Add(Rx, Ry, tx, ty)
//计算hash1的值
h1Big := hash1(curve, ID, Qx, Qy, PKx, PKy)
//计算 d = r + msk * h1 mod n
temp := new(big.Int).Mul(msk, h1Big)
d := new(big.Int).Add(r, temp)
d.Mod(d, curve.Params().N)
return d, Qx, Qy
}
func VerifyEquation(curve elliptic.Curve, ID []byte, d, Qx, Qy, Xx, Xy, PKx, PKy *big.Int) error {
//计算等式左值 + h2 * X
lx, ly := curve.ScalarBaseMult(d.Bytes())
h2Big := hash2(curve, ID, Xx, Xy)
h2Xx, h2Xy := curve.ScalarMult(Xx, Xy, h2Big.Bytes())
lx, ly = curve.Add(lx, ly, h2Xx, h2Xy)
//计算 Q + h1 * PK(t)
h1Big := hash1(curve, ID, Qx, Qy, PKx, PKy)
tx, ty := curve.ScalarMult(PKx, PKy, h1Big.Bytes())
rx, ry := curve.Add(Qx, Qy, tx, ty)
err := equal(lx, ly, rx, ry)
if err != nil {
return err
}
return nil
}
func genPriKey(curve elliptic.Curve, ID []byte, x, d, Xx, Xy *big.Int) *big.Int {
//计算h2Big
h2Big := hash2(curve, ID, Xx, Xy)
//计算s = d + h2Big * x
temp := new(big.Int).Mul(h2Big, x)
sUser := new(big.Int).Add(d, temp)
//s = s mod n
sUser = sUser.Mod(sUser, curve.Params().N)
return sUser
}
func SignThumb(curve elliptic.Curve, ID, msg []byte, s, Qx, Qy, PKx, PKy *big.Int) (*big.Int, *big.Int, *big.Int) {
// 生成小于素数 的随机数 u
u, _ := rand.Int(rand.Reader, curve.Params().N)
// 生成点U
Ux, Uy := curve.ScalarMult(curve.Params().Gx, curve.Params().Gy, u.Bytes())
if !curve.IsOnCurve(Ux, Uy) {
fmt.Println("zhuyi ----------------------------------------------U不在曲线上")
}
//计算h3
h3 := hash3(curve, ID, msg, Qx, Qy, Ux, Uy, PKx, PKy)
// 计算 v = u + h3 * s mod n
// 计算 tmp = sa * h_3_big
tmp := new(big.Int).Mul(h3, s)
// 计算 v = u + tmp
v := new(big.Int).Add(u, tmp)
// 计算 v = v mod N
v.Mod(v, curve.Params().N)
return Ux, Uy, v
}
func VerifyThumb(curve elliptic.Curve, ID, msg []byte, Qx, Qy, PKx, PKy, Ux, Uy, v *big.Int) error {
//计算h1和h3
h1Big := hash1(curve, ID, Qx, Qy, PKx, PKy)
h3Big := hash3(curve, ID, msg, Qx, Qy, Ux, Uy, PKx, PKy)
//验证等式 v*P = U + h_3(Q + h_1 * PK_pub)
//等式左边
lx, ly := curve.ScalarBaseMult(v.Bytes())
// 计算 t = h_1_big * PK_pub
tx, ty := curve.ScalarMult(PKx, PKy, h1Big.Bytes())
// 计算 out = Q + t
tx, ty = curve.Add(Qx, Qy, tx, ty)
// 计算 t = h_3_big * t
tx, ty = curve.ScalarMult(tx, ty, h3Big.Bytes())
// 计算 t = U + tmp_p
rx, ry := curve.Add(Ux, Uy, tx, ty)
//比较等式左右两值是否相等
err := equal(lx, ly, rx, ry)
if err != nil {
fmt.Println("invalid!")
return err
}
//fmt.Println("valid!")
return nil
}

View File

@ -1,52 +1,72 @@
package kgc
import (
"bytes"
"crypto/sha256"
"errors"
"crypto/elliptic"
"fmt"
)
func Certificateless_Sign(pri_key []byte, digest []byte) (signature []byte, err error) {
// Certificateless_Sign(prikey, pubkey, msg, ID)
func Certificateless_Sign(pri_key, pub_key, msg []byte, id string) (signature []byte, err error) {
curve := elliptic.P256()
s := BytesToBig(pri_key)
x, y := bytes_halve(pub_key)
Qx := BytesToBig(x)
Qy := BytesToBig(y)
//fmt.Println("公钥第一部分长度为:", len(x))
//fmt.Println("公钥第二部分长度为:", len(y))
ID := []byte(id)
PK_pub := "7196e9722d561a0fea21485fc16140096cc9b88f7174aec7508f14a7b85b61ba299292c726367bbcbf8f00b977849164bf9d3fd6260e55dd6498517bc0dec541"
pk1, pk2 := string_halve(PK_pub)
//fmt.Println("KGC公钥第二部分长度为", len([]byte(pk1)))
//fmt.Println("KGC公钥第二部分长度为", len([]byte(pk2)))
PKx := BytesToBig([]byte(pk1))
PKy := BytesToBig([]byte(pk2))
Ux, Uy, v := SignThumb(curve, ID, msg, s, Qx, Qy, PKx, PKy)
sig := mergeBigInts(Ux, Uy, v)
//fmt.Println("打印签名长度:", len(sig))
//fmt.Println("签名得到的v值为", v)
return sig, nil
var signed_data []byte
if pri_key[5] == '1' {
digest[31] = '1'
signed_data = digest[:]
}
if pri_key[5] == '2' {
digest[31] = '2'
signed_data = digest[:]
}
if pri_key[5] == '3' {
digest[31] = '3'
signed_data = digest[:]
}
//signed_data := append(pri_key, digest...)
//fmt.Println("signdata is ", signed_data)
return signed_data, nil
}
func Certificateless_Verify(publicKey []byte, signature, msg []byte) error {
// Certificateless_Verify(publicKey, ID, signature, msg)
func Certificateless_Verify(pubkey []byte, id string, signature, msg []byte) error {
digest := sha256.Sum256(msg)
var data []byte
if publicKey[5] == '1' {
digest[31] = '1'
data = digest[:]
}
if publicKey[5] == '2' {
digest[31] = '2'
data = digest[:]
}
if publicKey[5] == '3' {
digest[31] = '3'
data = digest[:]
}
fmt.Println("明文的摘要值为:", digest, "应该的签名为:", data, "实际的签名:", signature)
if equal := bytes.Equal(data, signature); !equal {
//x, y := bytes_halve(pubkey)
//fmt.Println("公钥第一部分长度为:", len(x))
//fmt.Println("公钥第二部分长度为:", len(y))
//fmt.Println("接收到消息!!!!!!!!!")
curve := elliptic.P256()
return errors.New("verifySignature 11 error")
x, y := bytes_halve(pubkey)
Qx := BytesToBig(x)
Qy := BytesToBig(y)
//fmt.Println(pubkey)
ID := []byte(id)
PK_pub := "7196e9722d561a0fea21485fc16140096cc9b88f7174aec7508f14a7b85b61ba299292c726367bbcbf8f00b977849164bf9d3fd6260e55dd6498517bc0dec541"
pk1, pk2 := string_halve(PK_pub)
PKx := BytesToBig([]byte(pk1))
PKy := BytesToBig([]byte(pk2))
Ux, Uy, v := AnalSig(signature)
//fmt.Println("打印接收参数111", v)
//fmt.Println("转换后的签名结果为", Ux, Uy, v)
err := VerifyThumb(curve, ID, msg, Qx, Qy, PKx, PKy, Ux, Uy, v)
if err != nil {
fmt.Println("shibai-----------------------------------------")
return err
}
//fmt.Println("签名验证chenggongllllllllsssssssssssssssssss")*/
return nil
}

View File

@ -3,7 +3,9 @@ package kgc
import (
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
pb "schain/proto"
"schain/proto/util"
)
@ -49,3 +51,125 @@ func InitIdentity(IdentityPath string, mspID string) ([]byte, error) {
}
return util.MarshalOrPanic(sId), nil
}
func string_halve(str string) (string, string) {
// 计算字符串长度
length := len(str)
// 将字符串分成两部分
mid := length / 2
part1 := str[:mid]
part2 := str[mid:]
//fmt.Println("第一部分:", part1)
//fmt.Println("第二部分:", part2)
return part1, part2
}
func string_trisect(str string) (string, string, string) {
length := len(str)
// 计算每个子字符串的长度
subStrLen := length / 3
// 切分字符串
part1 := str[:subStrLen]
part2 := str[subStrLen : 2*subStrLen]
part3 := str[2*subStrLen:]
return part1, part2, part3
}
func bytes_halve(slice []byte) ([]byte, []byte) {
length := len(slice)
// 计算每个部分的长度
halfLength := length / 2
// 切分切片
part1 := slice[:halfLength]
part2 := slice[halfLength:]
return part1, part2
}
func bytes_trisect(slice []byte) ([]byte, []byte, []byte) {
length := len(slice)
// 计算每个部分的长度
partLength := length / 3
// 切分切片
part1 := slice[:partLength]
part2 := slice[partLength : 2*partLength]
part3 := slice[2*partLength:]
return part1, part2, part3
}
func mergeBigInts(bigInts ...*big.Int) []byte {
var mergedBytes []byte
for _, num := range bigInts {
buf := make([]byte, 32)
num.FillBytes(buf)
//fmt.Println("各个部分长度为:", len(numBytes))
mergedBytes = append(mergedBytes, buf...)
}
return mergedBytes
}
func readFileFromDirectory(directory, filename string) ([]byte, error) {
// 构造文件的完整路径
filePath := filepath.Join(directory, filename)
// 读取文件内容
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
return data, nil
}
func writeToFolder(directory, filename string, data []byte) error {
// 检查 cert 文件夹是否存在
if _, err := os.Stat(directory); os.IsNotExist(err) {
// 如果文件夹不存在,则创建它
err := os.Mkdir(directory, 0755)
if err != nil {
return fmt.Errorf("创建文件夹时出错: %v", err)
}
}
// 构造文件的完整路径
filePath := filepath.Join(directory, filename)
err := os.WriteFile(filePath, []byte(data), 0644)
if err != nil {
return fmt.Errorf("写入文件时出错: %v", err)
}
fmt.Println("字符串已成功写入文件:", filePath)
return nil
}
func BytesToBig(bytes []byte) *big.Int {
//不能直接转为big转为字符串后用16进制解码
big := new(big.Int)
big.SetString(string(bytes), 16)
return big
}
func AnalSig(sig []byte) (*big.Int, *big.Int, *big.Int) {
uxbytes, uybytes, vbytes := bytes_trisect(sig)
//fmt.Println("打印长度:", len(uxbytes))
//fmt.Println(len(uybytes))
Ux := new(big.Int).SetBytes(uxbytes)
Uy := new(big.Int).SetBytes(uybytes)
v := new(big.Int).SetBytes(vbytes)
return Ux, Uy, v
}

View File

@ -9,6 +9,7 @@ import (
"math/big"
"os"
"strings"
"sync"
kgc "schain/common/kgc"
pb "schain/proto"
@ -35,6 +36,8 @@ type ECDSASignature struct {
R, S *big.Int
}
var mutex sync.Mutex
// NewSigner 根据配置创建一个用户签名
func NewSigner(conf *Config) (*Signer, error) {
sId, err := kgc.InitIdentity(conf.IdentityPath, conf.MSPID)
@ -163,12 +166,41 @@ func (si *Signer) Serialize() ([]byte, error) {
return si.creator, nil
}
// 返回一个长度为 32 的字节数组,表示计算得到的 SHA256 校验和
// 返回KGC签名算法签名参数用户公私钥、原来的消息、用户id
func (si *Signer) Sign(msg []byte) ([]byte, error) {
digest := sha256.Sum256(msg)
return kgc.Certificateless_Sign(si.privateKey, digest[:])
//return signECDSA(si.privateKey, digest[:])
mutex.Lock()
defer mutex.Unlock()
prikey := si.privateKey
pubkey := si.publicKey
Creator, err := util.UnmarshalCreator(si.creator)
if err != nil {
return nil, err
}
ID := Creator.Mspid
return kgc.Certificateless_Sign(prikey, pubkey, msg, ID)
//return si.privateKey, nil
}
func Verify(creatorBytes []byte, signature, msg []byte) error {
mutex.Lock()
defer mutex.Unlock()
//fmt.Println("成功进入verify函数")
creator, err := util.UnmarshalCreator(creatorBytes)
if err != nil {
return err
}
publicKey := creator.IdBytes
ID := creator.Mspid
return kgc.Certificateless_Verify(publicKey, ID, signature, msg)
//return nil
}
// 验证 ECDSA 签名,此处用不到
@ -214,20 +246,6 @@ func Verify(creatorBytes []byte, signature, msg []byte) error {
}
*/
func Verify(creatorBytes []byte, signature, msg []byte) error {
creator, err := util.UnmarshalCreator(creatorBytes)
if err != nil {
return err
}
publicKey := creator.IdBytes
return kgc.Certificateless_Verify(publicKey, signature, msg)
}
/*
// 根据文件加载用户的私钥
func loadPrivateKey(file string) (*ecdsa.PrivateKey, error) {

View File

@ -34,7 +34,7 @@ organizations:
# - ":1408"
client:
MSPID: "*.wxy.com"
MSPID: "1234567890111213141516171819202122232425"
#IdentityPath: "cert/client.pem"
#KeyPath: "cert/client.key"
ClientPubPath: "cert/client_pubkey.txt"
@ -42,7 +42,7 @@ client:
peer:
address: ":1408"
MSPID: "*.wxy.com"
MSPID: "1234567890111213141516171819202122232425"
#IdentityPath: "cert/peer.pem"
#KeyPath: "cert/peer.key"
PeerPubPath: "cert/peer_pubkey.txt"
@ -50,7 +50,7 @@ peer:
order:
address: ":1409"
MSPID: "*.wxy.com"
MSPID: "1234567890111213141516171819202122232425"
#IdentityPath: "cert/order.pem"
#KeyPath: "cert/order.key"
OrderPubPath: "cert/order_pubkey.txt"

View File

@ -84,7 +84,7 @@ func (cs *ChaincodeServer) EndPeer(ctx context.Context, e *pb.Empty) (*pb.Empty,
latency := float64(totalLatency) / float64(SuccessNum)
fmt.Printf("吞吐量为:%f\n", throughput*successRatio)
fmt.Printf("延迟为: %f ms\n", latency)
fmt.Printf("成功率为: %f%%\n", successRatio*100)
//fmt.Printf("成功率为: %f%%\n", successRatio*100)
BlockNum = 0
SuccessNum = 0

View File

@ -70,7 +70,7 @@ func GetStateDB() {
if config.ContractName == "SmallBank" {
fmt.Println("账号数量为:", num)
fmt.Println("所有账户的总余额为:", valueNum)
//fmt.Println("所有账户的总余额为:", valueNum)
}
}

View File

@ -89,6 +89,7 @@ func validateEndorserTransaction(payload *pb.Payload, hdr *pb.SignatureHeader) (
fmt.Printf("验证失败!!!!!!!!!!!!!!!!!!!!")
return nil, err
}
//fmt.Println("验证提案签名成功 20210501")
}
kVRWSet, err := util.UnmarshalKVRWSet(ca.Results)
@ -218,7 +219,7 @@ func validateBlockMataData(block *pb.Block) error {
fmt.Println("验证bad bad bad bad bad bad")
return err
}
fmt.Println("验证成功!!!!!!!!!!!!!!!!!!!")
//fmt.Println("验证成功!!!!!!!!!!!!!!!!!!!")
return nil
}

View File

@ -3,7 +3,6 @@ package util
import (
"bytes"
"crypto/sha256"
"fmt"
pb "schain/proto"
"github.com/pkg/errors"
@ -347,7 +346,7 @@ func GetSignedProposal(proposal *pb.Proposal, signer Signer) (*pb.SignedProposal
}
signature, err := signer.Sign(proposalBytes)
fmt.Println("111111111signature proposal:", signature)
//fmt.Println("111111111signature proposal:", signature)
if err != nil {
return nil, err