t23855.nim 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # issue #23855, 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. func bits*[Name: static Algebra](T: type FF[Name]): static int =
  28. T.getBigInt().bits
  29. # ------------------------------------------------------------------------------
  30. type
  31. ECFFT_Descriptor*[EC] = object
  32. ## Metadata for FFT on Elliptic Curve
  33. order*: int
  34. rootsOfUnity*: ptr UncheckedArray[BigInt[Fr[EC.F.Name].bits()]] # Undeclared identifier `Name`
  35. func new*(T: type ECFFT_Descriptor): T =
  36. discard
  37. # ------------------------------------------------------------------------------
  38. template getBits[bits: static int](x: ptr UncheckedArray[BigInt[bits]]): int = bits
  39. proc main() =
  40. let ctx = ECFFT_Descriptor[EC_ShortW_Aff[Fp[BLS12_381]]].new()
  41. doAssert getBits(ctx.rootsOfUnity) == 255
  42. doAssert ctx.rootsOfUnity[0].limbs.len == wordsRequired(255)
  43. main()