-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate libsodium (Algorand's VRF lib) into Tendermint #4
Closed
Closed
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
10bcded
port Algorand's libsodium as VRF APIs
torao 122646c
port Algorand's libsodium as VRF APIs
torao 0295e0f
Merge branch 'feature/integrate_libsodium' of github.com:line/tenderm…
torao 1fecb81
fixed private key log output in case of error
torao f6e7728
Fix circleCI error
zemyblue c539829
Fix circleCI error(2)
zemyblue f009ced
Fix circleCI error(3)
zemyblue c63f848
Fix circleCI error(4)
zemyblue 5bf7df9
Merge branch 'upstream/master' into feature/integrate_libsodium
zemyblue e0251d4
fixed private key log output in case of error
torao b985b7b
merge
torao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "crypto/vrf/internal/vrf/libsodium"] | ||
path = crypto/vrf/internal/vrf/libsodium | ||
url = https://github.com/algorand/libsodium.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// This vrf package makes the VRF API in Algorand's libsodium C library available to golang. | ||
package vrf | ||
|
||
/* | ||
#cgo CFLAGS: -Wall -std=c99 | ||
#cgo CFLAGS: -I./include/ | ||
#cgo LDFLAGS: -L./lib -lsodium | ||
#include "sodium.h" | ||
*/ | ||
import "C" | ||
import ( | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"unsafe" | ||
) | ||
|
||
const PUBLICKEYBYTES = uint32(C.crypto_vrf_PUBLICKEYBYTES) | ||
|
||
const SECRETKEYBYTES = uint32(C.crypto_vrf_SECRETKEYBYTES) | ||
|
||
const SEEDBYTES = uint32(C.crypto_vrf_SEEDBYTES) | ||
|
||
const PROOFBYTES = uint32(C.crypto_vrf_PROOFBYTES) | ||
|
||
const OUTPUTBYTES = uint32(C.crypto_vrf_OUTPUTBYTES) | ||
|
||
const PRIMITIVE = C.crypto_vrf_PRIMITIVE | ||
|
||
// Generate an Ed25519 key pair for use with VRF. | ||
func KeyPair() (*[PUBLICKEYBYTES]byte, *[SECRETKEYBYTES]byte) { | ||
publicKey := [PUBLICKEYBYTES]byte{} | ||
privateKey := [SECRETKEYBYTES]byte{} | ||
publicKeyPtr := (*C.uchar)(unsafe.Pointer(&publicKey)) | ||
privateKeyPtr := (*C.uchar)(unsafe.Pointer(&privateKey)) | ||
C.crypto_vrf_keypair(publicKeyPtr, privateKeyPtr) | ||
return &publicKey, &privateKey | ||
} | ||
|
||
// Generate an Ed25519 key pair for use with VRF. Parameter `seed` means the cofactor in Curve25519 and EdDSA. | ||
func KeyPairFromSeed(seed *[SEEDBYTES]byte) (*[PUBLICKEYBYTES]byte, *[SECRETKEYBYTES]byte) { | ||
publicKey := [PUBLICKEYBYTES]byte{} | ||
privateKey := [SECRETKEYBYTES]byte{} | ||
publicKeyPtr := (*C.uchar)(unsafe.Pointer(&publicKey)) | ||
privateKeyPtr := (*C.uchar)(unsafe.Pointer(&privateKey)) | ||
seedPtr := (*C.uchar)(unsafe.Pointer(seed)) | ||
C.crypto_vrf_keypair_from_seed(publicKeyPtr, privateKeyPtr, seedPtr) | ||
return &publicKey, &privateKey | ||
} | ||
|
||
// Verifies that the specified public key is valid. | ||
func IsValidKey(publicKey *[PUBLICKEYBYTES]byte) bool { | ||
publicKeyPtr := (*C.uchar)(unsafe.Pointer(publicKey)) | ||
return C.crypto_vrf_is_valid_key(publicKeyPtr) != 0 | ||
} | ||
|
||
// Construct a VRF proof from given secret key and message. | ||
func Prove(privateKey *[SECRETKEYBYTES]byte, message []byte) (*[PROOFBYTES]byte, error) { | ||
proof := [PROOFBYTES]byte{} | ||
proofPtr := (*C.uchar)(unsafe.Pointer(&proof)) | ||
privateKeyPtr := (*C.uchar)(unsafe.Pointer(privateKey)) | ||
messagePtr := (*C.uchar)(unsafe.Pointer(&message)) | ||
messageLen := (C.ulonglong)(len(message)) | ||
if C.crypto_vrf_prove(proofPtr, privateKeyPtr, messagePtr, messageLen) != 0 { | ||
return nil, errors.New(fmt.Sprintf("unable to decode the given privateKey")) | ||
} | ||
return &proof, nil | ||
} | ||
|
||
// Verifies that proof was legitimately generated by private key for the given public key, and stores the | ||
// VRF hash in output. Note that VRF "verify()" means the process of generating output from public key, | ||
// proof, and message. | ||
// https://tools.ietf.org/html/draft-irtf-cfrg-vrf-04#section-5.3 | ||
func Verify(publicKey *[PUBLICKEYBYTES]byte, proof *[PROOFBYTES]byte, message []byte) (*[OUTPUTBYTES]byte, error) { | ||
output := [OUTPUTBYTES]byte{} | ||
outputPtr := (*C.uchar)(unsafe.Pointer(&output)) | ||
publicKeyPtr := (*C.uchar)(unsafe.Pointer(publicKey)) | ||
proofPtr := (*C.uchar)(unsafe.Pointer(proof)) | ||
messagePtr := (*C.uchar)(unsafe.Pointer(&message)) | ||
messageLen := (C.ulonglong)(len(message)) | ||
if C.crypto_vrf_verify(outputPtr, publicKeyPtr, proofPtr, messagePtr, messageLen) != 0 { | ||
return nil, errors.New(fmt.Sprintf( | ||
"given public key is invalid, or the proof isn't legitimately generated for the message:"+ | ||
" public_key=%s, proof=%s, message=%s", | ||
hex.EncodeToString(publicKey[:]), hex.EncodeToString(proof[:]), hex.EncodeToString(message[:]))) | ||
} | ||
return &output, nil | ||
} | ||
|
||
// Calculate the output (hash value) from the specified proof. | ||
// In essence, this function returns a valid value if given proof is any point on the finite field. Otherwise, | ||
// this will return an error. | ||
func ProofToHash(proof *[PROOFBYTES]byte) (*[OUTPUTBYTES]byte, error) { | ||
output := [OUTPUTBYTES]byte{} | ||
outputPtr := (*C.uchar)(unsafe.Pointer(&output)) | ||
proofPtr := (*C.uchar)(unsafe.Pointer(proof)) | ||
if C.crypto_vrf_proof_to_hash(outputPtr, proofPtr) != 0 { | ||
return nil, errors.New(fmt.Sprintf( | ||
"given proof isn't legitimately generated: proof=%s", hex.EncodeToString(proof[:]))) | ||
} | ||
return &output, nil | ||
} | ||
|
||
func SkToPk(privateKey *[SECRETKEYBYTES]byte) *[PUBLICKEYBYTES]byte { | ||
publicKey := [PUBLICKEYBYTES]byte{} | ||
publicKeyPtr := (*C.uchar)(unsafe.Pointer(&publicKey)) | ||
privateKeyPtr := (*C.uchar)(unsafe.Pointer(privateKey)) | ||
C.crypto_vrf_sk_to_pk(publicKeyPtr, privateKeyPtr) // void | ||
return &publicKey | ||
} | ||
|
||
func SkToSeed(privateKey *[SECRETKEYBYTES]byte) *[SEEDBYTES]byte { | ||
seed := [SEEDBYTES]byte{} | ||
seedPtr := (*C.uchar)(unsafe.Pointer(&seed)) | ||
privateKeyPtr := (*C.uchar)(unsafe.Pointer(privateKey)) | ||
C.crypto_vrf_sk_to_seed(seedPtr, privateKeyPtr) // void | ||
return &seed | ||
} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that VRF has four functions(hash(), prove(), proofToHash(), verify()). But I cannot see hash() function in this file. Must the prover use proofToHash() rather than hash() to get beta?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is. This PR is to integrate the libsodium API and the library doesn't provide a
hash()
function. I think this is becausehash()
can be composited fromprove()
andhash_to_poof()
. This probably follows the IETF policy.