-
Notifications
You must be signed in to change notification settings - Fork 0
/
ed25519.go
147 lines (117 loc) · 3.6 KB
/
ed25519.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
141
142
143
144
145
146
147
package nizk
import (
"crypto/rand"
"crypto/sha256"
"fmt"
"io"
"filippo.io/edwards25519"
"golang.org/x/crypto/sha3"
)
const (
SecretSize = 32
ProofSize = 64
// this prefix is needed to eliminate accidental generation of the same hash outside the proof
magicPrefix = "Fiat-Shamir NIZK proof made by https://github.com/eliastor?d3eac842bf90905fc958c42422132e984676521f EOL"
)
var (
_ Nizk = &ed25519Nizk{}
)
type ed25519Nizk struct {
hashSum func(data []byte) [SecretSize]byte
}
func (n *ed25519Nizk) fingerprintPointScalar(sum []byte) (*edwards25519.Point, *edwards25519.Scalar) {
sc, _ := edwards25519.NewScalar().SetBytesWithClamping(sum)
point := edwards25519.NewIdentityPoint().ScalarBaseMult(sc)
return point, sc
}
func (n *ed25519Nizk) messagePointID(message []byte) (*edwards25519.Point, *edwards25519.Scalar) {
sum := n.hashSum(append([]byte(magicPrefix), message...))
s := make([]byte, SecretSize)
copy(s, sum[:])
X, x := n.fingerprintPointScalar(s)
return X, x
}
func (n *ed25519Nizk) Fingerprint(message []byte) (id []byte) {
point, _ := n.messagePointID(message)
id = point.Bytes()
return
}
func (n *ed25519Nizk) calcGYTPHashScalar(fingerprint, pT, stamp []byte) (*edwards25519.Scalar, error) {
gytp := []byte{}
gytp = append(gytp, edwards25519.NewGeneratorPoint().Bytes()...)
gytp = append(gytp, fingerprint...)
gytp = append(gytp, pT...)
gytp = append(gytp, stamp...)
sum := n.hashSum(gytp)
c, err := edwards25519.NewScalar().SetBytesWithClamping(sum[:])
if err != nil {
return nil, fmt.Errorf("can't create c scalar: %w", err)
}
return c, nil
}
func (n *ed25519Nizk) Proove(msg []byte, stamp []byte) (Proof, []byte, error) {
vb := [SecretSize]byte{}
_, err := io.ReadFull(rand.Reader, vb[:])
if err != nil {
return nil, nil, err
}
v, err := edwards25519.NewScalar().SetBytesWithClamping(vb[:])
if err != nil {
return nil, nil, fmt.Errorf("can't create r scalar: %w", err)
}
T := edwards25519.NewIdentityPoint().ScalarBaseMult(v)
Tb := T.Bytes()
X, x := n.messagePointID(msg)
fingerprint := X.Bytes()
c, err := n.calcGYTPHashScalar(fingerprint, Tb, stamp) // h = hash(G||id||vG||stamp)
if err != nil {
return nil, nil, fmt.Errorf("can't calculate GYTP hash: %w", err)
}
c.Negate(c)
r := edwards25519.NewScalar().MultiplyAdd(c, x, v) // (-h) * x + v
var proof [ProofSize]byte
copy(proof[:SecretSize], Tb)
copy(proof[SecretSize:], r.Bytes())
return proof[:], fingerprint, nil
}
func (n *ed25519Nizk) ProofSize() int {
return ProofSize
}
func (n *ed25519Nizk) FingerprintSize() int {
return SecretSize
}
func (n *ed25519Nizk) Verify(id []byte, stamp []byte, proof Proof) (bool, error) {
if len(proof) != n.ProofSize() {
return false, fmt.Errorf("wrong size of proof, expected %d", n.ProofSize())
}
Tb := proof[:SecretSize]
rb := proof[SecretSize:]
r, err := edwards25519.NewScalar().SetCanonicalBytes(rb)
if err != nil {
return false, fmt.Errorf("can't read r scalar: %w", err)
}
X, err := edwards25519.NewIdentityPoint().SetBytes(id)
if err != nil {
return false, fmt.Errorf("can't read X point: %w", err)
}
c, err := n.calcGYTPHashScalar(id, Tb, stamp)
if err != nil {
return false, fmt.Errorf("can't calculate GYTP hash: %w", err)
}
T, err := edwards25519.NewIdentityPoint().SetBytes(Tb)
if err != nil {
return false, fmt.Errorf("can't read T point: %w", err)
}
calculatedT := edwards25519.NewIdentityPoint().VarTimeDoubleScalarBaseMult(c, X, r)
return T.Equal(calculatedT) == 1, nil
}
func NewEd25519Sha3() Nizk {
return &ed25519Nizk{
hashSum: sha3.Sum256,
}
}
func NewEd25519Sha256() Nizk {
return &ed25519Nizk{
hashSum: sha256.Sum256,
}
}