t23854.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # issue #23854, not entirely fixed
  2. import std/bitops
  3. const WordBitWidth = sizeof(pointer) * 8
  4. func wordsRequired*(bits: int): int {.inline.} =
  5. const divShiftor = fastLog2(uint32(WordBitWidth))
  6. result = (bits + WordBitWidth - 1) shr divShiftor
  7. type
  8. Algebra* = enum
  9. BLS12_381
  10. BigInt*[bits: static int] = object
  11. limbs*: array[wordsRequired(bits), uint]
  12. Fr*[Name: static Algebra] = object
  13. residue_form*: BigInt[255]
  14. Fp*[Name: static Algebra] = object
  15. residue_form*: BigInt[381]
  16. FF*[Name: static Algebra] = Fp[Name] or Fr[Name]
  17. template getBigInt*[Name: static Algebra](T: type FF[Name]): untyped =
  18. ## Get the underlying BigInt type.
  19. typeof(default(T).residue_form)
  20. type
  21. EC_ShortW_Aff*[F] = object
  22. ## Elliptic curve point for a curve in Short Weierstrass form
  23. ## y² = x³ + a x + b
  24. ##
  25. ## over a field F
  26. x*, y*: F
  27. type FieldKind* = enum
  28. kBaseField
  29. kScalarField
  30. func bits*[Name: static Algebra](T: type FF[Name]): static int =
  31. T.getBigInt().bits
  32. template getScalarField*(EC: type EC_ShortW_Aff): untyped =
  33. Fr[EC.F.Name]
  34. # ------------------------------------------------------------------------------
  35. type
  36. ECFFT_Descriptor*[EC] = object
  37. ## Metadata for FFT on Elliptic Curve
  38. order*: int
  39. rootsOfUnity1*: ptr UncheckedArray[BigInt[EC.getScalarField().bits()]] # Error: in expression 'EC.getScalarField()': identifier expected, but found 'EC.getScalarField'
  40. rootsOfUnity2*: ptr UncheckedArray[BigInt[getScalarField(EC).bits()]] # Compiler SIGSEGV: Illegal Storage Access
  41. func new*(T: type ECFFT_Descriptor): T =
  42. discard
  43. # ------------------------------------------------------------------------------
  44. template getBits[bits: static int](x: ptr UncheckedArray[BigInt[bits]]): int = bits
  45. proc main() =
  46. let ctx = ECFFT_Descriptor[EC_ShortW_Aff[Fp[BLS12_381]]].new()
  47. doAssert getBits(ctx.rootsOfUnity1) == 255
  48. doAssert getBits(ctx.rootsOfUnity2) == 255
  49. doAssert ctx.rootsOfUnity1[0].limbs.len == wordsRequired(255)
  50. doAssert ctx.rootsOfUnity2[0].limbs.len == wordsRequired(255)
  51. main()