/
crypto.go
140 lines (117 loc) · 2.98 KB
/
crypto.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// simple crypto bridge between crypto/elliptic and what's needed for PVSS
package main
import (
"crypto/elliptic"
"crypto/rand"
"math/big"
"fmt"
"encoding/hex"
)
var fakeRandom = true
type Scalar struct {
data []byte
}
type Point struct {
x *big.Int
y *big.Int
}
type DhSecret struct {
data []byte // pre-form of an actual DH secret on elliptic curve
}
type KeyPair struct {
private Scalar
public Point
}
func (s Scalar) String() string {
bi := s.toInt()
return fmt.Sprintf("Scalar %s", bi.String())
}
func (p Point) String() string {
return fmt.Sprintf("Point {x = %d,y = %d}", p.x, p.y)
}
func (dh DhSecret) String() string {
return hex.EncodeToString(dh.data)
}
// hardcoded choice of curve
func getCurve() elliptic.Curve {
return elliptic.P256()
}
func getCurveParams() *elliptic.CurveParams {
return getCurve().Params()
}
func keypairGen() KeyPair {
if fakeRandom {
priv := new (Scalar).fromSmallInt(1)
pub := priv.toPoint()
return KeyPair { *priv, pub }
} else {
priv, x, y, _ := elliptic.GenerateKey(getCurve(), rand.Reader)
return KeyPair { Scalar { priv }, Point { x, y } }
}
}
func (s Scalar) toInt() *big.Int {
return new(big.Int).SetBytes(s.data)
}
func (s *Scalar) fromInt(bi *big.Int) *Scalar {
order := getCurveParams().N
bitSize := getCurveParams().BitSize
if bi.Cmp(big.NewInt(0)) == -1 {
bi.Add(bi, order)
}
b := bi.Bytes()
blen := cap(b)
nbBytes := bitSize / 8
switch {
case blen == nbBytes: // expected size already
s.data = b
case blen < nbBytes: // complete with 0
s.data = make([]byte, nbBytes)
copy(s.data[nbBytes - blen:], b)
}
return s
}
func (s *Scalar) fromSmallInt(i int) *Scalar {
bi := big.NewInt(int64(i))
return s.fromInt(bi)
}
func (s *Scalar) Add(a *Scalar, b *Scalar) *Scalar {
r := new (big.Int).Add(a.toInt(),b.toInt())
r2 := r.Mod(r, getCurveParams().N)
s.fromInt(r2)
return s
}
func (s *Scalar) Mul(a *Scalar, b *Scalar) *Scalar {
r := new (big.Int).Mul(a.toInt(),b.toInt())
r2 := r.Mod(r, getCurveParams().N)
s.fromInt(r2)
return s
}
func (s *Scalar) Inverse(a *Scalar) *Scalar {
bi := a.toInt()
inv := bi.ModInverse(bi, getCurveParams().N)
s.fromInt(inv)
return s
}
// lift to curve a scalar
func (s *Scalar) toPoint() Point {
x,y := getCurve().ScalarBaseMult(s.data)
return (Point { x, y })
}
func (p *Point) Add(a *Point, b *Point) *Point{
r := new(Point)
r.x, r.y = getCurve().Add(a.x, a.y, b.x, b.y)
return r
}
func PointMul(p *Point, s *Scalar) *Point {
r := new(Point)
r.x, r.y = getCurve().ScalarMult(p.x, p.y, s.data)
return r
}
// same as PointMul, but multiply by the modular inverse
func PointDiv(p *Point, s *Scalar) *Point {
sInv := new (Scalar).Inverse(s)
return PointMul(p, sInv)
}
func (p *Point) ToDhSecret() DhSecret {
return DhSecret {p.x.Bytes()}
}