-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.go
61 lines (46 loc) · 2.12 KB
/
misc.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
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2021 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 internal
import (
cryptorand "crypto/rand"
"errors"
"fmt"
)
var (
// ErrParamNilScalar indicates a forbidden nil or empty scalar.
ErrParamNilScalar = errors.New("nil or empty scalar")
// ErrParamScalarLength indicates an invalid scalar length.
ErrParamScalarLength = errors.New("invalid scalar length")
// ErrParamNilPoint indicated a forbidden nil or empty point.
ErrParamNilPoint = errors.New("nil or empty point")
// ErrParamInvalidPointEncoding indicates an invalid point encoding has been provided.
ErrParamInvalidPointEncoding = errors.New("invalid point encoding")
// ErrCastElement indicates a failed attempt to cast to a point.
ErrCastElement = errors.New("could not cast to same group element (wrong group ?)")
// ErrCastScalar indicates a failed attempt to cast to a scalar.
ErrCastScalar = errors.New("could not cast to same group scalar (wrong group ?)")
// ErrWrongField indicates an incompatible field has been encountered.
ErrWrongField = errors.New("incompatible fields")
// ErrIdentity indicates that the identity point (or point at infinity) has been encountered.
ErrIdentity = errors.New("infinity/identity point")
// ErrBigIntConversion reports an error in converting to a *big.int.
ErrBigIntConversion = errors.New("conversion error")
// ErrParamNegScalar reports an error when the input scalar is negative.
ErrParamNegScalar = errors.New("negative scalar")
// ErrParamScalarTooBig reports an error when the input scalar is too big.
ErrParamScalarTooBig = errors.New("scalar too big")
)
// RandomBytes returns random bytes of length len (wrapper for crypto/rand).
func RandomBytes(length int) []byte {
random := make([]byte, length)
if _, err := cryptorand.Read(random); err != nil {
// We can as well not panic and try again in a loop
panic(fmt.Errorf("unexpected error in generating random bytes : %w", err))
}
return random
}