field.go 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. // Copyright (c) 2013-2014 The btcsuite developers
  2. // Copyright (c) 2015-2020 The Decred developers
  3. // Copyright (c) 2013-2020 Dave Collins
  4. // Use of this source code is governed by an ISC
  5. // license that can be found in the LICENSE file.
  6. package secp256k1
  7. // References:
  8. // [HAC]: Handbook of Applied Cryptography Menezes, van Oorschot, Vanstone.
  9. // http://cacr.uwaterloo.ca/hac/
  10. // All elliptic curve operations for secp256k1 are done in a finite field
  11. // characterized by a 256-bit prime. Given this precision is larger than the
  12. // biggest available native type, obviously some form of bignum math is needed.
  13. // This package implements specialized fixed-precision field arithmetic rather
  14. // than relying on an arbitrary-precision arithmetic package such as math/big
  15. // for dealing with the field math since the size is known. As a result, rather
  16. // large performance gains are achieved by taking advantage of many
  17. // optimizations not available to arbitrary-precision arithmetic and generic
  18. // modular arithmetic algorithms.
  19. //
  20. // There are various ways to internally represent each finite field element.
  21. // For example, the most obvious representation would be to use an array of 4
  22. // uint64s (64 bits * 4 = 256 bits). However, that representation suffers from
  23. // a couple of issues. First, there is no native Go type large enough to handle
  24. // the intermediate results while adding or multiplying two 64-bit numbers, and
  25. // second there is no space left for overflows when performing the intermediate
  26. // arithmetic between each array element which would lead to expensive carry
  27. // propagation.
  28. //
  29. // Given the above, this implementation represents the field elements as
  30. // 10 uint32s with each word (array entry) treated as base 2^26. This was
  31. // chosen for the following reasons:
  32. // 1) Most systems at the current time are 64-bit (or at least have 64-bit
  33. // registers available for specialized purposes such as MMX) so the
  34. // intermediate results can typically be done using a native register (and
  35. // using uint64s to avoid the need for additional half-word arithmetic)
  36. // 2) In order to allow addition of the internal words without having to
  37. // propagate the carry, the max normalized value for each register must
  38. // be less than the number of bits available in the register
  39. // 3) Since we're dealing with 32-bit values, 64-bits of overflow is a
  40. // reasonable choice for #2
  41. // 4) Given the need for 256-bits of precision and the properties stated in #1,
  42. // #2, and #3, the representation which best accommodates this is 10 uint32s
  43. // with base 2^26 (26 bits * 10 = 260 bits, so the final word only needs 22
  44. // bits) which leaves the desired 64 bits (32 * 10 = 320, 320 - 256 = 64) for
  45. // overflow
  46. //
  47. // Since it is so important that the field arithmetic is extremely fast for high
  48. // performance crypto, this type does not perform any validation where it
  49. // ordinarily would. See the documentation for FieldVal for more details.
  50. import (
  51. "encoding/hex"
  52. )
  53. // Constants used to make the code more readable.
  54. const (
  55. twoBitsMask = 0x3
  56. fourBitsMask = 0xf
  57. sixBitsMask = 0x3f
  58. eightBitsMask = 0xff
  59. )
  60. // Constants related to the field representation.
  61. const (
  62. // fieldWords is the number of words used to internally represent the
  63. // 256-bit value.
  64. fieldWords = 10
  65. // fieldBase is the exponent used to form the numeric base of each word.
  66. // 2^(fieldBase*i) where i is the word position.
  67. fieldBase = 26
  68. // fieldBaseMask is the mask for the bits in each word needed to
  69. // represent the numeric base of each word (except the most significant
  70. // word).
  71. fieldBaseMask = (1 << fieldBase) - 1
  72. // fieldMSBBits is the number of bits in the most significant word used
  73. // to represent the value.
  74. fieldMSBBits = 256 - (fieldBase * (fieldWords - 1))
  75. // fieldMSBMask is the mask for the bits in the most significant word
  76. // needed to represent the value.
  77. fieldMSBMask = (1 << fieldMSBBits) - 1
  78. // These fields provide convenient access to each of the words of the
  79. // secp256k1 prime in the internal field representation to improve code
  80. // readability.
  81. fieldPrimeWordZero = 0x03fffc2f
  82. fieldPrimeWordOne = 0x03ffffbf
  83. fieldPrimeWordTwo = 0x03ffffff
  84. fieldPrimeWordThree = 0x03ffffff
  85. fieldPrimeWordFour = 0x03ffffff
  86. fieldPrimeWordFive = 0x03ffffff
  87. fieldPrimeWordSix = 0x03ffffff
  88. fieldPrimeWordSeven = 0x03ffffff
  89. fieldPrimeWordEight = 0x03ffffff
  90. fieldPrimeWordNine = 0x003fffff
  91. )
  92. // FieldVal implements optimized fixed-precision arithmetic over the
  93. // secp256k1 finite field. This means all arithmetic is performed modulo
  94. // 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f.
  95. //
  96. // WARNING: Since it is so important for the field arithmetic to be extremely
  97. // fast for high performance crypto, this type does not perform any validation
  98. // of documented preconditions where it ordinarily would. As a result, it is
  99. // IMPERATIVE for callers to understand some key concepts that are described
  100. // below and ensure the methods are called with the necessary preconditions that
  101. // each method is documented with. For example, some methods only give the
  102. // correct result if the field value is normalized and others require the field
  103. // values involved to have a maximum magnitude and THERE ARE NO EXPLICIT CHECKS
  104. // TO ENSURE THOSE PRECONDITIONS ARE SATISFIED. This does, unfortunately, make
  105. // the type more difficult to use correctly and while I typically prefer to
  106. // ensure all state and input is valid for most code, this is a bit of an
  107. // exception because those extra checks really add up in what ends up being
  108. // critical hot paths.
  109. //
  110. // The first key concept when working with this type is normalization. In order
  111. // to avoid the need to propagate a ton of carries, the internal representation
  112. // provides additional overflow bits for each word of the overall 256-bit value.
  113. // This means that there are multiple internal representations for the same
  114. // value and, as a result, any methods that rely on comparison of the value,
  115. // such as equality and oddness determination, require the caller to provide a
  116. // normalized value.
  117. //
  118. // The second key concept when working with this type is magnitude. As
  119. // previously mentioned, the internal representation provides additional
  120. // overflow bits which means that the more math operations that are performed on
  121. // the field value between normalizations, the more those overflow bits
  122. // accumulate. The magnitude is effectively that maximum possible number of
  123. // those overflow bits that could possibly be required as a result of a given
  124. // operation. Since there are only a limited number of overflow bits available,
  125. // this implies that the max possible magnitude MUST be tracked by the caller
  126. // and the caller MUST normalize the field value if a given operation would
  127. // cause the magnitude of the result to exceed the max allowed value.
  128. //
  129. // IMPORTANT: The max allowed magnitude of a field value is 64.
  130. type FieldVal struct {
  131. // Each 256-bit value is represented as 10 32-bit integers in base 2^26.
  132. // This provides 6 bits of overflow in each word (10 bits in the most
  133. // significant word) for a total of 64 bits of overflow (9*6 + 10 = 64). It
  134. // only implements the arithmetic needed for elliptic curve operations.
  135. //
  136. // The following depicts the internal representation:
  137. // -----------------------------------------------------------------
  138. // | n[9] | n[8] | ... | n[0] |
  139. // | 32 bits available | 32 bits available | ... | 32 bits available |
  140. // | 22 bits for value | 26 bits for value | ... | 26 bits for value |
  141. // | 10 bits overflow | 6 bits overflow | ... | 6 bits overflow |
  142. // | Mult: 2^(26*9) | Mult: 2^(26*8) | ... | Mult: 2^(26*0) |
  143. // -----------------------------------------------------------------
  144. //
  145. // For example, consider the number 2^49 + 1. It would be represented as:
  146. // n[0] = 1
  147. // n[1] = 2^23
  148. // n[2..9] = 0
  149. //
  150. // The full 256-bit value is then calculated by looping i from 9..0 and
  151. // doing sum(n[i] * 2^(26i)) like so:
  152. // n[9] * 2^(26*9) = 0 * 2^234 = 0
  153. // n[8] * 2^(26*8) = 0 * 2^208 = 0
  154. // ...
  155. // n[1] * 2^(26*1) = 2^23 * 2^26 = 2^49
  156. // n[0] * 2^(26*0) = 1 * 2^0 = 1
  157. // Sum: 0 + 0 + ... + 2^49 + 1 = 2^49 + 1
  158. n [10]uint32
  159. }
  160. // String returns the field value as a normalized human-readable hex string.
  161. //
  162. // Preconditions: None
  163. // Output Normalized: Field is not modified -- same as input value
  164. // Output Max Magnitude: Field is not modified -- same as input value
  165. func (f FieldVal) String() string {
  166. // f is a copy, so it's safe to normalize it without mutating the original.
  167. f.Normalize()
  168. return hex.EncodeToString(f.Bytes()[:])
  169. }
  170. // Zero sets the field value to zero in constant time. A newly created field
  171. // value is already set to zero. This function can be useful to clear an
  172. // existing field value for reuse.
  173. //
  174. // Preconditions: None
  175. // Output Normalized: Yes
  176. // Output Max Magnitude: 1
  177. func (f *FieldVal) Zero() {
  178. f.n[0] = 0
  179. f.n[1] = 0
  180. f.n[2] = 0
  181. f.n[3] = 0
  182. f.n[4] = 0
  183. f.n[5] = 0
  184. f.n[6] = 0
  185. f.n[7] = 0
  186. f.n[8] = 0
  187. f.n[9] = 0
  188. }
  189. // Set sets the field value equal to the passed value in constant time. The
  190. // normalization and magnitude of the two fields will be identical.
  191. //
  192. // The field value is returned to support chaining. This enables syntax like:
  193. // f := new(FieldVal).Set(f2).Add(1) so that f = f2 + 1 where f2 is not
  194. // modified.
  195. //
  196. // Preconditions: None
  197. // Output Normalized: Same as input value
  198. // Output Max Magnitude: Same as input value
  199. func (f *FieldVal) Set(val *FieldVal) *FieldVal {
  200. *f = *val
  201. return f
  202. }
  203. // SetInt sets the field value to the passed integer in constant time. This is
  204. // a convenience function since it is fairly common to perform some arithmetic
  205. // with small native integers.
  206. //
  207. // The field value is returned to support chaining. This enables syntax such
  208. // as f := new(FieldVal).SetInt(2).Mul(f2) so that f = 2 * f2.
  209. //
  210. // Preconditions: None
  211. // Output Normalized: Yes
  212. // Output Max Magnitude: 1
  213. func (f *FieldVal) SetInt(ui uint16) *FieldVal {
  214. f.Zero()
  215. f.n[0] = uint32(ui)
  216. return f
  217. }
  218. // SetBytes packs the passed 32-byte big-endian value into the internal field
  219. // value representation in constant time. SetBytes interprets the provided
  220. // array as a 256-bit big-endian unsigned integer, packs it into the internal
  221. // field value representation, and returns either 1 if it is greater than or
  222. // equal to the field prime (aka it overflowed) or 0 otherwise in constant time.
  223. //
  224. // Note that a bool is not used here because it is not possible in Go to convert
  225. // from a bool to numeric value in constant time and many constant-time
  226. // operations require a numeric value.
  227. //
  228. // Preconditions: None
  229. // Output Normalized: Yes if no overflow, no otherwise
  230. // Output Max Magnitude: 1
  231. func (f *FieldVal) SetBytes(b *[32]byte) uint32 {
  232. // Pack the 256 total bits across the 10 uint32 words with a max of
  233. // 26-bits per word. This could be done with a couple of for loops,
  234. // but this unrolled version is significantly faster. Benchmarks show
  235. // this is about 34 times faster than the variant which uses loops.
  236. f.n[0] = uint32(b[31]) | uint32(b[30])<<8 | uint32(b[29])<<16 |
  237. (uint32(b[28])&twoBitsMask)<<24
  238. f.n[1] = uint32(b[28])>>2 | uint32(b[27])<<6 | uint32(b[26])<<14 |
  239. (uint32(b[25])&fourBitsMask)<<22
  240. f.n[2] = uint32(b[25])>>4 | uint32(b[24])<<4 | uint32(b[23])<<12 |
  241. (uint32(b[22])&sixBitsMask)<<20
  242. f.n[3] = uint32(b[22])>>6 | uint32(b[21])<<2 | uint32(b[20])<<10 |
  243. uint32(b[19])<<18
  244. f.n[4] = uint32(b[18]) | uint32(b[17])<<8 | uint32(b[16])<<16 |
  245. (uint32(b[15])&twoBitsMask)<<24
  246. f.n[5] = uint32(b[15])>>2 | uint32(b[14])<<6 | uint32(b[13])<<14 |
  247. (uint32(b[12])&fourBitsMask)<<22
  248. f.n[6] = uint32(b[12])>>4 | uint32(b[11])<<4 | uint32(b[10])<<12 |
  249. (uint32(b[9])&sixBitsMask)<<20
  250. f.n[7] = uint32(b[9])>>6 | uint32(b[8])<<2 | uint32(b[7])<<10 |
  251. uint32(b[6])<<18
  252. f.n[8] = uint32(b[5]) | uint32(b[4])<<8 | uint32(b[3])<<16 |
  253. (uint32(b[2])&twoBitsMask)<<24
  254. f.n[9] = uint32(b[2])>>2 | uint32(b[1])<<6 | uint32(b[0])<<14
  255. // The intuition here is that the field value is greater than the prime if
  256. // one of the higher individual words is greater than corresponding word of
  257. // the prime and all higher words in the field value are equal to their
  258. // corresponding word of the prime. Since this type is modulo the prime,
  259. // being equal is also an overflow back to 0.
  260. //
  261. // Note that because the input is 32 bytes and it was just packed into the
  262. // field representation, the only words that can possibly be greater are
  263. // zero and one, because ceil(log_2(2^256 - 1 - P)) = 33 bits max and the
  264. // internal field representation encodes 26 bits with each word.
  265. //
  266. // Thus, there is no need to test if the upper words of the field value
  267. // exceeds them, hence, only equality is checked for them.
  268. highWordsEq := constantTimeEq(f.n[9], fieldPrimeWordNine)
  269. highWordsEq &= constantTimeEq(f.n[8], fieldPrimeWordEight)
  270. highWordsEq &= constantTimeEq(f.n[7], fieldPrimeWordSeven)
  271. highWordsEq &= constantTimeEq(f.n[6], fieldPrimeWordSix)
  272. highWordsEq &= constantTimeEq(f.n[5], fieldPrimeWordFive)
  273. highWordsEq &= constantTimeEq(f.n[4], fieldPrimeWordFour)
  274. highWordsEq &= constantTimeEq(f.n[3], fieldPrimeWordThree)
  275. highWordsEq &= constantTimeEq(f.n[2], fieldPrimeWordTwo)
  276. overflow := highWordsEq & constantTimeGreater(f.n[1], fieldPrimeWordOne)
  277. highWordsEq &= constantTimeEq(f.n[1], fieldPrimeWordOne)
  278. overflow |= highWordsEq & constantTimeGreaterOrEq(f.n[0], fieldPrimeWordZero)
  279. return overflow
  280. }
  281. // SetByteSlice interprets the provided slice as a 256-bit big-endian unsigned
  282. // integer (meaning it is truncated to the first 32 bytes), packs it into the
  283. // internal field value representation, and returns whether or not the resulting
  284. // truncated 256-bit integer is greater than or equal to the field prime (aka it
  285. // overflowed) in constant time.
  286. //
  287. // Note that since passing a slice with more than 32 bytes is truncated, it is
  288. // possible that the truncated value is less than the field prime and hence it
  289. // will not be reported as having overflowed in that case. It is up to the
  290. // caller to decide whether it needs to provide numbers of the appropriate size
  291. // or it if is acceptable to use this function with the described truncation and
  292. // overflow behavior.
  293. //
  294. // Preconditions: None
  295. // Output Normalized: Yes if no overflow, no otherwise
  296. // Output Max Magnitude: 1
  297. func (f *FieldVal) SetByteSlice(b []byte) bool {
  298. var b32 [32]byte
  299. b = b[:constantTimeMin(uint32(len(b)), 32)]
  300. copy(b32[:], b32[:32-len(b)])
  301. copy(b32[32-len(b):], b)
  302. result := f.SetBytes(&b32)
  303. zeroArray32(&b32)
  304. return result != 0
  305. }
  306. // Normalize normalizes the internal field words into the desired range and
  307. // performs fast modular reduction over the secp256k1 prime by making use of the
  308. // special form of the prime in constant time.
  309. //
  310. // Preconditions: None
  311. // Output Normalized: Yes
  312. // Output Max Magnitude: 1
  313. func (f *FieldVal) Normalize() *FieldVal {
  314. // The field representation leaves 6 bits of overflow in each word so
  315. // intermediate calculations can be performed without needing to
  316. // propagate the carry to each higher word during the calculations. In
  317. // order to normalize, we need to "compact" the full 256-bit value to
  318. // the right while propagating any carries through to the high order
  319. // word.
  320. //
  321. // Since this field is doing arithmetic modulo the secp256k1 prime, we
  322. // also need to perform modular reduction over the prime.
  323. //
  324. // Per [HAC] section 14.3.4: Reduction method of moduli of special form,
  325. // when the modulus is of the special form m = b^t - c, highly efficient
  326. // reduction can be achieved.
  327. //
  328. // The secp256k1 prime is equivalent to 2^256 - 4294968273, so it fits
  329. // this criteria.
  330. //
  331. // 4294968273 in field representation (base 2^26) is:
  332. // n[0] = 977
  333. // n[1] = 64
  334. // That is to say (2^26 * 64) + 977 = 4294968273
  335. //
  336. // The algorithm presented in the referenced section typically repeats
  337. // until the quotient is zero. However, due to our field representation
  338. // we already know to within one reduction how many times we would need
  339. // to repeat as it's the uppermost bits of the high order word. Thus we
  340. // can simply multiply the magnitude by the field representation of the
  341. // prime and do a single iteration. After this step there might be an
  342. // additional carry to bit 256 (bit 22 of the high order word).
  343. t9 := f.n[9]
  344. m := t9 >> fieldMSBBits
  345. t9 = t9 & fieldMSBMask
  346. t0 := f.n[0] + m*977
  347. t1 := (t0 >> fieldBase) + f.n[1] + (m << 6)
  348. t0 = t0 & fieldBaseMask
  349. t2 := (t1 >> fieldBase) + f.n[2]
  350. t1 = t1 & fieldBaseMask
  351. t3 := (t2 >> fieldBase) + f.n[3]
  352. t2 = t2 & fieldBaseMask
  353. t4 := (t3 >> fieldBase) + f.n[4]
  354. t3 = t3 & fieldBaseMask
  355. t5 := (t4 >> fieldBase) + f.n[5]
  356. t4 = t4 & fieldBaseMask
  357. t6 := (t5 >> fieldBase) + f.n[6]
  358. t5 = t5 & fieldBaseMask
  359. t7 := (t6 >> fieldBase) + f.n[7]
  360. t6 = t6 & fieldBaseMask
  361. t8 := (t7 >> fieldBase) + f.n[8]
  362. t7 = t7 & fieldBaseMask
  363. t9 = (t8 >> fieldBase) + t9
  364. t8 = t8 & fieldBaseMask
  365. // At this point, the magnitude is guaranteed to be one, however, the
  366. // value could still be greater than the prime if there was either a
  367. // carry through to bit 256 (bit 22 of the higher order word) or the
  368. // value is greater than or equal to the field characteristic. The
  369. // following determines if either or these conditions are true and does
  370. // the final reduction in constant time.
  371. //
  372. // Also note that 'm' will be zero when neither of the aforementioned
  373. // conditions are true and the value will not be changed when 'm' is zero.
  374. m = constantTimeEq(t9, fieldMSBMask)
  375. m &= constantTimeEq(t8&t7&t6&t5&t4&t3&t2, fieldBaseMask)
  376. m &= constantTimeGreater(t1+64+((t0+977)>>fieldBase), fieldBaseMask)
  377. m |= t9 >> fieldMSBBits
  378. t0 = t0 + m*977
  379. t1 = (t0 >> fieldBase) + t1 + (m << 6)
  380. t0 = t0 & fieldBaseMask
  381. t2 = (t1 >> fieldBase) + t2
  382. t1 = t1 & fieldBaseMask
  383. t3 = (t2 >> fieldBase) + t3
  384. t2 = t2 & fieldBaseMask
  385. t4 = (t3 >> fieldBase) + t4
  386. t3 = t3 & fieldBaseMask
  387. t5 = (t4 >> fieldBase) + t5
  388. t4 = t4 & fieldBaseMask
  389. t6 = (t5 >> fieldBase) + t6
  390. t5 = t5 & fieldBaseMask
  391. t7 = (t6 >> fieldBase) + t7
  392. t6 = t6 & fieldBaseMask
  393. t8 = (t7 >> fieldBase) + t8
  394. t7 = t7 & fieldBaseMask
  395. t9 = (t8 >> fieldBase) + t9
  396. t8 = t8 & fieldBaseMask
  397. t9 = t9 & fieldMSBMask // Remove potential multiple of 2^256.
  398. // Finally, set the normalized and reduced words.
  399. f.n[0] = t0
  400. f.n[1] = t1
  401. f.n[2] = t2
  402. f.n[3] = t3
  403. f.n[4] = t4
  404. f.n[5] = t5
  405. f.n[6] = t6
  406. f.n[7] = t7
  407. f.n[8] = t8
  408. f.n[9] = t9
  409. return f
  410. }
  411. // PutBytesUnchecked unpacks the field value to a 32-byte big-endian value
  412. // directly into the passed byte slice in constant time. The target slice must
  413. // must have at least 32 bytes available or it will panic.
  414. //
  415. // There is a similar function, PutBytes, which unpacks the field value into a
  416. // 32-byte array directly. This version is provided since it can be useful
  417. // to write directly into part of a larger buffer without needing a separate
  418. // allocation.
  419. //
  420. // Preconditions:
  421. // - The field value MUST be normalized
  422. // - The target slice MUST have at least 32 bytes available
  423. func (f *FieldVal) PutBytesUnchecked(b []byte) {
  424. // Unpack the 256 total bits from the 10 uint32 words with a max of
  425. // 26-bits per word. This could be done with a couple of for loops,
  426. // but this unrolled version is a bit faster. Benchmarks show this is
  427. // about 10 times faster than the variant which uses loops.
  428. b[31] = byte(f.n[0] & eightBitsMask)
  429. b[30] = byte((f.n[0] >> 8) & eightBitsMask)
  430. b[29] = byte((f.n[0] >> 16) & eightBitsMask)
  431. b[28] = byte((f.n[0]>>24)&twoBitsMask | (f.n[1]&sixBitsMask)<<2)
  432. b[27] = byte((f.n[1] >> 6) & eightBitsMask)
  433. b[26] = byte((f.n[1] >> 14) & eightBitsMask)
  434. b[25] = byte((f.n[1]>>22)&fourBitsMask | (f.n[2]&fourBitsMask)<<4)
  435. b[24] = byte((f.n[2] >> 4) & eightBitsMask)
  436. b[23] = byte((f.n[2] >> 12) & eightBitsMask)
  437. b[22] = byte((f.n[2]>>20)&sixBitsMask | (f.n[3]&twoBitsMask)<<6)
  438. b[21] = byte((f.n[3] >> 2) & eightBitsMask)
  439. b[20] = byte((f.n[3] >> 10) & eightBitsMask)
  440. b[19] = byte((f.n[3] >> 18) & eightBitsMask)
  441. b[18] = byte(f.n[4] & eightBitsMask)
  442. b[17] = byte((f.n[4] >> 8) & eightBitsMask)
  443. b[16] = byte((f.n[4] >> 16) & eightBitsMask)
  444. b[15] = byte((f.n[4]>>24)&twoBitsMask | (f.n[5]&sixBitsMask)<<2)
  445. b[14] = byte((f.n[5] >> 6) & eightBitsMask)
  446. b[13] = byte((f.n[5] >> 14) & eightBitsMask)
  447. b[12] = byte((f.n[5]>>22)&fourBitsMask | (f.n[6]&fourBitsMask)<<4)
  448. b[11] = byte((f.n[6] >> 4) & eightBitsMask)
  449. b[10] = byte((f.n[6] >> 12) & eightBitsMask)
  450. b[9] = byte((f.n[6]>>20)&sixBitsMask | (f.n[7]&twoBitsMask)<<6)
  451. b[8] = byte((f.n[7] >> 2) & eightBitsMask)
  452. b[7] = byte((f.n[7] >> 10) & eightBitsMask)
  453. b[6] = byte((f.n[7] >> 18) & eightBitsMask)
  454. b[5] = byte(f.n[8] & eightBitsMask)
  455. b[4] = byte((f.n[8] >> 8) & eightBitsMask)
  456. b[3] = byte((f.n[8] >> 16) & eightBitsMask)
  457. b[2] = byte((f.n[8]>>24)&twoBitsMask | (f.n[9]&sixBitsMask)<<2)
  458. b[1] = byte((f.n[9] >> 6) & eightBitsMask)
  459. b[0] = byte((f.n[9] >> 14) & eightBitsMask)
  460. }
  461. // PutBytes unpacks the field value to a 32-byte big-endian value using the
  462. // passed byte array in constant time.
  463. //
  464. // There is a similar function, PutBytesUnchecked, which unpacks the field value
  465. // into a slice that must have at least 32 bytes available. This version is
  466. // provided since it can be useful to write directly into an array that is type
  467. // checked.
  468. //
  469. // Alternatively, there is also Bytes, which unpacks the field value into a new
  470. // array and returns that which can sometimes be more ergonomic in applications
  471. // that aren't concerned about an additional copy.
  472. //
  473. // Preconditions:
  474. // - The field value MUST be normalized
  475. func (f *FieldVal) PutBytes(b *[32]byte) {
  476. f.PutBytesUnchecked(b[:])
  477. }
  478. // Bytes unpacks the field value to a 32-byte big-endian value in constant time.
  479. //
  480. // See PutBytes and PutBytesUnchecked for variants that allow an array or slice
  481. // to be passed which can be useful to cut down on the number of allocations by
  482. // allowing the caller to reuse a buffer or write directly into part of a larger
  483. // buffer.
  484. //
  485. // Preconditions:
  486. // - The field value MUST be normalized
  487. func (f *FieldVal) Bytes() *[32]byte {
  488. b := new([32]byte)
  489. f.PutBytesUnchecked(b[:])
  490. return b
  491. }
  492. // IsZeroBit returns 1 when the field value is equal to zero or 0 otherwise in
  493. // constant time.
  494. //
  495. // Note that a bool is not used here because it is not possible in Go to convert
  496. // from a bool to numeric value in constant time and many constant-time
  497. // operations require a numeric value. See IsZero for the version that returns
  498. // a bool.
  499. //
  500. // Preconditions:
  501. // - The field value MUST be normalized
  502. func (f *FieldVal) IsZeroBit() uint32 {
  503. // The value can only be zero if no bits are set in any of the words.
  504. // This is a constant time implementation.
  505. bits := f.n[0] | f.n[1] | f.n[2] | f.n[3] | f.n[4] |
  506. f.n[5] | f.n[6] | f.n[7] | f.n[8] | f.n[9]
  507. return constantTimeEq(bits, 0)
  508. }
  509. // IsZero returns whether or not the field value is equal to zero in constant
  510. // time.
  511. //
  512. // Preconditions:
  513. // - The field value MUST be normalized
  514. func (f *FieldVal) IsZero() bool {
  515. // The value can only be zero if no bits are set in any of the words.
  516. // This is a constant time implementation.
  517. bits := f.n[0] | f.n[1] | f.n[2] | f.n[3] | f.n[4] |
  518. f.n[5] | f.n[6] | f.n[7] | f.n[8] | f.n[9]
  519. return bits == 0
  520. }
  521. // IsOneBit returns 1 when the field value is equal to one or 0 otherwise in
  522. // constant time.
  523. //
  524. // Note that a bool is not used here because it is not possible in Go to convert
  525. // from a bool to numeric value in constant time and many constant-time
  526. // operations require a numeric value. See IsOne for the version that returns a
  527. // bool.
  528. //
  529. // Preconditions:
  530. // - The field value MUST be normalized
  531. func (f *FieldVal) IsOneBit() uint32 {
  532. // The value can only be one if the single lowest significant bit is set in
  533. // the first word and no other bits are set in any of the other words.
  534. // This is a constant time implementation.
  535. bits := (f.n[0] ^ 1) | f.n[1] | f.n[2] | f.n[3] | f.n[4] | f.n[5] |
  536. f.n[6] | f.n[7] | f.n[8] | f.n[9]
  537. return constantTimeEq(bits, 0)
  538. }
  539. // IsOne returns whether or not the field value is equal to one in constant
  540. // time.
  541. //
  542. // Preconditions:
  543. // - The field value MUST be normalized
  544. func (f *FieldVal) IsOne() bool {
  545. // The value can only be one if the single lowest significant bit is set in
  546. // the first word and no other bits are set in any of the other words.
  547. // This is a constant time implementation.
  548. bits := (f.n[0] ^ 1) | f.n[1] | f.n[2] | f.n[3] | f.n[4] | f.n[5] |
  549. f.n[6] | f.n[7] | f.n[8] | f.n[9]
  550. return bits == 0
  551. }
  552. // IsOddBit returns 1 when the field value is an odd number or 0 otherwise in
  553. // constant time.
  554. //
  555. // Note that a bool is not used here because it is not possible in Go to convert
  556. // from a bool to numeric value in constant time and many constant-time
  557. // operations require a numeric value. See IsOdd for the version that returns a
  558. // bool.
  559. //
  560. // Preconditions:
  561. // - The field value MUST be normalized
  562. func (f *FieldVal) IsOddBit() uint32 {
  563. // Only odd numbers have the bottom bit set.
  564. return f.n[0] & 1
  565. }
  566. // IsOdd returns whether or not the field value is an odd number in constant
  567. // time.
  568. //
  569. // Preconditions:
  570. // - The field value MUST be normalized
  571. func (f *FieldVal) IsOdd() bool {
  572. // Only odd numbers have the bottom bit set.
  573. return f.n[0]&1 == 1
  574. }
  575. // Equals returns whether or not the two field values are the same in constant
  576. // time.
  577. //
  578. // Preconditions:
  579. // - Both field values being compared MUST be normalized
  580. func (f *FieldVal) Equals(val *FieldVal) bool {
  581. // Xor only sets bits when they are different, so the two field values
  582. // can only be the same if no bits are set after xoring each word.
  583. // This is a constant time implementation.
  584. bits := (f.n[0] ^ val.n[0]) | (f.n[1] ^ val.n[1]) | (f.n[2] ^ val.n[2]) |
  585. (f.n[3] ^ val.n[3]) | (f.n[4] ^ val.n[4]) | (f.n[5] ^ val.n[5]) |
  586. (f.n[6] ^ val.n[6]) | (f.n[7] ^ val.n[7]) | (f.n[8] ^ val.n[8]) |
  587. (f.n[9] ^ val.n[9])
  588. return bits == 0
  589. }
  590. // NegateVal negates the passed value and stores the result in f in constant
  591. // time. The caller must provide the magnitude of the passed value for a
  592. // correct result.
  593. //
  594. // The field value is returned to support chaining. This enables syntax like:
  595. // f.NegateVal(f2).AddInt(1) so that f = -f2 + 1.
  596. //
  597. // Preconditions:
  598. // - The max magnitude MUST be 63
  599. // Output Normalized: No
  600. // Output Max Magnitude: Input magnitude + 1
  601. func (f *FieldVal) NegateVal(val *FieldVal, magnitude uint32) *FieldVal {
  602. // Negation in the field is just the prime minus the value. However,
  603. // in order to allow negation against a field value without having to
  604. // normalize/reduce it first, multiply by the magnitude (that is how
  605. // "far" away it is from the normalized value) to adjust. Also, since
  606. // negating a value pushes it one more order of magnitude away from the
  607. // normalized range, add 1 to compensate.
  608. //
  609. // For some intuition here, imagine you're performing mod 12 arithmetic
  610. // (picture a clock) and you are negating the number 7. So you start at
  611. // 12 (which is of course 0 under mod 12) and count backwards (left on
  612. // the clock) 7 times to arrive at 5. Notice this is just 12-7 = 5.
  613. // Now, assume you're starting with 19, which is a number that is
  614. // already larger than the modulus and congruent to 7 (mod 12). When a
  615. // value is already in the desired range, its magnitude is 1. Since 19
  616. // is an additional "step", its magnitude (mod 12) is 2. Since any
  617. // multiple of the modulus is congruent to zero (mod m), the answer can
  618. // be shortcut by simply multiplying the magnitude by the modulus and
  619. // subtracting. Keeping with the example, this would be (2*12)-19 = 5.
  620. f.n[0] = (magnitude+1)*fieldPrimeWordZero - val.n[0]
  621. f.n[1] = (magnitude+1)*fieldPrimeWordOne - val.n[1]
  622. f.n[2] = (magnitude+1)*fieldBaseMask - val.n[2]
  623. f.n[3] = (magnitude+1)*fieldBaseMask - val.n[3]
  624. f.n[4] = (magnitude+1)*fieldBaseMask - val.n[4]
  625. f.n[5] = (magnitude+1)*fieldBaseMask - val.n[5]
  626. f.n[6] = (magnitude+1)*fieldBaseMask - val.n[6]
  627. f.n[7] = (magnitude+1)*fieldBaseMask - val.n[7]
  628. f.n[8] = (magnitude+1)*fieldBaseMask - val.n[8]
  629. f.n[9] = (magnitude+1)*fieldMSBMask - val.n[9]
  630. return f
  631. }
  632. // Negate negates the field value in constant time. The existing field value is
  633. // modified. The caller must provide the magnitude of the field value for a
  634. // correct result.
  635. //
  636. // The field value is returned to support chaining. This enables syntax like:
  637. // f.Negate().AddInt(1) so that f = -f + 1.
  638. //
  639. // Preconditions:
  640. // - The max magnitude MUST be 63
  641. // Output Normalized: No
  642. // Output Max Magnitude: Input magnitude + 1
  643. func (f *FieldVal) Negate(magnitude uint32) *FieldVal {
  644. return f.NegateVal(f, magnitude)
  645. }
  646. // AddInt adds the passed integer to the existing field value and stores the
  647. // result in f in constant time. This is a convenience function since it is
  648. // fairly common to perform some arithmetic with small native integers.
  649. //
  650. // The field value is returned to support chaining. This enables syntax like:
  651. // f.AddInt(1).Add(f2) so that f = f + 1 + f2.
  652. //
  653. // Preconditions:
  654. // - The field value MUST have a max magnitude of 63
  655. // Output Normalized: No
  656. // Output Max Magnitude: Existing field magnitude + 1
  657. func (f *FieldVal) AddInt(ui uint16) *FieldVal {
  658. // Since the field representation intentionally provides overflow bits,
  659. // it's ok to use carryless addition as the carry bit is safely part of
  660. // the word and will be normalized out.
  661. f.n[0] += uint32(ui)
  662. return f
  663. }
  664. // Add adds the passed value to the existing field value and stores the result
  665. // in f in constant time.
  666. //
  667. // The field value is returned to support chaining. This enables syntax like:
  668. // f.Add(f2).AddInt(1) so that f = f + f2 + 1.
  669. //
  670. // Preconditions:
  671. // - The sum of the magnitudes of the two field values MUST be a max of 64
  672. // Output Normalized: No
  673. // Output Max Magnitude: Sum of the magnitude of the two individual field values
  674. func (f *FieldVal) Add(val *FieldVal) *FieldVal {
  675. // Since the field representation intentionally provides overflow bits,
  676. // it's ok to use carryless addition as the carry bit is safely part of
  677. // each word and will be normalized out. This could obviously be done
  678. // in a loop, but the unrolled version is faster.
  679. f.n[0] += val.n[0]
  680. f.n[1] += val.n[1]
  681. f.n[2] += val.n[2]
  682. f.n[3] += val.n[3]
  683. f.n[4] += val.n[4]
  684. f.n[5] += val.n[5]
  685. f.n[6] += val.n[6]
  686. f.n[7] += val.n[7]
  687. f.n[8] += val.n[8]
  688. f.n[9] += val.n[9]
  689. return f
  690. }
  691. // Add2 adds the passed two field values together and stores the result in f in
  692. // constant time.
  693. //
  694. // The field value is returned to support chaining. This enables syntax like:
  695. // f3.Add2(f, f2).AddInt(1) so that f3 = f + f2 + 1.
  696. //
  697. // Preconditions:
  698. // - The sum of the magnitudes of the two field values MUST be a max of 64
  699. // Output Normalized: No
  700. // Output Max Magnitude: Sum of the magnitude of the two field values
  701. func (f *FieldVal) Add2(val *FieldVal, val2 *FieldVal) *FieldVal {
  702. // Since the field representation intentionally provides overflow bits,
  703. // it's ok to use carryless addition as the carry bit is safely part of
  704. // each word and will be normalized out. This could obviously be done
  705. // in a loop, but the unrolled version is faster.
  706. f.n[0] = val.n[0] + val2.n[0]
  707. f.n[1] = val.n[1] + val2.n[1]
  708. f.n[2] = val.n[2] + val2.n[2]
  709. f.n[3] = val.n[3] + val2.n[3]
  710. f.n[4] = val.n[4] + val2.n[4]
  711. f.n[5] = val.n[5] + val2.n[5]
  712. f.n[6] = val.n[6] + val2.n[6]
  713. f.n[7] = val.n[7] + val2.n[7]
  714. f.n[8] = val.n[8] + val2.n[8]
  715. f.n[9] = val.n[9] + val2.n[9]
  716. return f
  717. }
  718. // MulInt multiplies the field value by the passed int and stores the result in
  719. // f in constant time. Note that this function can overflow if multiplying the
  720. // value by any of the individual words exceeds a max uint32. Therefore it is
  721. // important that the caller ensures no overflows will occur before using this
  722. // function.
  723. //
  724. // The field value is returned to support chaining. This enables syntax like:
  725. // f.MulInt(2).Add(f2) so that f = 2 * f + f2.
  726. //
  727. // Preconditions:
  728. // - The field value magnitude multiplied by given val MUST be a max of 64
  729. // Output Normalized: No
  730. // Output Max Magnitude: Existing field magnitude times the provided integer val
  731. func (f *FieldVal) MulInt(val uint8) *FieldVal {
  732. // Since each word of the field representation can hold up to
  733. // 32 - fieldBase extra bits which will be normalized out, it's safe
  734. // to multiply each word without using a larger type or carry
  735. // propagation so long as the values won't overflow a uint32. This
  736. // could obviously be done in a loop, but the unrolled version is
  737. // faster.
  738. ui := uint32(val)
  739. f.n[0] *= ui
  740. f.n[1] *= ui
  741. f.n[2] *= ui
  742. f.n[3] *= ui
  743. f.n[4] *= ui
  744. f.n[5] *= ui
  745. f.n[6] *= ui
  746. f.n[7] *= ui
  747. f.n[8] *= ui
  748. f.n[9] *= ui
  749. return f
  750. }
  751. // Mul multiplies the passed value to the existing field value and stores the
  752. // result in f in constant time. Note that this function can overflow if
  753. // multiplying any of the individual words exceeds a max uint32. In practice,
  754. // this means the magnitude of either value involved in the multiplication must
  755. // be a max of 8.
  756. //
  757. // The field value is returned to support chaining. This enables syntax like:
  758. // f.Mul(f2).AddInt(1) so that f = (f * f2) + 1.
  759. //
  760. // Preconditions:
  761. // - Both field values MUST have a max magnitude of 8
  762. // Output Normalized: No
  763. // Output Max Magnitude: 1
  764. func (f *FieldVal) Mul(val *FieldVal) *FieldVal {
  765. return f.Mul2(f, val)
  766. }
  767. // Mul2 multiplies the passed two field values together and stores the result
  768. // result in f in constant time. Note that this function can overflow if
  769. // multiplying any of the individual words exceeds a max uint32. In practice,
  770. // this means the magnitude of either value involved in the multiplication must
  771. // be a max of 8.
  772. //
  773. // The field value is returned to support chaining. This enables syntax like:
  774. // f3.Mul2(f, f2).AddInt(1) so that f3 = (f * f2) + 1.
  775. //
  776. // Preconditions:
  777. // - Both input field values MUST have a max magnitude of 8
  778. // Output Normalized: No
  779. // Output Max Magnitude: 1
  780. func (f *FieldVal) Mul2(val *FieldVal, val2 *FieldVal) *FieldVal {
  781. // This could be done with a couple of for loops and an array to store
  782. // the intermediate terms, but this unrolled version is significantly
  783. // faster.
  784. // Terms for 2^(fieldBase*0).
  785. m := uint64(val.n[0]) * uint64(val2.n[0])
  786. t0 := m & fieldBaseMask
  787. // Terms for 2^(fieldBase*1).
  788. m = (m >> fieldBase) +
  789. uint64(val.n[0])*uint64(val2.n[1]) +
  790. uint64(val.n[1])*uint64(val2.n[0])
  791. t1 := m & fieldBaseMask
  792. // Terms for 2^(fieldBase*2).
  793. m = (m >> fieldBase) +
  794. uint64(val.n[0])*uint64(val2.n[2]) +
  795. uint64(val.n[1])*uint64(val2.n[1]) +
  796. uint64(val.n[2])*uint64(val2.n[0])
  797. t2 := m & fieldBaseMask
  798. // Terms for 2^(fieldBase*3).
  799. m = (m >> fieldBase) +
  800. uint64(val.n[0])*uint64(val2.n[3]) +
  801. uint64(val.n[1])*uint64(val2.n[2]) +
  802. uint64(val.n[2])*uint64(val2.n[1]) +
  803. uint64(val.n[3])*uint64(val2.n[0])
  804. t3 := m & fieldBaseMask
  805. // Terms for 2^(fieldBase*4).
  806. m = (m >> fieldBase) +
  807. uint64(val.n[0])*uint64(val2.n[4]) +
  808. uint64(val.n[1])*uint64(val2.n[3]) +
  809. uint64(val.n[2])*uint64(val2.n[2]) +
  810. uint64(val.n[3])*uint64(val2.n[1]) +
  811. uint64(val.n[4])*uint64(val2.n[0])
  812. t4 := m & fieldBaseMask
  813. // Terms for 2^(fieldBase*5).
  814. m = (m >> fieldBase) +
  815. uint64(val.n[0])*uint64(val2.n[5]) +
  816. uint64(val.n[1])*uint64(val2.n[4]) +
  817. uint64(val.n[2])*uint64(val2.n[3]) +
  818. uint64(val.n[3])*uint64(val2.n[2]) +
  819. uint64(val.n[4])*uint64(val2.n[1]) +
  820. uint64(val.n[5])*uint64(val2.n[0])
  821. t5 := m & fieldBaseMask
  822. // Terms for 2^(fieldBase*6).
  823. m = (m >> fieldBase) +
  824. uint64(val.n[0])*uint64(val2.n[6]) +
  825. uint64(val.n[1])*uint64(val2.n[5]) +
  826. uint64(val.n[2])*uint64(val2.n[4]) +
  827. uint64(val.n[3])*uint64(val2.n[3]) +
  828. uint64(val.n[4])*uint64(val2.n[2]) +
  829. uint64(val.n[5])*uint64(val2.n[1]) +
  830. uint64(val.n[6])*uint64(val2.n[0])
  831. t6 := m & fieldBaseMask
  832. // Terms for 2^(fieldBase*7).
  833. m = (m >> fieldBase) +
  834. uint64(val.n[0])*uint64(val2.n[7]) +
  835. uint64(val.n[1])*uint64(val2.n[6]) +
  836. uint64(val.n[2])*uint64(val2.n[5]) +
  837. uint64(val.n[3])*uint64(val2.n[4]) +
  838. uint64(val.n[4])*uint64(val2.n[3]) +
  839. uint64(val.n[5])*uint64(val2.n[2]) +
  840. uint64(val.n[6])*uint64(val2.n[1]) +
  841. uint64(val.n[7])*uint64(val2.n[0])
  842. t7 := m & fieldBaseMask
  843. // Terms for 2^(fieldBase*8).
  844. m = (m >> fieldBase) +
  845. uint64(val.n[0])*uint64(val2.n[8]) +
  846. uint64(val.n[1])*uint64(val2.n[7]) +
  847. uint64(val.n[2])*uint64(val2.n[6]) +
  848. uint64(val.n[3])*uint64(val2.n[5]) +
  849. uint64(val.n[4])*uint64(val2.n[4]) +
  850. uint64(val.n[5])*uint64(val2.n[3]) +
  851. uint64(val.n[6])*uint64(val2.n[2]) +
  852. uint64(val.n[7])*uint64(val2.n[1]) +
  853. uint64(val.n[8])*uint64(val2.n[0])
  854. t8 := m & fieldBaseMask
  855. // Terms for 2^(fieldBase*9).
  856. m = (m >> fieldBase) +
  857. uint64(val.n[0])*uint64(val2.n[9]) +
  858. uint64(val.n[1])*uint64(val2.n[8]) +
  859. uint64(val.n[2])*uint64(val2.n[7]) +
  860. uint64(val.n[3])*uint64(val2.n[6]) +
  861. uint64(val.n[4])*uint64(val2.n[5]) +
  862. uint64(val.n[5])*uint64(val2.n[4]) +
  863. uint64(val.n[6])*uint64(val2.n[3]) +
  864. uint64(val.n[7])*uint64(val2.n[2]) +
  865. uint64(val.n[8])*uint64(val2.n[1]) +
  866. uint64(val.n[9])*uint64(val2.n[0])
  867. t9 := m & fieldBaseMask
  868. // Terms for 2^(fieldBase*10).
  869. m = (m >> fieldBase) +
  870. uint64(val.n[1])*uint64(val2.n[9]) +
  871. uint64(val.n[2])*uint64(val2.n[8]) +
  872. uint64(val.n[3])*uint64(val2.n[7]) +
  873. uint64(val.n[4])*uint64(val2.n[6]) +
  874. uint64(val.n[5])*uint64(val2.n[5]) +
  875. uint64(val.n[6])*uint64(val2.n[4]) +
  876. uint64(val.n[7])*uint64(val2.n[3]) +
  877. uint64(val.n[8])*uint64(val2.n[2]) +
  878. uint64(val.n[9])*uint64(val2.n[1])
  879. t10 := m & fieldBaseMask
  880. // Terms for 2^(fieldBase*11).
  881. m = (m >> fieldBase) +
  882. uint64(val.n[2])*uint64(val2.n[9]) +
  883. uint64(val.n[3])*uint64(val2.n[8]) +
  884. uint64(val.n[4])*uint64(val2.n[7]) +
  885. uint64(val.n[5])*uint64(val2.n[6]) +
  886. uint64(val.n[6])*uint64(val2.n[5]) +
  887. uint64(val.n[7])*uint64(val2.n[4]) +
  888. uint64(val.n[8])*uint64(val2.n[3]) +
  889. uint64(val.n[9])*uint64(val2.n[2])
  890. t11 := m & fieldBaseMask
  891. // Terms for 2^(fieldBase*12).
  892. m = (m >> fieldBase) +
  893. uint64(val.n[3])*uint64(val2.n[9]) +
  894. uint64(val.n[4])*uint64(val2.n[8]) +
  895. uint64(val.n[5])*uint64(val2.n[7]) +
  896. uint64(val.n[6])*uint64(val2.n[6]) +
  897. uint64(val.n[7])*uint64(val2.n[5]) +
  898. uint64(val.n[8])*uint64(val2.n[4]) +
  899. uint64(val.n[9])*uint64(val2.n[3])
  900. t12 := m & fieldBaseMask
  901. // Terms for 2^(fieldBase*13).
  902. m = (m >> fieldBase) +
  903. uint64(val.n[4])*uint64(val2.n[9]) +
  904. uint64(val.n[5])*uint64(val2.n[8]) +
  905. uint64(val.n[6])*uint64(val2.n[7]) +
  906. uint64(val.n[7])*uint64(val2.n[6]) +
  907. uint64(val.n[8])*uint64(val2.n[5]) +
  908. uint64(val.n[9])*uint64(val2.n[4])
  909. t13 := m & fieldBaseMask
  910. // Terms for 2^(fieldBase*14).
  911. m = (m >> fieldBase) +
  912. uint64(val.n[5])*uint64(val2.n[9]) +
  913. uint64(val.n[6])*uint64(val2.n[8]) +
  914. uint64(val.n[7])*uint64(val2.n[7]) +
  915. uint64(val.n[8])*uint64(val2.n[6]) +
  916. uint64(val.n[9])*uint64(val2.n[5])
  917. t14 := m & fieldBaseMask
  918. // Terms for 2^(fieldBase*15).
  919. m = (m >> fieldBase) +
  920. uint64(val.n[6])*uint64(val2.n[9]) +
  921. uint64(val.n[7])*uint64(val2.n[8]) +
  922. uint64(val.n[8])*uint64(val2.n[7]) +
  923. uint64(val.n[9])*uint64(val2.n[6])
  924. t15 := m & fieldBaseMask
  925. // Terms for 2^(fieldBase*16).
  926. m = (m >> fieldBase) +
  927. uint64(val.n[7])*uint64(val2.n[9]) +
  928. uint64(val.n[8])*uint64(val2.n[8]) +
  929. uint64(val.n[9])*uint64(val2.n[7])
  930. t16 := m & fieldBaseMask
  931. // Terms for 2^(fieldBase*17).
  932. m = (m >> fieldBase) +
  933. uint64(val.n[8])*uint64(val2.n[9]) +
  934. uint64(val.n[9])*uint64(val2.n[8])
  935. t17 := m & fieldBaseMask
  936. // Terms for 2^(fieldBase*18).
  937. m = (m >> fieldBase) + uint64(val.n[9])*uint64(val2.n[9])
  938. t18 := m & fieldBaseMask
  939. // What's left is for 2^(fieldBase*19).
  940. t19 := m >> fieldBase
  941. // At this point, all of the terms are grouped into their respective
  942. // base.
  943. //
  944. // Per [HAC] section 14.3.4: Reduction method of moduli of special form,
  945. // when the modulus is of the special form m = b^t - c, highly efficient
  946. // reduction can be achieved per the provided algorithm.
  947. //
  948. // The secp256k1 prime is equivalent to 2^256 - 4294968273, so it fits
  949. // this criteria.
  950. //
  951. // 4294968273 in field representation (base 2^26) is:
  952. // n[0] = 977
  953. // n[1] = 64
  954. // That is to say (2^26 * 64) + 977 = 4294968273
  955. //
  956. // Since each word is in base 26, the upper terms (t10 and up) start
  957. // at 260 bits (versus the final desired range of 256 bits), so the
  958. // field representation of 'c' from above needs to be adjusted for the
  959. // extra 4 bits by multiplying it by 2^4 = 16. 4294968273 * 16 =
  960. // 68719492368. Thus, the adjusted field representation of 'c' is:
  961. // n[0] = 977 * 16 = 15632
  962. // n[1] = 64 * 16 = 1024
  963. // That is to say (2^26 * 1024) + 15632 = 68719492368
  964. //
  965. // To reduce the final term, t19, the entire 'c' value is needed instead
  966. // of only n[0] because there are no more terms left to handle n[1].
  967. // This means there might be some magnitude left in the upper bits that
  968. // is handled below.
  969. m = t0 + t10*15632
  970. t0 = m & fieldBaseMask
  971. m = (m >> fieldBase) + t1 + t10*1024 + t11*15632
  972. t1 = m & fieldBaseMask
  973. m = (m >> fieldBase) + t2 + t11*1024 + t12*15632
  974. t2 = m & fieldBaseMask
  975. m = (m >> fieldBase) + t3 + t12*1024 + t13*15632
  976. t3 = m & fieldBaseMask
  977. m = (m >> fieldBase) + t4 + t13*1024 + t14*15632
  978. t4 = m & fieldBaseMask
  979. m = (m >> fieldBase) + t5 + t14*1024 + t15*15632
  980. t5 = m & fieldBaseMask
  981. m = (m >> fieldBase) + t6 + t15*1024 + t16*15632
  982. t6 = m & fieldBaseMask
  983. m = (m >> fieldBase) + t7 + t16*1024 + t17*15632
  984. t7 = m & fieldBaseMask
  985. m = (m >> fieldBase) + t8 + t17*1024 + t18*15632
  986. t8 = m & fieldBaseMask
  987. m = (m >> fieldBase) + t9 + t18*1024 + t19*68719492368
  988. t9 = m & fieldMSBMask
  989. m = m >> fieldMSBBits
  990. // At this point, if the magnitude is greater than 0, the overall value
  991. // is greater than the max possible 256-bit value. In particular, it is
  992. // "how many times larger" than the max value it is.
  993. //
  994. // The algorithm presented in [HAC] section 14.3.4 repeats until the
  995. // quotient is zero. However, due to the above, we already know at
  996. // least how many times we would need to repeat as it's the value
  997. // currently in m. Thus we can simply multiply the magnitude by the
  998. // field representation of the prime and do a single iteration. Notice
  999. // that nothing will be changed when the magnitude is zero, so we could
  1000. // skip this in that case, however always running regardless allows it
  1001. // to run in constant time. The final result will be in the range
  1002. // 0 <= result <= prime + (2^64 - c), so it is guaranteed to have a
  1003. // magnitude of 1, but it is denormalized.
  1004. d := t0 + m*977
  1005. f.n[0] = uint32(d & fieldBaseMask)
  1006. d = (d >> fieldBase) + t1 + m*64
  1007. f.n[1] = uint32(d & fieldBaseMask)
  1008. f.n[2] = uint32((d >> fieldBase) + t2)
  1009. f.n[3] = uint32(t3)
  1010. f.n[4] = uint32(t4)
  1011. f.n[5] = uint32(t5)
  1012. f.n[6] = uint32(t6)
  1013. f.n[7] = uint32(t7)
  1014. f.n[8] = uint32(t8)
  1015. f.n[9] = uint32(t9)
  1016. return f
  1017. }
  1018. // SquareRootVal either calculates the square root of the passed value when it
  1019. // exists or the square root of the negation of the value when it does not exist
  1020. // and stores the result in f in constant time. The return flag is true when
  1021. // the calculated square root is for the passed value itself and false when it
  1022. // is for its negation.
  1023. //
  1024. // Note that this function can overflow if multiplying any of the individual
  1025. // words exceeds a max uint32. In practice, this means the magnitude of the
  1026. // field must be a max of 8 to prevent overflow. The magnitude of the result
  1027. // will be 1.
  1028. //
  1029. // Preconditions:
  1030. // - The input field value MUST have a max magnitude of 8
  1031. // Output Normalized: No
  1032. // Output Max Magnitude: 1
  1033. func (f *FieldVal) SquareRootVal(val *FieldVal) bool {
  1034. // This uses the Tonelli-Shanks method for calculating the square root of
  1035. // the value when it exists. The key principles of the method follow.
  1036. //
  1037. // Fermat's little theorem states that for a nonzero number 'a' and prime
  1038. // 'p', a^(p-1) ≡ 1 (mod p).
  1039. //
  1040. // Further, Euler's criterion states that an integer 'a' has a square root
  1041. // (aka is a quadratic residue) modulo a prime if a^((p-1)/2) ≡ 1 (mod p)
  1042. // and, conversely, when it does NOT have a square root (aka 'a' is a
  1043. // non-residue) a^((p-1)/2) ≡ -1 (mod p).
  1044. //
  1045. // This can be seen by considering that Fermat's little theorem can be
  1046. // written as (a^((p-1)/2) - 1)(a^((p-1)/2) + 1) ≡ 0 (mod p). Therefore,
  1047. // one of the two factors must be 0. Then, when a ≡ x^2 (aka 'a' is a
  1048. // quadratic residue), (x^2)^((p-1)/2) ≡ x^(p-1) ≡ 1 (mod p) which implies
  1049. // the first factor must be zero. Finally, per Lagrange's theorem, the
  1050. // non-residues are the only remaining possible solutions and thus must make
  1051. // the second factor zero to satisfy Fermat's little theorem implying that
  1052. // a^((p-1)/2) ≡ -1 (mod p) for that case.
  1053. //
  1054. // The Tonelli-Shanks method uses these facts along with factoring out
  1055. // powers of two to solve a congruence that results in either the solution
  1056. // when the square root exists or the square root of the negation of the
  1057. // value when it does not. In the case of primes that are ≡ 3 (mod 4), the
  1058. // possible solutions are r = ±a^((p+1)/4) (mod p). Therefore, either r^2 ≡
  1059. // a (mod p) is true in which case ±r are the two solutions, or r^2 ≡ -a
  1060. // (mod p) in which case 'a' is a non-residue and there are no solutions.
  1061. //
  1062. // The secp256k1 prime is ≡ 3 (mod 4), so this result applies.
  1063. //
  1064. // In other words, calculate a^((p+1)/4) and then square it and check it
  1065. // against the original value to determine if it is actually the square
  1066. // root.
  1067. //
  1068. // In order to efficiently compute a^((p+1)/4), (p+1)/4 needs to be split
  1069. // into a sequence of squares and multiplications that minimizes the number
  1070. // of multiplications needed (since they are more costly than squarings).
  1071. //
  1072. // The secp256k1 prime + 1 / 4 is 2^254 - 2^30 - 244. In binary, that is:
  1073. //
  1074. // 00111111 11111111 11111111 11111111
  1075. // 11111111 11111111 11111111 11111111
  1076. // 11111111 11111111 11111111 11111111
  1077. // 11111111 11111111 11111111 11111111
  1078. // 11111111 11111111 11111111 11111111
  1079. // 11111111 11111111 11111111 11111111
  1080. // 11111111 11111111 11111111 11111111
  1081. // 10111111 11111111 11111111 00001100
  1082. //
  1083. // Notice that can be broken up into three windows of consecutive 1s (in
  1084. // order of least to most signifcant) as:
  1085. //
  1086. // 6-bit window with two bits set (bits 4, 5, 6, 7 unset)
  1087. // 23-bit window with 22 bits set (bit 30 unset)
  1088. // 223-bit window with all 223 bits set
  1089. //
  1090. // Thus, the groups of 1 bits in each window forms the set:
  1091. // S = {2, 22, 223}.
  1092. //
  1093. // The strategy is to calculate a^(2^n - 1) for each grouping via an
  1094. // addition chain with a sliding window.
  1095. //
  1096. // The addition chain used is (credits to Peter Dettman):
  1097. // (0,0),(1,0),(2,2),(3,2),(4,1),(5,5),(6,6),(7,7),(8,8),(9,7),(10,2)
  1098. // => 2^1 2^[2] 2^3 2^6 2^9 2^11 2^[22] 2^44 2^88 2^176 2^220 2^[223]
  1099. //
  1100. // This has a cost of 254 field squarings and 13 field multiplications.
  1101. var a, a2, a3, a6, a9, a11, a22, a44, a88, a176, a220, a223 FieldVal
  1102. a.Set(val)
  1103. a2.SquareVal(&a).Mul(&a) // a2 = a^(2^2 - 1)
  1104. a3.SquareVal(&a2).Mul(&a) // a3 = a^(2^3 - 1)
  1105. a6.SquareVal(&a3).Square().Square() // a6 = a^(2^6 - 2^3)
  1106. a6.Mul(&a3) // a6 = a^(2^6 - 1)
  1107. a9.SquareVal(&a6).Square().Square() // a9 = a^(2^9 - 2^3)
  1108. a9.Mul(&a3) // a9 = a^(2^9 - 1)
  1109. a11.SquareVal(&a9).Square() // a11 = a^(2^11 - 2^2)
  1110. a11.Mul(&a2) // a11 = a^(2^11 - 1)
  1111. a22.SquareVal(&a11).Square().Square().Square().Square() // a22 = a^(2^16 - 2^5)
  1112. a22.Square().Square().Square().Square().Square() // a22 = a^(2^21 - 2^10)
  1113. a22.Square() // a22 = a^(2^22 - 2^11)
  1114. a22.Mul(&a11) // a22 = a^(2^22 - 1)
  1115. a44.SquareVal(&a22).Square().Square().Square().Square() // a44 = a^(2^27 - 2^5)
  1116. a44.Square().Square().Square().Square().Square() // a44 = a^(2^32 - 2^10)
  1117. a44.Square().Square().Square().Square().Square() // a44 = a^(2^37 - 2^15)
  1118. a44.Square().Square().Square().Square().Square() // a44 = a^(2^42 - 2^20)
  1119. a44.Square().Square() // a44 = a^(2^44 - 2^22)
  1120. a44.Mul(&a22) // a44 = a^(2^44 - 1)
  1121. a88.SquareVal(&a44).Square().Square().Square().Square() // a88 = a^(2^49 - 2^5)
  1122. a88.Square().Square().Square().Square().Square() // a88 = a^(2^54 - 2^10)
  1123. a88.Square().Square().Square().Square().Square() // a88 = a^(2^59 - 2^15)
  1124. a88.Square().Square().Square().Square().Square() // a88 = a^(2^64 - 2^20)
  1125. a88.Square().Square().Square().Square().Square() // a88 = a^(2^69 - 2^25)
  1126. a88.Square().Square().Square().Square().Square() // a88 = a^(2^74 - 2^30)
  1127. a88.Square().Square().Square().Square().Square() // a88 = a^(2^79 - 2^35)
  1128. a88.Square().Square().Square().Square().Square() // a88 = a^(2^84 - 2^40)
  1129. a88.Square().Square().Square().Square() // a88 = a^(2^88 - 2^44)
  1130. a88.Mul(&a44) // a88 = a^(2^88 - 1)
  1131. a176.SquareVal(&a88).Square().Square().Square().Square() // a176 = a^(2^93 - 2^5)
  1132. a176.Square().Square().Square().Square().Square() // a176 = a^(2^98 - 2^10)
  1133. a176.Square().Square().Square().Square().Square() // a176 = a^(2^103 - 2^15)
  1134. a176.Square().Square().Square().Square().Square() // a176 = a^(2^108 - 2^20)
  1135. a176.Square().Square().Square().Square().Square() // a176 = a^(2^113 - 2^25)
  1136. a176.Square().Square().Square().Square().Square() // a176 = a^(2^118 - 2^30)
  1137. a176.Square().Square().Square().Square().Square() // a176 = a^(2^123 - 2^35)
  1138. a176.Square().Square().Square().Square().Square() // a176 = a^(2^128 - 2^40)
  1139. a176.Square().Square().Square().Square().Square() // a176 = a^(2^133 - 2^45)
  1140. a176.Square().Square().Square().Square().Square() // a176 = a^(2^138 - 2^50)
  1141. a176.Square().Square().Square().Square().Square() // a176 = a^(2^143 - 2^55)
  1142. a176.Square().Square().Square().Square().Square() // a176 = a^(2^148 - 2^60)
  1143. a176.Square().Square().Square().Square().Square() // a176 = a^(2^153 - 2^65)
  1144. a176.Square().Square().Square().Square().Square() // a176 = a^(2^158 - 2^70)
  1145. a176.Square().Square().Square().Square().Square() // a176 = a^(2^163 - 2^75)
  1146. a176.Square().Square().Square().Square().Square() // a176 = a^(2^168 - 2^80)
  1147. a176.Square().Square().Square().Square().Square() // a176 = a^(2^173 - 2^85)
  1148. a176.Square().Square().Square() // a176 = a^(2^176 - 2^88)
  1149. a176.Mul(&a88) // a176 = a^(2^176 - 1)
  1150. a220.SquareVal(&a176).Square().Square().Square().Square() // a220 = a^(2^181 - 2^5)
  1151. a220.Square().Square().Square().Square().Square() // a220 = a^(2^186 - 2^10)
  1152. a220.Square().Square().Square().Square().Square() // a220 = a^(2^191 - 2^15)
  1153. a220.Square().Square().Square().Square().Square() // a220 = a^(2^196 - 2^20)
  1154. a220.Square().Square().Square().Square().Square() // a220 = a^(2^201 - 2^25)
  1155. a220.Square().Square().Square().Square().Square() // a220 = a^(2^206 - 2^30)
  1156. a220.Square().Square().Square().Square().Square() // a220 = a^(2^211 - 2^35)
  1157. a220.Square().Square().Square().Square().Square() // a220 = a^(2^216 - 2^40)
  1158. a220.Square().Square().Square().Square() // a220 = a^(2^220 - 2^44)
  1159. a220.Mul(&a44) // a220 = a^(2^220 - 1)
  1160. a223.SquareVal(&a220).Square().Square() // a223 = a^(2^223 - 2^3)
  1161. a223.Mul(&a3) // a223 = a^(2^223 - 1)
  1162. f.SquareVal(&a223).Square().Square().Square().Square() // f = a^(2^228 - 2^5)
  1163. f.Square().Square().Square().Square().Square() // f = a^(2^233 - 2^10)
  1164. f.Square().Square().Square().Square().Square() // f = a^(2^238 - 2^15)
  1165. f.Square().Square().Square().Square().Square() // f = a^(2^243 - 2^20)
  1166. f.Square().Square().Square() // f = a^(2^246 - 2^23)
  1167. f.Mul(&a22) // f = a^(2^246 - 2^22 - 1)
  1168. f.Square().Square().Square().Square().Square() // f = a^(2^251 - 2^27 - 2^5)
  1169. f.Square() // f = a^(2^252 - 2^28 - 2^6)
  1170. f.Mul(&a2) // f = a^(2^252 - 2^28 - 2^6 - 2^1 - 1)
  1171. f.Square().Square() // f = a^(2^254 - 2^30 - 2^8 - 2^3 - 2^2)
  1172. // // = a^(2^254 - 2^30 - 244)
  1173. // // = a^((p+1)/4)
  1174. // Ensure the calculated result is actually the square root by squaring it
  1175. // and checking against the original value.
  1176. var sqr FieldVal
  1177. return sqr.SquareVal(f).Normalize().Equals(val.Normalize())
  1178. }
  1179. // Square squares the field value in constant time. The existing field value is
  1180. // modified. Note that this function can overflow if multiplying any of the
  1181. // individual words exceeds a max uint32. In practice, this means the magnitude
  1182. // of the field must be a max of 8 to prevent overflow.
  1183. //
  1184. // The field value is returned to support chaining. This enables syntax like:
  1185. // f.Square().Mul(f2) so that f = f^2 * f2.
  1186. //
  1187. // Preconditions:
  1188. // - The field value MUST have a max magnitude of 8
  1189. // Output Normalized: No
  1190. // Output Max Magnitude: 1
  1191. func (f *FieldVal) Square() *FieldVal {
  1192. return f.SquareVal(f)
  1193. }
  1194. // SquareVal squares the passed value and stores the result in f in constant
  1195. // time. Note that this function can overflow if multiplying any of the
  1196. // individual words exceeds a max uint32. In practice, this means the magnitude
  1197. // of the field being squared must be a max of 8 to prevent overflow.
  1198. //
  1199. // The field value is returned to support chaining. This enables syntax like:
  1200. // f3.SquareVal(f).Mul(f) so that f3 = f^2 * f = f^3.
  1201. //
  1202. // Preconditions:
  1203. // - The input field value MUST have a max magnitude of 8
  1204. // Output Normalized: No
  1205. // Output Max Magnitude: 1
  1206. func (f *FieldVal) SquareVal(val *FieldVal) *FieldVal {
  1207. // This could be done with a couple of for loops and an array to store
  1208. // the intermediate terms, but this unrolled version is significantly
  1209. // faster.
  1210. // Terms for 2^(fieldBase*0).
  1211. m := uint64(val.n[0]) * uint64(val.n[0])
  1212. t0 := m & fieldBaseMask
  1213. // Terms for 2^(fieldBase*1).
  1214. m = (m >> fieldBase) + 2*uint64(val.n[0])*uint64(val.n[1])
  1215. t1 := m & fieldBaseMask
  1216. // Terms for 2^(fieldBase*2).
  1217. m = (m >> fieldBase) +
  1218. 2*uint64(val.n[0])*uint64(val.n[2]) +
  1219. uint64(val.n[1])*uint64(val.n[1])
  1220. t2 := m & fieldBaseMask
  1221. // Terms for 2^(fieldBase*3).
  1222. m = (m >> fieldBase) +
  1223. 2*uint64(val.n[0])*uint64(val.n[3]) +
  1224. 2*uint64(val.n[1])*uint64(val.n[2])
  1225. t3 := m & fieldBaseMask
  1226. // Terms for 2^(fieldBase*4).
  1227. m = (m >> fieldBase) +
  1228. 2*uint64(val.n[0])*uint64(val.n[4]) +
  1229. 2*uint64(val.n[1])*uint64(val.n[3]) +
  1230. uint64(val.n[2])*uint64(val.n[2])
  1231. t4 := m & fieldBaseMask
  1232. // Terms for 2^(fieldBase*5).
  1233. m = (m >> fieldBase) +
  1234. 2*uint64(val.n[0])*uint64(val.n[5]) +
  1235. 2*uint64(val.n[1])*uint64(val.n[4]) +
  1236. 2*uint64(val.n[2])*uint64(val.n[3])
  1237. t5 := m & fieldBaseMask
  1238. // Terms for 2^(fieldBase*6).
  1239. m = (m >> fieldBase) +
  1240. 2*uint64(val.n[0])*uint64(val.n[6]) +
  1241. 2*uint64(val.n[1])*uint64(val.n[5]) +
  1242. 2*uint64(val.n[2])*uint64(val.n[4]) +
  1243. uint64(val.n[3])*uint64(val.n[3])
  1244. t6 := m & fieldBaseMask
  1245. // Terms for 2^(fieldBase*7).
  1246. m = (m >> fieldBase) +
  1247. 2*uint64(val.n[0])*uint64(val.n[7]) +
  1248. 2*uint64(val.n[1])*uint64(val.n[6]) +
  1249. 2*uint64(val.n[2])*uint64(val.n[5]) +
  1250. 2*uint64(val.n[3])*uint64(val.n[4])
  1251. t7 := m & fieldBaseMask
  1252. // Terms for 2^(fieldBase*8).
  1253. m = (m >> fieldBase) +
  1254. 2*uint64(val.n[0])*uint64(val.n[8]) +
  1255. 2*uint64(val.n[1])*uint64(val.n[7]) +
  1256. 2*uint64(val.n[2])*uint64(val.n[6]) +
  1257. 2*uint64(val.n[3])*uint64(val.n[5]) +
  1258. uint64(val.n[4])*uint64(val.n[4])
  1259. t8 := m & fieldBaseMask
  1260. // Terms for 2^(fieldBase*9).
  1261. m = (m >> fieldBase) +
  1262. 2*uint64(val.n[0])*uint64(val.n[9]) +
  1263. 2*uint64(val.n[1])*uint64(val.n[8]) +
  1264. 2*uint64(val.n[2])*uint64(val.n[7]) +
  1265. 2*uint64(val.n[3])*uint64(val.n[6]) +
  1266. 2*uint64(val.n[4])*uint64(val.n[5])
  1267. t9 := m & fieldBaseMask
  1268. // Terms for 2^(fieldBase*10).
  1269. m = (m >> fieldBase) +
  1270. 2*uint64(val.n[1])*uint64(val.n[9]) +
  1271. 2*uint64(val.n[2])*uint64(val.n[8]) +
  1272. 2*uint64(val.n[3])*uint64(val.n[7]) +
  1273. 2*uint64(val.n[4])*uint64(val.n[6]) +
  1274. uint64(val.n[5])*uint64(val.n[5])
  1275. t10 := m & fieldBaseMask
  1276. // Terms for 2^(fieldBase*11).
  1277. m = (m >> fieldBase) +
  1278. 2*uint64(val.n[2])*uint64(val.n[9]) +
  1279. 2*uint64(val.n[3])*uint64(val.n[8]) +
  1280. 2*uint64(val.n[4])*uint64(val.n[7]) +
  1281. 2*uint64(val.n[5])*uint64(val.n[6])
  1282. t11 := m & fieldBaseMask
  1283. // Terms for 2^(fieldBase*12).
  1284. m = (m >> fieldBase) +
  1285. 2*uint64(val.n[3])*uint64(val.n[9]) +
  1286. 2*uint64(val.n[4])*uint64(val.n[8]) +
  1287. 2*uint64(val.n[5])*uint64(val.n[7]) +
  1288. uint64(val.n[6])*uint64(val.n[6])
  1289. t12 := m & fieldBaseMask
  1290. // Terms for 2^(fieldBase*13).
  1291. m = (m >> fieldBase) +
  1292. 2*uint64(val.n[4])*uint64(val.n[9]) +
  1293. 2*uint64(val.n[5])*uint64(val.n[8]) +
  1294. 2*uint64(val.n[6])*uint64(val.n[7])
  1295. t13 := m & fieldBaseMask
  1296. // Terms for 2^(fieldBase*14).
  1297. m = (m >> fieldBase) +
  1298. 2*uint64(val.n[5])*uint64(val.n[9]) +
  1299. 2*uint64(val.n[6])*uint64(val.n[8]) +
  1300. uint64(val.n[7])*uint64(val.n[7])
  1301. t14 := m & fieldBaseMask
  1302. // Terms for 2^(fieldBase*15).
  1303. m = (m >> fieldBase) +
  1304. 2*uint64(val.n[6])*uint64(val.n[9]) +
  1305. 2*uint64(val.n[7])*uint64(val.n[8])
  1306. t15 := m & fieldBaseMask
  1307. // Terms for 2^(fieldBase*16).
  1308. m = (m >> fieldBase) +
  1309. 2*uint64(val.n[7])*uint64(val.n[9]) +
  1310. uint64(val.n[8])*uint64(val.n[8])
  1311. t16 := m & fieldBaseMask
  1312. // Terms for 2^(fieldBase*17).
  1313. m = (m >> fieldBase) + 2*uint64(val.n[8])*uint64(val.n[9])
  1314. t17 := m & fieldBaseMask
  1315. // Terms for 2^(fieldBase*18).
  1316. m = (m >> fieldBase) + uint64(val.n[9])*uint64(val.n[9])
  1317. t18 := m & fieldBaseMask
  1318. // What's left is for 2^(fieldBase*19).
  1319. t19 := m >> fieldBase
  1320. // At this point, all of the terms are grouped into their respective
  1321. // base.
  1322. //
  1323. // Per [HAC] section 14.3.4: Reduction method of moduli of special form,
  1324. // when the modulus is of the special form m = b^t - c, highly efficient
  1325. // reduction can be achieved per the provided algorithm.
  1326. //
  1327. // The secp256k1 prime is equivalent to 2^256 - 4294968273, so it fits
  1328. // this criteria.
  1329. //
  1330. // 4294968273 in field representation (base 2^26) is:
  1331. // n[0] = 977
  1332. // n[1] = 64
  1333. // That is to say (2^26 * 64) + 977 = 4294968273
  1334. //
  1335. // Since each word is in base 26, the upper terms (t10 and up) start
  1336. // at 260 bits (versus the final desired range of 256 bits), so the
  1337. // field representation of 'c' from above needs to be adjusted for the
  1338. // extra 4 bits by multiplying it by 2^4 = 16. 4294968273 * 16 =
  1339. // 68719492368. Thus, the adjusted field representation of 'c' is:
  1340. // n[0] = 977 * 16 = 15632
  1341. // n[1] = 64 * 16 = 1024
  1342. // That is to say (2^26 * 1024) + 15632 = 68719492368
  1343. //
  1344. // To reduce the final term, t19, the entire 'c' value is needed instead
  1345. // of only n[0] because there are no more terms left to handle n[1].
  1346. // This means there might be some magnitude left in the upper bits that
  1347. // is handled below.
  1348. m = t0 + t10*15632
  1349. t0 = m & fieldBaseMask
  1350. m = (m >> fieldBase) + t1 + t10*1024 + t11*15632
  1351. t1 = m & fieldBaseMask
  1352. m = (m >> fieldBase) + t2 + t11*1024 + t12*15632
  1353. t2 = m & fieldBaseMask
  1354. m = (m >> fieldBase) + t3 + t12*1024 + t13*15632
  1355. t3 = m & fieldBaseMask
  1356. m = (m >> fieldBase) + t4 + t13*1024 + t14*15632
  1357. t4 = m & fieldBaseMask
  1358. m = (m >> fieldBase) + t5 + t14*1024 + t15*15632
  1359. t5 = m & fieldBaseMask
  1360. m = (m >> fieldBase) + t6 + t15*1024 + t16*15632
  1361. t6 = m & fieldBaseMask
  1362. m = (m >> fieldBase) + t7 + t16*1024 + t17*15632
  1363. t7 = m & fieldBaseMask
  1364. m = (m >> fieldBase) + t8 + t17*1024 + t18*15632
  1365. t8 = m & fieldBaseMask
  1366. m = (m >> fieldBase) + t9 + t18*1024 + t19*68719492368
  1367. t9 = m & fieldMSBMask
  1368. m = m >> fieldMSBBits
  1369. // At this point, if the magnitude is greater than 0, the overall value
  1370. // is greater than the max possible 256-bit value. In particular, it is
  1371. // "how many times larger" than the max value it is.
  1372. //
  1373. // The algorithm presented in [HAC] section 14.3.4 repeats until the
  1374. // quotient is zero. However, due to the above, we already know at
  1375. // least how many times we would need to repeat as it's the value
  1376. // currently in m. Thus we can simply multiply the magnitude by the
  1377. // field representation of the prime and do a single iteration. Notice
  1378. // that nothing will be changed when the magnitude is zero, so we could
  1379. // skip this in that case, however always running regardless allows it
  1380. // to run in constant time. The final result will be in the range
  1381. // 0 <= result <= prime + (2^64 - c), so it is guaranteed to have a
  1382. // magnitude of 1, but it is denormalized.
  1383. n := t0 + m*977
  1384. f.n[0] = uint32(n & fieldBaseMask)
  1385. n = (n >> fieldBase) + t1 + m*64
  1386. f.n[1] = uint32(n & fieldBaseMask)
  1387. f.n[2] = uint32((n >> fieldBase) + t2)
  1388. f.n[3] = uint32(t3)
  1389. f.n[4] = uint32(t4)
  1390. f.n[5] = uint32(t5)
  1391. f.n[6] = uint32(t6)
  1392. f.n[7] = uint32(t7)
  1393. f.n[8] = uint32(t8)
  1394. f.n[9] = uint32(t9)
  1395. return f
  1396. }
  1397. // Inverse finds the modular multiplicative inverse of the field value in
  1398. // constant time. The existing field value is modified.
  1399. //
  1400. // The field value is returned to support chaining. This enables syntax like:
  1401. // f.Inverse().Mul(f2) so that f = f^-1 * f2.
  1402. //
  1403. // Preconditions:
  1404. // - The field value MUST have a max magnitude of 8
  1405. // Output Normalized: No
  1406. // Output Max Magnitude: 1
  1407. func (f *FieldVal) Inverse() *FieldVal {
  1408. // Fermat's little theorem states that for a nonzero number a and prime
  1409. // prime p, a^(p-1) = 1 (mod p). Since the multiplicative inverse is
  1410. // a*b = 1 (mod p), it follows that b = a*a^(p-2) = a^(p-1) = 1 (mod p).
  1411. // Thus, a^(p-2) is the multiplicative inverse.
  1412. //
  1413. // In order to efficiently compute a^(p-2), p-2 needs to be split into
  1414. // a sequence of squares and multiplications that minimizes the number
  1415. // of multiplications needed (since they are more costly than
  1416. // squarings). Intermediate results are saved and reused as well.
  1417. //
  1418. // The secp256k1 prime - 2 is 2^256 - 4294968275.
  1419. //
  1420. // This has a cost of 258 field squarings and 33 field multiplications.
  1421. var a2, a3, a4, a10, a11, a21, a42, a45, a63, a1019, a1023 FieldVal
  1422. a2.SquareVal(f)
  1423. a3.Mul2(&a2, f)
  1424. a4.SquareVal(&a2)
  1425. a10.SquareVal(&a4).Mul(&a2)
  1426. a11.Mul2(&a10, f)
  1427. a21.Mul2(&a10, &a11)
  1428. a42.SquareVal(&a21)
  1429. a45.Mul2(&a42, &a3)
  1430. a63.Mul2(&a42, &a21)
  1431. a1019.SquareVal(&a63).Square().Square().Square().Mul(&a11)
  1432. a1023.Mul2(&a1019, &a4)
  1433. f.Set(&a63) // f = a^(2^6 - 1)
  1434. f.Square().Square().Square().Square().Square() // f = a^(2^11 - 32)
  1435. f.Square().Square().Square().Square().Square() // f = a^(2^16 - 1024)
  1436. f.Mul(&a1023) // f = a^(2^16 - 1)
  1437. f.Square().Square().Square().Square().Square() // f = a^(2^21 - 32)
  1438. f.Square().Square().Square().Square().Square() // f = a^(2^26 - 1024)
  1439. f.Mul(&a1023) // f = a^(2^26 - 1)
  1440. f.Square().Square().Square().Square().Square() // f = a^(2^31 - 32)
  1441. f.Square().Square().Square().Square().Square() // f = a^(2^36 - 1024)
  1442. f.Mul(&a1023) // f = a^(2^36 - 1)
  1443. f.Square().Square().Square().Square().Square() // f = a^(2^41 - 32)
  1444. f.Square().Square().Square().Square().Square() // f = a^(2^46 - 1024)
  1445. f.Mul(&a1023) // f = a^(2^46 - 1)
  1446. f.Square().Square().Square().Square().Square() // f = a^(2^51 - 32)
  1447. f.Square().Square().Square().Square().Square() // f = a^(2^56 - 1024)
  1448. f.Mul(&a1023) // f = a^(2^56 - 1)
  1449. f.Square().Square().Square().Square().Square() // f = a^(2^61 - 32)
  1450. f.Square().Square().Square().Square().Square() // f = a^(2^66 - 1024)
  1451. f.Mul(&a1023) // f = a^(2^66 - 1)
  1452. f.Square().Square().Square().Square().Square() // f = a^(2^71 - 32)
  1453. f.Square().Square().Square().Square().Square() // f = a^(2^76 - 1024)
  1454. f.Mul(&a1023) // f = a^(2^76 - 1)
  1455. f.Square().Square().Square().Square().Square() // f = a^(2^81 - 32)
  1456. f.Square().Square().Square().Square().Square() // f = a^(2^86 - 1024)
  1457. f.Mul(&a1023) // f = a^(2^86 - 1)
  1458. f.Square().Square().Square().Square().Square() // f = a^(2^91 - 32)
  1459. f.Square().Square().Square().Square().Square() // f = a^(2^96 - 1024)
  1460. f.Mul(&a1023) // f = a^(2^96 - 1)
  1461. f.Square().Square().Square().Square().Square() // f = a^(2^101 - 32)
  1462. f.Square().Square().Square().Square().Square() // f = a^(2^106 - 1024)
  1463. f.Mul(&a1023) // f = a^(2^106 - 1)
  1464. f.Square().Square().Square().Square().Square() // f = a^(2^111 - 32)
  1465. f.Square().Square().Square().Square().Square() // f = a^(2^116 - 1024)
  1466. f.Mul(&a1023) // f = a^(2^116 - 1)
  1467. f.Square().Square().Square().Square().Square() // f = a^(2^121 - 32)
  1468. f.Square().Square().Square().Square().Square() // f = a^(2^126 - 1024)
  1469. f.Mul(&a1023) // f = a^(2^126 - 1)
  1470. f.Square().Square().Square().Square().Square() // f = a^(2^131 - 32)
  1471. f.Square().Square().Square().Square().Square() // f = a^(2^136 - 1024)
  1472. f.Mul(&a1023) // f = a^(2^136 - 1)
  1473. f.Square().Square().Square().Square().Square() // f = a^(2^141 - 32)
  1474. f.Square().Square().Square().Square().Square() // f = a^(2^146 - 1024)
  1475. f.Mul(&a1023) // f = a^(2^146 - 1)
  1476. f.Square().Square().Square().Square().Square() // f = a^(2^151 - 32)
  1477. f.Square().Square().Square().Square().Square() // f = a^(2^156 - 1024)
  1478. f.Mul(&a1023) // f = a^(2^156 - 1)
  1479. f.Square().Square().Square().Square().Square() // f = a^(2^161 - 32)
  1480. f.Square().Square().Square().Square().Square() // f = a^(2^166 - 1024)
  1481. f.Mul(&a1023) // f = a^(2^166 - 1)
  1482. f.Square().Square().Square().Square().Square() // f = a^(2^171 - 32)
  1483. f.Square().Square().Square().Square().Square() // f = a^(2^176 - 1024)
  1484. f.Mul(&a1023) // f = a^(2^176 - 1)
  1485. f.Square().Square().Square().Square().Square() // f = a^(2^181 - 32)
  1486. f.Square().Square().Square().Square().Square() // f = a^(2^186 - 1024)
  1487. f.Mul(&a1023) // f = a^(2^186 - 1)
  1488. f.Square().Square().Square().Square().Square() // f = a^(2^191 - 32)
  1489. f.Square().Square().Square().Square().Square() // f = a^(2^196 - 1024)
  1490. f.Mul(&a1023) // f = a^(2^196 - 1)
  1491. f.Square().Square().Square().Square().Square() // f = a^(2^201 - 32)
  1492. f.Square().Square().Square().Square().Square() // f = a^(2^206 - 1024)
  1493. f.Mul(&a1023) // f = a^(2^206 - 1)
  1494. f.Square().Square().Square().Square().Square() // f = a^(2^211 - 32)
  1495. f.Square().Square().Square().Square().Square() // f = a^(2^216 - 1024)
  1496. f.Mul(&a1023) // f = a^(2^216 - 1)
  1497. f.Square().Square().Square().Square().Square() // f = a^(2^221 - 32)
  1498. f.Square().Square().Square().Square().Square() // f = a^(2^226 - 1024)
  1499. f.Mul(&a1019) // f = a^(2^226 - 5)
  1500. f.Square().Square().Square().Square().Square() // f = a^(2^231 - 160)
  1501. f.Square().Square().Square().Square().Square() // f = a^(2^236 - 5120)
  1502. f.Mul(&a1023) // f = a^(2^236 - 4097)
  1503. f.Square().Square().Square().Square().Square() // f = a^(2^241 - 131104)
  1504. f.Square().Square().Square().Square().Square() // f = a^(2^246 - 4195328)
  1505. f.Mul(&a1023) // f = a^(2^246 - 4194305)
  1506. f.Square().Square().Square().Square().Square() // f = a^(2^251 - 134217760)
  1507. f.Square().Square().Square().Square().Square() // f = a^(2^256 - 4294968320)
  1508. return f.Mul(&a45) // f = a^(2^256 - 4294968275) = a^(p-2)
  1509. }
  1510. // IsGtOrEqPrimeMinusOrder returns whether or not the field value exceeds the
  1511. // group order divided by 2 in constant time.
  1512. //
  1513. // Preconditions:
  1514. // - The field value MUST be normalized
  1515. func (f *FieldVal) IsGtOrEqPrimeMinusOrder() bool {
  1516. // The secp256k1 prime is equivalent to 2^256 - 4294968273 and the group
  1517. // order is 2^256 - 432420386565659656852420866394968145599. Thus,
  1518. // the prime minus the group order is:
  1519. // 432420386565659656852420866390673177326
  1520. //
  1521. // In hex that is:
  1522. // 0x00000000 00000000 00000000 00000001 45512319 50b75fc4 402da172 2fc9baee
  1523. //
  1524. // Converting that to field representation (base 2^26) is:
  1525. //
  1526. // n[0] = 0x03c9baee
  1527. // n[1] = 0x03685c8b
  1528. // n[2] = 0x01fc4402
  1529. // n[3] = 0x006542dd
  1530. // n[4] = 0x01455123
  1531. //
  1532. // This can be verified with the following test code:
  1533. // pMinusN := new(big.Int).Sub(curveParams.P, curveParams.N)
  1534. // var fv FieldVal
  1535. // fv.SetByteSlice(pMinusN.Bytes())
  1536. // t.Logf("%x", fv.n)
  1537. //
  1538. // Outputs: [3c9baee 3685c8b 1fc4402 6542dd 1455123 0 0 0 0 0]
  1539. const (
  1540. pMinusNWordZero = 0x03c9baee
  1541. pMinusNWordOne = 0x03685c8b
  1542. pMinusNWordTwo = 0x01fc4402
  1543. pMinusNWordThree = 0x006542dd
  1544. pMinusNWordFour = 0x01455123
  1545. pMinusNWordFive = 0x00000000
  1546. pMinusNWordSix = 0x00000000
  1547. pMinusNWordSeven = 0x00000000
  1548. pMinusNWordEight = 0x00000000
  1549. pMinusNWordNine = 0x00000000
  1550. )
  1551. // The intuition here is that the value is greater than field prime minus
  1552. // the group order if one of the higher individual words is greater than the
  1553. // corresponding word and all higher words in the value are equal.
  1554. result := constantTimeGreater(f.n[9], pMinusNWordNine)
  1555. highWordsEqual := constantTimeEq(f.n[9], pMinusNWordNine)
  1556. result |= highWordsEqual & constantTimeGreater(f.n[8], pMinusNWordEight)
  1557. highWordsEqual &= constantTimeEq(f.n[8], pMinusNWordEight)
  1558. result |= highWordsEqual & constantTimeGreater(f.n[7], pMinusNWordSeven)
  1559. highWordsEqual &= constantTimeEq(f.n[7], pMinusNWordSeven)
  1560. result |= highWordsEqual & constantTimeGreater(f.n[6], pMinusNWordSix)
  1561. highWordsEqual &= constantTimeEq(f.n[6], pMinusNWordSix)
  1562. result |= highWordsEqual & constantTimeGreater(f.n[5], pMinusNWordFive)
  1563. highWordsEqual &= constantTimeEq(f.n[5], pMinusNWordFive)
  1564. result |= highWordsEqual & constantTimeGreater(f.n[4], pMinusNWordFour)
  1565. highWordsEqual &= constantTimeEq(f.n[4], pMinusNWordFour)
  1566. result |= highWordsEqual & constantTimeGreater(f.n[3], pMinusNWordThree)
  1567. highWordsEqual &= constantTimeEq(f.n[3], pMinusNWordThree)
  1568. result |= highWordsEqual & constantTimeGreater(f.n[2], pMinusNWordTwo)
  1569. highWordsEqual &= constantTimeEq(f.n[2], pMinusNWordTwo)
  1570. result |= highWordsEqual & constantTimeGreater(f.n[1], pMinusNWordOne)
  1571. highWordsEqual &= constantTimeEq(f.n[1], pMinusNWordOne)
  1572. result |= highWordsEqual & constantTimeGreaterOrEq(f.n[0], pMinusNWordZero)
  1573. return result != 0
  1574. }