Skip to content

Commit

Permalink
update (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: bytemare <[email protected]>
  • Loading branch information
bytemare authored Jan 9, 2023
1 parent 77e4fa5 commit 3717de0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
12 changes: 6 additions & 6 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func exchangeWithServer(blinded []byte, verifiable bool) []byte {
privateKey, _ := hex.DecodeString("8132542d5ed08594e7522b5eac6bee38bab5868996c25a3fd2a7739be1856b04")

if verifiable {
server, err = voprf.RistrettoSha512.Server(voprf.VOPRF, privateKey)
server, err = voprf.Ristretto255Sha512.Server(voprf.VOPRF, privateKey)
if err != nil {
panic(err)
}
} else {
server, err = voprf.RistrettoSha512.Server(voprf.OPRF, privateKey)
server, err = voprf.Ristretto255Sha512.Server(voprf.OPRF, privateKey)
if err != nil {
panic(err)
}
Expand All @@ -45,7 +45,7 @@ func ExampleClient() {
input := []byte("input")

// Set up a new client. Not indicating a server public key indicates we don't use the verifiable mode.
client, err := voprf.RistrettoSha512.Client(voprf.OPRF, nil)
client, err := voprf.Ristretto255Sha512.Client(voprf.OPRF, nil)
if err != nil {
panic(err)
}
Expand All @@ -71,7 +71,7 @@ func ExampleClient() {
}

func Example_verifiableClient() {
ciphersuite := voprf.RistrettoSha512
ciphersuite := voprf.Ristretto255Sha512
input := []byte("input")
serverPubKey, _ := hex.DecodeString("066c39841db2ca3c2e83e251e71b619013674149692ca2ab41d1b33a1a4fff38")

Expand Down Expand Up @@ -107,7 +107,7 @@ func Example_baseServer() {
blinded, _ := hex.DecodeString("7eaf3d7cbe43d54637274342ce53578b2aba836f297f4f07997a6e1dced1c058")

// Set up a new server. A private key is automatically created if none is given.
server, err := voprf.RistrettoSha512.Server(voprf.OPRF, nil)
server, err := voprf.Ristretto255Sha512.Server(voprf.OPRF, nil)
if err != nil {
panic(err)
}
Expand All @@ -130,7 +130,7 @@ func Example_verifiableServer() {
blinded, _ := hex.DecodeString("7eaf3d7cbe43d54637274342ce53578b2aba836f297f4f07997a6e1dced1c058")

// Set up a new server.
server, err := voprf.RistrettoSha512.Server(voprf.VOPRF, privateKey)
server, err := voprf.Ristretto255Sha512.Server(voprf.VOPRF, privateKey)
if err != nil {
panic(err)
}
Expand Down
72 changes: 38 additions & 34 deletions oprf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const (
type Identifier string

const (
// RistrettoSha512 is the OPRF cipher suite of the Ristretto255 group and SHA-512.
RistrettoSha512 Identifier = "ristretto255-SHA512"
// Ristretto255Sha512 is the OPRF cipher suite of the Ristretto255 group and SHA-512.
Ristretto255Sha512 Identifier = "ristretto255-SHA512"

// Decaf448Sha512 is the OPRF cipher suite of the Decaf448 group and SHA-512.
// decaf448Sha512 Identifier = "decaf448-SHAKE256".
Expand Down Expand Up @@ -68,42 +68,46 @@ var (
hashes = make(map[Identifier]hash.Hashing, nbIDs)
)

func (c Identifier) new(mode Mode) *oprf {
func (i Identifier) new(mode Mode) *oprf {
return &oprf{
hash: hashes[c].Get(),
contextString: contextString(mode, c),
id: c,
hash: hashes[i].Get(),
contextString: contextString(mode, i),
id: i,
mode: mode,
group: groups[c],
group: groups[i],
}
}

// Available returns whether the Identifier is registered and available for usage.
func (c Identifier) Available() bool {
func (i Identifier) Available() bool {
// Check for invalid identifiers
switch c {
case RistrettoSha512, P256Sha256, P384Sha384, P521Sha512:
switch i {
case Ristretto255Sha512, P256Sha256, P384Sha384, P521Sha512:
break
default:
return false
}

// Check for unregistered groups
if _, ok := groups[c]; !ok {
// Check for unregistered groups and hashes
if _, ok := groups[i]; !ok {
return false
}

if _, ok := hashes[i]; !ok {
return false
}

return true
}

// Group returns the group identifier used in the cipher suite.
func (c Identifier) Group() group.Group {
return groups[c]
func (i Identifier) Group() group.Group {
return groups[i]
}

// Hash returns the hash function identifier used in the cipher suite.
func (c Identifier) Hash() hash.Hashing {
return hashes[c]
func (i Identifier) Hash() hash.Hashing {
return hashes[i]
}

// FromGroup returns a (V)OPRF Identifier given a Group Identifier.
Expand All @@ -118,24 +122,24 @@ func FromGroup(g group.Group) (Identifier, error) {
}

// KeyGen returns a fresh KeyPair for the given cipher suite.
func (c Identifier) KeyGen() *KeyPair {
sk := c.Group().NewScalar().Random()
pk := c.Group().Base().Multiply(sk)
func (i Identifier) KeyGen() *KeyPair {
sk := i.Group().NewScalar().Random()
pk := i.Group().Base().Multiply(sk)

return &KeyPair{
ID: c,
ID: i,
PublicKey: pk.Encode(),
SecretKey: sk.Encode(),
}
}

// Client returns a (P|V)OPRF client. For the OPRF mode, serverPublicKey should be nil, and non-nil otherwise.
func (c Identifier) Client(mode Mode, serverPublicKey []byte) (*Client, error) {
func (i Identifier) Client(mode Mode, serverPublicKey []byte) (*Client, error) {
if mode != OPRF && mode != VOPRF && mode != POPRF {
return nil, errParamInvalidMode
}

client := c.client(mode)
client := i.client(mode)

if mode == VOPRF || mode == POPRF {
if serverPublicKey == nil {
Expand All @@ -152,12 +156,12 @@ func (c Identifier) Client(mode Mode, serverPublicKey []byte) (*Client, error) {

// Server returns a (P|V)OPRF server instantiated with the given encoded private key.
// If privateKey is nil, a new private/public key pair is created.
func (c Identifier) Server(mode Mode, privateKey []byte) (*Server, error) {
func (i Identifier) Server(mode Mode, privateKey []byte) (*Server, error) {
if mode != OPRF && mode != VOPRF && mode != POPRF {
return nil, errParamInvalidMode
}

return c.server(mode, privateKey)
return i.server(mode, privateKey)
}

type oprf struct {
Expand Down Expand Up @@ -209,11 +213,11 @@ func (o *oprf) HashToScalar(data []byte) *group.Scalar {
return o.group.HashToScalar(data, dst(hash2scalarDSTPrefix, o.contextString))
}

func (c Identifier) client(mode Mode) *Client {
func (i Identifier) client(mode Mode) *Client {
return &Client{
tweakedKey: nil,
serverPublicKey: nil,
oprf: c.new(mode),
oprf: i.new(mode),
input: nil,
blind: nil,
blindedElement: nil,
Expand All @@ -235,11 +239,11 @@ func (c *Client) setServerPublicKey(serverPublicKey []byte) error {
return nil
}

func (c Identifier) server(mode Mode, privateKey []byte) (*Server, error) {
func (i Identifier) server(mode Mode, privateKey []byte) (*Server, error) {
s := &Server{
privateKey: nil,
publicKey: nil,
oprf: c.new(mode),
oprf: i.new(mode),
nonceR: nil,
}

Expand Down Expand Up @@ -284,21 +288,21 @@ func (o *oprf) hashTranscript(input, info, unblinded []byte) []byte {
}

// String implements the Stringer() interface for the Identifier.
func (c Identifier) String() string {
return string(c)
func (i Identifier) String() string {
return string(i)
}

func (c Identifier) register(g group.Group, h hash.Hashing) {
func (i Identifier) register(g group.Group, h hash.Hashing) {
if g.Available() && h.Available() {
groups[c] = g
hashes[c] = h
groups[i] = g
hashes[i] = h
} else {
panic(fmt.Sprintf("OPRF dependencies not available - Group: %v, Hash: %v", g.Available(), h.Available()))
}
}

func init() {
RistrettoSha512.register(group.Ristretto255Sha512, hash.SHA512)
Ristretto255Sha512.register(group.Ristretto255Sha512, hash.SHA512)
// Decaf448Sha512.register(group.Curve448Sha512, hash.SHA512).
P256Sha256.register(group.P256Sha256, hash.SHA256)
P384Sha384.register(group.P384Sha384, hash.SHA384)
Expand Down
2 changes: 1 addition & 1 deletion tests/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestClient_State(t *testing.T) {
suite := voprf.RistrettoSha512
suite := voprf.Ristretto255Sha512
input := []byte("input")
kp := suite.KeyGen() // only used in VOPRF and POPRF
info := []byte("additional data") // only used in POPRF
Expand Down

0 comments on commit 3717de0

Please sign in to comment.