|
| 1 | +// Reference: https://eprint.iacr.org/2018/499.pdf |
| 2 | +// 2 out of 2 party threhsold signature scheme |
| 3 | +// Figure 1 and Protocol 1 and 2 |
| 4 | + |
| 5 | +package dkls |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/ecdsa" |
| 9 | + "crypto/elliptic" |
| 10 | + "crypto/rand" |
| 11 | + "errors" |
| 12 | + "math/big" |
| 13 | + |
| 14 | + "github.com/cloudflare/circl/group" |
| 15 | +) |
| 16 | + |
| 17 | +// Input: myGroup, the group we operate in |
| 18 | +// Input: sk, the real secret key |
| 19 | +// Output: share1, share2 the multiplicative secret key shares for 2 parties. |
| 20 | +func KeyShareGen(myGroup group.Group, sk group.Scalar) (group.Scalar, group.Scalar) { |
| 21 | + share1 := myGroup.RandomNonZeroScalar(rand.Reader) |
| 22 | + share1Inv := myGroup.NewScalar() |
| 23 | + share1Inv.Inv(share1) |
| 24 | + |
| 25 | + share2 := myGroup.NewScalar() |
| 26 | + share2.Mul(share1Inv, sk) |
| 27 | + |
| 28 | + return share1, share2 |
| 29 | +} |
| 30 | + |
| 31 | +func hashToInt(hash []byte, c elliptic.Curve) *big.Int { |
| 32 | + orderBits := c.Params().N.BitLen() |
| 33 | + orderBytes := (orderBits + 7) / 8 |
| 34 | + |
| 35 | + if len(hash) > orderBytes { |
| 36 | + hash = hash[:orderBytes] |
| 37 | + } |
| 38 | + |
| 39 | + ret := new(big.Int).SetBytes(hash) |
| 40 | + excess := len(hash)*8 - orderBits |
| 41 | + if excess > 0 { |
| 42 | + ret.Rsh(ret, uint(excess)) |
| 43 | + } |
| 44 | + return ret |
| 45 | +} |
| 46 | + |
| 47 | +// ECDSA threshold signature verification |
| 48 | +// Input: (r,s), the signature |
| 49 | +// Input: hashMSG, the message |
| 50 | +// Input: publicKey, the ECDSA public key |
| 51 | +// Output: verification passed or not |
| 52 | +func Verify(r, s group.Scalar, hashMSG []byte, publicKey *ecdsa.PublicKey) error { |
| 53 | + rBig := new(big.Int) |
| 54 | + sBig := new(big.Int) |
| 55 | + |
| 56 | + rByte, errByte := r.MarshalBinary() |
| 57 | + if errByte != nil { |
| 58 | + panic(errByte) |
| 59 | + } |
| 60 | + rBig.SetBytes(rByte) |
| 61 | + |
| 62 | + sByte, errByte := s.MarshalBinary() |
| 63 | + if errByte != nil { |
| 64 | + panic(errByte) |
| 65 | + } |
| 66 | + sBig.SetBytes(sByte) |
| 67 | + |
| 68 | + verify := ecdsa.Verify(publicKey, hashMSG, rBig, sBig) |
| 69 | + if !verify { |
| 70 | + return errors.New("ECDSA threshold verification failed") |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
0 commit comments