vechain / go-ecvrf

Elliptic Curve Verifiable Radom Function(ECVRF) library written in Go
MIT License
8 stars 11 forks source link

Verify could not pass #5

Closed xumy29 closed 6 months ago

xumy29 commented 1 year ago

It does not work for me.

package main

import (
    "crypto/ecdsa"
    "crypto/elliptic"
    "crypto/rand"
    "fmt"
    "reflect"

    "github.com/vechain/go-ecvrf"
)

type VRFResult struct {
    Proof       []byte 
    RandomValue []byte
}

func GenerateVRF(privateKey *ecdsa.PrivateKey, input []byte) *VRFResult {
    output, proof, err := ecvrf.Secp256k1Sha256Tai.Prove(privateKey, input)
    if err != nil {
        // log.Error("GenerateVRF fail", "err", err)
        fmt.Printf("GenerateVRF fail, err: %v\n", err)
    }
    return &VRFResult{
        Proof:       proof,
        RandomValue: output,
    }
}

func VerifyVRF(publicKey *ecdsa.PublicKey, input []byte, vrfResult *VRFResult) bool {
    output, err := ecvrf.Secp256k1Sha256Tai.Verify(publicKey, input, vrfResult.Proof)
    if err != nil {
        // log.Error("VerifyVRF fail", "err", err)
        fmt.Printf("VerifyVRF fail, err: %v\n", err)
    }

    return reflect.DeepEqual(output, vrfResult.RandomValue)
}

func main() {
    curve := elliptic.P256()

    privateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
    if err != nil {
        // log.Error("generate private key fail", "err", err)
    }

        inputData := []byte("This is some input data.")

        vrfResult := GenerateVRF(privateKey, inputData)

        fmt.Printf("VRF Proof: %x\n", vrfResult.Proof)
        fmt.Printf("Random Value: %x\n", vrfResult.RandomValue)

        isValid := VerifyVRF(&privateKey.PublicKey, inputData, vrfResult)
        fmt.Println("VRF Verification:", isValid)
}
qianbin commented 1 year ago

The generated private key is on P256 curve, which does not match secp256k1 VRF.

Do so to generate secp256k1 pk.

       s, err := secp256k1.GeneratePrivateKey()
    if err != nil {
        // log.Error("generate private key fail", "err", err)
    }
    privateKey := s.ToECDSA()
xumy29 commented 1 year ago

It works now, thanks! @qianbin