-
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
Import libsodium (Algorand's VRF lib) into Tendermint #12
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,125 @@ | ||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about collecting all similar constant values like below? const (
PUBLICKEYBYTES = uint32(C.crypto_vrf_PUBLICKEYBYTES)
SECRETKEYBYTES = uint32(C.crypto_vrf_SECRETKEYBYTES)
SEEDBYTES = uint32(C.crypto_vrf_SEEDBYTES)
PROOFBYTES = uint32(C.crypto_vrf_PROOFBYTES)
OUTPUTBYTES = uint32(C.crypto_vrf_OUTPUTBYTES)
PRIMITIVE = C.crypto_vrf_PRIMITIVE
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it seems better readability. I'll fix it. |
||
|
||
// 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 := bytesToUnsignedCharPointer(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 := bytesToUnsignedCharPointer(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 | ||
} | ||
|
||
func bytesToUnsignedCharPointer(msg []byte) *C.uchar { | ||
if len(msg) == 0 { | ||
return (*C.uchar)(C.NULL) | ||
} | ||
return (*C.uchar)(unsafe.Pointer(&msg[0])) | ||
} |
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.
How about adding our license comment?
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.
Then we need to decide on a license declaration for our new files. I'll save it as another discussion ticket.