This commit is contained in:
bryanqiu 2022-11-15 15:18:16 +08:00
parent a1ade9e06f
commit 8d97cdae89

42
zkp.go
View File

@ -15,9 +15,21 @@ import (
) )
type DLogProof struct { type DLogProof struct {
ChallengeResponse *big_Int `json:"cr"` ChallengeResponse *big_Int `json:"cr"`
ChallengeResponseXXXX *big.Int `json:"cr___TODO______debug"` RandomPoint string `json:"pkr"`
RandomPoint string `json:"pkr"` }
func (dlogproof *DLogProof) ToString() string {
var b, _ = json.Marshal(dlogproof)
return string(b)
}
func DLogProofFromString(jsonstr string) (*DLogProof, error) {
var proof = DLogProof{}
if err := json.Unmarshal([]byte(jsonstr), &proof); err != nil {
return nil, err
}
return &proof, nil
} }
/* /*
@ -48,7 +60,8 @@ func DLogProve(sk *big.Int) (proof *DLogProof, err error) {
// Step 2. caculate challenge: c = SHA256(pkr || G || pk) // Step 2. caculate challenge: c = SHA256(pkr || G || pk)
challenge := fmt.Sprintf( challenge := fmt.Sprintf(
"%x,%x@%x,%x@%x,%x", pkr.X, pkr.Y, Gx, Gy, pk.X, pk.Y) "%064x,%064x@%064x,%064x@%064x,%064x",
pkr.X, pkr.Y, Gx, Gy, pk.X, pk.Y)
chash256 := sha256.Sum256([]byte(challenge)) chash256 := sha256.Sum256([]byte(challenge))
var c = new(big.Int).SetBytes(chash256[:]) var c = new(big.Int).SetBytes(chash256[:])
log.Debugf("challenge='%s',c='%x'", challenge, c) log.Debugf("challenge='%s',c='%x'", challenge, c)
@ -61,9 +74,8 @@ func DLogProve(sk *big.Int) (proof *DLogProof, err error) {
// return // return
proof = &DLogProof{ proof = &DLogProof{
RandomPoint: fmt.Sprintf("%x,%x", pkr.X, pkr.Y), RandomPoint: fmt.Sprintf("%x,%x", pkr.X, pkr.Y),
ChallengeResponse: (*big_Int)(cr), ChallengeResponse: (*big_Int)(cr),
ChallengeResponseXXXX: cr,
} }
return proof, nil return proof, nil
} }
@ -93,7 +105,8 @@ func DLogVerify(pkstr string, proof *DLogProof) bool {
Gx := cv.Params().Gx Gx := cv.Params().Gx
Gy := cv.Params().Gy Gy := cv.Params().Gy
challenge := fmt.Sprintf( challenge := fmt.Sprintf(
"%x,%x@%x,%x@%x,%x", pkr.X, pkr.Y, Gx, Gy, pk.X, pk.Y) "%064x,%064x@%064x,%064x@%064x,%064x",
pkr.X, pkr.Y, Gx, Gy, pk.X, pk.Y)
chash256 := sha256.Sum256([]byte(challenge)) chash256 := sha256.Sum256([]byte(challenge))
var c = new(big.Int).SetBytes(chash256[:]) var c = new(big.Int).SetBytes(chash256[:])
var cr = (*big.Int)(proof.ChallengeResponse) var cr = (*big.Int)(proof.ChallengeResponse)
@ -129,16 +142,3 @@ func HexStringToS256Point(pstr string) (*ecdsa.PublicKey, error) {
} }
return &pk, nil return &pk, nil
} }
func (dlogproof *DLogProof) ToString() string {
var b, _ = json.Marshal(dlogproof)
return string(b)
}
func DLogProofFromString(jsonstr string) (*DLogProof, error) {
var proof = DLogProof{}
if err := json.Unmarshal([]byte(jsonstr), &proof); err != nil {
return nil, err
}
return &proof, nil
}