-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
71 lines (55 loc) · 1.36 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2024 Daniel Bourdrez. All Rights Reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree or at
// https://spdx.org/licenses/MIT.html
package voprf
import (
"crypto/subtle"
"encoding/binary"
group "github.com/bytemare/crypto"
)
// KeyPair assembles a VOPRF key pair. The SecretKey can be used as the evaluation key for
// the group identified by Ciphersuite.
type KeyPair struct {
PublicKey *group.Element
SecretKey *group.Scalar
Ciphersuite Ciphersuite
}
func i2osp2(value int) []byte {
out := make([]byte, 2)
binary.BigEndian.PutUint16(out, uint16(value))
return out
}
func lengthPrefixEncode(input []byte) []byte {
return append(i2osp2(len(input)), input...)
}
func ctEqual(a, b []byte) bool {
return subtle.ConstantTimeCompare(a, b) == 1
}
func concatenate(input ...[]byte) []byte {
if len(input) == 1 {
if len(input[0]) == 0 {
return nil
}
return input[0]
}
length := 0
for _, in := range input {
length += len(in)
}
buf := make([]byte, 0, length)
for _, in := range input {
buf = append(buf, in...)
}
return buf
}
func dst(prefix string, contextString []byte) []byte {
p := []byte(prefix)
t := make([]byte, 0, len(p)+len(contextString))
t = append(t, p...)
t = append(t, contextString...)
return t
}