Merge pull request #460 from harmony-one/rj_temp

Remove keytransparency and trillian dependency, we actually doesn't necessarily need them.
pull/462/head
Rongjian Lan 6 years ago committed by GitHub
commit e64820f2a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      crypto/vrf/p256/p256.go
  2. 121
      crypto/vrf/p256/p256_test.go
  3. 28
      test/testdata/directory.json
  4. 2137
      test/testdata/getentryresponse.json

@ -22,7 +22,6 @@ package p256
import ( import (
"bytes" "bytes"
"context"
"crypto" "crypto"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
@ -34,13 +33,9 @@ import (
"encoding/binary" "encoding/binary"
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt"
"math/big" "math/big"
"github.com/google/trillian/crypto/keys"
"github.com/harmony-one/harmony/crypto/vrf" "github.com/harmony-one/harmony/crypto/vrf"
"github.com/golang/protobuf/proto"
) )
var ( var (
@ -229,23 +224,6 @@ func (pk *PublicKey) ProofToHash(m, proof []byte) (index [32]byte, err error) {
return sha256.Sum256(vrf), nil return sha256.Sum256(vrf), nil
} }
// NewFromWrappedKey creates a VRF signer object from an encrypted private key.
// The opaque private key must resolve to an `ecdsa.PrivateKey` in order to work.
func NewFromWrappedKey(ctx context.Context, wrapped proto.Message) (vrf.PrivateKey, error) {
// Unwrap.
signer, err := keys.NewSigner(ctx, wrapped)
if err != nil {
return nil, err
}
switch key := signer.(type) {
case *ecdsa.PrivateKey:
return NewVRFSigner(key)
default:
return nil, fmt.Errorf("wrapped key has wrong type: %T, want ecdsa.PrivateKey", key)
}
}
// NewVRFSigner creates a signer object from a private key. // NewVRFSigner creates a signer object from a private key.
func NewVRFSigner(key *ecdsa.PrivateKey) (vrf.PrivateKey, error) { func NewVRFSigner(key *ecdsa.PrivateKey) (vrf.PrivateKey, error) {
if *(key.Params()) != *curve.Params() { if *(key.Params()) != *curve.Params() {

@ -16,24 +16,10 @@ package p256
import ( import (
"bytes" "bytes"
"context"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"encoding/json"
"io/ioutil"
"math" "math"
"os"
"testing" "testing"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/google/keytransparency/core/testdata"
"github.com/google/trillian/crypto/keys"
"github.com/google/trillian/crypto/keys/der"
"github.com/google/trillian/crypto/keyspb"
pb "github.com/google/keytransparency/core/api/v1/keytransparency_go_proto"
_ "github.com/google/trillian/crypto/keys/der/proto"
) )
const ( const (
@ -80,76 +66,6 @@ func TestH2(t *testing.T) {
} }
} }
func TestNewFromWrappedKey(t *testing.T) {
ctx := context.Background()
for _, tc := range []struct {
desc string
wantFromWrappedErr bool
spec *keyspb.Specification
keygen keys.ProtoGenerator
}{
{
desc: "DER with ECDSA spec",
spec: &keyspb.Specification{
Params: &keyspb.Specification_EcdsaParams{
EcdsaParams: &keyspb.Specification_ECDSA{
Curve: keyspb.Specification_ECDSA_P256,
},
},
},
keygen: func(ctx context.Context, spec *keyspb.Specification) (proto.Message, error) {
return der.NewProtoFromSpec(spec)
},
},
{
desc: "DER with Non-ECDSA spec",
wantFromWrappedErr: true,
spec: &keyspb.Specification{
Params: &keyspb.Specification_RsaParams{
RsaParams: &keyspb.Specification_RSA{Bits: 2048},
},
},
keygen: func(ctx context.Context, spec *keyspb.Specification) (proto.Message, error) {
return der.NewProtoFromSpec(spec)
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
// Generate VRF key.
wrapped, err := tc.keygen(ctx, tc.spec)
if err != nil {
t.Fatalf("keygen failed: %v", err)
}
vrfPriv, err := NewFromWrappedKey(ctx, wrapped)
if got, want := err != nil, tc.wantFromWrappedErr; got != want {
t.Errorf("NewFromWrappedKey (): %v, want err: %v", err, want)
}
if err != nil {
return
}
vrfPubDER, err := der.MarshalPublicKey(vrfPriv.Public())
if err != nil {
t.Fatalf("MarshalPublicKey failed: %v", err)
}
vrfPub, err := NewVRFVerifierFromRawKey(vrfPubDER)
if err != nil {
t.Fatalf("NewVRFVerifierFromRawKey(): %v", err)
}
// Test that the public and private components match.
m := []byte("foobar")
indexA, proof := vrfPriv.Evaluate(m)
indexB, err := vrfPub.ProofToHash(m, proof)
if err != nil {
t.Fatalf("ProofToHash(): %v", err)
}
if got, want := indexB, indexA; got != want {
t.Errorf("ProofToHash(%s, %x): %x, want %x", m, proof, got, want)
}
})
}
}
func TestVRF(t *testing.T) { func TestVRF(t *testing.T) {
k, pk := GenerateKey() k, pk := GenerateKey()
@ -184,43 +100,6 @@ func TestVRF(t *testing.T) {
} }
} }
// Test vectors in core/testdata are generated by running
// go generate ./core/testdata
func TestProofToHash(t *testing.T) {
directoryFile := "../../../test/testdata/directory.json"
f, err := os.Open(directoryFile)
if err != nil {
t.Fatalf("ReadFile(%v): %v", directoryFile, err)
}
defer f.Close()
var directory pb.Directory
if err := jsonpb.Unmarshal(f, &directory); err != nil {
t.Fatalf("jsonpb.Unmarshal(): %v", err)
}
pk, err := NewVRFVerifierFromRawKey(directory.GetVrf().GetDer())
if err != nil {
t.Fatalf("NewVRFVerifier failure: %v", err)
}
respFile := "../../../test/testdata/getentryresponse.json"
b, err := ioutil.ReadFile(respFile)
if err != nil {
t.Fatalf("ReadFile(%v): %v", respFile, err)
}
var getUserResponses []testdata.ResponseVector
if err := json.Unmarshal(b, &getUserResponses); err != nil {
t.Fatalf("Unmarshal(): %v", err)
}
for _, tc := range getUserResponses {
t.Run(tc.Desc, func(t *testing.T) {
_, err := pk.ProofToHash([]byte(tc.UserIDs[0]), tc.GetUserResp.GetLeaf().GetVrfProof())
if err != nil {
t.Errorf("ProofToHash(%v): %v)", tc.Desc, err)
}
})
}
}
func TestReadFromOpenSSL(t *testing.T) { func TestReadFromOpenSSL(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
priv string priv string

@ -1,28 +0,0 @@
{
"directoryId": "integration",
"log": {
"treeId": "6511398593182094144",
"treeType": "PREORDERED_LOG",
"hashStrategy": "RFC6962_SHA256",
"hashAlgorithm": "SHA256",
"signatureAlgorithm": "ECDSA",
"publicKey": {
"der": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEqGXPnhMIclRmYHSmAnCMmfDUJ9iNBMmFxR/wHJdL12AuVUkgcuhbEp2hy5ETs7bfFc2P95IYFlmbiuHMq3UY/A=="
}
},
"map": {
"treeId": "7627063266021945174",
"treeType": "MAP",
"hashStrategy": "CONIKS_SHA256",
"hashAlgorithm": "SHA256",
"signatureAlgorithm": "ECDSA",
"publicKey": {
"der": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWLHm0TLYaTzENpPkBl2E79ySqJI+EW51VpoWh7wqY3OjSJcft4zgEeNeHYEb/T2jBFH4eYg4iSN7D/VYaJxJRA=="
}
},
"vrf": {
"der": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEF2Pm2kKya+JBun1QRmKQMcoMOIBNWp8fjECkJX+/hNWdV1UKb12W+yXcX2MqN7ZMX77hS9mLus/WaE0NS370mA=="
},
"minInterval": "0.100s",
"maxInterval": "216000s"
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save