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.

93 lines
3.0 KiB

  1. // +build !amd64
  2. // Copyright 2020 ConsenSys AG
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // Code generated by goff (v0.2.0) DO NOT EDIT
  16. // Package ff contains field arithmetic operations
  17. package ff
  18. // /!\ WARNING /!\
  19. // this code has not been audited and is provided as-is. In particular,
  20. // there is no security guarantees such as constant time implementation
  21. // or side-channel attack resistance
  22. // /!\ WARNING /!\
  23. import "math/bits"
  24. // Square z = x * x mod q
  25. // see https://hackmd.io/@zkteam/modular_multiplication
  26. func (z *Element) Square(x *Element) *Element {
  27. var p [4]uint64
  28. var u, v uint64
  29. {
  30. // round 0
  31. u, p[0] = bits.Mul64(x[0], x[0])
  32. m := p[0] * 14042775128853446655
  33. C := madd0(m, 4891460686036598785, p[0])
  34. var t uint64
  35. t, u, v = madd1sb(x[0], x[1], u)
  36. C, p[0] = madd2(m, 2896914383306846353, v, C)
  37. t, u, v = madd1s(x[0], x[2], t, u)
  38. C, p[1] = madd2(m, 13281191951274694749, v, C)
  39. _, u, v = madd1s(x[0], x[3], t, u)
  40. p[3], p[2] = madd3(m, 3486998266802970665, v, C, u)
  41. }
  42. {
  43. // round 1
  44. m := p[0] * 14042775128853446655
  45. C := madd0(m, 4891460686036598785, p[0])
  46. u, v = madd1(x[1], x[1], p[1])
  47. C, p[0] = madd2(m, 2896914383306846353, v, C)
  48. var t uint64
  49. t, u, v = madd2sb(x[1], x[2], p[2], u)
  50. C, p[1] = madd2(m, 13281191951274694749, v, C)
  51. _, u, v = madd2s(x[1], x[3], p[3], t, u)
  52. p[3], p[2] = madd3(m, 3486998266802970665, v, C, u)
  53. }
  54. {
  55. // round 2
  56. m := p[0] * 14042775128853446655
  57. C := madd0(m, 4891460686036598785, p[0])
  58. C, p[0] = madd2(m, 2896914383306846353, p[1], C)
  59. u, v = madd1(x[2], x[2], p[2])
  60. C, p[1] = madd2(m, 13281191951274694749, v, C)
  61. _, u, v = madd2sb(x[2], x[3], p[3], u)
  62. p[3], p[2] = madd3(m, 3486998266802970665, v, C, u)
  63. }
  64. {
  65. // round 3
  66. m := p[0] * 14042775128853446655
  67. C := madd0(m, 4891460686036598785, p[0])
  68. C, z[0] = madd2(m, 2896914383306846353, p[1], C)
  69. C, z[1] = madd2(m, 13281191951274694749, p[2], C)
  70. u, v = madd1(x[3], x[3], p[3])
  71. z[3], z[2] = madd3(m, 3486998266802970665, v, C, u)
  72. }
  73. // if z > q --> z -= q
  74. // note: this is NOT constant time
  75. if !(z[3] < 3486998266802970665 || (z[3] == 3486998266802970665 && (z[2] < 13281191951274694749 || (z[2] == 13281191951274694749 && (z[1] < 2896914383306846353 || (z[1] == 2896914383306846353 && (z[0] < 4891460686036598785))))))) {
  76. var b uint64
  77. z[0], b = bits.Sub64(z[0], 4891460686036598785, 0)
  78. z[1], b = bits.Sub64(z[1], 2896914383306846353, b)
  79. z[2], b = bits.Sub64(z[2], 13281191951274694749, b)
  80. z[3], _ = bits.Sub64(z[3], 3486998266802970665, b)
  81. }
  82. return z
  83. }