fe.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // Copyright (c) 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package field implements fast arithmetic modulo 2^255-19.
  5. package field
  6. import (
  7. "crypto/subtle"
  8. "encoding/binary"
  9. "math/bits"
  10. )
  11. // Element represents an element of the field GF(2^255-19). Note that this
  12. // is not a cryptographically secure group, and should only be used to interact
  13. // with edwards25519.Point coordinates.
  14. //
  15. // This type works similarly to math/big.Int, and all arguments and receivers
  16. // are allowed to alias.
  17. //
  18. // The zero value is a valid zero element.
  19. type Element struct {
  20. // An element t represents the integer
  21. // t.l0 + t.l1*2^51 + t.l2*2^102 + t.l3*2^153 + t.l4*2^204
  22. //
  23. // Between operations, all limbs are expected to be lower than 2^52.
  24. l0 uint64
  25. l1 uint64
  26. l2 uint64
  27. l3 uint64
  28. l4 uint64
  29. }
  30. const maskLow51Bits uint64 = (1 << 51) - 1
  31. var feZero = &Element{0, 0, 0, 0, 0}
  32. // Zero sets v = 0, and returns v.
  33. func (v *Element) Zero() *Element {
  34. *v = *feZero
  35. return v
  36. }
  37. var feOne = &Element{1, 0, 0, 0, 0}
  38. // One sets v = 1, and returns v.
  39. func (v *Element) One() *Element {
  40. *v = *feOne
  41. return v
  42. }
  43. // reduce reduces v modulo 2^255 - 19 and returns it.
  44. func (v *Element) reduce() *Element {
  45. v.carryPropagate()
  46. // After the light reduction we now have a field element representation
  47. // v < 2^255 + 2^13 * 19, but need v < 2^255 - 19.
  48. // If v >= 2^255 - 19, then v + 19 >= 2^255, which would overflow 2^255 - 1,
  49. // generating a carry. That is, c will be 0 if v < 2^255 - 19, and 1 otherwise.
  50. c := (v.l0 + 19) >> 51
  51. c = (v.l1 + c) >> 51
  52. c = (v.l2 + c) >> 51
  53. c = (v.l3 + c) >> 51
  54. c = (v.l4 + c) >> 51
  55. // If v < 2^255 - 19 and c = 0, this will be a no-op. Otherwise, it's
  56. // effectively applying the reduction identity to the carry.
  57. v.l0 += 19 * c
  58. v.l1 += v.l0 >> 51
  59. v.l0 = v.l0 & maskLow51Bits
  60. v.l2 += v.l1 >> 51
  61. v.l1 = v.l1 & maskLow51Bits
  62. v.l3 += v.l2 >> 51
  63. v.l2 = v.l2 & maskLow51Bits
  64. v.l4 += v.l3 >> 51
  65. v.l3 = v.l3 & maskLow51Bits
  66. // no additional carry
  67. v.l4 = v.l4 & maskLow51Bits
  68. return v
  69. }
  70. // Add sets v = a + b, and returns v.
  71. func (v *Element) Add(a, b *Element) *Element {
  72. v.l0 = a.l0 + b.l0
  73. v.l1 = a.l1 + b.l1
  74. v.l2 = a.l2 + b.l2
  75. v.l3 = a.l3 + b.l3
  76. v.l4 = a.l4 + b.l4
  77. // Using the generic implementation here is actually faster than the
  78. // assembly. Probably because the body of this function is so simple that
  79. // the compiler can figure out better optimizations by inlining the carry
  80. // propagation.
  81. return v.carryPropagateGeneric()
  82. }
  83. // Subtract sets v = a - b, and returns v.
  84. func (v *Element) Subtract(a, b *Element) *Element {
  85. // We first add 2 * p, to guarantee the subtraction won't underflow, and
  86. // then subtract b (which can be up to 2^255 + 2^13 * 19).
  87. v.l0 = (a.l0 + 0xFFFFFFFFFFFDA) - b.l0
  88. v.l1 = (a.l1 + 0xFFFFFFFFFFFFE) - b.l1
  89. v.l2 = (a.l2 + 0xFFFFFFFFFFFFE) - b.l2
  90. v.l3 = (a.l3 + 0xFFFFFFFFFFFFE) - b.l3
  91. v.l4 = (a.l4 + 0xFFFFFFFFFFFFE) - b.l4
  92. return v.carryPropagate()
  93. }
  94. // Negate sets v = -a, and returns v.
  95. func (v *Element) Negate(a *Element) *Element {
  96. return v.Subtract(feZero, a)
  97. }
  98. // Invert sets v = 1/z mod p, and returns v.
  99. //
  100. // If z == 0, Invert returns v = 0.
  101. func (v *Element) Invert(z *Element) *Element {
  102. // Inversion is implemented as exponentiation with exponent p − 2. It uses the
  103. // same sequence of 255 squarings and 11 multiplications as [Curve25519].
  104. var z2, z9, z11, z2_5_0, z2_10_0, z2_20_0, z2_50_0, z2_100_0, t Element
  105. z2.Square(z) // 2
  106. t.Square(&z2) // 4
  107. t.Square(&t) // 8
  108. z9.Multiply(&t, z) // 9
  109. z11.Multiply(&z9, &z2) // 11
  110. t.Square(&z11) // 22
  111. z2_5_0.Multiply(&t, &z9) // 31 = 2^5 - 2^0
  112. t.Square(&z2_5_0) // 2^6 - 2^1
  113. for i := 0; i < 4; i++ {
  114. t.Square(&t) // 2^10 - 2^5
  115. }
  116. z2_10_0.Multiply(&t, &z2_5_0) // 2^10 - 2^0
  117. t.Square(&z2_10_0) // 2^11 - 2^1
  118. for i := 0; i < 9; i++ {
  119. t.Square(&t) // 2^20 - 2^10
  120. }
  121. z2_20_0.Multiply(&t, &z2_10_0) // 2^20 - 2^0
  122. t.Square(&z2_20_0) // 2^21 - 2^1
  123. for i := 0; i < 19; i++ {
  124. t.Square(&t) // 2^40 - 2^20
  125. }
  126. t.Multiply(&t, &z2_20_0) // 2^40 - 2^0
  127. t.Square(&t) // 2^41 - 2^1
  128. for i := 0; i < 9; i++ {
  129. t.Square(&t) // 2^50 - 2^10
  130. }
  131. z2_50_0.Multiply(&t, &z2_10_0) // 2^50 - 2^0
  132. t.Square(&z2_50_0) // 2^51 - 2^1
  133. for i := 0; i < 49; i++ {
  134. t.Square(&t) // 2^100 - 2^50
  135. }
  136. z2_100_0.Multiply(&t, &z2_50_0) // 2^100 - 2^0
  137. t.Square(&z2_100_0) // 2^101 - 2^1
  138. for i := 0; i < 99; i++ {
  139. t.Square(&t) // 2^200 - 2^100
  140. }
  141. t.Multiply(&t, &z2_100_0) // 2^200 - 2^0
  142. t.Square(&t) // 2^201 - 2^1
  143. for i := 0; i < 49; i++ {
  144. t.Square(&t) // 2^250 - 2^50
  145. }
  146. t.Multiply(&t, &z2_50_0) // 2^250 - 2^0
  147. t.Square(&t) // 2^251 - 2^1
  148. t.Square(&t) // 2^252 - 2^2
  149. t.Square(&t) // 2^253 - 2^3
  150. t.Square(&t) // 2^254 - 2^4
  151. t.Square(&t) // 2^255 - 2^5
  152. return v.Multiply(&t, &z11) // 2^255 - 21
  153. }
  154. // Set sets v = a, and returns v.
  155. func (v *Element) Set(a *Element) *Element {
  156. *v = *a
  157. return v
  158. }
  159. // SetBytes sets v to x, which must be a 32-byte little-endian encoding.
  160. //
  161. // Consistent with RFC 7748, the most significant bit (the high bit of the
  162. // last byte) is ignored, and non-canonical values (2^255-19 through 2^255-1)
  163. // are accepted. Note that this is laxer than specified by RFC 8032.
  164. func (v *Element) SetBytes(x []byte) *Element {
  165. if len(x) != 32 {
  166. panic("edwards25519: invalid field element input size")
  167. }
  168. // Bits 0:51 (bytes 0:8, bits 0:64, shift 0, mask 51).
  169. v.l0 = binary.LittleEndian.Uint64(x[0:8])
  170. v.l0 &= maskLow51Bits
  171. // Bits 51:102 (bytes 6:14, bits 48:112, shift 3, mask 51).
  172. v.l1 = binary.LittleEndian.Uint64(x[6:14]) >> 3
  173. v.l1 &= maskLow51Bits
  174. // Bits 102:153 (bytes 12:20, bits 96:160, shift 6, mask 51).
  175. v.l2 = binary.LittleEndian.Uint64(x[12:20]) >> 6
  176. v.l2 &= maskLow51Bits
  177. // Bits 153:204 (bytes 19:27, bits 152:216, shift 1, mask 51).
  178. v.l3 = binary.LittleEndian.Uint64(x[19:27]) >> 1
  179. v.l3 &= maskLow51Bits
  180. // Bits 204:251 (bytes 24:32, bits 192:256, shift 12, mask 51).
  181. // Note: not bytes 25:33, shift 4, to avoid overread.
  182. v.l4 = binary.LittleEndian.Uint64(x[24:32]) >> 12
  183. v.l4 &= maskLow51Bits
  184. return v
  185. }
  186. // Bytes returns the canonical 32-byte little-endian encoding of v.
  187. func (v *Element) Bytes() []byte {
  188. // This function is outlined to make the allocations inline in the caller
  189. // rather than happen on the heap.
  190. var out [32]byte
  191. return v.bytes(&out)
  192. }
  193. func (v *Element) bytes(out *[32]byte) []byte {
  194. t := *v
  195. t.reduce()
  196. var buf [8]byte
  197. for i, l := range [5]uint64{t.l0, t.l1, t.l2, t.l3, t.l4} {
  198. bitsOffset := i * 51
  199. binary.LittleEndian.PutUint64(buf[:], l<<uint(bitsOffset%8))
  200. for i, bb := range buf {
  201. off := bitsOffset/8 + i
  202. if off >= len(out) {
  203. break
  204. }
  205. out[off] |= bb
  206. }
  207. }
  208. return out[:]
  209. }
  210. // Equal returns 1 if v and u are equal, and 0 otherwise.
  211. func (v *Element) Equal(u *Element) int {
  212. sa, sv := u.Bytes(), v.Bytes()
  213. return subtle.ConstantTimeCompare(sa, sv)
  214. }
  215. // mask64Bits returns 0xffffffff if cond is 1, and 0 otherwise.
  216. func mask64Bits(cond int) uint64 { return ^(uint64(cond) - 1) }
  217. // Select sets v to a if cond == 1, and to b if cond == 0.
  218. func (v *Element) Select(a, b *Element, cond int) *Element {
  219. m := mask64Bits(cond)
  220. v.l0 = (m & a.l0) | (^m & b.l0)
  221. v.l1 = (m & a.l1) | (^m & b.l1)
  222. v.l2 = (m & a.l2) | (^m & b.l2)
  223. v.l3 = (m & a.l3) | (^m & b.l3)
  224. v.l4 = (m & a.l4) | (^m & b.l4)
  225. return v
  226. }
  227. // Swap swaps v and u if cond == 1 or leaves them unchanged if cond == 0, and returns v.
  228. func (v *Element) Swap(u *Element, cond int) {
  229. m := mask64Bits(cond)
  230. t := m & (v.l0 ^ u.l0)
  231. v.l0 ^= t
  232. u.l0 ^= t
  233. t = m & (v.l1 ^ u.l1)
  234. v.l1 ^= t
  235. u.l1 ^= t
  236. t = m & (v.l2 ^ u.l2)
  237. v.l2 ^= t
  238. u.l2 ^= t
  239. t = m & (v.l3 ^ u.l3)
  240. v.l3 ^= t
  241. u.l3 ^= t
  242. t = m & (v.l4 ^ u.l4)
  243. v.l4 ^= t
  244. u.l4 ^= t
  245. }
  246. // IsNegative returns 1 if v is negative, and 0 otherwise.
  247. func (v *Element) IsNegative() int {
  248. return int(v.Bytes()[0] & 1)
  249. }
  250. // Absolute sets v to |u|, and returns v.
  251. func (v *Element) Absolute(u *Element) *Element {
  252. return v.Select(new(Element).Negate(u), u, u.IsNegative())
  253. }
  254. // Multiply sets v = x * y, and returns v.
  255. func (v *Element) Multiply(x, y *Element) *Element {
  256. feMul(v, x, y)
  257. return v
  258. }
  259. // Square sets v = x * x, and returns v.
  260. func (v *Element) Square(x *Element) *Element {
  261. feSquare(v, x)
  262. return v
  263. }
  264. // Mult32 sets v = x * y, and returns v.
  265. func (v *Element) Mult32(x *Element, y uint32) *Element {
  266. x0lo, x0hi := mul51(x.l0, y)
  267. x1lo, x1hi := mul51(x.l1, y)
  268. x2lo, x2hi := mul51(x.l2, y)
  269. x3lo, x3hi := mul51(x.l3, y)
  270. x4lo, x4hi := mul51(x.l4, y)
  271. v.l0 = x0lo + 19*x4hi // carried over per the reduction identity
  272. v.l1 = x1lo + x0hi
  273. v.l2 = x2lo + x1hi
  274. v.l3 = x3lo + x2hi
  275. v.l4 = x4lo + x3hi
  276. // The hi portions are going to be only 32 bits, plus any previous excess,
  277. // so we can skip the carry propagation.
  278. return v
  279. }
  280. // mul51 returns lo + hi * 2⁵¹ = a * b.
  281. func mul51(a uint64, b uint32) (lo uint64, hi uint64) {
  282. mh, ml := bits.Mul64(a, uint64(b))
  283. lo = ml & maskLow51Bits
  284. hi = (mh << 13) | (ml >> 51)
  285. return
  286. }
  287. // Pow22523 set v = x^((p-5)/8), and returns v. (p-5)/8 is 2^252-3.
  288. func (v *Element) Pow22523(x *Element) *Element {
  289. var t0, t1, t2 Element
  290. t0.Square(x) // x^2
  291. t1.Square(&t0) // x^4
  292. t1.Square(&t1) // x^8
  293. t1.Multiply(x, &t1) // x^9
  294. t0.Multiply(&t0, &t1) // x^11
  295. t0.Square(&t0) // x^22
  296. t0.Multiply(&t1, &t0) // x^31
  297. t1.Square(&t0) // x^62
  298. for i := 1; i < 5; i++ { // x^992
  299. t1.Square(&t1)
  300. }
  301. t0.Multiply(&t1, &t0) // x^1023 -> 1023 = 2^10 - 1
  302. t1.Square(&t0) // 2^11 - 2
  303. for i := 1; i < 10; i++ { // 2^20 - 2^10
  304. t1.Square(&t1)
  305. }
  306. t1.Multiply(&t1, &t0) // 2^20 - 1
  307. t2.Square(&t1) // 2^21 - 2
  308. for i := 1; i < 20; i++ { // 2^40 - 2^20
  309. t2.Square(&t2)
  310. }
  311. t1.Multiply(&t2, &t1) // 2^40 - 1
  312. t1.Square(&t1) // 2^41 - 2
  313. for i := 1; i < 10; i++ { // 2^50 - 2^10
  314. t1.Square(&t1)
  315. }
  316. t0.Multiply(&t1, &t0) // 2^50 - 1
  317. t1.Square(&t0) // 2^51 - 2
  318. for i := 1; i < 50; i++ { // 2^100 - 2^50
  319. t1.Square(&t1)
  320. }
  321. t1.Multiply(&t1, &t0) // 2^100 - 1
  322. t2.Square(&t1) // 2^101 - 2
  323. for i := 1; i < 100; i++ { // 2^200 - 2^100
  324. t2.Square(&t2)
  325. }
  326. t1.Multiply(&t2, &t1) // 2^200 - 1
  327. t1.Square(&t1) // 2^201 - 2
  328. for i := 1; i < 50; i++ { // 2^250 - 2^50
  329. t1.Square(&t1)
  330. }
  331. t0.Multiply(&t1, &t0) // 2^250 - 1
  332. t0.Square(&t0) // 2^251 - 2
  333. t0.Square(&t0) // 2^252 - 4
  334. return v.Multiply(&t0, x) // 2^252 - 3 -> x^(2^252-3)
  335. }
  336. // sqrtM1 is 2^((p-1)/4), which squared is equal to -1 by Euler's Criterion.
  337. var sqrtM1 = &Element{1718705420411056, 234908883556509,
  338. 2233514472574048, 2117202627021982, 765476049583133}
  339. // SqrtRatio sets r to the non-negative square root of the ratio of u and v.
  340. //
  341. // If u/v is square, SqrtRatio returns r and 1. If u/v is not square, SqrtRatio
  342. // sets r according to Section 4.3 of draft-irtf-cfrg-ristretto255-decaf448-00,
  343. // and returns r and 0.
  344. func (r *Element) SqrtRatio(u, v *Element) (rr *Element, wasSquare int) {
  345. var a, b Element
  346. // r = (u * v3) * (u * v7)^((p-5)/8)
  347. v2 := a.Square(v)
  348. uv3 := b.Multiply(u, b.Multiply(v2, v))
  349. uv7 := a.Multiply(uv3, a.Square(v2))
  350. r.Multiply(uv3, r.Pow22523(uv7))
  351. check := a.Multiply(v, a.Square(r)) // check = v * r^2
  352. uNeg := b.Negate(u)
  353. correctSignSqrt := check.Equal(u)
  354. flippedSignSqrt := check.Equal(uNeg)
  355. flippedSignSqrtI := check.Equal(uNeg.Multiply(uNeg, sqrtM1))
  356. rPrime := b.Multiply(r, sqrtM1) // r_prime = SQRT_M1 * r
  357. // r = CT_SELECT(r_prime IF flipped_sign_sqrt | flipped_sign_sqrt_i ELSE r)
  358. r.Select(rPrime, r, flippedSignSqrt|flippedSignSqrtI)
  359. r.Absolute(r) // Choose the nonnegative square root.
  360. return r, correctSignSqrt | flippedSignSqrt
  361. }