-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissuer.go
152 lines (122 loc) · 2.44 KB
/
issuer.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
148
149
150
151
152
package ecdaa
import (
"fmt"
"github.com/akakou-fork/amcl-go/miracl/core"
"github.com/akakou-fork/amcl-go/miracl/core/FP256BN"
amcl_utils "github.com/akakou/fp256bn-amcl-utils"
)
/**
* ISK: Issuer's Secret Key.
*/
type ISK struct {
X *FP256BN.BIG
Y *FP256BN.BIG
}
/**
* Generate IPK with random.
*/
func RandomISK(rng *core.RAND) ISK {
var isk ISK
x := FP256BN.Random(rng)
y := FP256BN.Random(rng)
x.Mod(amcl_utils.P())
y.Mod(amcl_utils.P())
isk.X = x
isk.Y = y
return isk
}
/**
* IPL: Issuer's Public Key.
*/
type IPK struct {
X *FP256BN.ECP2
Y *FP256BN.ECP2
C *FP256BN.BIG
SX *FP256BN.BIG
SY *FP256BN.BIG
}
/**
* Generate IPK with random and ISK.
*/
func RandomIPK(isk *ISK, rng *core.RAND) IPK {
// random r_x, r_y
var ipk IPK
x := isk.X
y := isk.Y
rX := FP256BN.Random(rng)
rY := FP256BN.Random(rng)
rX.Mod(amcl_utils.P())
rY.Mod(amcl_utils.P())
// calc X, Y
// X = g2^x
// Y = g2^y
X := amcl_utils.G2().Mul(x)
Y := amcl_utils.G2().Mul(y)
// calc U_x, U_y
// U_x = g2 ^ r_x
// U_y = g2 ^ r_y
Ux := amcl_utils.G2().Mul(rX)
Uy := amcl_utils.G2().Mul(rY)
// calc `c = H(U_x | U_y | g2 | X | Y)`
hash := amcl_utils.NewHash()
hash.WriteECP2(Ux, Uy, amcl_utils.G2(), X, Y)
c := hash.SumToBIG()
// calc s_x, s_y
// s_x = r_x + cx
// s_y = r_y + cy
// todo: mod p
sX := FP256BN.Modmul(c, x, amcl_utils.P())
sX = FP256BN.Modadd(rX, sX, amcl_utils.P())
sY := FP256BN.Modmul(y, c, amcl_utils.P())
sY = FP256BN.Modadd(rY, sY, amcl_utils.P())
// copy pointers to ipk
ipk.X = X
ipk.Y = Y
ipk.C = c
ipk.SX = sX
ipk.SY = sY
return ipk
}
/**
* Check IPK is valid.
*/
func VerifyIPK(ipk *IPK) error {
X := ipk.X
Y := ipk.Y
c := ipk.C
sX := ipk.SX
sY := ipk.SY
// calc U_x = g2^s_x * X^{-c}
Ux := amcl_utils.G2().Mul(sX)
tmp := X.Mul(c)
Ux.Sub(tmp)
// calc U_y = g2^s_y * Y^{-c}
Uy := amcl_utils.G2().Mul(sY)
tmp = Y.Mul(c)
Uy.Sub(tmp)
// calc `c' = H(U_x | U_y | g2 | X | Y)`
hash := amcl_utils.NewHash()
hash.WriteECP2(Ux, Uy, amcl_utils.G2(), X, Y)
cDash := hash.SumToBIG()
if FP256BN.Comp(c, cDash) == 0 {
return nil
} else {
return fmt.Errorf("IPK is not valid\n")
}
}
type Issuer struct {
Ipk IPK
Isk ISK
}
func NewIssuer(isk ISK, ipk IPK) Issuer {
var issuer Issuer
issuer.Isk = isk
issuer.Ipk = ipk
return issuer
}
func RandomIssuer(rng *core.RAND) Issuer {
isk := RandomISK(rng)
ipk := RandomIPK(&isk, rng)
issuer := NewIssuer(isk, ipk)
return issuer
}