Skip to content

Commit

Permalink
Change ProofLength() to const ProofSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Mdaiki0730 committed Nov 28, 2022
1 parent 409c903 commit b44ab05
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 28 deletions.
4 changes: 3 additions & 1 deletion crypto/vrf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ VRF implementation is set by `func init()` with `build` option
type vrfEd25519 interface {
Prove(privateKey []byte, message []byte) (Proof, error)
Verify(publicKey []byte, proof Proof, message []byte) (bool, error)
ProofLength() int
ProofToHash(proof Proof) (Output, error)
```
Expand All @@ -26,17 +25,20 @@ Use `func init()` with `build` option
* `//go:build libsodium`
* `// +build !libsodium,!coniks`
* `func init() { defaultVrf = newVrfEd25519r2ishiguro() }`
* `const ProofSize = 81`
* vrf_r2ishiguro.go
* (coniks)
* `//go:build coniks`
* `// +build coniks`
* `func init() { defaultVrf = newVrfEd25519coniks() }`
* `const ProofSize = 96`
* vrf_coniks.go
* vrf_coniks_test.go
* (libsodium)
* `//go:build libsodium`
* `// +build libsodium`
* `func init() { defaultVrf = newVrfEd25519libsodium() }`
* `const ProofSize = int(libsodium.PROOFBYTES)`
* vrf_libsodium.go
* vrf_libsodium_test.go

Expand Down
5 changes: 0 additions & 5 deletions crypto/vrf/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type Output []byte
type vrfEd25519 interface {
Prove(privateKey []byte, message []byte) (Proof, error)
Verify(publicKey []byte, proof Proof, message []byte) (bool, error)
ProofLength() int
ProofToHash(proof Proof) (Output, error)
}

Expand All @@ -34,10 +33,6 @@ func Verify(publicKey []byte, proof Proof, message []byte) (bool, error) {
return defaultVrf.Verify(publicKey, proof, message)
}

func ProofLength() int {
return defaultVrf.ProofLength()
}

func ProofToHash(proof Proof) (Output, error) {
return defaultVrf.ProofToHash(proof)
}
6 changes: 1 addition & 5 deletions crypto/vrf/vrf_coniks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
defaultVrf = newVrfEd25519coniks()
}

const ConiksProofLength = 96
const ProofSize = 96

func newVrfEd25519coniks() *vrfEd25519coniks {
return &vrfEd25519coniks{nil, nil}
Expand Down Expand Up @@ -56,10 +56,6 @@ func (base *vrfEd25519coniks) Verify(publicKey []byte, proof Proof, message []by
return coniksPubKey.Verify(message, base.generatedHash, proof), nil
}

func (base *vrfEd25519coniks) ProofLength() int {
return ConiksProofLength
}

func (base *vrfEd25519coniks) ProofToHash(proof Proof) (Output, error) {
if base.generatedHash == nil {
return nil, errors.New("vrf hash was not given")
Expand Down
6 changes: 1 addition & 5 deletions crypto/vrf/vrf_libsodium.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
defaultVrf = newVrfEd25519libsodium()
}

const LibsodiumProofLength = 80
const ProofSize = int(libsodium.PROOFBYTES)

func newVrfEd25519libsodium() vrfEd25519libsodium {
return vrfEd25519libsodium{}
Expand Down Expand Up @@ -48,10 +48,6 @@ func (base vrfEd25519libsodium) Verify(publicKey []byte, proof Proof, message []
return bytes.Compare(op[:], hash) == 0, nil
}

func (base vrfEd25519libsodium) ProofLength() int {
return LibsodiumProofLength
}

func (base vrfEd25519libsodium) ProofToHash(proof Proof) (Output, error) {
op, err := libsodium.ProofToHash(toArray(proof))
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions crypto/vrf/vrf_r2ishiguro.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}
}

const R2ishiguroProofLength = 81
const ProofSize = 81

func newVrfEd25519r2ishiguro() vrfEd25519r2ishiguro {
return vrfEd25519r2ishiguro{}
Expand All @@ -34,10 +34,6 @@ func (base vrfEd25519r2ishiguro) Verify(publicKey []byte, proof Proof, message [
return r2ishiguro.ECVRF_verify(publicKey, proof, message)
}

func (base vrfEd25519r2ishiguro) ProofLength() int {
return R2ishiguroProofLength
}

func (base vrfEd25519r2ishiguro) ProofToHash(proof Proof) (Output, error) {
// validate proof with ECVRF_decode_proof
_, _, _, err := r2ishiguro.ECVRF_decode_proof(proof)
Expand Down
2 changes: 1 addition & 1 deletion evidence/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ func makeHeaderRandom(height int64) *types.Header {
LastResultsHash: crypto.CRandBytes(tmhash.Size),
EvidenceHash: crypto.CRandBytes(tmhash.Size),
ProposerAddress: crypto.CRandBytes(crypto.AddressSize),
Proof: crypto.CRandBytes(vrf.ProofLength()),
Proof: crypto.CRandBytes(vrf.ProofSize),
}
}

Expand Down
6 changes: 3 additions & 3 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func TestHeaderValidateBasic(t *testing.T) {
header.Round = -1
}, true},
{"Invalid Proof", func(header *Header) {
header.Proof = make([]byte, vrf.ProofLength()-1)
header.Proof = make([]byte, vrf.ProofSize-1)
}, true},
{"Invalid Validators Hash", func(header *Header) {
header.ValidatorsHash = []byte(strings.Repeat("h", invalidHashLength))
Expand All @@ -671,7 +671,7 @@ func TestHeaderValidateBasic(t *testing.T) {
EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
ProposerAddress: crypto.AddressHash([]byte("proposer_address")),
Round: 1,
Proof: make([]byte, vrf.ProofLength()),
Proof: make([]byte, vrf.ProofSize),
}
tc.malleateHeader(header)
err := header.ValidateBasic()
Expand Down Expand Up @@ -1279,7 +1279,7 @@ func makeRandHeader() Header {
height := tmrand.Int63()
randBytes := tmrand.Bytes(tmhash.Size)
randAddress := tmrand.Bytes(crypto.AddressSize)
randProof := tmrand.Bytes(vrf.ProofLength())
randProof := tmrand.Bytes(vrf.ProofSize)
h := Header{
Version: tmversion.Consensus{Block: version.BlockProtocol, App: 1},
ChainID: chainID,
Expand Down
6 changes: 3 additions & 3 deletions types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func ValidateHash(h []byte) error {
}

// ValidateProof returns an error if the proof is not empty, but its
// size != vrf.ProofLength().
// size != vrf.ProofSize.
func ValidateProof(h []byte) error {
if len(h) > 0 && len(h) != vrf.ProofLength() {
if len(h) > 0 && len(h) != vrf.ProofSize {
return fmt.Errorf("expected size to be %d bytes, got %d bytes",
vrf.ProofLength(),
vrf.ProofSize,
len(h),
)
}
Expand Down

0 comments on commit b44ab05

Please sign in to comment.