Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: bytemare <[email protected]>
  • Loading branch information
bytemare committed Jun 7, 2024
1 parent 85e2d48 commit 72f293d
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 70 deletions.
14 changes: 0 additions & 14 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@
package internal

import (
"errors"
"slices"

group "github.com/bytemare/crypto"
)

var (
errBatchNoElements = errors.New("no evaluated elements provided to Finalize()")
errBatchDifferentSize = errors.New("number of evaluations is different thant number of previously blinded inputs")
)

// A Client holds the core functionalities for all OPRF, TOPRF, VOPRF, and POPRF.
type Client struct {
// Core abstracts configuration dependent operations.
Expand Down Expand Up @@ -106,14 +100,6 @@ func (c *Client) Finalize(index int, evaluated *group.Element, info ...byte) []b
// FinalizeBatch unblinds the evaluated elements and returns the corresponding protocol outputs. The optional info
// argument must only be provided when using the POPRF mode.
func (c *Client) FinalizeBatch(evaluated []*group.Element, info ...byte) ([][]byte, error) {
if len(evaluated) == 0 {
return nil, errBatchNoElements
}

if len(evaluated) != c.Size() {
return nil, errBatchDifferentSize
}

out := make([][]byte, len(evaluated))

for i, e := range evaluated {
Expand Down
2 changes: 1 addition & 1 deletion internal/nizk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
dstChallenge = "Challenge"
)

var errProofFailed = errors.New("proof fails")
var errProofFailed = errors.New("invalid proof")

// Verifiable enables VOPRF and POPRF functions over OPRF operations.
type Verifiable struct {
Expand Down
15 changes: 15 additions & 0 deletions oprf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
package voprf

import (
"errors"

group "github.com/bytemare/crypto"

"github.com/bytemare/voprf/internal"
Expand Down Expand Up @@ -40,6 +42,11 @@ const (
Secp256k1 = Ciphersuite(group.Secp256k1)
)

var (
errBatchNoElements = errors.New("no evaluated elements provided to Finalize()")
errBatchDifferentSize = errors.New("number of evaluations is different thant number of previously blinded inputs")
)

// FromGroup returns a Ciphersuite given a Group.
func FromGroup(g group.Group) Ciphersuite {
return Ciphersuite(g)
Expand Down Expand Up @@ -112,6 +119,14 @@ func (c *Client) Finalize(evaluated *group.Element) []byte {

// FinalizeBatch unblinds the evaluated elements and returns the corresponding protocol outputs.
func (c *Client) FinalizeBatch(evaluated []*group.Element) ([][]byte, error) {
if len(evaluated) == 0 {
return nil, errBatchNoElements
}

if len(evaluated) != c.Size() {
return nil, errBatchDifferentSize
}

return c.Client.FinalizeBatch(evaluated)
}

Expand Down
16 changes: 6 additions & 10 deletions tests/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,25 @@ package voprf_test

import (
"bytes"
"strings"
"testing"

"github.com/bytemare/voprf/voprf"
)

func Test_DecodeElement(t *testing.T) {
testAll(t, func(c *configuration) {
bad := getBadElement(t, c)

if _, err := c.ciphersuite.DecodeElement(bad); err == nil ||
!strings.Contains(err.Error(), "element Decode: ") {
t.Errorf("expected error, got %v", err)
element := c.group.NewElement().Base().Multiply(c.group.NewScalar().Random()).Encode()
if _, err := c.ciphersuite.DecodeElement(element); err != nil {
t.Errorf("unexpected error, got %v", err)
}
})
}

func Test_DecodeScalar(t *testing.T) {
testAll(t, func(c *configuration) {
bad := getBadScalar(t, c)

if _, err := c.ciphersuite.DecodeScalar(bad); err == nil || !strings.Contains(err.Error(), "scalar Decode: ") {
t.Errorf("expected error, got %v", err)
scalar := c.group.NewScalar().Random().Encode()
if _, err := c.ciphersuite.DecodeScalar(scalar); err != nil {
t.Errorf("unexpected error, got %v", err)
}
})
}
Expand Down
Loading

0 comments on commit 72f293d

Please sign in to comment.