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.

1037 lines
25 KiB

  1. // Copyright 2020 ConsenSys Software Inc.
  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 consensys/gnark-crypto DO NOT EDIT
  15. package ff
  16. import (
  17. "crypto/rand"
  18. "encoding/binary"
  19. "errors"
  20. "io"
  21. "math/big"
  22. "math/bits"
  23. "reflect"
  24. "strconv"
  25. "sync"
  26. )
  27. // Element represents a field element stored on 4 words (uint64)
  28. // Element are assumed to be in Montgomery form in all methods
  29. // field modulus q =
  30. //
  31. // 21888242871839275222246405745257275088548364400416034343698204186575808495617
  32. type Element [4]uint64
  33. // Limbs number of 64 bits words needed to represent Element
  34. const Limbs = 4
  35. // Bits number bits needed to represent Element
  36. const Bits = 254
  37. // Bytes number bytes needed to represent Element
  38. const Bytes = Limbs * 8
  39. // field modulus stored as big.Int
  40. var _modulus big.Int
  41. // Modulus returns q as a big.Int
  42. // q =
  43. //
  44. // 21888242871839275222246405745257275088548364400416034343698204186575808495617
  45. func Modulus() *big.Int {
  46. return new(big.Int).Set(&_modulus)
  47. }
  48. // q (modulus)
  49. var qElement = Element{
  50. 4891460686036598785,
  51. 2896914383306846353,
  52. 13281191951274694749,
  53. 3486998266802970665,
  54. }
  55. // rSquare
  56. var rSquare = Element{
  57. 1997599621687373223,
  58. 6052339484930628067,
  59. 10108755138030829701,
  60. 150537098327114917,
  61. }
  62. var bigIntPool = sync.Pool{
  63. New: func() interface{} {
  64. return new(big.Int)
  65. },
  66. }
  67. func init() {
  68. _modulus.SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  69. }
  70. // NewElement returns a new Element
  71. func NewElement() *Element {
  72. return &Element{}
  73. }
  74. // NewElementFromUint64 returns a new Element from a uint64 value
  75. //
  76. // it is equivalent to
  77. // var v NewElement
  78. // v.SetUint64(...)
  79. func NewElementFromUint64(v uint64) Element {
  80. z := Element{v}
  81. z.Mul(&z, &rSquare)
  82. return z
  83. }
  84. // SetUint64 z = v, sets z LSB to v (non-Montgomery form) and convert z to Montgomery form
  85. func (z *Element) SetUint64(v uint64) *Element {
  86. *z = Element{v}
  87. return z.Mul(z, &rSquare) // z.ToMont()
  88. }
  89. // Set z = x
  90. func (z *Element) Set(x *Element) *Element {
  91. z[0] = x[0]
  92. z[1] = x[1]
  93. z[2] = x[2]
  94. z[3] = x[3]
  95. return z
  96. }
  97. // SetInterface converts provided interface into Element
  98. // returns an error if provided type is not supported
  99. // supported types: Element, *Element, uint64, int, string (interpreted as base10 integer),
  100. // *big.Int, big.Int, []byte
  101. func (z *Element) SetInterface(i1 interface{}) (*Element, error) {
  102. switch c1 := i1.(type) {
  103. case Element:
  104. return z.Set(&c1), nil
  105. case *Element:
  106. return z.Set(c1), nil
  107. case uint64:
  108. return z.SetUint64(c1), nil
  109. case int:
  110. return z.SetString(strconv.Itoa(c1)), nil
  111. case string:
  112. return z.SetString(c1), nil
  113. case *big.Int:
  114. return z.SetBigInt(c1), nil
  115. case big.Int:
  116. return z.SetBigInt(&c1), nil
  117. case []byte:
  118. return z.SetBytes(c1), nil
  119. default:
  120. return nil, errors.New("can't set ff.Element from type " + reflect.TypeOf(i1).String())
  121. }
  122. }
  123. // SetZero z = 0
  124. func (z *Element) SetZero() *Element {
  125. z[0] = 0
  126. z[1] = 0
  127. z[2] = 0
  128. z[3] = 0
  129. return z
  130. }
  131. // SetOne z = 1 (in Montgomery form)
  132. func (z *Element) SetOne() *Element {
  133. z[0] = 12436184717236109307
  134. z[1] = 3962172157175319849
  135. z[2] = 7381016538464732718
  136. z[3] = 1011752739694698287
  137. return z
  138. }
  139. // Div z = x*y^-1 mod q
  140. func (z *Element) Div(x, y *Element) *Element {
  141. var yInv Element
  142. yInv.Inverse(y)
  143. z.Mul(x, &yInv)
  144. return z
  145. }
  146. // Bit returns the i'th bit, with lsb == bit 0.
  147. // It is the responsability of the caller to convert from Montgomery to Regular form if needed
  148. func (z *Element) Bit(i uint64) uint64 {
  149. j := i / 64
  150. if j >= 4 {
  151. return 0
  152. }
  153. return uint64(z[j] >> (i % 64) & 1)
  154. }
  155. // Equal returns z == x
  156. func (z *Element) Equal(x *Element) bool {
  157. return (z[3] == x[3]) && (z[2] == x[2]) && (z[1] == x[1]) && (z[0] == x[0])
  158. }
  159. // IsZero returns z == 0
  160. func (z *Element) IsZero() bool {
  161. return (z[3] | z[2] | z[1] | z[0]) == 0
  162. }
  163. // IsUint64 returns true if z[0] >= 0 and all other words are 0
  164. func (z *Element) IsUint64() bool {
  165. return (z[3] | z[2] | z[1]) == 0
  166. }
  167. // Cmp compares (lexicographic order) z and x and returns:
  168. //
  169. // -1 if z < x
  170. // 0 if z == x
  171. // +1 if z > x
  172. //
  173. func (z *Element) Cmp(x *Element) int {
  174. _z := *z
  175. _x := *x
  176. _z.FromMont()
  177. _x.FromMont()
  178. if _z[3] > _x[3] {
  179. return 1
  180. } else if _z[3] < _x[3] {
  181. return -1
  182. }
  183. if _z[2] > _x[2] {
  184. return 1
  185. } else if _z[2] < _x[2] {
  186. return -1
  187. }
  188. if _z[1] > _x[1] {
  189. return 1
  190. } else if _z[1] < _x[1] {
  191. return -1
  192. }
  193. if _z[0] > _x[0] {
  194. return 1
  195. } else if _z[0] < _x[0] {
  196. return -1
  197. }
  198. return 0
  199. }
  200. // LexicographicallyLargest returns true if this element is strictly lexicographically
  201. // larger than its negation, false otherwise
  202. func (z *Element) LexicographicallyLargest() bool {
  203. // adapted from github.com/zkcrypto/bls12_381
  204. // we check if the element is larger than (q-1) / 2
  205. // if z - (((q -1) / 2) + 1) have no underflow, then z > (q-1) / 2
  206. _z := *z
  207. _z.FromMont()
  208. var b uint64
  209. _, b = bits.Sub64(_z[0], 11669102379873075201, 0)
  210. _, b = bits.Sub64(_z[1], 10671829228508198984, b)
  211. _, b = bits.Sub64(_z[2], 15863968012492123182, b)
  212. _, b = bits.Sub64(_z[3], 1743499133401485332, b)
  213. return b == 0
  214. }
  215. // SetRandom sets z to a random element < q
  216. func (z *Element) SetRandom() (*Element, error) {
  217. var bytes [32]byte
  218. if _, err := io.ReadFull(rand.Reader, bytes[:]); err != nil {
  219. return nil, err
  220. }
  221. z[0] = binary.BigEndian.Uint64(bytes[0:8])
  222. z[1] = binary.BigEndian.Uint64(bytes[8:16])
  223. z[2] = binary.BigEndian.Uint64(bytes[16:24])
  224. z[3] = binary.BigEndian.Uint64(bytes[24:32])
  225. z[3] %= 3486998266802970665
  226. // if z > q --> z -= q
  227. // note: this is NOT constant time
  228. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  229. var b uint64
  230. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  231. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  232. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  233. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  234. }
  235. return z, nil
  236. }
  237. // One returns 1 (in montgommery form)
  238. func One() Element {
  239. var one Element
  240. one.SetOne()
  241. return one
  242. }
  243. // Halve sets z to z / 2 (mod p)
  244. func (z *Element) Halve() {
  245. if z[0]&1 == 1 {
  246. var carry uint64
  247. // z = z + q
  248. z[0], carry = bits.Add64(z[0], 4891460686036598785, 0)
  249. z[1], carry = bits.Add64(z[1], 2896914383306846353, carry)
  250. z[2], carry = bits.Add64(z[2], 13281191951274694749, carry)
  251. z[3], _ = bits.Add64(z[3], 3486998266802970665, carry)
  252. }
  253. // z = z >> 1
  254. z[0] = z[0]>>1 | z[1]<<63
  255. z[1] = z[1]>>1 | z[2]<<63
  256. z[2] = z[2]>>1 | z[3]<<63
  257. z[3] >>= 1
  258. }
  259. // API with assembly impl
  260. // Mul z = x * y mod q
  261. // see https://hackmd.io/@zkteam/modular_multiplication
  262. func (z *Element) Mul(x, y *Element) *Element {
  263. mul(z, x, y)
  264. return z
  265. }
  266. // Square z = x * x mod q
  267. // see https://hackmd.io/@zkteam/modular_multiplication
  268. func (z *Element) Square(x *Element) *Element {
  269. mul(z, x, x)
  270. return z
  271. }
  272. // FromMont converts z in place (i.e. mutates) from Montgomery to regular representation
  273. // sets and returns z = z * 1
  274. func (z *Element) FromMont() *Element {
  275. fromMont(z)
  276. return z
  277. }
  278. // Add z = x + y mod q
  279. func (z *Element) Add(x, y *Element) *Element {
  280. add(z, x, y)
  281. return z
  282. }
  283. // Double z = x + x mod q, aka Lsh 1
  284. func (z *Element) Double(x *Element) *Element {
  285. double(z, x)
  286. return z
  287. }
  288. // Sub z = x - y mod q
  289. func (z *Element) Sub(x, y *Element) *Element {
  290. sub(z, x, y)
  291. return z
  292. }
  293. // Neg z = q - x
  294. func (z *Element) Neg(x *Element) *Element {
  295. neg(z, x)
  296. return z
  297. }
  298. // Generic (no ADX instructions, no AMD64) versions of multiplication and squaring algorithms
  299. func _mulGeneric(z, x, y *Element) {
  300. var t [4]uint64
  301. var c [3]uint64
  302. {
  303. // round 0
  304. v := x[0]
  305. c[1], c[0] = bits.Mul64(v, y[0])
  306. m := c[0] * 14042775128853446655
  307. c[2] = madd0(m, 4891460686036598785, c[0])
  308. c[1], c[0] = madd1(v, y[1], c[1])
  309. c[2], t[0] = madd2(m, 2896914383306846353, c[2], c[0])
  310. c[1], c[0] = madd1(v, y[2], c[1])
  311. c[2], t[1] = madd2(m, 13281191951274694749, c[2], c[0])
  312. c[1], c[0] = madd1(v, y[3], c[1])
  313. t[3], t[2] = madd3(m, 3486998266802970665, c[0], c[2], c[1])
  314. }
  315. {
  316. // round 1
  317. v := x[1]
  318. c[1], c[0] = madd1(v, y[0], t[0])
  319. m := c[0] * 14042775128853446655
  320. c[2] = madd0(m, 4891460686036598785, c[0])
  321. c[1], c[0] = madd2(v, y[1], c[1], t[1])
  322. c[2], t[0] = madd2(m, 2896914383306846353, c[2], c[0])
  323. c[1], c[0] = madd2(v, y[2], c[1], t[2])
  324. c[2], t[1] = madd2(m, 13281191951274694749, c[2], c[0])
  325. c[1], c[0] = madd2(v, y[3], c[1], t[3])
  326. t[3], t[2] = madd3(m, 3486998266802970665, c[0], c[2], c[1])
  327. }
  328. {
  329. // round 2
  330. v := x[2]
  331. c[1], c[0] = madd1(v, y[0], t[0])
  332. m := c[0] * 14042775128853446655
  333. c[2] = madd0(m, 4891460686036598785, c[0])
  334. c[1], c[0] = madd2(v, y[1], c[1], t[1])
  335. c[2], t[0] = madd2(m, 2896914383306846353, c[2], c[0])
  336. c[1], c[0] = madd2(v, y[2], c[1], t[2])
  337. c[2], t[1] = madd2(m, 13281191951274694749, c[2], c[0])
  338. c[1], c[0] = madd2(v, y[3], c[1], t[3])
  339. t[3], t[2] = madd3(m, 3486998266802970665, c[0], c[2], c[1])
  340. }
  341. {
  342. // round 3
  343. v := x[3]
  344. c[1], c[0] = madd1(v, y[0], t[0])
  345. m := c[0] * 14042775128853446655
  346. c[2] = madd0(m, 4891460686036598785, c[0])
  347. c[1], c[0] = madd2(v, y[1], c[1], t[1])
  348. c[2], z[0] = madd2(m, 2896914383306846353, c[2], c[0])
  349. c[1], c[0] = madd2(v, y[2], c[1], t[2])
  350. c[2], z[1] = madd2(m, 13281191951274694749, c[2], c[0])
  351. c[1], c[0] = madd2(v, y[3], c[1], t[3])
  352. z[3], z[2] = madd3(m, 3486998266802970665, c[0], c[2], c[1])
  353. }
  354. // if z > q --> z -= q
  355. // note: this is NOT constant time
  356. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  357. var b uint64
  358. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  359. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  360. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  361. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  362. }
  363. }
  364. func _fromMontGeneric(z *Element) {
  365. // the following lines implement z = z * 1
  366. // with a modified CIOS montgomery multiplication
  367. {
  368. // m = z[0]n'[0] mod W
  369. m := z[0] * 14042775128853446655
  370. C := madd0(m, 4891460686036598785, z[0])
  371. C, z[0] = madd2(m, 2896914383306846353, z[1], C)
  372. C, z[1] = madd2(m, 13281191951274694749, z[2], C)
  373. C, z[2] = madd2(m, 3486998266802970665, z[3], C)
  374. z[3] = C
  375. }
  376. {
  377. // m = z[0]n'[0] mod W
  378. m := z[0] * 14042775128853446655
  379. C := madd0(m, 4891460686036598785, z[0])
  380. C, z[0] = madd2(m, 2896914383306846353, z[1], C)
  381. C, z[1] = madd2(m, 13281191951274694749, z[2], C)
  382. C, z[2] = madd2(m, 3486998266802970665, z[3], C)
  383. z[3] = C
  384. }
  385. {
  386. // m = z[0]n'[0] mod W
  387. m := z[0] * 14042775128853446655
  388. C := madd0(m, 4891460686036598785, z[0])
  389. C, z[0] = madd2(m, 2896914383306846353, z[1], C)
  390. C, z[1] = madd2(m, 13281191951274694749, z[2], C)
  391. C, z[2] = madd2(m, 3486998266802970665, z[3], C)
  392. z[3] = C
  393. }
  394. {
  395. // m = z[0]n'[0] mod W
  396. m := z[0] * 14042775128853446655
  397. C := madd0(m, 4891460686036598785, z[0])
  398. C, z[0] = madd2(m, 2896914383306846353, z[1], C)
  399. C, z[1] = madd2(m, 13281191951274694749, z[2], C)
  400. C, z[2] = madd2(m, 3486998266802970665, z[3], C)
  401. z[3] = C
  402. }
  403. // if z > q --> z -= q
  404. // note: this is NOT constant time
  405. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  406. var b uint64
  407. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  408. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  409. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  410. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  411. }
  412. }
  413. func _addGeneric(z, x, y *Element) {
  414. var carry uint64
  415. z[0], carry = bits.Add64(x[0], y[0], 0)
  416. z[1], carry = bits.Add64(x[1], y[1], carry)
  417. z[2], carry = bits.Add64(x[2], y[2], carry)
  418. z[3], _ = bits.Add64(x[3], y[3], carry)
  419. // if z > q --> z -= q
  420. // note: this is NOT constant time
  421. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  422. var b uint64
  423. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  424. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  425. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  426. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  427. }
  428. }
  429. func _doubleGeneric(z, x *Element) {
  430. var carry uint64
  431. z[0], carry = bits.Add64(x[0], x[0], 0)
  432. z[1], carry = bits.Add64(x[1], x[1], carry)
  433. z[2], carry = bits.Add64(x[2], x[2], carry)
  434. z[3], _ = bits.Add64(x[3], x[3], carry)
  435. // if z > q --> z -= q
  436. // note: this is NOT constant time
  437. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  438. var b uint64
  439. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  440. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  441. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  442. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  443. }
  444. }
  445. func _subGeneric(z, x, y *Element) {
  446. var b uint64
  447. z[0], b = bits.Sub64(x[0], y[0], 0)
  448. z[1], b = bits.Sub64(x[1], y[1], b)
  449. z[2], b = bits.Sub64(x[2], y[2], b)
  450. z[3], b = bits.Sub64(x[3], y[3], b)
  451. if b != 0 {
  452. var c uint64
  453. z[0], c = bits.Add64(z[0], 4891460686036598785, 0)
  454. z[1], c = bits.Add64(z[1], 2896914383306846353, c)
  455. z[2], c = bits.Add64(z[2], 13281191951274694749, c)
  456. z[3], _ = bits.Add64(z[3], 3486998266802970665, c)
  457. }
  458. }
  459. func _negGeneric(z, x *Element) {
  460. if x.IsZero() {
  461. z.SetZero()
  462. return
  463. }
  464. var borrow uint64
  465. z[0], borrow = bits.Sub64(4891460686036598785, x[0], 0)
  466. z[1], borrow = bits.Sub64(2896914383306846353, x[1], borrow)
  467. z[2], borrow = bits.Sub64(13281191951274694749, x[2], borrow)
  468. z[3], _ = bits.Sub64(3486998266802970665, x[3], borrow)
  469. }
  470. func _reduceGeneric(z *Element) {
  471. // if z > q --> z -= q
  472. // note: this is NOT constant time
  473. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  474. var b uint64
  475. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  476. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  477. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  478. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  479. }
  480. }
  481. func mulByConstant(z *Element, c uint8) {
  482. switch c {
  483. case 0:
  484. z.SetZero()
  485. return
  486. case 1:
  487. return
  488. case 2:
  489. z.Double(z)
  490. return
  491. case 3:
  492. _z := *z
  493. z.Double(z).Add(z, &_z)
  494. case 5:
  495. _z := *z
  496. z.Double(z).Double(z).Add(z, &_z)
  497. default:
  498. var y Element
  499. y.SetUint64(uint64(c))
  500. z.Mul(z, &y)
  501. }
  502. }
  503. // BatchInvert returns a new slice with every element inverted.
  504. // Uses Montgomery batch inversion trick
  505. func BatchInvert(a []Element) []Element {
  506. res := make([]Element, len(a))
  507. if len(a) == 0 {
  508. return res
  509. }
  510. zeroes := make([]bool, len(a))
  511. accumulator := One()
  512. for i := 0; i < len(a); i++ {
  513. if a[i].IsZero() {
  514. zeroes[i] = true
  515. continue
  516. }
  517. res[i] = accumulator
  518. accumulator.Mul(&accumulator, &a[i])
  519. }
  520. accumulator.Inverse(&accumulator)
  521. for i := len(a) - 1; i >= 0; i-- {
  522. if zeroes[i] {
  523. continue
  524. }
  525. res[i].Mul(&res[i], &accumulator)
  526. accumulator.Mul(&accumulator, &a[i])
  527. }
  528. return res
  529. }
  530. func _butterflyGeneric(a, b *Element) {
  531. t := *a
  532. a.Add(a, b)
  533. b.Sub(&t, b)
  534. }
  535. // BitLen returns the minimum number of bits needed to represent z
  536. // returns 0 if z == 0
  537. func (z *Element) BitLen() int {
  538. if z[3] != 0 {
  539. return 192 + bits.Len64(z[3])
  540. }
  541. if z[2] != 0 {
  542. return 128 + bits.Len64(z[2])
  543. }
  544. if z[1] != 0 {
  545. return 64 + bits.Len64(z[1])
  546. }
  547. return bits.Len64(z[0])
  548. }
  549. // Exp z = x^exponent mod q
  550. func (z *Element) Exp(x Element, exponent *big.Int) *Element {
  551. var bZero big.Int
  552. if exponent.Cmp(&bZero) == 0 {
  553. return z.SetOne()
  554. }
  555. z.Set(&x)
  556. for i := exponent.BitLen() - 2; i >= 0; i-- {
  557. z.Square(z)
  558. if exponent.Bit(i) == 1 {
  559. z.Mul(z, &x)
  560. }
  561. }
  562. return z
  563. }
  564. // ToMont converts z to Montgomery form
  565. // sets and returns z = z * r^2
  566. func (z *Element) ToMont() *Element {
  567. return z.Mul(z, &rSquare)
  568. }
  569. // ToRegular returns z in regular form (doesn't mutate z)
  570. func (z Element) ToRegular() Element {
  571. return *z.FromMont()
  572. }
  573. // String returns the string form of an Element in Montgomery form
  574. func (z *Element) String() string {
  575. zz := *z
  576. zz.FromMont()
  577. if zz.IsUint64() {
  578. return strconv.FormatUint(zz[0], 10)
  579. } else {
  580. var zzNeg Element
  581. zzNeg.Neg(z)
  582. zzNeg.FromMont()
  583. if zzNeg.IsUint64() {
  584. return "-" + strconv.FormatUint(zzNeg[0], 10)
  585. }
  586. }
  587. vv := bigIntPool.Get().(*big.Int)
  588. defer bigIntPool.Put(vv)
  589. return zz.ToBigInt(vv).String()
  590. }
  591. // ToBigInt returns z as a big.Int in Montgomery form
  592. func (z *Element) ToBigInt(res *big.Int) *big.Int {
  593. var b [Limbs * 8]byte
  594. binary.BigEndian.PutUint64(b[24:32], z[0])
  595. binary.BigEndian.PutUint64(b[16:24], z[1])
  596. binary.BigEndian.PutUint64(b[8:16], z[2])
  597. binary.BigEndian.PutUint64(b[0:8], z[3])
  598. return res.SetBytes(b[:])
  599. }
  600. // ToBigIntRegular returns z as a big.Int in regular form
  601. func (z Element) ToBigIntRegular(res *big.Int) *big.Int {
  602. z.FromMont()
  603. return z.ToBigInt(res)
  604. }
  605. // Bytes returns the regular (non montgomery) value
  606. // of z as a big-endian byte array.
  607. func (z *Element) Bytes() (res [Limbs * 8]byte) {
  608. _z := z.ToRegular()
  609. binary.BigEndian.PutUint64(res[24:32], _z[0])
  610. binary.BigEndian.PutUint64(res[16:24], _z[1])
  611. binary.BigEndian.PutUint64(res[8:16], _z[2])
  612. binary.BigEndian.PutUint64(res[0:8], _z[3])
  613. return
  614. }
  615. // Marshal returns the regular (non montgomery) value
  616. // of z as a big-endian byte slice.
  617. func (z *Element) Marshal() []byte {
  618. b := z.Bytes()
  619. return b[:]
  620. }
  621. // SetBytes interprets e as the bytes of a big-endian unsigned integer,
  622. // sets z to that value (in Montgomery form), and returns z.
  623. func (z *Element) SetBytes(e []byte) *Element {
  624. // get a big int from our pool
  625. vv := bigIntPool.Get().(*big.Int)
  626. vv.SetBytes(e)
  627. // set big int
  628. z.SetBigInt(vv)
  629. // put temporary object back in pool
  630. bigIntPool.Put(vv)
  631. return z
  632. }
  633. // SetBigInt sets z to v (regular form) and returns z in Montgomery form
  634. func (z *Element) SetBigInt(v *big.Int) *Element {
  635. z.SetZero()
  636. var zero big.Int
  637. // fast path
  638. c := v.Cmp(&_modulus)
  639. if c == 0 {
  640. // v == 0
  641. return z
  642. } else if c != 1 && v.Cmp(&zero) != -1 {
  643. // 0 < v < q
  644. return z.setBigInt(v)
  645. }
  646. // get temporary big int from the pool
  647. vv := bigIntPool.Get().(*big.Int)
  648. // copy input + modular reduction
  649. vv.Set(v)
  650. vv.Mod(v, &_modulus)
  651. // set big int byte value
  652. z.setBigInt(vv)
  653. // release object into pool
  654. bigIntPool.Put(vv)
  655. return z
  656. }
  657. // setBigInt assumes 0 <= v < q
  658. func (z *Element) setBigInt(v *big.Int) *Element {
  659. vBits := v.Bits()
  660. if bits.UintSize == 64 {
  661. for i := 0; i < len(vBits); i++ {
  662. z[i] = uint64(vBits[i])
  663. }
  664. } else {
  665. for i := 0; i < len(vBits); i++ {
  666. if i%2 == 0 {
  667. z[i/2] = uint64(vBits[i])
  668. } else {
  669. z[i/2] |= uint64(vBits[i]) << 32
  670. }
  671. }
  672. }
  673. return z.ToMont()
  674. }
  675. // SetString creates a big.Int with s (in base 10) and calls SetBigInt on z
  676. func (z *Element) SetString(s string) *Element {
  677. // get temporary big int from the pool
  678. vv := bigIntPool.Get().(*big.Int)
  679. if _, ok := vv.SetString(s, 10); !ok {
  680. panic("Element.SetString failed -> can't parse number in base10 into a big.Int")
  681. }
  682. z.SetBigInt(vv)
  683. // release object into pool
  684. bigIntPool.Put(vv)
  685. return z
  686. }
  687. var (
  688. _bLegendreExponentElement *big.Int
  689. _bSqrtExponentElement *big.Int
  690. )
  691. func init() {
  692. _bLegendreExponentElement, _ = new(big.Int).SetString("183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f8000000", 16)
  693. const sqrtExponentElement = "183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f"
  694. _bSqrtExponentElement, _ = new(big.Int).SetString(sqrtExponentElement, 16)
  695. }
  696. // Legendre returns the Legendre symbol of z (either +1, -1, or 0.)
  697. func (z *Element) Legendre() int {
  698. var l Element
  699. // z^((q-1)/2)
  700. l.Exp(*z, _bLegendreExponentElement)
  701. if l.IsZero() {
  702. return 0
  703. }
  704. // if l == 1
  705. if (l[3] == 1011752739694698287) && (l[2] == 7381016538464732718) && (l[1] == 3962172157175319849) && (l[0] == 12436184717236109307) {
  706. return 1
  707. }
  708. return -1
  709. }
  710. // Sqrt z = √x mod q
  711. // if the square root doesn't exist (x is not a square mod q)
  712. // Sqrt leaves z unchanged and returns nil
  713. func (z *Element) Sqrt(x *Element) *Element {
  714. // q ≡ 1 (mod 4)
  715. // see modSqrtTonelliShanks in math/big/int.go
  716. // using https://www.maa.org/sites/default/files/pdf/upload_library/22/Polya/07468342.di020786.02p0470a.pdf
  717. var y, b, t, w Element
  718. // w = x^((s-1)/2))
  719. w.Exp(*x, _bSqrtExponentElement)
  720. // y = x^((s+1)/2)) = w * x
  721. y.Mul(x, &w)
  722. // b = x^s = w * w * x = y * x
  723. b.Mul(&w, &y)
  724. // g = nonResidue ^ s
  725. var g = Element{
  726. 7164790868263648668,
  727. 11685701338293206998,
  728. 6216421865291908056,
  729. 1756667274303109607,
  730. }
  731. r := uint64(28)
  732. // compute legendre symbol
  733. // t = x^((q-1)/2) = r-1 squaring of x^s
  734. t = b
  735. for i := uint64(0); i < r-1; i++ {
  736. t.Square(&t)
  737. }
  738. if t.IsZero() {
  739. return z.SetZero()
  740. }
  741. if !((t[3] == 1011752739694698287) && (t[2] == 7381016538464732718) && (t[1] == 3962172157175319849) && (t[0] == 12436184717236109307)) {
  742. // t != 1, we don't have a square root
  743. return nil
  744. }
  745. for {
  746. var m uint64
  747. t = b
  748. // for t != 1
  749. for !((t[3] == 1011752739694698287) && (t[2] == 7381016538464732718) && (t[1] == 3962172157175319849) && (t[0] == 12436184717236109307)) {
  750. t.Square(&t)
  751. m++
  752. }
  753. if m == 0 {
  754. return z.Set(&y)
  755. }
  756. // t = g^(2^(r-m-1)) mod q
  757. ge := int(r - m - 1)
  758. t = g
  759. for ge > 0 {
  760. t.Square(&t)
  761. ge--
  762. }
  763. g.Square(&t)
  764. y.Mul(&y, &t)
  765. b.Mul(&b, &g)
  766. r = m
  767. }
  768. }
  769. // Inverse z = x^-1 mod q
  770. // Algorithm 16 in "Efficient Software-Implementation of Finite Fields with Applications to Cryptography"
  771. // if x == 0, sets and returns z = x
  772. func (z *Element) Inverse(x *Element) *Element {
  773. if x.IsZero() {
  774. z.SetZero()
  775. return z
  776. }
  777. // initialize u = q
  778. var u = Element{
  779. 4891460686036598785,
  780. 2896914383306846353,
  781. 13281191951274694749,
  782. 3486998266802970665,
  783. }
  784. // initialize s = r^2
  785. var s = Element{
  786. 1997599621687373223,
  787. 6052339484930628067,
  788. 10108755138030829701,
  789. 150537098327114917,
  790. }
  791. // r = 0
  792. r := Element{}
  793. v := *x
  794. var carry, borrow uint64
  795. var bigger bool
  796. for {
  797. for v[0]&1 == 0 {
  798. // v = v >> 1
  799. v[0] = v[0]>>1 | v[1]<<63
  800. v[1] = v[1]>>1 | v[2]<<63
  801. v[2] = v[2]>>1 | v[3]<<63
  802. v[3] >>= 1
  803. if s[0]&1 == 1 {
  804. // s = s + q
  805. s[0], carry = bits.Add64(s[0], 4891460686036598785, 0)
  806. s[1], carry = bits.Add64(s[1], 2896914383306846353, carry)
  807. s[2], carry = bits.Add64(s[2], 13281191951274694749, carry)
  808. s[3], _ = bits.Add64(s[3], 3486998266802970665, carry)
  809. }
  810. // s = s >> 1
  811. s[0] = s[0]>>1 | s[1]<<63
  812. s[1] = s[1]>>1 | s[2]<<63
  813. s[2] = s[2]>>1 | s[3]<<63
  814. s[3] >>= 1
  815. }
  816. for u[0]&1 == 0 {
  817. // u = u >> 1
  818. u[0] = u[0]>>1 | u[1]<<63
  819. u[1] = u[1]>>1 | u[2]<<63
  820. u[2] = u[2]>>1 | u[3]<<63
  821. u[3] >>= 1
  822. if r[0]&1 == 1 {
  823. // r = r + q
  824. r[0], carry = bits.Add64(r[0], 4891460686036598785, 0)
  825. r[1], carry = bits.Add64(r[1], 2896914383306846353, carry)
  826. r[2], carry = bits.Add64(r[2], 13281191951274694749, carry)
  827. r[3], _ = bits.Add64(r[3], 3486998266802970665, carry)
  828. }
  829. // r = r >> 1
  830. r[0] = r[0]>>1 | r[1]<<63
  831. r[1] = r[1]>>1 | r[2]<<63
  832. r[2] = r[2]>>1 | r[3]<<63
  833. r[3] >>= 1
  834. }
  835. // v >= u
  836. 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])))))))
  837. if bigger {
  838. // v = v - u
  839. v[0], borrow = bits.Sub64(v[0], u[0], 0)
  840. v[1], borrow = bits.Sub64(v[1], u[1], borrow)
  841. v[2], borrow = bits.Sub64(v[2], u[2], borrow)
  842. v[3], _ = bits.Sub64(v[3], u[3], borrow)
  843. // s = s - r
  844. s[0], borrow = bits.Sub64(s[0], r[0], 0)
  845. s[1], borrow = bits.Sub64(s[1], r[1], borrow)
  846. s[2], borrow = bits.Sub64(s[2], r[2], borrow)
  847. s[3], borrow = bits.Sub64(s[3], r[3], borrow)
  848. if borrow == 1 {
  849. // s = s + q
  850. s[0], carry = bits.Add64(s[0], 4891460686036598785, 0)
  851. s[1], carry = bits.Add64(s[1], 2896914383306846353, carry)
  852. s[2], carry = bits.Add64(s[2], 13281191951274694749, carry)
  853. s[3], _ = bits.Add64(s[3], 3486998266802970665, carry)
  854. }
  855. } else {
  856. // u = u - v
  857. u[0], borrow = bits.Sub64(u[0], v[0], 0)
  858. u[1], borrow = bits.Sub64(u[1], v[1], borrow)
  859. u[2], borrow = bits.Sub64(u[2], v[2], borrow)
  860. u[3], _ = bits.Sub64(u[3], v[3], borrow)
  861. // r = r - s
  862. r[0], borrow = bits.Sub64(r[0], s[0], 0)
  863. r[1], borrow = bits.Sub64(r[1], s[1], borrow)
  864. r[2], borrow = bits.Sub64(r[2], s[2], borrow)
  865. r[3], borrow = bits.Sub64(r[3], s[3], borrow)
  866. if borrow == 1 {
  867. // r = r + q
  868. r[0], carry = bits.Add64(r[0], 4891460686036598785, 0)
  869. r[1], carry = bits.Add64(r[1], 2896914383306846353, carry)
  870. r[2], carry = bits.Add64(r[2], 13281191951274694749, carry)
  871. r[3], _ = bits.Add64(r[3], 3486998266802970665, carry)
  872. }
  873. }
  874. if (u[0] == 1) && (u[3]|u[2]|u[1]) == 0 {
  875. z.Set(&r)
  876. return z
  877. }
  878. if (v[0] == 1) && (v[3]|v[2]|v[1]) == 0 {
  879. z.Set(&s)
  880. return z
  881. }
  882. }
  883. }