Skip to content

Add functions to retrieve params of a Suite. #325

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

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions oprf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c client) Blind(inputs [][]byte) (*FinalizeData, *EvaluationRequest, error

blinds := make([]Blind, len(inputs))
for i := range inputs {
blinds[i] = c.params.Group.RandomScalar(rand.Reader)
blinds[i] = c.params.group.RandomScalar(rand.Reader)
}

return c.blind(inputs, blinds)
Expand All @@ -51,11 +51,11 @@ func (c client) blind(inputs [][]byte, blinds []Blind) (*FinalizeData, *Evaluati
blindedElements := make([]Blinded, len(inputs))
dst := c.params.getDST(hashToGroupDST)
for i := range inputs {
point := c.params.Group.HashToElement(inputs[i], dst)
point := c.params.group.HashToElement(inputs[i], dst)
if point.IsIdentity() {
return nil, nil, ErrInvalidInput
}
blindedElements[i] = c.params.Group.NewElement().Mul(point, blinds[i])
blindedElements[i] = c.params.group.NewElement().Mul(point, blinds[i])
}

evalReq := &EvaluationRequest{blindedElements}
Expand All @@ -66,8 +66,8 @@ func (c client) blind(inputs [][]byte, blinds []Blind) (*FinalizeData, *Evaluati

func (c client) unblind(serUnblindeds [][]byte, blindeds []group.Element, blind []Blind) error {
var err error
invBlind := c.params.Group.NewScalar()
U := c.params.Group.NewElement()
invBlind := c.params.group.NewScalar()
U := c.params.group.NewElement()

for i := range blindeds {
invBlind.Inv(blind[i])
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c client) finalize(f *FinalizeData, e *Evaluation, info []byte) ([][]byte,
return nil, err
}

h := c.params.Hash.New()
h := c.params.hash.New()
outputs := make([][]byte, len(f.inputs))
for i := range f.inputs {
outputs[i] = c.params.finalizeHash(h, f.inputs[i], info, unblindedElements[i])
Expand All @@ -119,7 +119,7 @@ func (c VerifiableClient) Finalize(f *FinalizeData, e *Evaluation) (outputs [][]
}

if !(dleq.Verifier{Params: c.getDLEQParams()}).VerifyBatch(
c.params.Group.Generator(),
c.params.group.Generator(),
c.pkS.e,
f.evalReq.Elements,
e.Elements,
Expand All @@ -142,7 +142,7 @@ func (c PartialObliviousClient) Finalize(f *FinalizeData, e *Evaluation, info []
}

if !(dleq.Verifier{Params: c.getDLEQParams()}).VerifyBatch(
c.params.Group.Generator(),
c.params.group.Generator(),
tweakedKey,
e.Elements,
f.evalReq.Elements,
Expand All @@ -160,8 +160,8 @@ func (c PartialObliviousClient) pointFromInfo(info []byte) (group.Element, error
return nil, err
}

T := c.params.Group.NewElement().MulGen(m)
tweakedKey := c.params.Group.NewElement().Add(T, c.pkS.e)
T := c.params.group.NewElement().MulGen(m)
tweakedKey := c.params.group.NewElement().Add(T, c.pkS.e)
if tweakedKey.IsIdentity() {
return nil, ErrInvalidInfo
}
Expand Down
14 changes: 7 additions & 7 deletions oprf/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (k *PrivateKey) UnmarshalBinary(s Suite, data []byte) error {
return ErrInvalidSuite
}
k.p = p
k.k = k.p.Group.NewScalar()
k.k = k.p.group.NewScalar()

return k.k.UnmarshalBinary(data)
}
Expand All @@ -38,14 +38,14 @@ func (k *PublicKey) UnmarshalBinary(s Suite, data []byte) error {
return ErrInvalidSuite
}
k.p = p
k.e = k.p.Group.NewElement()
k.e = k.p.group.NewElement()

return k.e.UnmarshalBinary(data)
}

func (k *PrivateKey) Public() *PublicKey {
if k.pub == nil {
k.pub = &PublicKey{k.p, k.p.Group.NewElement().MulGen(k.k)}
k.pub = &PublicKey{k.p, k.p.group.NewElement().MulGen(k.k)}
}

return k.pub
Expand All @@ -61,7 +61,7 @@ func GenerateKey(s Suite, rnd io.Reader) (*PrivateKey, error) {
if !ok {
return nil, ErrInvalidSuite
}
privateKey := p.Group.RandomScalar(rnd)
privateKey := p.group.RandomScalar(rnd)

return &PrivateKey{p, privateKey, nil}, nil
}
Expand All @@ -83,13 +83,13 @@ func DeriveKey(s Suite, mode Mode, seed, info []byte) (*PrivateKey, error) {
deriveInput := append(append(append([]byte{}, seed...), lenInfo...), info...)

dst := p.getDST(deriveKeyPairDST)
zero := p.Group.NewScalar()
privateKey := p.Group.NewScalar()
zero := p.group.NewScalar()
privateKey := p.group.NewScalar()
for counter := byte(0); privateKey.IsEqual(zero); counter++ {
if counter > maxTries {
return nil, ErrDeriveKeyPairError
}
privateKey = p.Group.HashToScalar(append(deriveInput, counter), dst)
privateKey = p.group.HashToScalar(append(deriveInput, counter), dst)
}

return &PrivateKey{p, privateKey, nil}, nil
Expand Down
38 changes: 23 additions & 15 deletions oprf/oprf.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,29 @@ func isValidMode(m Mode) bool {
return m == BaseMode || m == VerifiableMode || m == PartialObliviousMode
}

type Suite interface{ cannotBeImplementedExternally() }
type Suite interface {
ID() int
Group() group.Group
Hash() crypto.Hash
cannotBeImplementedExternally()
}

var (
// SuiteP256 represents the OPRF with P-256 and SHA-256.
SuiteP256 Suite = params{ID: 3, Group: group.P256, Hash: crypto.SHA256}
SuiteP256 Suite = params{id: 3, group: group.P256, hash: crypto.SHA256}
// SuiteP384 represents the OPRF with P-384 and SHA-384.
SuiteP384 Suite = params{ID: 4, Group: group.P384, Hash: crypto.SHA384}
SuiteP384 Suite = params{id: 4, group: group.P384, hash: crypto.SHA384}
// SuiteP521 represents the OPRF with P-521 and SHA-512.
SuiteP521 Suite = params{ID: 5, Group: group.P521, Hash: crypto.SHA512}
SuiteP521 Suite = params{id: 5, group: group.P521, hash: crypto.SHA512}
)

func GetSuite(id int) (Suite, error) {
switch uint16(id) {
case SuiteP256.(params).ID:
case SuiteP256.(params).id:
return SuiteP256, nil
case SuiteP384.(params).ID:
case SuiteP384.(params).id:
return SuiteP384, nil
case SuiteP521.(params).ID:
case SuiteP521.(params).id:
return SuiteP521, nil
default:
return nil, ErrInvalidSuite
Expand Down Expand Up @@ -164,21 +169,24 @@ func NewPartialObliviousServer(s Suite, key *PrivateKey) PartialObliviousServer
}

type params struct {
ID uint16
id uint16
m Mode
Group group.Group
Hash crypto.Hash
group group.Group
hash crypto.Hash
}

func (p params) cannotBeImplementedExternally() {}

func (p params) String() string { return fmt.Sprintf("Suite%v", p.Group) }
func (p params) String() string { return fmt.Sprintf("Suite%v", p.group) }
func (p params) ID() int { return int(p.id) }
func (p params) Group() group.Group { return p.group }
func (p params) Hash() crypto.Hash { return p.hash }

func (p params) getDST(name string) []byte {
return append(append(append([]byte{},
[]byte(name)...),
[]byte(version)...),
[]byte{p.m, 0, byte(p.ID)}...)
[]byte{p.m, 0, byte(p.id)}...)
}

func (p params) scalarFromInfo(info []byte) (group.Scalar, error) {
Expand All @@ -192,7 +200,7 @@ func (p params) scalarFromInfo(info []byte) (group.Scalar, error) {
lenInfo...),
info...)

return p.Group.HashToScalar(framedInfo, p.getDST(hashToScalarDST)), nil
return p.group.HashToScalar(framedInfo, p.getDST(hashToScalarDST)), nil
}

func (p params) finalizeHash(h hash.Hash, input, info, element []byte) []byte {
Expand All @@ -219,8 +227,8 @@ func (p params) finalizeHash(h hash.Hash, input, info, element []byte) []byte {
}

func (p params) getDLEQParams() (out dleq.Params) {
out.G = p.Group
out.H = p.Hash
out.G = p.group
out.H = p.hash
out.DST = p.getDST("")

return
Expand Down
20 changes: 10 additions & 10 deletions oprf/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s server) PublicKey() *PublicKey { return s.privateKey.Public() }
func (s server) evaluate(elements []Blinded, secret Blind) []Evaluated {
evaluations := make([]Evaluated, len(elements))
for i := range elements {
evaluations[i] = s.params.Group.NewElement().Mul(elements[i], secret)
evaluations[i] = s.params.group.NewElement().Mul(elements[i], secret)
}

return evaluations
Expand All @@ -41,7 +41,7 @@ func (s VerifiableServer) Evaluate(req *EvaluationRequest) (*Evaluation, error)

proof, err := dleq.Prover{Params: s.getDLEQParams()}.ProveBatch(
s.privateKey.k,
s.params.Group.Generator(),
s.params.group.Generator(),
s.PublicKey().e,
req.Elements,
evaluations,
Expand All @@ -64,8 +64,8 @@ func (s PartialObliviousServer) Evaluate(req *EvaluationRequest, info []byte) (*

proof, err := dleq.Prover{Params: s.getDLEQParams()}.ProveBatch(
keyProof,
s.params.Group.Generator(),
s.params.Group.NewElement().MulGen(keyProof),
s.params.group.Generator(),
s.params.group.NewElement().MulGen(keyProof),
evaluations,
req.Elements,
rand.Reader,
Expand All @@ -82,12 +82,12 @@ func (s server) secretFromInfo(info []byte) (t, tInv group.Scalar, err error) {
if err != nil {
return nil, nil, err
}
t = s.params.Group.NewScalar().Add(m, s.privateKey.k)
t = s.params.group.NewScalar().Add(m, s.privateKey.k)

if zero := s.params.Group.NewScalar(); t.IsEqual(zero) {
if zero := s.params.group.NewScalar(); t.IsEqual(zero) {
return nil, nil, ErrInverseZero
}
tInv = s.params.Group.NewScalar().Inv(t)
tInv = s.params.group.NewScalar().Inv(t)

return t, tInv, nil
}
Expand All @@ -102,14 +102,14 @@ func (s server) fullEvaluate(input, info []byte) ([]byte, error) {
}
}

element := s.params.Group.HashToElement(input, s.params.getDST(hashToGroupDST))
evaluation := s.params.Group.NewElement().Mul(element, evalSecret)
element := s.params.group.HashToElement(input, s.params.getDST(hashToGroupDST))
evaluation := s.params.group.NewElement().Mul(element, evalSecret)
serEval, err := evaluation.MarshalBinaryCompress()
if err != nil {
return nil, err
}

return s.finalizeHash(s.params.Hash.New(), input, info, serEval), nil
return s.finalizeHash(s.params.hash.New(), input, info, serEval), nil
}

func (s Server) FullEvaluate(input []byte) (output []byte, err error) {
Expand Down
14 changes: 7 additions & 7 deletions oprf/vectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,33 +164,33 @@ func (v *vector) test(t *testing.T) {

blinds := make([]Blind, len(blindsBytes))
for j := range blindsBytes {
blinds[j] = params.Group.NewScalar()
blinds[j] = params.group.NewScalar()
err := blinds[j].UnmarshalBinary(blindsBytes[j])
test.CheckNoErr(t, err, "invalid blind")
}

finData, evalReq, err := client.blind(inputs, blinds)
test.CheckNoErr(t, err, "invalid client request")
evalReqBytes, err := elementsMarshalBinary(params.Group, evalReq.Elements)
evalReqBytes, err := elementsMarshalBinary(params.group, evalReq.Elements)
test.CheckNoErr(t, err, "bad serialization")
v.compareBytes(t, evalReqBytes, flattenList(t, vi.BlindedElement, "blindedElement"))

eval, err := server.Evaluate(evalReq)
test.CheckNoErr(t, err, "invalid evaluation")
elemBytes, err := elementsMarshalBinary(params.Group, eval.Elements)
elemBytes, err := elementsMarshalBinary(params.group, eval.Elements)
test.CheckNoErr(t, err, "invalid evaluations marshaling")
v.compareBytes(t, elemBytes, flattenList(t, vi.EvaluationElement, "evaluation"))

if v.Mode == VerifiableMode || v.Mode == PartialObliviousMode {
randomness := toScalar(t, params.Group, vi.Proof.R, "invalid proof random scalar")
randomness := toScalar(t, params.group, vi.Proof.R, "invalid proof random scalar")
var proof encoding.BinaryMarshaler
switch v.Mode {
case VerifiableMode:
ss := server.(VerifiableServer)
prover := dleq.Prover{Params: ss.getDLEQParams()}
proof, err = prover.ProveBatchWithRandomness(
ss.privateKey.k,
ss.params.Group.Generator(),
ss.params.group.Generator(),
server.PublicKey().e,
evalReq.Elements,
eval.Elements,
Expand All @@ -201,8 +201,8 @@ func (v *vector) test(t *testing.T) {
prover := dleq.Prover{Params: ss.getDLEQParams()}
proof, err = prover.ProveBatchWithRandomness(
keyProof,
ss.params.Group.Generator(),
ss.params.Group.NewElement().MulGen(keyProof),
ss.params.group.Generator(),
ss.params.group.NewElement().MulGen(keyProof),
eval.Elements,
evalReq.Elements,
randomness)
Expand Down