field.go 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. // Copyright (c) 2013-2016 The btcsuite developers
  2. // Copyright (c) 2013-2016 Dave Collins
  3. // Use of this source code is governed by an ISC
  4. // license that can be found in the LICENSE file.
  5. package btcec
  6. // References:
  7. // [HAC]: Handbook of Applied Cryptography Menezes, van Oorschot, Vanstone.
  8. // http://cacr.uwaterloo.ca/hac/
  9. // All elliptic curve operations for secp256k1 are done in a finite field
  10. // characterized by a 256-bit prime. Given this precision is larger than the
  11. // biggest available native type, obviously some form of bignum math is needed.
  12. // This package implements specialized fixed-precision field arithmetic rather
  13. // than relying on an arbitrary-precision arithmetic package such as math/big
  14. // for dealing with the field math since the size is known. As a result, rather
  15. // large performance gains are achieved by taking advantage of many
  16. // optimizations not available to arbitrary-precision arithmetic and generic
  17. // modular arithmetic algorithms.
  18. //
  19. // There are various ways to internally represent each finite field element.
  20. // For example, the most obvious representation would be to use an array of 4
  21. // uint64s (64 bits * 4 = 256 bits). However, that representation suffers from
  22. // a couple of issues. First, there is no native Go type large enough to handle
  23. // the intermediate results while adding or multiplying two 64-bit numbers, and
  24. // second there is no space left for overflows when performing the intermediate
  25. // arithmetic between each array element which would lead to expensive carry
  26. // propagation.
  27. //
  28. // Given the above, this implementation represents the the field elements as
  29. // 10 uint32s with each word (array entry) treated as base 2^26. This was
  30. // chosen for the following reasons:
  31. // 1) Most systems at the current time are 64-bit (or at least have 64-bit
  32. // registers available for specialized purposes such as MMX) so the
  33. // intermediate results can typically be done using a native register (and
  34. // using uint64s to avoid the need for additional half-word arithmetic)
  35. // 2) In order to allow addition of the internal words without having to
  36. // propagate the the carry, the max normalized value for each register must
  37. // be less than the number of bits available in the register
  38. // 3) Since we're dealing with 32-bit values, 64-bits of overflow is a
  39. // reasonable choice for #2
  40. // 4) Given the need for 256-bits of precision and the properties stated in #1,
  41. // #2, and #3, the representation which best accommodates this is 10 uint32s
  42. // with base 2^26 (26 bits * 10 = 260 bits, so the final word only needs 22
  43. // bits) which leaves the desired 64 bits (32 * 10 = 320, 320 - 256 = 64) for
  44. // overflow
  45. //
  46. // Since it is so important that the field arithmetic is extremely fast for
  47. // high performance crypto, this package does not perform any validation where
  48. // it ordinarily would. For example, some functions only give the correct
  49. // result is the field is normalized and there is no checking to ensure it is.
  50. // While I typically prefer to ensure all state and input is valid for most
  51. // packages, this code is really only used internally and every extra check
  52. // counts.
  53. import (
  54. "encoding/hex"
  55. )
  56. // Constants used to make the code more readable.
  57. const (
  58. twoBitsMask = 0x3
  59. fourBitsMask = 0xf
  60. sixBitsMask = 0x3f
  61. eightBitsMask = 0xff
  62. )
  63. // Constants related to the field representation.
  64. const (
  65. // fieldWords is the number of words used to internally represent the
  66. // 256-bit value.
  67. fieldWords = 10
  68. // fieldBase is the exponent used to form the numeric base of each word.
  69. // 2^(fieldBase*i) where i is the word position.
  70. fieldBase = 26
  71. // fieldOverflowBits is the minimum number of "overflow" bits for each
  72. // word in the field value.
  73. fieldOverflowBits = 32 - fieldBase
  74. // fieldBaseMask is the mask for the bits in each word needed to
  75. // represent the numeric base of each word (except the most significant
  76. // word).
  77. fieldBaseMask = (1 << fieldBase) - 1
  78. // fieldMSBBits is the number of bits in the most significant word used
  79. // to represent the value.
  80. fieldMSBBits = 256 - (fieldBase * (fieldWords - 1))
  81. // fieldMSBMask is the mask for the bits in the most significant word
  82. // needed to represent the value.
  83. fieldMSBMask = (1 << fieldMSBBits) - 1
  84. // fieldPrimeWordZero is word zero of the secp256k1 prime in the
  85. // internal field representation. It is used during negation.
  86. fieldPrimeWordZero = 0x3fffc2f
  87. // fieldPrimeWordOne is word one of the secp256k1 prime in the
  88. // internal field representation. It is used during negation.
  89. fieldPrimeWordOne = 0x3ffffbf
  90. )
  91. // fieldVal implements optimized fixed-precision arithmetic over the
  92. // secp256k1 finite field. This means all arithmetic is performed modulo
  93. // 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f. It
  94. // represents each 256-bit value as 10 32-bit integers in base 2^26. This
  95. // provides 6 bits of overflow in each word (10 bits in the most significant
  96. // word) for a total of 64 bits of overflow (9*6 + 10 = 64). It only implements
  97. // the arithmetic needed for elliptic curve operations.
  98. //
  99. // The following depicts the internal representation:
  100. // -----------------------------------------------------------------
  101. // | n[9] | n[8] | ... | n[0] |
  102. // | 32 bits available | 32 bits available | ... | 32 bits available |
  103. // | 22 bits for value | 26 bits for value | ... | 26 bits for value |
  104. // | 10 bits overflow | 6 bits overflow | ... | 6 bits overflow |
  105. // | Mult: 2^(26*9) | Mult: 2^(26*8) | ... | Mult: 2^(26*0) |
  106. // -----------------------------------------------------------------
  107. //
  108. // For example, consider the number 2^49 + 1. It would be represented as:
  109. // n[0] = 1
  110. // n[1] = 2^23
  111. // n[2..9] = 0
  112. //
  113. // The full 256-bit value is then calculated by looping i from 9..0 and
  114. // doing sum(n[i] * 2^(26i)) like so:
  115. // n[9] * 2^(26*9) = 0 * 2^234 = 0
  116. // n[8] * 2^(26*8) = 0 * 2^208 = 0
  117. // ...
  118. // n[1] * 2^(26*1) = 2^23 * 2^26 = 2^49
  119. // n[0] * 2^(26*0) = 1 * 2^0 = 1
  120. // Sum: 0 + 0 + ... + 2^49 + 1 = 2^49 + 1
  121. type fieldVal struct {
  122. n [10]uint32
  123. }
  124. // String returns the field value as a human-readable hex string.
  125. func (f fieldVal) String() string {
  126. t := new(fieldVal).Set(&f).Normalize()
  127. return hex.EncodeToString(t.Bytes()[:])
  128. }
  129. // Zero sets the field value to zero. A newly created field value is already
  130. // set to zero. This function can be useful to clear an existing field value
  131. // for reuse.
  132. func (f *fieldVal) Zero() {
  133. f.n[0] = 0
  134. f.n[1] = 0
  135. f.n[2] = 0
  136. f.n[3] = 0
  137. f.n[4] = 0
  138. f.n[5] = 0
  139. f.n[6] = 0
  140. f.n[7] = 0
  141. f.n[8] = 0
  142. f.n[9] = 0
  143. }
  144. // Set sets the field value equal to the passed value.
  145. //
  146. // The field value is returned to support chaining. This enables syntax like:
  147. // f := new(fieldVal).Set(f2).Add(1) so that f = f2 + 1 where f2 is not
  148. // modified.
  149. func (f *fieldVal) Set(val *fieldVal) *fieldVal {
  150. *f = *val
  151. return f
  152. }
  153. // SetInt sets the field value to the passed integer. This is a convenience
  154. // function since it is fairly common to perform some arithemetic with small
  155. // native integers.
  156. //
  157. // The field value is returned to support chaining. This enables syntax such
  158. // as f := new(fieldVal).SetInt(2).Mul(f2) so that f = 2 * f2.
  159. func (f *fieldVal) SetInt(ui uint) *fieldVal {
  160. f.Zero()
  161. f.n[0] = uint32(ui)
  162. return f
  163. }
  164. // SetBytes packs the passed 32-byte big-endian value into the internal field
  165. // value representation.
  166. //
  167. // The field value is returned to support chaining. This enables syntax like:
  168. // f := new(fieldVal).SetBytes(byteArray).Mul(f2) so that f = ba * f2.
  169. func (f *fieldVal) SetBytes(b *[32]byte) *fieldVal {
  170. // Pack the 256 total bits across the 10 uint32 words with a max of
  171. // 26-bits per word. This could be done with a couple of for loops,
  172. // but this unrolled version is significantly faster. Benchmarks show
  173. // this is about 34 times faster than the variant which uses loops.
  174. f.n[0] = uint32(b[31]) | uint32(b[30])<<8 | uint32(b[29])<<16 |
  175. (uint32(b[28])&twoBitsMask)<<24
  176. f.n[1] = uint32(b[28])>>2 | uint32(b[27])<<6 | uint32(b[26])<<14 |
  177. (uint32(b[25])&fourBitsMask)<<22
  178. f.n[2] = uint32(b[25])>>4 | uint32(b[24])<<4 | uint32(b[23])<<12 |
  179. (uint32(b[22])&sixBitsMask)<<20
  180. f.n[3] = uint32(b[22])>>6 | uint32(b[21])<<2 | uint32(b[20])<<10 |
  181. uint32(b[19])<<18
  182. f.n[4] = uint32(b[18]) | uint32(b[17])<<8 | uint32(b[16])<<16 |
  183. (uint32(b[15])&twoBitsMask)<<24
  184. f.n[5] = uint32(b[15])>>2 | uint32(b[14])<<6 | uint32(b[13])<<14 |
  185. (uint32(b[12])&fourBitsMask)<<22
  186. f.n[6] = uint32(b[12])>>4 | uint32(b[11])<<4 | uint32(b[10])<<12 |
  187. (uint32(b[9])&sixBitsMask)<<20
  188. f.n[7] = uint32(b[9])>>6 | uint32(b[8])<<2 | uint32(b[7])<<10 |
  189. uint32(b[6])<<18
  190. f.n[8] = uint32(b[5]) | uint32(b[4])<<8 | uint32(b[3])<<16 |
  191. (uint32(b[2])&twoBitsMask)<<24
  192. f.n[9] = uint32(b[2])>>2 | uint32(b[1])<<6 | uint32(b[0])<<14
  193. return f
  194. }
  195. // SetByteSlice packs the passed big-endian value into the internal field value
  196. // representation. Only the first 32-bytes are used. As a result, it is up to
  197. // the caller to ensure numbers of the appropriate size are used or the value
  198. // will be truncated.
  199. //
  200. // The field value is returned to support chaining. This enables syntax like:
  201. // f := new(fieldVal).SetByteSlice(byteSlice)
  202. func (f *fieldVal) SetByteSlice(b []byte) *fieldVal {
  203. var b32 [32]byte
  204. for i := 0; i < len(b); i++ {
  205. if i < 32 {
  206. b32[i+(32-len(b))] = b[i]
  207. }
  208. }
  209. return f.SetBytes(&b32)
  210. }
  211. // SetHex decodes the passed big-endian hex string into the internal field value
  212. // representation. Only the first 32-bytes are used.
  213. //
  214. // The field value is returned to support chaining. This enables syntax like:
  215. // f := new(fieldVal).SetHex("0abc").Add(1) so that f = 0x0abc + 1
  216. func (f *fieldVal) SetHex(hexString string) *fieldVal {
  217. if len(hexString)%2 != 0 {
  218. hexString = "0" + hexString
  219. }
  220. bytes, _ := hex.DecodeString(hexString)
  221. return f.SetByteSlice(bytes)
  222. }
  223. // Normalize normalizes the internal field words into the desired range and
  224. // performs fast modular reduction over the secp256k1 prime by making use of the
  225. // special form of the prime.
  226. func (f *fieldVal) Normalize() *fieldVal {
  227. // The field representation leaves 6 bits of overflow in each word so
  228. // intermediate calculations can be performed without needing to
  229. // propagate the carry to each higher word during the calculations. In
  230. // order to normalize, we need to "compact" the full 256-bit value to
  231. // the right while propagating any carries through to the high order
  232. // word.
  233. //
  234. // Since this field is doing arithmetic modulo the secp256k1 prime, we
  235. // also need to perform modular reduction over the prime.
  236. //
  237. // Per [HAC] section 14.3.4: Reduction method of moduli of special form,
  238. // when the modulus is of the special form m = b^t - c, highly efficient
  239. // reduction can be achieved.
  240. //
  241. // The secp256k1 prime is equivalent to 2^256 - 4294968273, so it fits
  242. // this criteria.
  243. //
  244. // 4294968273 in field representation (base 2^26) is:
  245. // n[0] = 977
  246. // n[1] = 64
  247. // That is to say (2^26 * 64) + 977 = 4294968273
  248. //
  249. // The algorithm presented in the referenced section typically repeats
  250. // until the quotient is zero. However, due to our field representation
  251. // we already know to within one reduction how many times we would need
  252. // to repeat as it's the uppermost bits of the high order word. Thus we
  253. // can simply multiply the magnitude by the field representation of the
  254. // prime and do a single iteration. After this step there might be an
  255. // additional carry to bit 256 (bit 22 of the high order word).
  256. t9 := f.n[9]
  257. m := t9 >> fieldMSBBits
  258. t9 = t9 & fieldMSBMask
  259. t0 := f.n[0] + m*977
  260. t1 := (t0 >> fieldBase) + f.n[1] + (m << 6)
  261. t0 = t0 & fieldBaseMask
  262. t2 := (t1 >> fieldBase) + f.n[2]
  263. t1 = t1 & fieldBaseMask
  264. t3 := (t2 >> fieldBase) + f.n[3]
  265. t2 = t2 & fieldBaseMask
  266. t4 := (t3 >> fieldBase) + f.n[4]
  267. t3 = t3 & fieldBaseMask
  268. t5 := (t4 >> fieldBase) + f.n[5]
  269. t4 = t4 & fieldBaseMask
  270. t6 := (t5 >> fieldBase) + f.n[6]
  271. t5 = t5 & fieldBaseMask
  272. t7 := (t6 >> fieldBase) + f.n[7]
  273. t6 = t6 & fieldBaseMask
  274. t8 := (t7 >> fieldBase) + f.n[8]
  275. t7 = t7 & fieldBaseMask
  276. t9 = (t8 >> fieldBase) + t9
  277. t8 = t8 & fieldBaseMask
  278. // At this point, the magnitude is guaranteed to be one, however, the
  279. // value could still be greater than the prime if there was either a
  280. // carry through to bit 256 (bit 22 of the higher order word) or the
  281. // value is greater than or equal to the field characteristic. The
  282. // following determines if either or these conditions are true and does
  283. // the final reduction in constant time.
  284. //
  285. // Note that the if/else statements here intentionally do the bitwise
  286. // operators even when it won't change the value to ensure constant time
  287. // between the branches. Also note that 'm' will be zero when neither
  288. // of the aforementioned conditions are true and the value will not be
  289. // changed when 'm' is zero.
  290. m = 1
  291. if t9 == fieldMSBMask {
  292. m &= 1
  293. } else {
  294. m &= 0
  295. }
  296. if t2&t3&t4&t5&t6&t7&t8 == fieldBaseMask {
  297. m &= 1
  298. } else {
  299. m &= 0
  300. }
  301. if ((t0+977)>>fieldBase + t1 + 64) > fieldBaseMask {
  302. m &= 1
  303. } else {
  304. m &= 0
  305. }
  306. if t9>>fieldMSBBits != 0 {
  307. m |= 1
  308. } else {
  309. m |= 0
  310. }
  311. t0 = t0 + m*977
  312. t1 = (t0 >> fieldBase) + t1 + (m << 6)
  313. t0 = t0 & fieldBaseMask
  314. t2 = (t1 >> fieldBase) + t2
  315. t1 = t1 & fieldBaseMask
  316. t3 = (t2 >> fieldBase) + t3
  317. t2 = t2 & fieldBaseMask
  318. t4 = (t3 >> fieldBase) + t4
  319. t3 = t3 & fieldBaseMask
  320. t5 = (t4 >> fieldBase) + t5
  321. t4 = t4 & fieldBaseMask
  322. t6 = (t5 >> fieldBase) + t6
  323. t5 = t5 & fieldBaseMask
  324. t7 = (t6 >> fieldBase) + t7
  325. t6 = t6 & fieldBaseMask
  326. t8 = (t7 >> fieldBase) + t8
  327. t7 = t7 & fieldBaseMask
  328. t9 = (t8 >> fieldBase) + t9
  329. t8 = t8 & fieldBaseMask
  330. t9 = t9 & fieldMSBMask // Remove potential multiple of 2^256.
  331. // Finally, set the normalized and reduced words.
  332. f.n[0] = t0
  333. f.n[1] = t1
  334. f.n[2] = t2
  335. f.n[3] = t3
  336. f.n[4] = t4
  337. f.n[5] = t5
  338. f.n[6] = t6
  339. f.n[7] = t7
  340. f.n[8] = t8
  341. f.n[9] = t9
  342. return f
  343. }
  344. // PutBytes unpacks the field value to a 32-byte big-endian value using the
  345. // passed byte array. There is a similar function, Bytes, which unpacks the
  346. // field value into a new array and returns that. This version is provided
  347. // since it can be useful to cut down on the number of allocations by allowing
  348. // the caller to reuse a buffer.
  349. //
  350. // The field value must be normalized for this function to return the correct
  351. // result.
  352. func (f *fieldVal) PutBytes(b *[32]byte) {
  353. // Unpack the 256 total bits from the 10 uint32 words with a max of
  354. // 26-bits per word. This could be done with a couple of for loops,
  355. // but this unrolled version is a bit faster. Benchmarks show this is
  356. // about 10 times faster than the variant which uses loops.
  357. b[31] = byte(f.n[0] & eightBitsMask)
  358. b[30] = byte((f.n[0] >> 8) & eightBitsMask)
  359. b[29] = byte((f.n[0] >> 16) & eightBitsMask)
  360. b[28] = byte((f.n[0]>>24)&twoBitsMask | (f.n[1]&sixBitsMask)<<2)
  361. b[27] = byte((f.n[1] >> 6) & eightBitsMask)
  362. b[26] = byte((f.n[1] >> 14) & eightBitsMask)
  363. b[25] = byte((f.n[1]>>22)&fourBitsMask | (f.n[2]&fourBitsMask)<<4)
  364. b[24] = byte((f.n[2] >> 4) & eightBitsMask)
  365. b[23] = byte((f.n[2] >> 12) & eightBitsMask)
  366. b[22] = byte((f.n[2]>>20)&sixBitsMask | (f.n[3]&twoBitsMask)<<6)
  367. b[21] = byte((f.n[3] >> 2) & eightBitsMask)
  368. b[20] = byte((f.n[3] >> 10) & eightBitsMask)
  369. b[19] = byte((f.n[3] >> 18) & eightBitsMask)
  370. b[18] = byte(f.n[4] & eightBitsMask)
  371. b[17] = byte((f.n[4] >> 8) & eightBitsMask)
  372. b[16] = byte((f.n[4] >> 16) & eightBitsMask)
  373. b[15] = byte((f.n[4]>>24)&twoBitsMask | (f.n[5]&sixBitsMask)<<2)
  374. b[14] = byte((f.n[5] >> 6) & eightBitsMask)
  375. b[13] = byte((f.n[5] >> 14) & eightBitsMask)
  376. b[12] = byte((f.n[5]>>22)&fourBitsMask | (f.n[6]&fourBitsMask)<<4)
  377. b[11] = byte((f.n[6] >> 4) & eightBitsMask)
  378. b[10] = byte((f.n[6] >> 12) & eightBitsMask)
  379. b[9] = byte((f.n[6]>>20)&sixBitsMask | (f.n[7]&twoBitsMask)<<6)
  380. b[8] = byte((f.n[7] >> 2) & eightBitsMask)
  381. b[7] = byte((f.n[7] >> 10) & eightBitsMask)
  382. b[6] = byte((f.n[7] >> 18) & eightBitsMask)
  383. b[5] = byte(f.n[8] & eightBitsMask)
  384. b[4] = byte((f.n[8] >> 8) & eightBitsMask)
  385. b[3] = byte((f.n[8] >> 16) & eightBitsMask)
  386. b[2] = byte((f.n[8]>>24)&twoBitsMask | (f.n[9]&sixBitsMask)<<2)
  387. b[1] = byte((f.n[9] >> 6) & eightBitsMask)
  388. b[0] = byte((f.n[9] >> 14) & eightBitsMask)
  389. }
  390. // Bytes unpacks the field value to a 32-byte big-endian value. See PutBytes
  391. // for a variant that allows the a buffer to be passed which can be useful to
  392. // to cut down on the number of allocations by allowing the caller to reuse a
  393. // buffer.
  394. //
  395. // The field value must be normalized for this function to return correct
  396. // result.
  397. func (f *fieldVal) Bytes() *[32]byte {
  398. b := new([32]byte)
  399. f.PutBytes(b)
  400. return b
  401. }
  402. // IsZero returns whether or not the field value is equal to zero.
  403. func (f *fieldVal) IsZero() bool {
  404. // The value can only be zero if no bits are set in any of the words.
  405. // This is a constant time implementation.
  406. bits := f.n[0] | f.n[1] | f.n[2] | f.n[3] | f.n[4] |
  407. f.n[5] | f.n[6] | f.n[7] | f.n[8] | f.n[9]
  408. return bits == 0
  409. }
  410. // IsOdd returns whether or not the field value is an odd number.
  411. //
  412. // The field value must be normalized for this function to return correct
  413. // result.
  414. func (f *fieldVal) IsOdd() bool {
  415. // Only odd numbers have the bottom bit set.
  416. return f.n[0]&1 == 1
  417. }
  418. // Equals returns whether or not the two field values are the same. Both
  419. // field values being compared must be normalized for this function to return
  420. // the correct result.
  421. func (f *fieldVal) Equals(val *fieldVal) bool {
  422. // Xor only sets bits when they are different, so the two field values
  423. // can only be the same if no bits are set after xoring each word.
  424. // This is a constant time implementation.
  425. bits := (f.n[0] ^ val.n[0]) | (f.n[1] ^ val.n[1]) | (f.n[2] ^ val.n[2]) |
  426. (f.n[3] ^ val.n[3]) | (f.n[4] ^ val.n[4]) | (f.n[5] ^ val.n[5]) |
  427. (f.n[6] ^ val.n[6]) | (f.n[7] ^ val.n[7]) | (f.n[8] ^ val.n[8]) |
  428. (f.n[9] ^ val.n[9])
  429. return bits == 0
  430. }
  431. // NegateVal negates the passed value and stores the result in f. The caller
  432. // must provide the magnitude of the passed value for a correct result.
  433. //
  434. // The field value is returned to support chaining. This enables syntax like:
  435. // f.NegateVal(f2).AddInt(1) so that f = -f2 + 1.
  436. func (f *fieldVal) NegateVal(val *fieldVal, magnitude uint32) *fieldVal {
  437. // Negation in the field is just the prime minus the value. However,
  438. // in order to allow negation against a field value without having to
  439. // normalize/reduce it first, multiply by the magnitude (that is how
  440. // "far" away it is from the normalized value) to adjust. Also, since
  441. // negating a value pushes it one more order of magnitude away from the
  442. // normalized range, add 1 to compensate.
  443. //
  444. // For some intuition here, imagine you're performing mod 12 arithmetic
  445. // (picture a clock) and you are negating the number 7. So you start at
  446. // 12 (which is of course 0 under mod 12) and count backwards (left on
  447. // the clock) 7 times to arrive at 5. Notice this is just 12-7 = 5.
  448. // Now, assume you're starting with 19, which is a number that is
  449. // already larger than the modulus and congruent to 7 (mod 12). When a
  450. // value is already in the desired range, its magnitude is 1. Since 19
  451. // is an additional "step", its magnitude (mod 12) is 2. Since any
  452. // multiple of the modulus is conguent to zero (mod m), the answer can
  453. // be shortcut by simply mulplying the magnitude by the modulus and
  454. // subtracting. Keeping with the example, this would be (2*12)-19 = 5.
  455. f.n[0] = (magnitude+1)*fieldPrimeWordZero - val.n[0]
  456. f.n[1] = (magnitude+1)*fieldPrimeWordOne - val.n[1]
  457. f.n[2] = (magnitude+1)*fieldBaseMask - val.n[2]
  458. f.n[3] = (magnitude+1)*fieldBaseMask - val.n[3]
  459. f.n[4] = (magnitude+1)*fieldBaseMask - val.n[4]
  460. f.n[5] = (magnitude+1)*fieldBaseMask - val.n[5]
  461. f.n[6] = (magnitude+1)*fieldBaseMask - val.n[6]
  462. f.n[7] = (magnitude+1)*fieldBaseMask - val.n[7]
  463. f.n[8] = (magnitude+1)*fieldBaseMask - val.n[8]
  464. f.n[9] = (magnitude+1)*fieldMSBMask - val.n[9]
  465. return f
  466. }
  467. // Negate negates the field value. The existing field value is modified. The
  468. // caller must provide the magnitude of the field value for a correct result.
  469. //
  470. // The field value is returned to support chaining. This enables syntax like:
  471. // f.Negate().AddInt(1) so that f = -f + 1.
  472. func (f *fieldVal) Negate(magnitude uint32) *fieldVal {
  473. return f.NegateVal(f, magnitude)
  474. }
  475. // AddInt adds the passed integer to the existing field value and stores the
  476. // result in f. This is a convenience function since it is fairly common to
  477. // perform some arithemetic with small native integers.
  478. //
  479. // The field value is returned to support chaining. This enables syntax like:
  480. // f.AddInt(1).Add(f2) so that f = f + 1 + f2.
  481. func (f *fieldVal) AddInt(ui uint) *fieldVal {
  482. // Since the field representation intentionally provides overflow bits,
  483. // it's ok to use carryless addition as the carry bit is safely part of
  484. // the word and will be normalized out.
  485. f.n[0] += uint32(ui)
  486. return f
  487. }
  488. // Add adds the passed value to the existing field value and stores the result
  489. // in f.
  490. //
  491. // The field value is returned to support chaining. This enables syntax like:
  492. // f.Add(f2).AddInt(1) so that f = f + f2 + 1.
  493. func (f *fieldVal) Add(val *fieldVal) *fieldVal {
  494. // Since the field representation intentionally provides overflow bits,
  495. // it's ok to use carryless addition as the carry bit is safely part of
  496. // each word and will be normalized out. This could obviously be done
  497. // in a loop, but the unrolled version is faster.
  498. f.n[0] += val.n[0]
  499. f.n[1] += val.n[1]
  500. f.n[2] += val.n[2]
  501. f.n[3] += val.n[3]
  502. f.n[4] += val.n[4]
  503. f.n[5] += val.n[5]
  504. f.n[6] += val.n[6]
  505. f.n[7] += val.n[7]
  506. f.n[8] += val.n[8]
  507. f.n[9] += val.n[9]
  508. return f
  509. }
  510. // Add2 adds the passed two field values together and stores the result in f.
  511. //
  512. // The field value is returned to support chaining. This enables syntax like:
  513. // f3.Add2(f, f2).AddInt(1) so that f3 = f + f2 + 1.
  514. func (f *fieldVal) Add2(val *fieldVal, val2 *fieldVal) *fieldVal {
  515. // Since the field representation intentionally provides overflow bits,
  516. // it's ok to use carryless addition as the carry bit is safely part of
  517. // each word and will be normalized out. This could obviously be done
  518. // in a loop, but the unrolled version is faster.
  519. f.n[0] = val.n[0] + val2.n[0]
  520. f.n[1] = val.n[1] + val2.n[1]
  521. f.n[2] = val.n[2] + val2.n[2]
  522. f.n[3] = val.n[3] + val2.n[3]
  523. f.n[4] = val.n[4] + val2.n[4]
  524. f.n[5] = val.n[5] + val2.n[5]
  525. f.n[6] = val.n[6] + val2.n[6]
  526. f.n[7] = val.n[7] + val2.n[7]
  527. f.n[8] = val.n[8] + val2.n[8]
  528. f.n[9] = val.n[9] + val2.n[9]
  529. return f
  530. }
  531. // MulInt multiplies the field value by the passed int and stores the result in
  532. // f. Note that this function can overflow if multiplying the value by any of
  533. // the individual words exceeds a max uint32. Therefore it is important that
  534. // the caller ensures no overflows will occur before using this function.
  535. //
  536. // The field value is returned to support chaining. This enables syntax like:
  537. // f.MulInt(2).Add(f2) so that f = 2 * f + f2.
  538. func (f *fieldVal) MulInt(val uint) *fieldVal {
  539. // Since each word of the field representation can hold up to
  540. // fieldOverflowBits extra bits which will be normalized out, it's safe
  541. // to multiply each word without using a larger type or carry
  542. // propagation so long as the values won't overflow a uint32. This
  543. // could obviously be done in a loop, but the unrolled version is
  544. // faster.
  545. ui := uint32(val)
  546. f.n[0] *= ui
  547. f.n[1] *= ui
  548. f.n[2] *= ui
  549. f.n[3] *= ui
  550. f.n[4] *= ui
  551. f.n[5] *= ui
  552. f.n[6] *= ui
  553. f.n[7] *= ui
  554. f.n[8] *= ui
  555. f.n[9] *= ui
  556. return f
  557. }
  558. // Mul multiplies the passed value to the existing field value and stores the
  559. // result in f. Note that this function can overflow if multiplying any
  560. // of the individual words exceeds a max uint32. In practice, this means the
  561. // magnitude of either value involved in the multiplication must be a max of
  562. // 8.
  563. //
  564. // The field value is returned to support chaining. This enables syntax like:
  565. // f.Mul(f2).AddInt(1) so that f = (f * f2) + 1.
  566. func (f *fieldVal) Mul(val *fieldVal) *fieldVal {
  567. return f.Mul2(f, val)
  568. }
  569. // Mul2 multiplies the passed two field values together and stores the result
  570. // result in f. Note that this function can overflow if multiplying any of
  571. // the individual words exceeds a max uint32. In practice, this means the
  572. // magnitude of either value involved in the multiplication must be a max of
  573. // 8.
  574. //
  575. // The field value is returned to support chaining. This enables syntax like:
  576. // f3.Mul2(f, f2).AddInt(1) so that f3 = (f * f2) + 1.
  577. func (f *fieldVal) Mul2(val *fieldVal, val2 *fieldVal) *fieldVal {
  578. // This could be done with a couple of for loops and an array to store
  579. // the intermediate terms, but this unrolled version is significantly
  580. // faster.
  581. // Terms for 2^(fieldBase*0).
  582. m := uint64(val.n[0]) * uint64(val2.n[0])
  583. t0 := m & fieldBaseMask
  584. // Terms for 2^(fieldBase*1).
  585. m = (m >> fieldBase) +
  586. uint64(val.n[0])*uint64(val2.n[1]) +
  587. uint64(val.n[1])*uint64(val2.n[0])
  588. t1 := m & fieldBaseMask
  589. // Terms for 2^(fieldBase*2).
  590. m = (m >> fieldBase) +
  591. uint64(val.n[0])*uint64(val2.n[2]) +
  592. uint64(val.n[1])*uint64(val2.n[1]) +
  593. uint64(val.n[2])*uint64(val2.n[0])
  594. t2 := m & fieldBaseMask
  595. // Terms for 2^(fieldBase*3).
  596. m = (m >> fieldBase) +
  597. uint64(val.n[0])*uint64(val2.n[3]) +
  598. uint64(val.n[1])*uint64(val2.n[2]) +
  599. uint64(val.n[2])*uint64(val2.n[1]) +
  600. uint64(val.n[3])*uint64(val2.n[0])
  601. t3 := m & fieldBaseMask
  602. // Terms for 2^(fieldBase*4).
  603. m = (m >> fieldBase) +
  604. uint64(val.n[0])*uint64(val2.n[4]) +
  605. uint64(val.n[1])*uint64(val2.n[3]) +
  606. uint64(val.n[2])*uint64(val2.n[2]) +
  607. uint64(val.n[3])*uint64(val2.n[1]) +
  608. uint64(val.n[4])*uint64(val2.n[0])
  609. t4 := m & fieldBaseMask
  610. // Terms for 2^(fieldBase*5).
  611. m = (m >> fieldBase) +
  612. uint64(val.n[0])*uint64(val2.n[5]) +
  613. uint64(val.n[1])*uint64(val2.n[4]) +
  614. uint64(val.n[2])*uint64(val2.n[3]) +
  615. uint64(val.n[3])*uint64(val2.n[2]) +
  616. uint64(val.n[4])*uint64(val2.n[1]) +
  617. uint64(val.n[5])*uint64(val2.n[0])
  618. t5 := m & fieldBaseMask
  619. // Terms for 2^(fieldBase*6).
  620. m = (m >> fieldBase) +
  621. uint64(val.n[0])*uint64(val2.n[6]) +
  622. uint64(val.n[1])*uint64(val2.n[5]) +
  623. uint64(val.n[2])*uint64(val2.n[4]) +
  624. uint64(val.n[3])*uint64(val2.n[3]) +
  625. uint64(val.n[4])*uint64(val2.n[2]) +
  626. uint64(val.n[5])*uint64(val2.n[1]) +
  627. uint64(val.n[6])*uint64(val2.n[0])
  628. t6 := m & fieldBaseMask
  629. // Terms for 2^(fieldBase*7).
  630. m = (m >> fieldBase) +
  631. uint64(val.n[0])*uint64(val2.n[7]) +
  632. uint64(val.n[1])*uint64(val2.n[6]) +
  633. uint64(val.n[2])*uint64(val2.n[5]) +
  634. uint64(val.n[3])*uint64(val2.n[4]) +
  635. uint64(val.n[4])*uint64(val2.n[3]) +
  636. uint64(val.n[5])*uint64(val2.n[2]) +
  637. uint64(val.n[6])*uint64(val2.n[1]) +
  638. uint64(val.n[7])*uint64(val2.n[0])
  639. t7 := m & fieldBaseMask
  640. // Terms for 2^(fieldBase*8).
  641. m = (m >> fieldBase) +
  642. uint64(val.n[0])*uint64(val2.n[8]) +
  643. uint64(val.n[1])*uint64(val2.n[7]) +
  644. uint64(val.n[2])*uint64(val2.n[6]) +
  645. uint64(val.n[3])*uint64(val2.n[5]) +
  646. uint64(val.n[4])*uint64(val2.n[4]) +
  647. uint64(val.n[5])*uint64(val2.n[3]) +
  648. uint64(val.n[6])*uint64(val2.n[2]) +
  649. uint64(val.n[7])*uint64(val2.n[1]) +
  650. uint64(val.n[8])*uint64(val2.n[0])
  651. t8 := m & fieldBaseMask
  652. // Terms for 2^(fieldBase*9).
  653. m = (m >> fieldBase) +
  654. uint64(val.n[0])*uint64(val2.n[9]) +
  655. uint64(val.n[1])*uint64(val2.n[8]) +
  656. uint64(val.n[2])*uint64(val2.n[7]) +
  657. uint64(val.n[3])*uint64(val2.n[6]) +
  658. uint64(val.n[4])*uint64(val2.n[5]) +
  659. uint64(val.n[5])*uint64(val2.n[4]) +
  660. uint64(val.n[6])*uint64(val2.n[3]) +
  661. uint64(val.n[7])*uint64(val2.n[2]) +
  662. uint64(val.n[8])*uint64(val2.n[1]) +
  663. uint64(val.n[9])*uint64(val2.n[0])
  664. t9 := m & fieldBaseMask
  665. // Terms for 2^(fieldBase*10).
  666. m = (m >> fieldBase) +
  667. uint64(val.n[1])*uint64(val2.n[9]) +
  668. uint64(val.n[2])*uint64(val2.n[8]) +
  669. uint64(val.n[3])*uint64(val2.n[7]) +
  670. uint64(val.n[4])*uint64(val2.n[6]) +
  671. uint64(val.n[5])*uint64(val2.n[5]) +
  672. uint64(val.n[6])*uint64(val2.n[4]) +
  673. uint64(val.n[7])*uint64(val2.n[3]) +
  674. uint64(val.n[8])*uint64(val2.n[2]) +
  675. uint64(val.n[9])*uint64(val2.n[1])
  676. t10 := m & fieldBaseMask
  677. // Terms for 2^(fieldBase*11).
  678. m = (m >> fieldBase) +
  679. uint64(val.n[2])*uint64(val2.n[9]) +
  680. uint64(val.n[3])*uint64(val2.n[8]) +
  681. uint64(val.n[4])*uint64(val2.n[7]) +
  682. uint64(val.n[5])*uint64(val2.n[6]) +
  683. uint64(val.n[6])*uint64(val2.n[5]) +
  684. uint64(val.n[7])*uint64(val2.n[4]) +
  685. uint64(val.n[8])*uint64(val2.n[3]) +
  686. uint64(val.n[9])*uint64(val2.n[2])
  687. t11 := m & fieldBaseMask
  688. // Terms for 2^(fieldBase*12).
  689. m = (m >> fieldBase) +
  690. uint64(val.n[3])*uint64(val2.n[9]) +
  691. uint64(val.n[4])*uint64(val2.n[8]) +
  692. uint64(val.n[5])*uint64(val2.n[7]) +
  693. uint64(val.n[6])*uint64(val2.n[6]) +
  694. uint64(val.n[7])*uint64(val2.n[5]) +
  695. uint64(val.n[8])*uint64(val2.n[4]) +
  696. uint64(val.n[9])*uint64(val2.n[3])
  697. t12 := m & fieldBaseMask
  698. // Terms for 2^(fieldBase*13).
  699. m = (m >> fieldBase) +
  700. uint64(val.n[4])*uint64(val2.n[9]) +
  701. uint64(val.n[5])*uint64(val2.n[8]) +
  702. uint64(val.n[6])*uint64(val2.n[7]) +
  703. uint64(val.n[7])*uint64(val2.n[6]) +
  704. uint64(val.n[8])*uint64(val2.n[5]) +
  705. uint64(val.n[9])*uint64(val2.n[4])
  706. t13 := m & fieldBaseMask
  707. // Terms for 2^(fieldBase*14).
  708. m = (m >> fieldBase) +
  709. uint64(val.n[5])*uint64(val2.n[9]) +
  710. uint64(val.n[6])*uint64(val2.n[8]) +
  711. uint64(val.n[7])*uint64(val2.n[7]) +
  712. uint64(val.n[8])*uint64(val2.n[6]) +
  713. uint64(val.n[9])*uint64(val2.n[5])
  714. t14 := m & fieldBaseMask
  715. // Terms for 2^(fieldBase*15).
  716. m = (m >> fieldBase) +
  717. uint64(val.n[6])*uint64(val2.n[9]) +
  718. uint64(val.n[7])*uint64(val2.n[8]) +
  719. uint64(val.n[8])*uint64(val2.n[7]) +
  720. uint64(val.n[9])*uint64(val2.n[6])
  721. t15 := m & fieldBaseMask
  722. // Terms for 2^(fieldBase*16).
  723. m = (m >> fieldBase) +
  724. uint64(val.n[7])*uint64(val2.n[9]) +
  725. uint64(val.n[8])*uint64(val2.n[8]) +
  726. uint64(val.n[9])*uint64(val2.n[7])
  727. t16 := m & fieldBaseMask
  728. // Terms for 2^(fieldBase*17).
  729. m = (m >> fieldBase) +
  730. uint64(val.n[8])*uint64(val2.n[9]) +
  731. uint64(val.n[9])*uint64(val2.n[8])
  732. t17 := m & fieldBaseMask
  733. // Terms for 2^(fieldBase*18).
  734. m = (m >> fieldBase) + uint64(val.n[9])*uint64(val2.n[9])
  735. t18 := m & fieldBaseMask
  736. // What's left is for 2^(fieldBase*19).
  737. t19 := m >> fieldBase
  738. // At this point, all of the terms are grouped into their respective
  739. // base.
  740. //
  741. // Per [HAC] section 14.3.4: Reduction method of moduli of special form,
  742. // when the modulus is of the special form m = b^t - c, highly efficient
  743. // reduction can be achieved per the provided algorithm.
  744. //
  745. // The secp256k1 prime is equivalent to 2^256 - 4294968273, so it fits
  746. // this criteria.
  747. //
  748. // 4294968273 in field representation (base 2^26) is:
  749. // n[0] = 977
  750. // n[1] = 64
  751. // That is to say (2^26 * 64) + 977 = 4294968273
  752. //
  753. // Since each word is in base 26, the upper terms (t10 and up) start
  754. // at 260 bits (versus the final desired range of 256 bits), so the
  755. // field representation of 'c' from above needs to be adjusted for the
  756. // extra 4 bits by multiplying it by 2^4 = 16. 4294968273 * 16 =
  757. // 68719492368. Thus, the adjusted field representation of 'c' is:
  758. // n[0] = 977 * 16 = 15632
  759. // n[1] = 64 * 16 = 1024
  760. // That is to say (2^26 * 1024) + 15632 = 68719492368
  761. //
  762. // To reduce the final term, t19, the entire 'c' value is needed instead
  763. // of only n[0] because there are no more terms left to handle n[1].
  764. // This means there might be some magnitude left in the upper bits that
  765. // is handled below.
  766. m = t0 + t10*15632
  767. t0 = m & fieldBaseMask
  768. m = (m >> fieldBase) + t1 + t10*1024 + t11*15632
  769. t1 = m & fieldBaseMask
  770. m = (m >> fieldBase) + t2 + t11*1024 + t12*15632
  771. t2 = m & fieldBaseMask
  772. m = (m >> fieldBase) + t3 + t12*1024 + t13*15632
  773. t3 = m & fieldBaseMask
  774. m = (m >> fieldBase) + t4 + t13*1024 + t14*15632
  775. t4 = m & fieldBaseMask
  776. m = (m >> fieldBase) + t5 + t14*1024 + t15*15632
  777. t5 = m & fieldBaseMask
  778. m = (m >> fieldBase) + t6 + t15*1024 + t16*15632
  779. t6 = m & fieldBaseMask
  780. m = (m >> fieldBase) + t7 + t16*1024 + t17*15632
  781. t7 = m & fieldBaseMask
  782. m = (m >> fieldBase) + t8 + t17*1024 + t18*15632
  783. t8 = m & fieldBaseMask
  784. m = (m >> fieldBase) + t9 + t18*1024 + t19*68719492368
  785. t9 = m & fieldMSBMask
  786. m = m >> fieldMSBBits
  787. // At this point, if the magnitude is greater than 0, the overall value
  788. // is greater than the max possible 256-bit value. In particular, it is
  789. // "how many times larger" than the max value it is.
  790. //
  791. // The algorithm presented in [HAC] section 14.3.4 repeats until the
  792. // quotient is zero. However, due to the above, we already know at
  793. // least how many times we would need to repeat as it's the value
  794. // currently in m. Thus we can simply multiply the magnitude by the
  795. // field representation of the prime and do a single iteration. Notice
  796. // that nothing will be changed when the magnitude is zero, so we could
  797. // skip this in that case, however always running regardless allows it
  798. // to run in constant time. The final result will be in the range
  799. // 0 <= result <= prime + (2^64 - c), so it is guaranteed to have a
  800. // magnitude of 1, but it is denormalized.
  801. d := t0 + m*977
  802. f.n[0] = uint32(d & fieldBaseMask)
  803. d = (d >> fieldBase) + t1 + m*64
  804. f.n[1] = uint32(d & fieldBaseMask)
  805. f.n[2] = uint32((d >> fieldBase) + t2)
  806. f.n[3] = uint32(t3)
  807. f.n[4] = uint32(t4)
  808. f.n[5] = uint32(t5)
  809. f.n[6] = uint32(t6)
  810. f.n[7] = uint32(t7)
  811. f.n[8] = uint32(t8)
  812. f.n[9] = uint32(t9)
  813. return f
  814. }
  815. // Square squares the field value. The existing field value is modified. Note
  816. // that this function can overflow if multiplying any of the individual words
  817. // exceeds a max uint32. In practice, this means the magnitude of the field
  818. // must be a max of 8 to prevent overflow.
  819. //
  820. // The field value is returned to support chaining. This enables syntax like:
  821. // f.Square().Mul(f2) so that f = f^2 * f2.
  822. func (f *fieldVal) Square() *fieldVal {
  823. return f.SquareVal(f)
  824. }
  825. // SquareVal squares the passed value and stores the result in f. Note that
  826. // this function can overflow if multiplying any of the individual words
  827. // exceeds a max uint32. In practice, this means the magnitude of the field
  828. // being squred must be a max of 8 to prevent overflow.
  829. //
  830. // The field value is returned to support chaining. This enables syntax like:
  831. // f3.SquareVal(f).Mul(f) so that f3 = f^2 * f = f^3.
  832. func (f *fieldVal) SquareVal(val *fieldVal) *fieldVal {
  833. // This could be done with a couple of for loops and an array to store
  834. // the intermediate terms, but this unrolled version is significantly
  835. // faster.
  836. // Terms for 2^(fieldBase*0).
  837. m := uint64(val.n[0]) * uint64(val.n[0])
  838. t0 := m & fieldBaseMask
  839. // Terms for 2^(fieldBase*1).
  840. m = (m >> fieldBase) + 2*uint64(val.n[0])*uint64(val.n[1])
  841. t1 := m & fieldBaseMask
  842. // Terms for 2^(fieldBase*2).
  843. m = (m >> fieldBase) +
  844. 2*uint64(val.n[0])*uint64(val.n[2]) +
  845. uint64(val.n[1])*uint64(val.n[1])
  846. t2 := m & fieldBaseMask
  847. // Terms for 2^(fieldBase*3).
  848. m = (m >> fieldBase) +
  849. 2*uint64(val.n[0])*uint64(val.n[3]) +
  850. 2*uint64(val.n[1])*uint64(val.n[2])
  851. t3 := m & fieldBaseMask
  852. // Terms for 2^(fieldBase*4).
  853. m = (m >> fieldBase) +
  854. 2*uint64(val.n[0])*uint64(val.n[4]) +
  855. 2*uint64(val.n[1])*uint64(val.n[3]) +
  856. uint64(val.n[2])*uint64(val.n[2])
  857. t4 := m & fieldBaseMask
  858. // Terms for 2^(fieldBase*5).
  859. m = (m >> fieldBase) +
  860. 2*uint64(val.n[0])*uint64(val.n[5]) +
  861. 2*uint64(val.n[1])*uint64(val.n[4]) +
  862. 2*uint64(val.n[2])*uint64(val.n[3])
  863. t5 := m & fieldBaseMask
  864. // Terms for 2^(fieldBase*6).
  865. m = (m >> fieldBase) +
  866. 2*uint64(val.n[0])*uint64(val.n[6]) +
  867. 2*uint64(val.n[1])*uint64(val.n[5]) +
  868. 2*uint64(val.n[2])*uint64(val.n[4]) +
  869. uint64(val.n[3])*uint64(val.n[3])
  870. t6 := m & fieldBaseMask
  871. // Terms for 2^(fieldBase*7).
  872. m = (m >> fieldBase) +
  873. 2*uint64(val.n[0])*uint64(val.n[7]) +
  874. 2*uint64(val.n[1])*uint64(val.n[6]) +
  875. 2*uint64(val.n[2])*uint64(val.n[5]) +
  876. 2*uint64(val.n[3])*uint64(val.n[4])
  877. t7 := m & fieldBaseMask
  878. // Terms for 2^(fieldBase*8).
  879. m = (m >> fieldBase) +
  880. 2*uint64(val.n[0])*uint64(val.n[8]) +
  881. 2*uint64(val.n[1])*uint64(val.n[7]) +
  882. 2*uint64(val.n[2])*uint64(val.n[6]) +
  883. 2*uint64(val.n[3])*uint64(val.n[5]) +
  884. uint64(val.n[4])*uint64(val.n[4])
  885. t8 := m & fieldBaseMask
  886. // Terms for 2^(fieldBase*9).
  887. m = (m >> fieldBase) +
  888. 2*uint64(val.n[0])*uint64(val.n[9]) +
  889. 2*uint64(val.n[1])*uint64(val.n[8]) +
  890. 2*uint64(val.n[2])*uint64(val.n[7]) +
  891. 2*uint64(val.n[3])*uint64(val.n[6]) +
  892. 2*uint64(val.n[4])*uint64(val.n[5])
  893. t9 := m & fieldBaseMask
  894. // Terms for 2^(fieldBase*10).
  895. m = (m >> fieldBase) +
  896. 2*uint64(val.n[1])*uint64(val.n[9]) +
  897. 2*uint64(val.n[2])*uint64(val.n[8]) +
  898. 2*uint64(val.n[3])*uint64(val.n[7]) +
  899. 2*uint64(val.n[4])*uint64(val.n[6]) +
  900. uint64(val.n[5])*uint64(val.n[5])
  901. t10 := m & fieldBaseMask
  902. // Terms for 2^(fieldBase*11).
  903. m = (m >> fieldBase) +
  904. 2*uint64(val.n[2])*uint64(val.n[9]) +
  905. 2*uint64(val.n[3])*uint64(val.n[8]) +
  906. 2*uint64(val.n[4])*uint64(val.n[7]) +
  907. 2*uint64(val.n[5])*uint64(val.n[6])
  908. t11 := m & fieldBaseMask
  909. // Terms for 2^(fieldBase*12).
  910. m = (m >> fieldBase) +
  911. 2*uint64(val.n[3])*uint64(val.n[9]) +
  912. 2*uint64(val.n[4])*uint64(val.n[8]) +
  913. 2*uint64(val.n[5])*uint64(val.n[7]) +
  914. uint64(val.n[6])*uint64(val.n[6])
  915. t12 := m & fieldBaseMask
  916. // Terms for 2^(fieldBase*13).
  917. m = (m >> fieldBase) +
  918. 2*uint64(val.n[4])*uint64(val.n[9]) +
  919. 2*uint64(val.n[5])*uint64(val.n[8]) +
  920. 2*uint64(val.n[6])*uint64(val.n[7])
  921. t13 := m & fieldBaseMask
  922. // Terms for 2^(fieldBase*14).
  923. m = (m >> fieldBase) +
  924. 2*uint64(val.n[5])*uint64(val.n[9]) +
  925. 2*uint64(val.n[6])*uint64(val.n[8]) +
  926. uint64(val.n[7])*uint64(val.n[7])
  927. t14 := m & fieldBaseMask
  928. // Terms for 2^(fieldBase*15).
  929. m = (m >> fieldBase) +
  930. 2*uint64(val.n[6])*uint64(val.n[9]) +
  931. 2*uint64(val.n[7])*uint64(val.n[8])
  932. t15 := m & fieldBaseMask
  933. // Terms for 2^(fieldBase*16).
  934. m = (m >> fieldBase) +
  935. 2*uint64(val.n[7])*uint64(val.n[9]) +
  936. uint64(val.n[8])*uint64(val.n[8])
  937. t16 := m & fieldBaseMask
  938. // Terms for 2^(fieldBase*17).
  939. m = (m >> fieldBase) + 2*uint64(val.n[8])*uint64(val.n[9])
  940. t17 := m & fieldBaseMask
  941. // Terms for 2^(fieldBase*18).
  942. m = (m >> fieldBase) + uint64(val.n[9])*uint64(val.n[9])
  943. t18 := m & fieldBaseMask
  944. // What's left is for 2^(fieldBase*19).
  945. t19 := m >> fieldBase
  946. // At this point, all of the terms are grouped into their respective
  947. // base.
  948. //
  949. // Per [HAC] section 14.3.4: Reduction method of moduli of special form,
  950. // when the modulus is of the special form m = b^t - c, highly efficient
  951. // reduction can be achieved per the provided algorithm.
  952. //
  953. // The secp256k1 prime is equivalent to 2^256 - 4294968273, so it fits
  954. // this criteria.
  955. //
  956. // 4294968273 in field representation (base 2^26) is:
  957. // n[0] = 977
  958. // n[1] = 64
  959. // That is to say (2^26 * 64) + 977 = 4294968273
  960. //
  961. // Since each word is in base 26, the upper terms (t10 and up) start
  962. // at 260 bits (versus the final desired range of 256 bits), so the
  963. // field representation of 'c' from above needs to be adjusted for the
  964. // extra 4 bits by multiplying it by 2^4 = 16. 4294968273 * 16 =
  965. // 68719492368. Thus, the adjusted field representation of 'c' is:
  966. // n[0] = 977 * 16 = 15632
  967. // n[1] = 64 * 16 = 1024
  968. // That is to say (2^26 * 1024) + 15632 = 68719492368
  969. //
  970. // To reduce the final term, t19, the entire 'c' value is needed instead
  971. // of only n[0] because there are no more terms left to handle n[1].
  972. // This means there might be some magnitude left in the upper bits that
  973. // is handled below.
  974. m = t0 + t10*15632
  975. t0 = m & fieldBaseMask
  976. m = (m >> fieldBase) + t1 + t10*1024 + t11*15632
  977. t1 = m & fieldBaseMask
  978. m = (m >> fieldBase) + t2 + t11*1024 + t12*15632
  979. t2 = m & fieldBaseMask
  980. m = (m >> fieldBase) + t3 + t12*1024 + t13*15632
  981. t3 = m & fieldBaseMask
  982. m = (m >> fieldBase) + t4 + t13*1024 + t14*15632
  983. t4 = m & fieldBaseMask
  984. m = (m >> fieldBase) + t5 + t14*1024 + t15*15632
  985. t5 = m & fieldBaseMask
  986. m = (m >> fieldBase) + t6 + t15*1024 + t16*15632
  987. t6 = m & fieldBaseMask
  988. m = (m >> fieldBase) + t7 + t16*1024 + t17*15632
  989. t7 = m & fieldBaseMask
  990. m = (m >> fieldBase) + t8 + t17*1024 + t18*15632
  991. t8 = m & fieldBaseMask
  992. m = (m >> fieldBase) + t9 + t18*1024 + t19*68719492368
  993. t9 = m & fieldMSBMask
  994. m = m >> fieldMSBBits
  995. // At this point, if the magnitude is greater than 0, the overall value
  996. // is greater than the max possible 256-bit value. In particular, it is
  997. // "how many times larger" than the max value it is.
  998. //
  999. // The algorithm presented in [HAC] section 14.3.4 repeats until the
  1000. // quotient is zero. However, due to the above, we already know at
  1001. // least how many times we would need to repeat as it's the value
  1002. // currently in m. Thus we can simply multiply the magnitude by the
  1003. // field representation of the prime and do a single iteration. Notice
  1004. // that nothing will be changed when the magnitude is zero, so we could
  1005. // skip this in that case, however always running regardless allows it
  1006. // to run in constant time. The final result will be in the range
  1007. // 0 <= result <= prime + (2^64 - c), so it is guaranteed to have a
  1008. // magnitude of 1, but it is denormalized.
  1009. n := t0 + m*977
  1010. f.n[0] = uint32(n & fieldBaseMask)
  1011. n = (n >> fieldBase) + t1 + m*64
  1012. f.n[1] = uint32(n & fieldBaseMask)
  1013. f.n[2] = uint32((n >> fieldBase) + t2)
  1014. f.n[3] = uint32(t3)
  1015. f.n[4] = uint32(t4)
  1016. f.n[5] = uint32(t5)
  1017. f.n[6] = uint32(t6)
  1018. f.n[7] = uint32(t7)
  1019. f.n[8] = uint32(t8)
  1020. f.n[9] = uint32(t9)
  1021. return f
  1022. }
  1023. // Inverse finds the modular multiplicative inverse of the field value. The
  1024. // existing field value is modified.
  1025. //
  1026. // The field value is returned to support chaining. This enables syntax like:
  1027. // f.Inverse().Mul(f2) so that f = f^-1 * f2.
  1028. func (f *fieldVal) Inverse() *fieldVal {
  1029. // Fermat's little theorem states that for a nonzero number a and prime
  1030. // prime p, a^(p-1) = 1 (mod p). Since the multipliciative inverse is
  1031. // a*b = 1 (mod p), it follows that b = a*a^(p-2) = a^(p-1) = 1 (mod p).
  1032. // Thus, a^(p-2) is the multiplicative inverse.
  1033. //
  1034. // In order to efficiently compute a^(p-2), p-2 needs to be split into
  1035. // a sequence of squares and multipications that minimizes the number of
  1036. // multiplications needed (since they are more costly than squarings).
  1037. // Intermediate results are saved and reused as well.
  1038. //
  1039. // The secp256k1 prime - 2 is 2^256 - 4294968275.
  1040. //
  1041. // This has a cost of 258 field squarings and 33 field multiplications.
  1042. var a2, a3, a4, a10, a11, a21, a42, a45, a63, a1019, a1023 fieldVal
  1043. a2.SquareVal(f)
  1044. a3.Mul2(&a2, f)
  1045. a4.SquareVal(&a2)
  1046. a10.SquareVal(&a4).Mul(&a2)
  1047. a11.Mul2(&a10, f)
  1048. a21.Mul2(&a10, &a11)
  1049. a42.SquareVal(&a21)
  1050. a45.Mul2(&a42, &a3)
  1051. a63.Mul2(&a42, &a21)
  1052. a1019.SquareVal(&a63).Square().Square().Square().Mul(&a11)
  1053. a1023.Mul2(&a1019, &a4)
  1054. f.Set(&a63) // f = a^(2^6 - 1)
  1055. f.Square().Square().Square().Square().Square() // f = a^(2^11 - 32)
  1056. f.Square().Square().Square().Square().Square() // f = a^(2^16 - 1024)
  1057. f.Mul(&a1023) // f = a^(2^16 - 1)
  1058. f.Square().Square().Square().Square().Square() // f = a^(2^21 - 32)
  1059. f.Square().Square().Square().Square().Square() // f = a^(2^26 - 1024)
  1060. f.Mul(&a1023) // f = a^(2^26 - 1)
  1061. f.Square().Square().Square().Square().Square() // f = a^(2^31 - 32)
  1062. f.Square().Square().Square().Square().Square() // f = a^(2^36 - 1024)
  1063. f.Mul(&a1023) // f = a^(2^36 - 1)
  1064. f.Square().Square().Square().Square().Square() // f = a^(2^41 - 32)
  1065. f.Square().Square().Square().Square().Square() // f = a^(2^46 - 1024)
  1066. f.Mul(&a1023) // f = a^(2^46 - 1)
  1067. f.Square().Square().Square().Square().Square() // f = a^(2^51 - 32)
  1068. f.Square().Square().Square().Square().Square() // f = a^(2^56 - 1024)
  1069. f.Mul(&a1023) // f = a^(2^56 - 1)
  1070. f.Square().Square().Square().Square().Square() // f = a^(2^61 - 32)
  1071. f.Square().Square().Square().Square().Square() // f = a^(2^66 - 1024)
  1072. f.Mul(&a1023) // f = a^(2^66 - 1)
  1073. f.Square().Square().Square().Square().Square() // f = a^(2^71 - 32)
  1074. f.Square().Square().Square().Square().Square() // f = a^(2^76 - 1024)
  1075. f.Mul(&a1023) // f = a^(2^76 - 1)
  1076. f.Square().Square().Square().Square().Square() // f = a^(2^81 - 32)
  1077. f.Square().Square().Square().Square().Square() // f = a^(2^86 - 1024)
  1078. f.Mul(&a1023) // f = a^(2^86 - 1)
  1079. f.Square().Square().Square().Square().Square() // f = a^(2^91 - 32)
  1080. f.Square().Square().Square().Square().Square() // f = a^(2^96 - 1024)
  1081. f.Mul(&a1023) // f = a^(2^96 - 1)
  1082. f.Square().Square().Square().Square().Square() // f = a^(2^101 - 32)
  1083. f.Square().Square().Square().Square().Square() // f = a^(2^106 - 1024)
  1084. f.Mul(&a1023) // f = a^(2^106 - 1)
  1085. f.Square().Square().Square().Square().Square() // f = a^(2^111 - 32)
  1086. f.Square().Square().Square().Square().Square() // f = a^(2^116 - 1024)
  1087. f.Mul(&a1023) // f = a^(2^116 - 1)
  1088. f.Square().Square().Square().Square().Square() // f = a^(2^121 - 32)
  1089. f.Square().Square().Square().Square().Square() // f = a^(2^126 - 1024)
  1090. f.Mul(&a1023) // f = a^(2^126 - 1)
  1091. f.Square().Square().Square().Square().Square() // f = a^(2^131 - 32)
  1092. f.Square().Square().Square().Square().Square() // f = a^(2^136 - 1024)
  1093. f.Mul(&a1023) // f = a^(2^136 - 1)
  1094. f.Square().Square().Square().Square().Square() // f = a^(2^141 - 32)
  1095. f.Square().Square().Square().Square().Square() // f = a^(2^146 - 1024)
  1096. f.Mul(&a1023) // f = a^(2^146 - 1)
  1097. f.Square().Square().Square().Square().Square() // f = a^(2^151 - 32)
  1098. f.Square().Square().Square().Square().Square() // f = a^(2^156 - 1024)
  1099. f.Mul(&a1023) // f = a^(2^156 - 1)
  1100. f.Square().Square().Square().Square().Square() // f = a^(2^161 - 32)
  1101. f.Square().Square().Square().Square().Square() // f = a^(2^166 - 1024)
  1102. f.Mul(&a1023) // f = a^(2^166 - 1)
  1103. f.Square().Square().Square().Square().Square() // f = a^(2^171 - 32)
  1104. f.Square().Square().Square().Square().Square() // f = a^(2^176 - 1024)
  1105. f.Mul(&a1023) // f = a^(2^176 - 1)
  1106. f.Square().Square().Square().Square().Square() // f = a^(2^181 - 32)
  1107. f.Square().Square().Square().Square().Square() // f = a^(2^186 - 1024)
  1108. f.Mul(&a1023) // f = a^(2^186 - 1)
  1109. f.Square().Square().Square().Square().Square() // f = a^(2^191 - 32)
  1110. f.Square().Square().Square().Square().Square() // f = a^(2^196 - 1024)
  1111. f.Mul(&a1023) // f = a^(2^196 - 1)
  1112. f.Square().Square().Square().Square().Square() // f = a^(2^201 - 32)
  1113. f.Square().Square().Square().Square().Square() // f = a^(2^206 - 1024)
  1114. f.Mul(&a1023) // f = a^(2^206 - 1)
  1115. f.Square().Square().Square().Square().Square() // f = a^(2^211 - 32)
  1116. f.Square().Square().Square().Square().Square() // f = a^(2^216 - 1024)
  1117. f.Mul(&a1023) // f = a^(2^216 - 1)
  1118. f.Square().Square().Square().Square().Square() // f = a^(2^221 - 32)
  1119. f.Square().Square().Square().Square().Square() // f = a^(2^226 - 1024)
  1120. f.Mul(&a1019) // f = a^(2^226 - 5)
  1121. f.Square().Square().Square().Square().Square() // f = a^(2^231 - 160)
  1122. f.Square().Square().Square().Square().Square() // f = a^(2^236 - 5120)
  1123. f.Mul(&a1023) // f = a^(2^236 - 4097)
  1124. f.Square().Square().Square().Square().Square() // f = a^(2^241 - 131104)
  1125. f.Square().Square().Square().Square().Square() // f = a^(2^246 - 4195328)
  1126. f.Mul(&a1023) // f = a^(2^246 - 4194305)
  1127. f.Square().Square().Square().Square().Square() // f = a^(2^251 - 134217760)
  1128. f.Square().Square().Square().Square().Square() // f = a^(2^256 - 4294968320)
  1129. return f.Mul(&a45) // f = a^(2^256 - 4294968275) = a^(p-2)
  1130. }