github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/crypto/ed25519/chainkd/chainkd.go (about) 1 package chainkd 2 3 import ( 4 "crypto/ed25519" 5 "crypto/hmac" 6 "crypto/rand" 7 "crypto/sha512" 8 "io" 9 10 "github.com/bytom/bytom/crypto/ed25519/ecmath" 11 ) 12 13 type ( 14 //XPrv external private key 15 XPrv [64]byte 16 //XPub external public key 17 XPub [64]byte 18 ) 19 20 // NewXPrv takes a source of random bytes and produces a new XPrv. 21 // If r is nil, crypto/rand.Reader is used. 22 func NewXPrv(r io.Reader) (xprv XPrv, err error) { 23 if r == nil { 24 r = rand.Reader 25 } 26 var entropy [64]byte 27 _, err = io.ReadFull(r, entropy[:]) 28 if err != nil { 29 return xprv, err 30 } 31 return RootXPrv(entropy[:]), nil 32 } 33 34 // RootXPrv takes a seed binary string and produces a new xprv. 35 func RootXPrv(seed []byte) (xprv XPrv) { 36 h := hmac.New(sha512.New, []byte{'R', 'o', 'o', 't'}) 37 h.Write(seed) 38 h.Sum(xprv[:0]) 39 pruneRootScalar(xprv[:32]) 40 return 41 } 42 43 // XPub derives an extended public key from a given xprv. 44 func (xprv XPrv) XPub() (xpub XPub) { 45 var scalar ecmath.Scalar 46 copy(scalar[:], xprv[:32]) 47 48 var P ecmath.Point 49 P.ScMulBase(&scalar) 50 buf := P.Encode() 51 52 copy(xpub[:32], buf[:]) 53 copy(xpub[32:], xprv[32:]) 54 55 return 56 } 57 58 // Child derives a child xprv based on `selector` string and `hardened` flag. 59 // If `hardened` is false, child xpub can be derived independently 60 // from the parent xpub without using the parent xprv. 61 // If `hardened` is true, child key can only be derived from the parent xprv. 62 func (xprv XPrv) Child(sel []byte, hardened bool) XPrv { 63 if hardened { 64 return xprv.hardenedChild(sel) 65 } 66 return xprv.nonhardenedChild(sel) 67 } 68 69 func (xprv XPrv) hardenedChild(sel []byte) (res XPrv) { 70 h := hmac.New(sha512.New, xprv[32:]) 71 h.Write([]byte{'H'}) 72 h.Write(xprv[:32]) 73 h.Write(sel) 74 h.Sum(res[:0]) 75 pruneRootScalar(res[:32]) 76 return 77 } 78 79 func (xprv XPrv) nonhardenedChild(sel []byte) (res XPrv) { 80 xpub := xprv.XPub() 81 82 h := hmac.New(sha512.New, xpub[32:]) 83 h.Write([]byte{'N'}) 84 h.Write(xpub[:32]) 85 h.Write(sel) 86 h.Sum(res[:0]) 87 88 pruneIntermediateScalar(res[:32]) 89 90 sum := int(0) 91 92 sum = int(xprv[0]) + int(res[0]) + (sum >> 8) 93 res[0] = byte(sum & 0xff) 94 sum = int(xprv[1]) + int(res[1]) + (sum >> 8) 95 res[1] = byte(sum & 0xff) 96 sum = int(xprv[2]) + int(res[2]) + (sum >> 8) 97 res[2] = byte(sum & 0xff) 98 sum = int(xprv[3]) + int(res[3]) + (sum >> 8) 99 res[3] = byte(sum & 0xff) 100 sum = int(xprv[4]) + int(res[4]) + (sum >> 8) 101 res[4] = byte(sum & 0xff) 102 sum = int(xprv[5]) + int(res[5]) + (sum >> 8) 103 res[5] = byte(sum & 0xff) 104 sum = int(xprv[6]) + int(res[6]) + (sum >> 8) 105 res[6] = byte(sum & 0xff) 106 sum = int(xprv[7]) + int(res[7]) + (sum >> 8) 107 res[7] = byte(sum & 0xff) 108 sum = int(xprv[8]) + int(res[8]) + (sum >> 8) 109 res[8] = byte(sum & 0xff) 110 sum = int(xprv[9]) + int(res[9]) + (sum >> 8) 111 res[9] = byte(sum & 0xff) 112 sum = int(xprv[10]) + int(res[10]) + (sum >> 8) 113 res[10] = byte(sum & 0xff) 114 sum = int(xprv[11]) + int(res[11]) + (sum >> 8) 115 res[11] = byte(sum & 0xff) 116 sum = int(xprv[12]) + int(res[12]) + (sum >> 8) 117 res[12] = byte(sum & 0xff) 118 sum = int(xprv[13]) + int(res[13]) + (sum >> 8) 119 res[13] = byte(sum & 0xff) 120 sum = int(xprv[14]) + int(res[14]) + (sum >> 8) 121 res[14] = byte(sum & 0xff) 122 sum = int(xprv[15]) + int(res[15]) + (sum >> 8) 123 res[15] = byte(sum & 0xff) 124 sum = int(xprv[16]) + int(res[16]) + (sum >> 8) 125 res[16] = byte(sum & 0xff) 126 sum = int(xprv[17]) + int(res[17]) + (sum >> 8) 127 res[17] = byte(sum & 0xff) 128 sum = int(xprv[18]) + int(res[18]) + (sum >> 8) 129 res[18] = byte(sum & 0xff) 130 sum = int(xprv[19]) + int(res[19]) + (sum >> 8) 131 res[19] = byte(sum & 0xff) 132 sum = int(xprv[20]) + int(res[20]) + (sum >> 8) 133 res[20] = byte(sum & 0xff) 134 sum = int(xprv[21]) + int(res[21]) + (sum >> 8) 135 res[21] = byte(sum & 0xff) 136 sum = int(xprv[22]) + int(res[22]) + (sum >> 8) 137 res[22] = byte(sum & 0xff) 138 sum = int(xprv[23]) + int(res[23]) + (sum >> 8) 139 res[23] = byte(sum & 0xff) 140 sum = int(xprv[24]) + int(res[24]) + (sum >> 8) 141 res[24] = byte(sum & 0xff) 142 sum = int(xprv[25]) + int(res[25]) + (sum >> 8) 143 res[25] = byte(sum & 0xff) 144 sum = int(xprv[26]) + int(res[26]) + (sum >> 8) 145 res[26] = byte(sum & 0xff) 146 sum = int(xprv[27]) + int(res[27]) + (sum >> 8) 147 res[27] = byte(sum & 0xff) 148 sum = int(xprv[28]) + int(res[28]) + (sum >> 8) 149 res[28] = byte(sum & 0xff) 150 sum = int(xprv[29]) + int(res[29]) + (sum >> 8) 151 res[29] = byte(sum & 0xff) 152 sum = int(xprv[30]) + int(res[30]) + (sum >> 8) 153 res[30] = byte(sum & 0xff) 154 sum = int(xprv[31]) + int(res[31]) + (sum >> 8) 155 res[31] = byte(sum & 0xff) 156 157 if (sum >> 8) != 0 { 158 panic("sum does not fit in 256-bit int") 159 } 160 return 161 } 162 163 // Child derives a child xpub based on `selector` string. 164 // The corresponding child xprv can be derived from the parent xprv 165 // using non-hardened derivation: `parentxprv.Child(sel, false)`. 166 func (xpub XPub) Child(sel []byte) (res XPub) { 167 h := hmac.New(sha512.New, xpub[32:]) 168 h.Write([]byte{'N'}) 169 h.Write(xpub[:32]) 170 h.Write(sel) 171 h.Sum(res[:0]) 172 173 pruneIntermediateScalar(res[:32]) 174 175 var ( 176 f ecmath.Scalar 177 F ecmath.Point 178 ) 179 copy(f[:], res[:32]) 180 F.ScMulBase(&f) 181 182 var ( 183 pubkey [32]byte 184 P ecmath.Point 185 ) 186 copy(pubkey[:], xpub[:32]) 187 _, ok := P.Decode(pubkey) 188 if !ok { 189 panic("XPub should have been validated on initialization") 190 } 191 192 P.Add(&P, &F) 193 pubkey = P.Encode() 194 copy(res[:32], pubkey[:]) 195 196 return 197 } 198 199 // Derive generates a child xprv by recursively deriving 200 // non-hardened child xprvs over the list of selectors: 201 // `Derive([a,b,c,...]) == Child(a).Child(b).Child(c)...` 202 func (xprv XPrv) Derive(path [][]byte) XPrv { 203 res := xprv 204 for _, p := range path { 205 res = res.Child(p, false) 206 } 207 return res 208 } 209 210 // Derive generates a child xpub by recursively deriving 211 // non-hardened child xpubs over the list of selectors: 212 // `Derive([a,b,c,...]) == Child(a).Child(b).Child(c)...` 213 func (xpub XPub) Derive(path [][]byte) XPub { 214 res := xpub 215 for _, p := range path { 216 res = res.Child(p) 217 } 218 return res 219 } 220 221 // Sign creates an EdDSA signature using expanded private key 222 // derived from the xprv. 223 func (xprv XPrv) Sign(msg []byte) []byte { 224 return Ed25519InnerSign(xprv.ExpandedPrivateKey(), msg) 225 } 226 227 // Verify checks an EdDSA signature using public key 228 // extracted from the first 32 bytes of the xpub. 229 func (xpub XPub) Verify(msg []byte, sig []byte) bool { 230 return ed25519.Verify(xpub.PublicKey(), msg, sig) 231 } 232 233 // ExpandedPrivateKey generates a 64-byte key where 234 // the first half is the scalar copied from xprv, 235 // and the second half is the `prefix` is generated via PRF 236 // from the xprv. 237 func (xprv XPrv) ExpandedPrivateKey() ExpandedPrivateKey { 238 var res [64]byte 239 h := hmac.New(sha512.New, []byte{'E', 'x', 'p', 'a', 'n', 'd'}) 240 h.Write(xprv[:]) 241 h.Sum(res[:0]) 242 copy(res[:32], xprv[:32]) 243 return res[:] 244 } 245 246 // PublicKey extracts the ed25519 public key from an xpub. 247 func (xpub XPub) PublicKey() ed25519.PublicKey { 248 return ed25519.PublicKey(xpub[:32]) 249 } 250 251 // s must be >= 32 bytes long and gets rewritten in place. 252 // This is NOT the same pruning as in Ed25519: it additionally clears the third 253 // highest bit to ensure subkeys do not overflow the second highest bit. 254 func pruneRootScalar(s []byte) { 255 s[0] &= 248 256 s[31] &= 31 // clear top 3 bits 257 s[31] |= 64 // set second highest bit 258 } 259 260 // Clears lowest 3 bits and highest 23 bits of `f`. 261 func pruneIntermediateScalar(f []byte) { 262 f[0] &= 248 // clear bottom 3 bits 263 f[29] &= 1 // clear 7 high bits 264 f[30] = 0 // clear 8 bits 265 f[31] = 0 // clear 8 bits 266 }