forked from jamesruan/sodium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalarmult.go
60 lines (48 loc) · 1.32 KB
/
scalarmult.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
package sodium
// #cgo pkg-config: libsodium
// #include <stdlib.h>
// #include <sodium.h>
import "C"
var (
cryptoScalarmultBytes = int(C.crypto_scalarmult_bytes())
cryptoScalarmultScalarBytes = int(C.crypto_scalarmult_scalarbytes())
)
type Scalar struct {
Bytes
}
func (s Scalar) Size() int {
return cryptoScalarmultScalarBytes
}
// ScalarMult is the mulitiplication of two Scalar, used to calculate shared key
// from BoxSecertKey and other end's BoxPublicKey.
type ScalarMult struct {
Bytes
}
func (s ScalarMult) Size() int {
return cryptoScalarmultBytes
}
// CryptoScalarmultBase calculates BoxPublicKey 'q' from BoxSecertKey 'n'.
func CryptoScalarmultBase(n Scalar) (q Scalar) {
checkTypedSize(&n, "SecretKey")
qb := make([]byte, cryptoScalarmultScalarBytes)
if int(C.crypto_scalarmult_base(
(*C.uchar)(&qb[0]),
(*C.uchar)(&n.Bytes[0]))) != 0 {
panic("see libsodium")
}
return Scalar{qb}
}
// CryptoScalarmult calculates common key 'q' from private key 'n' and
// other's public key 'p'
func CryptoScalarmult(n, p Scalar) (q ScalarMult) {
checkTypedSize(&n, "SecretKey")
checkTypedSize(&p, "PublicKey")
qb := make([]byte, cryptoScalarmultBytes)
if int(C.crypto_scalarmult(
(*C.uchar)(&qb[0]),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&p.Bytes[0]))) != 0 {
panic("see libsodium")
}
return ScalarMult{qb}
}