fe.go 12 KB

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