You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

754 lines
19 KiB

  1. // Copyright 2020 ConsenSys AG
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Code generated by goff (v0.2.0) DO NOT EDIT
  15. // Package ff contains field arithmetic operations
  16. package ff
  17. // /!\ WARNING /!\
  18. // this code has not been audited and is provided as-is. In particular,
  19. // there is no security guarantees such as constant time implementation
  20. // or side-channel attack resistance
  21. // /!\ WARNING /!\
  22. import (
  23. "crypto/rand"
  24. "encoding/binary"
  25. "io"
  26. "math/big"
  27. "math/bits"
  28. "strconv"
  29. "sync"
  30. "unsafe"
  31. )
  32. // Element represents a field element stored on 4 words (uint64)
  33. // Element are assumed to be in Montgomery form in all methods
  34. // field modulus q =
  35. //
  36. // 21888242871839275222246405745257275088548364400416034343698204186575808495617
  37. type Element [4]uint64
  38. // ElementLimbs number of 64 bits words needed to represent Element
  39. const ElementLimbs = 4
  40. // ElementBits number bits needed to represent Element
  41. const ElementBits = 254
  42. // SetUint64 z = v, sets z LSB to v (non-Montgomery form) and convert z to Montgomery form
  43. func (z *Element) SetUint64(v uint64) *Element {
  44. z[0] = v
  45. z[1] = 0
  46. z[2] = 0
  47. z[3] = 0
  48. return z.ToMont()
  49. }
  50. // Set z = x
  51. func (z *Element) Set(x *Element) *Element {
  52. z[0] = x[0]
  53. z[1] = x[1]
  54. z[2] = x[2]
  55. z[3] = x[3]
  56. return z
  57. }
  58. // SetZero z = 0
  59. func (z *Element) SetZero() *Element {
  60. z[0] = 0
  61. z[1] = 0
  62. z[2] = 0
  63. z[3] = 0
  64. return z
  65. }
  66. // SetOne z = 1 (in Montgomery form)
  67. func (z *Element) SetOne() *Element {
  68. z[0] = 12436184717236109307
  69. z[1] = 3962172157175319849
  70. z[2] = 7381016538464732718
  71. z[3] = 1011752739694698287
  72. return z
  73. }
  74. // Neg z = q - x
  75. func (z *Element) Neg(x *Element) *Element {
  76. if x.IsZero() {
  77. return z.SetZero()
  78. }
  79. var borrow uint64
  80. z[0], borrow = bits.Sub64(4891460686036598785, x[0], 0)
  81. z[1], borrow = bits.Sub64(2896914383306846353, x[1], borrow)
  82. z[2], borrow = bits.Sub64(13281191951274694749, x[2], borrow)
  83. z[3], _ = bits.Sub64(3486998266802970665, x[3], borrow)
  84. return z
  85. }
  86. // Div z = x*y^-1 mod q
  87. func (z *Element) Div(x, y *Element) *Element {
  88. var yInv Element
  89. yInv.Inverse(y)
  90. z.Mul(x, &yInv)
  91. return z
  92. }
  93. // Equal returns z == x
  94. func (z *Element) Equal(x *Element) bool {
  95. return (z[3] == x[3]) && (z[2] == x[2]) && (z[1] == x[1]) && (z[0] == x[0])
  96. }
  97. // IsZero returns z == 0
  98. func (z *Element) IsZero() bool {
  99. return (z[3] | z[2] | z[1] | z[0]) == 0
  100. }
  101. // field modulus stored as big.Int
  102. var _elementModulusBigInt big.Int
  103. var onceelementModulus sync.Once
  104. func elementModulusBigInt() *big.Int {
  105. onceelementModulus.Do(func() {
  106. _elementModulusBigInt.SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  107. })
  108. return &_elementModulusBigInt
  109. }
  110. // Inverse z = x^-1 mod q
  111. // Algorithm 16 in "Efficient Software-Implementation of Finite Fields with Applications to Cryptography"
  112. // if x == 0, sets and returns z = x
  113. func (z *Element) Inverse(x *Element) *Element {
  114. if x.IsZero() {
  115. return z.Set(x)
  116. }
  117. // initialize u = q
  118. var u = Element{
  119. 4891460686036598785,
  120. 2896914383306846353,
  121. 13281191951274694749,
  122. 3486998266802970665,
  123. }
  124. // initialize s = r^2
  125. var s = Element{
  126. 1997599621687373223,
  127. 6052339484930628067,
  128. 10108755138030829701,
  129. 150537098327114917,
  130. }
  131. // r = 0
  132. r := Element{}
  133. v := *x
  134. var carry, borrow, t, t2 uint64
  135. var bigger, uIsOne, vIsOne bool
  136. for !uIsOne && !vIsOne {
  137. for v[0]&1 == 0 {
  138. // v = v >> 1
  139. t2 = v[3] << 63
  140. v[3] >>= 1
  141. t = t2
  142. t2 = v[2] << 63
  143. v[2] = (v[2] >> 1) | t
  144. t = t2
  145. t2 = v[1] << 63
  146. v[1] = (v[1] >> 1) | t
  147. t = t2
  148. v[0] = (v[0] >> 1) | t
  149. if s[0]&1 == 1 {
  150. // s = s + q
  151. s[0], carry = bits.Add64(s[0], 4891460686036598785, 0)
  152. s[1], carry = bits.Add64(s[1], 2896914383306846353, carry)
  153. s[2], carry = bits.Add64(s[2], 13281191951274694749, carry)
  154. s[3], _ = bits.Add64(s[3], 3486998266802970665, carry)
  155. }
  156. // s = s >> 1
  157. t2 = s[3] << 63
  158. s[3] >>= 1
  159. t = t2
  160. t2 = s[2] << 63
  161. s[2] = (s[2] >> 1) | t
  162. t = t2
  163. t2 = s[1] << 63
  164. s[1] = (s[1] >> 1) | t
  165. t = t2
  166. s[0] = (s[0] >> 1) | t
  167. }
  168. for u[0]&1 == 0 {
  169. // u = u >> 1
  170. t2 = u[3] << 63
  171. u[3] >>= 1
  172. t = t2
  173. t2 = u[2] << 63
  174. u[2] = (u[2] >> 1) | t
  175. t = t2
  176. t2 = u[1] << 63
  177. u[1] = (u[1] >> 1) | t
  178. t = t2
  179. u[0] = (u[0] >> 1) | t
  180. if r[0]&1 == 1 {
  181. // r = r + q
  182. r[0], carry = bits.Add64(r[0], 4891460686036598785, 0)
  183. r[1], carry = bits.Add64(r[1], 2896914383306846353, carry)
  184. r[2], carry = bits.Add64(r[2], 13281191951274694749, carry)
  185. r[3], _ = bits.Add64(r[3], 3486998266802970665, carry)
  186. }
  187. // r = r >> 1
  188. t2 = r[3] << 63
  189. r[3] >>= 1
  190. t = t2
  191. t2 = r[2] << 63
  192. r[2] = (r[2] >> 1) | t
  193. t = t2
  194. t2 = r[1] << 63
  195. r[1] = (r[1] >> 1) | t
  196. t = t2
  197. r[0] = (r[0] >> 1) | t
  198. }
  199. // v >= u
  200. bigger = !(v[3] < u[3] || (v[3] == u[3] && (v[2] < u[2] || (v[2] == u[2] && (v[1] < u[1] || (v[1] == u[1] && (v[0] < u[0])))))))
  201. if bigger {
  202. // v = v - u
  203. v[0], borrow = bits.Sub64(v[0], u[0], 0)
  204. v[1], borrow = bits.Sub64(v[1], u[1], borrow)
  205. v[2], borrow = bits.Sub64(v[2], u[2], borrow)
  206. v[3], _ = bits.Sub64(v[3], u[3], borrow)
  207. // r >= s
  208. bigger = !(r[3] < s[3] || (r[3] == s[3] && (r[2] < s[2] || (r[2] == s[2] && (r[1] < s[1] || (r[1] == s[1] && (r[0] < s[0])))))))
  209. if bigger {
  210. // s = s + q
  211. s[0], carry = bits.Add64(s[0], 4891460686036598785, 0)
  212. s[1], carry = bits.Add64(s[1], 2896914383306846353, carry)
  213. s[2], carry = bits.Add64(s[2], 13281191951274694749, carry)
  214. s[3], _ = bits.Add64(s[3], 3486998266802970665, carry)
  215. }
  216. // s = s - r
  217. s[0], borrow = bits.Sub64(s[0], r[0], 0)
  218. s[1], borrow = bits.Sub64(s[1], r[1], borrow)
  219. s[2], borrow = bits.Sub64(s[2], r[2], borrow)
  220. s[3], _ = bits.Sub64(s[3], r[3], borrow)
  221. } else {
  222. // u = u - v
  223. u[0], borrow = bits.Sub64(u[0], v[0], 0)
  224. u[1], borrow = bits.Sub64(u[1], v[1], borrow)
  225. u[2], borrow = bits.Sub64(u[2], v[2], borrow)
  226. u[3], _ = bits.Sub64(u[3], v[3], borrow)
  227. // s >= r
  228. bigger = !(s[3] < r[3] || (s[3] == r[3] && (s[2] < r[2] || (s[2] == r[2] && (s[1] < r[1] || (s[1] == r[1] && (s[0] < r[0])))))))
  229. if bigger {
  230. // r = r + q
  231. r[0], carry = bits.Add64(r[0], 4891460686036598785, 0)
  232. r[1], carry = bits.Add64(r[1], 2896914383306846353, carry)
  233. r[2], carry = bits.Add64(r[2], 13281191951274694749, carry)
  234. r[3], _ = bits.Add64(r[3], 3486998266802970665, carry)
  235. }
  236. // r = r - s
  237. r[0], borrow = bits.Sub64(r[0], s[0], 0)
  238. r[1], borrow = bits.Sub64(r[1], s[1], borrow)
  239. r[2], borrow = bits.Sub64(r[2], s[2], borrow)
  240. r[3], _ = bits.Sub64(r[3], s[3], borrow)
  241. }
  242. uIsOne = (u[0] == 1) && (u[3]|u[2]|u[1]) == 0
  243. vIsOne = (v[0] == 1) && (v[3]|v[2]|v[1]) == 0
  244. }
  245. if uIsOne {
  246. z.Set(&r)
  247. } else {
  248. z.Set(&s)
  249. }
  250. return z
  251. }
  252. // SetRandom sets z to a random element < q
  253. func (z *Element) SetRandom() *Element {
  254. bytes := make([]byte, 32)
  255. io.ReadFull(rand.Reader, bytes)
  256. z[0] = binary.BigEndian.Uint64(bytes[0:8])
  257. z[1] = binary.BigEndian.Uint64(bytes[8:16])
  258. z[2] = binary.BigEndian.Uint64(bytes[16:24])
  259. z[3] = binary.BigEndian.Uint64(bytes[24:32])
  260. z[3] %= 3486998266802970665
  261. // if z > q --> z -= q
  262. // note: this is NOT constant time
  263. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  264. var b uint64
  265. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  266. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  267. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  268. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  269. }
  270. return z
  271. }
  272. // One returns 1 (in montgommery form)
  273. func One() Element {
  274. var one Element
  275. one.SetOne()
  276. return one
  277. }
  278. // FromInterface converts i1 from uint64, int, string, or Element, big.Int into Element
  279. // panic if provided type is not supported
  280. func FromInterface(i1 interface{}) Element {
  281. var val Element
  282. switch c1 := i1.(type) {
  283. case uint64:
  284. val.SetUint64(c1)
  285. case int:
  286. val.SetString(strconv.Itoa(c1))
  287. case string:
  288. val.SetString(c1)
  289. case big.Int:
  290. val.SetBigInt(&c1)
  291. case Element:
  292. val = c1
  293. case *Element:
  294. val.Set(c1)
  295. default:
  296. panic("invalid type")
  297. }
  298. return val
  299. }
  300. // Add z = x + y mod q
  301. func (z *Element) Add(x, y *Element) *Element {
  302. var carry uint64
  303. z[0], carry = bits.Add64(x[0], y[0], 0)
  304. z[1], carry = bits.Add64(x[1], y[1], carry)
  305. z[2], carry = bits.Add64(x[2], y[2], carry)
  306. z[3], _ = bits.Add64(x[3], y[3], carry)
  307. // if z > q --> z -= q
  308. // note: this is NOT constant time
  309. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  310. var b uint64
  311. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  312. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  313. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  314. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  315. }
  316. return z
  317. }
  318. // AddAssign z = z + x mod q
  319. func (z *Element) AddAssign(x *Element) *Element {
  320. var carry uint64
  321. z[0], carry = bits.Add64(z[0], x[0], 0)
  322. z[1], carry = bits.Add64(z[1], x[1], carry)
  323. z[2], carry = bits.Add64(z[2], x[2], carry)
  324. z[3], _ = bits.Add64(z[3], x[3], carry)
  325. // if z > q --> z -= q
  326. // note: this is NOT constant time
  327. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  328. var b uint64
  329. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  330. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  331. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  332. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  333. }
  334. return z
  335. }
  336. // Double z = x + x mod q, aka Lsh 1
  337. func (z *Element) Double(x *Element) *Element {
  338. var carry uint64
  339. z[0], carry = bits.Add64(x[0], x[0], 0)
  340. z[1], carry = bits.Add64(x[1], x[1], carry)
  341. z[2], carry = bits.Add64(x[2], x[2], carry)
  342. z[3], _ = bits.Add64(x[3], x[3], carry)
  343. // if z > q --> z -= q
  344. // note: this is NOT constant time
  345. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  346. var b uint64
  347. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  348. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  349. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  350. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  351. }
  352. return z
  353. }
  354. // Sub z = x - y mod q
  355. func (z *Element) Sub(x, y *Element) *Element {
  356. var b uint64
  357. z[0], b = bits.Sub64(x[0], y[0], 0)
  358. z[1], b = bits.Sub64(x[1], y[1], b)
  359. z[2], b = bits.Sub64(x[2], y[2], b)
  360. z[3], b = bits.Sub64(x[3], y[3], b)
  361. if b != 0 {
  362. var c uint64
  363. z[0], c = bits.Add64(z[0], 4891460686036598785, 0)
  364. z[1], c = bits.Add64(z[1], 2896914383306846353, c)
  365. z[2], c = bits.Add64(z[2], 13281191951274694749, c)
  366. z[3], _ = bits.Add64(z[3], 3486998266802970665, c)
  367. }
  368. return z
  369. }
  370. // SubAssign z = z - x mod q
  371. func (z *Element) SubAssign(x *Element) *Element {
  372. var b uint64
  373. z[0], b = bits.Sub64(z[0], x[0], 0)
  374. z[1], b = bits.Sub64(z[1], x[1], b)
  375. z[2], b = bits.Sub64(z[2], x[2], b)
  376. z[3], b = bits.Sub64(z[3], x[3], b)
  377. if b != 0 {
  378. var c uint64
  379. z[0], c = bits.Add64(z[0], 4891460686036598785, 0)
  380. z[1], c = bits.Add64(z[1], 2896914383306846353, c)
  381. z[2], c = bits.Add64(z[2], 13281191951274694749, c)
  382. z[3], _ = bits.Add64(z[3], 3486998266802970665, c)
  383. }
  384. return z
  385. }
  386. // Exp z = x^exponent mod q
  387. // (not optimized)
  388. // exponent (non-montgomery form) is ordered from least significant word to most significant word
  389. func (z *Element) Exp(x Element, exponent ...uint64) *Element {
  390. r := 0
  391. msb := 0
  392. for i := len(exponent) - 1; i >= 0; i-- {
  393. if exponent[i] == 0 {
  394. r++
  395. } else {
  396. msb = (i * 64) + bits.Len64(exponent[i])
  397. break
  398. }
  399. }
  400. exponent = exponent[:len(exponent)-r]
  401. if len(exponent) == 0 {
  402. return z.SetOne()
  403. }
  404. z.Set(&x)
  405. l := msb - 2
  406. for i := l; i >= 0; i-- {
  407. z.Square(z)
  408. if exponent[i/64]&(1<<uint(i%64)) != 0 {
  409. z.MulAssign(&x)
  410. }
  411. }
  412. return z
  413. }
  414. // FromMont converts z in place (i.e. mutates) from Montgomery to regular representation
  415. // sets and returns z = z * 1
  416. func (z *Element) FromMont() *Element {
  417. // the following lines implement z = z * 1
  418. // with a modified CIOS montgomery multiplication
  419. {
  420. // m = z[0]n'[0] mod W
  421. m := z[0] * 14042775128853446655
  422. C := madd0(m, 4891460686036598785, z[0])
  423. C, z[0] = madd2(m, 2896914383306846353, z[1], C)
  424. C, z[1] = madd2(m, 13281191951274694749, z[2], C)
  425. C, z[2] = madd2(m, 3486998266802970665, z[3], C)
  426. z[3] = C
  427. }
  428. {
  429. // m = z[0]n'[0] mod W
  430. m := z[0] * 14042775128853446655
  431. C := madd0(m, 4891460686036598785, z[0])
  432. C, z[0] = madd2(m, 2896914383306846353, z[1], C)
  433. C, z[1] = madd2(m, 13281191951274694749, z[2], C)
  434. C, z[2] = madd2(m, 3486998266802970665, z[3], C)
  435. z[3] = C
  436. }
  437. {
  438. // m = z[0]n'[0] mod W
  439. m := z[0] * 14042775128853446655
  440. C := madd0(m, 4891460686036598785, z[0])
  441. C, z[0] = madd2(m, 2896914383306846353, z[1], C)
  442. C, z[1] = madd2(m, 13281191951274694749, z[2], C)
  443. C, z[2] = madd2(m, 3486998266802970665, z[3], C)
  444. z[3] = C
  445. }
  446. {
  447. // m = z[0]n'[0] mod W
  448. m := z[0] * 14042775128853446655
  449. C := madd0(m, 4891460686036598785, z[0])
  450. C, z[0] = madd2(m, 2896914383306846353, z[1], C)
  451. C, z[1] = madd2(m, 13281191951274694749, z[2], C)
  452. C, z[2] = madd2(m, 3486998266802970665, z[3], C)
  453. z[3] = C
  454. }
  455. // if z > q --> z -= q
  456. // note: this is NOT constant time
  457. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  458. var b uint64
  459. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  460. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  461. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  462. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  463. }
  464. return z
  465. }
  466. // ToMont converts z to Montgomery form
  467. // sets and returns z = z * r^2
  468. func (z *Element) ToMont() *Element {
  469. var rSquare = Element{
  470. 1997599621687373223,
  471. 6052339484930628067,
  472. 10108755138030829701,
  473. 150537098327114917,
  474. }
  475. return z.MulAssign(&rSquare)
  476. }
  477. // ToRegular returns z in regular form (doesn't mutate z)
  478. func (z Element) ToRegular() Element {
  479. return *z.FromMont()
  480. }
  481. // String returns the string form of an Element in Montgomery form
  482. func (z *Element) String() string {
  483. var _z big.Int
  484. return z.ToBigIntRegular(&_z).String()
  485. }
  486. // ToBigInt returns z as a big.Int in Montgomery form
  487. func (z *Element) ToBigInt(res *big.Int) *big.Int {
  488. if bits.UintSize == 64 {
  489. bits := (*[4]big.Word)(unsafe.Pointer(z))
  490. return res.SetBits(bits[:])
  491. } else {
  492. var bits [8]big.Word
  493. for i := 0; i < len(z); i++ {
  494. bits[i*2] = big.Word(z[i])
  495. bits[i*2+1] = big.Word(z[i] >> 32)
  496. }
  497. return res.SetBits(bits[:])
  498. }
  499. }
  500. // ToBigIntRegular returns z as a big.Int in regular form
  501. func (z Element) ToBigIntRegular(res *big.Int) *big.Int {
  502. z.FromMont()
  503. if bits.UintSize == 64 {
  504. bits := (*[4]big.Word)(unsafe.Pointer(&z))
  505. return res.SetBits(bits[:])
  506. } else {
  507. var bits [8]big.Word
  508. for i := 0; i < len(z); i++ {
  509. bits[i*2] = big.Word(z[i])
  510. bits[i*2+1] = big.Word(z[i] >> 32)
  511. }
  512. return res.SetBits(bits[:])
  513. }
  514. }
  515. // SetBigInt sets z to v (regular form) and returns z in Montgomery form
  516. func (z *Element) SetBigInt(v *big.Int) *Element {
  517. z.SetZero()
  518. zero := big.NewInt(0)
  519. q := elementModulusBigInt()
  520. // fast path
  521. c := v.Cmp(q)
  522. if c == 0 {
  523. return z
  524. } else if c != 1 && v.Cmp(zero) != -1 {
  525. // v should
  526. vBits := v.Bits()
  527. for i := 0; i < len(vBits); i++ {
  528. z[i] = uint64(vBits[i])
  529. }
  530. return z.ToMont()
  531. }
  532. // copy input
  533. vv := new(big.Int).Set(v)
  534. // while v < 0, v+=q
  535. for vv.Cmp(zero) == -1 {
  536. vv.Add(vv, q)
  537. }
  538. // while v > q, v-=q
  539. for vv.Cmp(q) == 1 {
  540. vv.Sub(vv, q)
  541. }
  542. // if v == q, return 0
  543. if vv.Cmp(q) == 0 {
  544. return z
  545. }
  546. // v should
  547. vBits := vv.Bits()
  548. if bits.UintSize == 64 {
  549. for i := 0; i < len(vBits); i++ {
  550. z[i] = uint64(vBits[i])
  551. }
  552. } else {
  553. for i := 0; i < len(vBits); i++ {
  554. if i%2 == 0 {
  555. z[i/2] = uint64(vBits[i])
  556. } else {
  557. z[i/2] |= uint64(vBits[i]) << 32
  558. }
  559. }
  560. }
  561. return z.ToMont()
  562. }
  563. // SetString creates a big.Int with s (in base 10) and calls SetBigInt on z
  564. func (z *Element) SetString(s string) *Element {
  565. x, ok := new(big.Int).SetString(s, 10)
  566. if !ok {
  567. panic("Element.SetString failed -> can't parse number in base10 into a big.Int")
  568. }
  569. return z.SetBigInt(x)
  570. }
  571. // Legendre returns the Legendre symbol of z (either +1, -1, or 0.)
  572. func (z *Element) Legendre() int {
  573. var l Element
  574. // z^((q-1)/2)
  575. l.Exp(*z,
  576. 11669102379873075200,
  577. 10671829228508198984,
  578. 15863968012492123182,
  579. 1743499133401485332,
  580. )
  581. if l.IsZero() {
  582. return 0
  583. }
  584. // if l == 1
  585. if (l[3] == 1011752739694698287) && (l[2] == 7381016538464732718) && (l[1] == 3962172157175319849) && (l[0] == 12436184717236109307) {
  586. return 1
  587. }
  588. return -1
  589. }
  590. // Sqrt z = √x mod q
  591. // if the square root doesn't exist (x is not a square mod q)
  592. // Sqrt leaves z unchanged and returns nil
  593. func (z *Element) Sqrt(x *Element) *Element {
  594. // q ≡ 1 (mod 4)
  595. // see modSqrtTonelliShanks in math/big/int.go
  596. // using https://www.maa.org/sites/default/files/pdf/upload_library/22/Polya/07468342.di020786.02p0470a.pdf
  597. var y, b, t, w Element
  598. // w = x^((s-1)/2))
  599. w.Exp(*x,
  600. 14829091926808964255,
  601. 867720185306366531,
  602. 688207751544974772,
  603. 6495040407,
  604. )
  605. // y = x^((s+1)/2)) = w * x
  606. y.Mul(x, &w)
  607. // b = x^s = w * w * x = y * x
  608. b.Mul(&w, &y)
  609. // g = nonResidue ^ s
  610. var g = Element{
  611. 7164790868263648668,
  612. 11685701338293206998,
  613. 6216421865291908056,
  614. 1756667274303109607,
  615. }
  616. r := uint64(28)
  617. // compute legendre symbol
  618. // t = x^((q-1)/2) = r-1 squaring of x^s
  619. t = b
  620. for i := uint64(0); i < r-1; i++ {
  621. t.Square(&t)
  622. }
  623. if t.IsZero() {
  624. return z.SetZero()
  625. }
  626. if !((t[3] == 1011752739694698287) && (t[2] == 7381016538464732718) && (t[1] == 3962172157175319849) && (t[0] == 12436184717236109307)) {
  627. // t != 1, we don't have a square root
  628. return nil
  629. }
  630. for {
  631. var m uint64
  632. t = b
  633. // for t != 1
  634. for !((t[3] == 1011752739694698287) && (t[2] == 7381016538464732718) && (t[1] == 3962172157175319849) && (t[0] == 12436184717236109307)) {
  635. t.Square(&t)
  636. m++
  637. }
  638. if m == 0 {
  639. return z.Set(&y)
  640. }
  641. // t = g^(2^(r-m-1)) mod q
  642. ge := int(r - m - 1)
  643. t = g
  644. for ge > 0 {
  645. t.Square(&t)
  646. ge--
  647. }
  648. g.Square(&t)
  649. y.MulAssign(&t)
  650. b.MulAssign(&g)
  651. r = m
  652. }
  653. }