-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclsignature.go
92 lines (75 loc) · 2.97 KB
/
clsignature.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
// Copyright 2016 Maarten Everts. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gabi
import (
"crypto/rand"
"math/big"
)
// CLSignature is a data structure for holding a Camenisch-Lysyanskaya signature.
type CLSignature struct {
A *big.Int
E *big.Int `json:"e"`
V *big.Int `json:"v"`
KeyshareP *big.Int `json:"KeyshareP"` // R_0^{keysharesecret}, necessary for verification
}
// SignMessageBlock signs a message block (ms) and a commitment (U) using the
// Camenisch-Lysyanskaya signature scheme as used in the IdeMix system.
func signMessageBlockAndCommitment(sk *PrivateKey, pk *PublicKey, U *big.Int, ms []*big.Int, Rs []*big.Int) (*CLSignature, error) {
R := representToBases(Rs, ms, pk.N, pk.Params.Lm)
vTilde, _ := RandomBigInt(pk.Params.Lv - 1)
twoLv := new(big.Int).Lsh(bigONE, pk.Params.Lv-1)
v := new(big.Int).Add(twoLv, vTilde)
// Q = inv( S^v * R * U) * Z
numerator := new(big.Int).Exp(pk.S, v, pk.N)
numerator.Mul(numerator, R).Mul(numerator, U).Mod(numerator, pk.N)
invNumerator, _ := modInverse(numerator, pk.N)
Q := new(big.Int).Mul(pk.Z, invNumerator)
Q.Mod(Q, pk.N)
e, err := randomPrimeInRange(rand.Reader, pk.Params.Le-1, pk.Params.LePrime-1)
if err != nil {
return nil, err
}
order := new(big.Int).Mul(sk.PPrime, sk.QPrime)
d, _ := modInverse(e, order)
A := new(big.Int).Exp(Q, d, pk.N)
// TODO: this is probably open to side channel attacks, maybe use a
// safe (raw) RSA signature?
return &CLSignature{A: A, E: e, V: v}, nil
}
// SignMessageBlock signs a message block (ms) using the Camenisch-Lysyanskaya
// signature scheme as used in the IdeMix system.
func SignMessageBlock(sk *PrivateKey, pk *PublicKey, ms []*big.Int) (*CLSignature, error) {
return signMessageBlockAndCommitment(sk, pk, big.NewInt(1), ms, pk.R)
}
// Verify checks whether the signature is correct while being given a public key
// and the messages.
func (s *CLSignature) Verify(pk *PublicKey, ms []*big.Int) bool {
// First check that e is in the range [2^{l_e - 1}, 2^{l_e - 1} + 2^{l_e_prime - 1}]
start := new(big.Int).Lsh(bigONE, pk.Params.Le-1)
end := new(big.Int).Lsh(bigONE, pk.Params.LePrime-1)
end.Add(end, start)
if s.E.Cmp(start) < 0 || s.E.Cmp(end) > 0 {
return false
}
// Q = A^e * R * S^v
Ae := new(big.Int).Exp(s.A, s.E, pk.N)
R := representToBases(pk.R, ms, pk.N, pk.Params.Lm)
if s.KeyshareP != nil {
R.Mul(R, s.KeyshareP)
}
Sv := modPow(pk.S, s.V, pk.N)
Q := new(big.Int).Mul(Ae, R)
Q.Mul(Q, Sv).Mod(Q, pk.N)
// Signature verifies if Q == Z
return pk.Z.Cmp(Q) == 0
}
// Randomize returns a randomized copy of the signature.
func (s *CLSignature) Randomize(pk *PublicKey) *CLSignature {
r, _ := RandomBigInt(pk.Params.LRA)
APrime := new(big.Int).Mul(s.A, new(big.Int).Exp(pk.S, r, pk.N))
APrime.Mod(APrime, pk.N)
t := new(big.Int).Mul(s.E, r)
VPrime := new(big.Int).Sub(s.V, t)
return &CLSignature{A: APrime, E: new(big.Int).Set(s.E), V: VPrime}
}