-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
60 lines (52 loc) · 1.4 KB
/
utils.go
File metadata and controls
60 lines (52 loc) · 1.4 KB
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
package spake
import (
"crypto/sha256"
"encoding/base64"
"errors"
"github.com/bwesterb/go-ristretto"
)
// deriveBytes hashes arbitrary input into a fixed 32-byte value,
// suitable for conversion into a Ristretto scalar.
func deriveBytes(b []byte) *[32]byte {
result := sha256.Sum256(b)
return &result
}
// pointFromBase64 decodes a base64-encoded Ristretto point and validates it
// before returning the corresponding curve point.
func pointFromBase64(s string) (ristretto.Point, error) {
var P ristretto.Point
raw, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return P, err
}
if len(raw) != 32 {
return P, errors.New("invalid ristretto point length")
}
var buf [32]byte
copy(buf[:], raw)
if ok := P.SetBytes(&buf); !ok {
return P, errors.New("could not load ristretto point")
}
return P, nil
}
// boolToByte converts a boolean value into a single byte (0 or 1)
// for compact protocol serialization.
func boolToByte(b bool) byte {
if b {
return 1
} else {
return 0
}
}
// byteToBool converts a single byte into a boolean value,
// treating any non-zero value as true.
func byteToBool(b byte) bool {
return b != 0
}
// GetRandomPoint generates a random Ristretto point and returns it
// encoded in base64 format, suitable for testing or parameter generation.
func GetRandomPoint() string {
var p ristretto.Point
p.Rand()
return base64.StdEncoding.EncodeToString(p.Bytes())
}