github.com/ronperry/cryptoedge@v0.0.0-20150815114006-cc363e290743/singhdas/signer.go (about) 1 package singhdas 2 3 import ( 4 "github.com/ronperry/cryptoedge/eccutil" 5 "math/big" 6 ) 7 8 // Signer is a signer instance 9 type Signer struct { 10 privkey *big.Int 11 pubkey *eccutil.Point 12 curve *eccutil.Curve 13 } 14 15 // SignParamsInt encapsulates a single signature temporary key 16 type SignParamsInt struct { 17 k *big.Int // Private, never share 18 Q *eccutil.Point // Public, given to requestor 19 r *big.Int 20 used bool // Never use twice 21 } 22 23 // BlindSignatureInt represents a single blind signature 24 type BlindSignatureInt struct { 25 SignerBlind *eccutil.Point 26 S *big.Int 27 } 28 29 // NewSigner returns a new signer 30 func NewSigner(privkey []byte, pubkey *eccutil.Point, curve *eccutil.Curve) *Signer { 31 s := new(Signer) 32 s.privkey = new(big.Int) 33 s.privkey = s.privkey.SetBytes(privkey) 34 s.pubkey = pubkey 35 s.curve = curve 36 return s 37 } 38 39 // NewRequest issues a new request keypair 40 func (signer Signer) NewRequest() (signparams *SignParamsInt, err error) { 41 var loopcount int 42 for { 43 if loopcount > eccutil.MaxLoopCount { 44 return nil, eccutil.ErrMaxLoop 45 } 46 loopcount++ 47 Kt, Qt, err := signer.curve.GenerateKey() 48 if err != nil { 49 continue 50 } 51 r, err := signer.curve.ExtractR(Qt) 52 if err != nil { 53 continue 54 } 55 if !signer.curve.WithinRange(r) { 56 continue 57 } 58 sp := new(SignParamsInt) 59 sp.k = eccutil.BytesToInt(Kt) 60 sp.Q = Qt 61 sp.r = r 62 sp.used = false 63 return sp, nil 64 } 65 } 66 67 // Sign signs a blinded message 68 func (signer Signer) Sign(blindMessage *BlindMessageInt, signParams *SignParamsInt) (S *BlindSignatureInt, err error) { 69 //signer.privkey * signParams.r + signParams.k * blindMessage.Message mod p 70 if signParams.used { 71 return nil, eccutil.ErrParamReuse 72 } 73 _, err = signer.curve.TestParams(blindMessage.Message, signer.privkey, signParams.r, signParams.k) 74 if err != nil { 75 return nil, eccutil.ErrBadBlindParam 76 } 77 78 Ar1 := eccutil.ManyMult(signer.privkey, signParams.r) 79 Km := eccutil.ManyMult(signParams.k, blindMessage.Message) 80 St := eccutil.ManyAdd(Ar1, Km) 81 Sm := signer.curve.Mod(St) 82 signParams.used = true 83 S = new(BlindSignatureInt) 84 S.S = Sm 85 S.SignerBlind = blindMessage.SignerBlind 86 return S, nil 87 }