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.

127 lines
3.2 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. import (
  18. "math/bits"
  19. "golang.org/x/sys/cpu"
  20. )
  21. var supportAdx = cpu.X86.HasADX && cpu.X86.HasBMI2
  22. func madd(a, b, t, u, v uint64) (uint64, uint64, uint64) {
  23. var carry uint64
  24. hi, lo := bits.Mul64(a, b)
  25. v, carry = bits.Add64(lo, v, 0)
  26. u, carry = bits.Add64(hi, u, carry)
  27. t, _ = bits.Add64(t, 0, carry)
  28. return t, u, v
  29. }
  30. // madd0 hi = a*b + c (discards lo bits)
  31. func madd0(a, b, c uint64) (hi uint64) {
  32. var carry, lo uint64
  33. hi, lo = bits.Mul64(a, b)
  34. _, carry = bits.Add64(lo, c, 0)
  35. hi, _ = bits.Add64(hi, 0, carry)
  36. return
  37. }
  38. // madd1 hi, lo = a*b + c
  39. func madd1(a, b, c uint64) (hi uint64, lo uint64) {
  40. var carry uint64
  41. hi, lo = bits.Mul64(a, b)
  42. lo, carry = bits.Add64(lo, c, 0)
  43. hi, _ = bits.Add64(hi, 0, carry)
  44. return
  45. }
  46. // madd2 hi, lo = a*b + c + d
  47. func madd2(a, b, c, d uint64) (hi uint64, lo uint64) {
  48. var carry uint64
  49. hi, lo = bits.Mul64(a, b)
  50. c, carry = bits.Add64(c, d, 0)
  51. hi, _ = bits.Add64(hi, 0, carry)
  52. lo, carry = bits.Add64(lo, c, 0)
  53. hi, _ = bits.Add64(hi, 0, carry)
  54. return
  55. }
  56. // madd2s superhi, hi, lo = 2*a*b + c + d + e
  57. func madd2s(a, b, c, d, e uint64) (superhi, hi, lo uint64) {
  58. var carry, sum uint64
  59. hi, lo = bits.Mul64(a, b)
  60. lo, carry = bits.Add64(lo, lo, 0)
  61. hi, superhi = bits.Add64(hi, hi, carry)
  62. sum, carry = bits.Add64(c, e, 0)
  63. hi, _ = bits.Add64(hi, 0, carry)
  64. lo, carry = bits.Add64(lo, sum, 0)
  65. hi, _ = bits.Add64(hi, 0, carry)
  66. hi, _ = bits.Add64(hi, 0, d)
  67. return
  68. }
  69. func madd1s(a, b, d, e uint64) (superhi, hi, lo uint64) {
  70. var carry uint64
  71. hi, lo = bits.Mul64(a, b)
  72. lo, carry = bits.Add64(lo, lo, 0)
  73. hi, superhi = bits.Add64(hi, hi, carry)
  74. lo, carry = bits.Add64(lo, e, 0)
  75. hi, _ = bits.Add64(hi, 0, carry)
  76. hi, _ = bits.Add64(hi, 0, d)
  77. return
  78. }
  79. func madd2sb(a, b, c, e uint64) (superhi, hi, lo uint64) {
  80. var carry, sum uint64
  81. hi, lo = bits.Mul64(a, b)
  82. lo, carry = bits.Add64(lo, lo, 0)
  83. hi, superhi = bits.Add64(hi, hi, carry)
  84. sum, carry = bits.Add64(c, e, 0)
  85. hi, _ = bits.Add64(hi, 0, carry)
  86. lo, carry = bits.Add64(lo, sum, 0)
  87. hi, _ = bits.Add64(hi, 0, carry)
  88. return
  89. }
  90. func madd1sb(a, b, e uint64) (superhi, hi, lo uint64) {
  91. var carry uint64
  92. hi, lo = bits.Mul64(a, b)
  93. lo, carry = bits.Add64(lo, lo, 0)
  94. hi, superhi = bits.Add64(hi, hi, carry)
  95. lo, carry = bits.Add64(lo, e, 0)
  96. hi, _ = bits.Add64(hi, 0, carry)
  97. return
  98. }
  99. func madd3(a, b, c, d, e uint64) (hi uint64, lo uint64) {
  100. var carry uint64
  101. hi, lo = bits.Mul64(a, b)
  102. c, carry = bits.Add64(c, d, 0)
  103. hi, _ = bits.Add64(hi, 0, carry)
  104. lo, carry = bits.Add64(lo, c, 0)
  105. hi, _ = bits.Add64(hi, e, carry)
  106. return
  107. }