Coder Social home page Coder Social logo

go-ecvrf's People

Contributors

laalaguer avatar qianbin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-ecvrf's Issues

panic: math/big: buffer too small to fit value when using ecvrf.Secp256k1Sha256Tai.Prove

Thanks for your contributions of the implementation of ecvrf.

When I'm using ecvrf.Secp256k1Sha256Tai.Prove in my code, a panic comes:

`panic: math/big: buffer too small to fit value

goroutine 1 [running]:
math/big.nat.bytes(...)
/usr/local/go1.18/src/math/big/nat.go:1166
math/big.(*Int).FillBytes(0xc00007c8e0, {0xc000022961, 0x20, 0x20})
/usr/local/go1.18/src/math/big/int.go:466 +0xf8
crypto/elliptic.MarshalCompressed({0x63dbf8?, 0x6e7558?}, 0xc0000b3d30?, 0x47fcbb?)
/usr/local/go1.18/src/crypto/elliptic/elliptic.go:382 +0xb6
github.com/vechain/go-ecvrf.(*core).Marshal(...)
/home/flybear/go/pkg/mod/github.com/vechain/[email protected]/core.go:46
github.com/vechain/go-ecvrf.(*core).HashToCurveTryAndIncrement(0xc0000b3e28, 0xc0000b3e18, {0xc0000242e0, 0x20, 0x20})
/home/flybear/go/pkg/mod/github.com/vechain/[email protected]/core.go:94 +0xca
github.com/vechain/go-ecvrf.(*vrf).Prove(0x63d640?, 0xc0000a0720, {0xc0000242e0, 0x20, 0x20})
/home/flybear/go/pkg/mod/github.com/vechain/[email protected]/vrf.go:92 +0xd5
main.main()
/home/flybear/Code/new/main.go:73 +0x137
exit status 2`

I am confused whether there is any wrong operations in my code.

ecdsa private key

-----BEGIN PRIVATE KEY----- MIGkAgEBBDCwC9t6gX86b+U3LhLQb87W+B6HSSq7+oXXcd2Arya0s0Fxf6kS/zbv R3al96T8QxigBwYFK4EEACKhZANiAAQMv937zAcd6ZnrU//ZivqFMuoScQ4+vKP8 7P/6MLPXpzh53fHDdyHim815OHU7adCN/lT8ARZO2TXEoxYezZojqPsUdCS6LSVe eIEfJ7qdf+E2oLiLubzKamwpxj0b2WI= -----END PRIVATE KEY-----

data(string form) for prove

\xd7\x03\xb2\xfb\x04\xda\x1c\x03\xab\xa0dI\x96\x98\xc5Uru \xb5V\xdasWͽ\xa4\xf5k\xa5\xbdN`

Verify could not pass

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)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.