t23854.nim 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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()
  52. block: # bug #24045
  53. type ArrayBuf[N: static int, T] = object
  54. buf: array[N, T]
  55. func maxLen(T: type): int =
  56. sizeof(T) * 2
  57. type MyBuf[I: type] = ArrayBuf[maxLen(I), byte]
  58. var v: MyBuf[int]
  59. block: # bug #24043
  60. type ArrayBuf[N: static int, T = byte] = object
  61. buf: array[N, T]
  62. func maxLen(T: type): int =
  63. sizeof(T) * 2
  64. type MyBuf[I] = ArrayBuf[maxLen(I)]
  65. var v: MyBuf[int]