forked from winderica/kryptology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkg.go
121 lines (103 loc) · 2.61 KB
/
dkg.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
package dkg
import (
"crypto/rand"
"github.com/coinbase/kryptology/pkg/core/curves"
"github.com/coinbase/kryptology/pkg/ot/base/simplest"
"github.com/coinbase/kryptology/pkg/zkp/schnorr"
)
type Alice struct {
prover *schnorr.Prover
proof *schnorr.Proof
curve *curves.Curve
sk curves.Scalar
pk curves.Point
pkPeer curves.Point
pkJoint curves.Point
}
type Bob struct {
prover *schnorr.Prover
curve *curves.Curve
sk curves.Scalar
pk curves.Point
pkPeer curves.Point
pkJoint curves.Point
aliceCommitment schnorr.Commitment
}
type Output struct {
Sk curves.Scalar
Pk curves.Point
PkPeer curves.Point
PkJoint curves.Point
}
func NewAlice(curve *curves.Curve) *Alice {
sk := curve.Scalar.Random(rand.Reader)
return &Alice{
sk: sk,
pk: curve.ScalarBaseMult(sk),
curve: curve,
}
}
func NewBob(curve *curves.Curve) *Bob {
sk := curve.Scalar.Random(rand.Reader)
return &Bob{
sk: sk,
pk: curve.ScalarBaseMult(sk),
curve: curve,
}
}
func (alice *Alice) Step1() (schnorr.Commitment, error) {
uniqueSessionId := [simplest.DigestSize]byte{}
alice.prover = schnorr.NewProver(alice.curve, nil, uniqueSessionId[:])
proof, commitment, err := alice.prover.ProveCommit(alice.sk)
if err != nil {
return nil, err
}
alice.proof = proof
return commitment, nil
}
func (bob *Bob) Step2(commitment schnorr.Commitment) (*schnorr.Proof, error) {
bob.aliceCommitment = commitment
uniqueSessionId := [simplest.DigestSize]byte{}
bob.prover = schnorr.NewProver(bob.curve, nil, uniqueSessionId[:])
proof, err := bob.prover.Prove(bob.sk)
if err != nil {
return nil, err
}
return proof, err
}
func (alice *Alice) Step3(proof *schnorr.Proof) (*schnorr.Proof, error) {
uniqueSessionId := [simplest.DigestSize]byte{}
err := schnorr.Verify(proof, alice.curve, nil, uniqueSessionId[:])
if err != nil {
return nil, err
}
alice.pkPeer = proof.Statement
alice.pkJoint = alice.pkPeer.Add(alice.pk)
return alice.proof, nil
}
func (bob *Bob) Step4(proof *schnorr.Proof) error {
uniqueSessionId := [simplest.DigestSize]byte{}
err := schnorr.DecommitVerify(proof, bob.aliceCommitment, bob.curve, nil, uniqueSessionId[:])
if err != nil {
return err
}
bob.pkPeer = proof.Statement
bob.pkJoint = bob.pkPeer.Add(bob.pk)
return nil
}
func (alice *Alice) Output() *Output {
return &Output{
Pk: alice.pk,
Sk: alice.sk,
PkJoint: alice.pkJoint,
PkPeer: alice.pkPeer,
}
}
func (bob *Bob) Output() *Output {
return &Output{
Pk: bob.pk,
Sk: bob.sk,
PkJoint: bob.pkJoint,
PkPeer: bob.pkPeer,
}
}