t23853.nim 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # issue #23853
  2. block simplified:
  3. type QuadraticExt[F] = object
  4. coords: array[2, F]
  5. template Name(E: type QuadraticExt): int = 123
  6. template getBigInt(Name: static int): untyped = int
  7. type Foo[GT] = object
  8. a: getBigInt(GT.Name)
  9. var x: Foo[QuadraticExt[int]]
  10. import std/macros
  11. type
  12. Algebra* = enum
  13. BN254_Snarks
  14. BLS12_381
  15. Fp*[Name: static Algebra] = object
  16. limbs*: array[4, uint64]
  17. QuadraticExt*[F] = object
  18. ## Quadratic Extension field
  19. coords*: array[2, F]
  20. CubicExt*[F] = object
  21. ## Cubic Extension field
  22. coords*: array[3, F]
  23. ExtensionField*[F] = QuadraticExt[F] or CubicExt[F]
  24. Fp2*[Name: static Algebra] =
  25. QuadraticExt[Fp[Name]]
  26. Fp4*[Name: static Algebra] =
  27. QuadraticExt[Fp2[Name]]
  28. Fp6*[Name: static Algebra] =
  29. CubicExt[Fp2[Name]]
  30. Fp12*[Name: static Algebra] =
  31. CubicExt[Fp4[Name]]
  32. # QuadraticExt[Fp6[Name]]
  33. template Name*(E: type ExtensionField): Algebra =
  34. E.F.Name
  35. const BLS12_381_Order = [uint64 0x1, 0x2, 0x3, 0x4]
  36. const BLS12_381_Modulus = [uint64 0x5, 0x6, 0x7, 0x8]
  37. {.experimental: "dynamicBindSym".}
  38. macro baseFieldModulus*(Name: static Algebra): untyped =
  39. result = bindSym($Name & "_Modulus")
  40. macro scalarFieldModulus*(Name: static Algebra): untyped =
  41. result = bindSym($Name & "_Order")
  42. type FieldKind* = enum
  43. kBaseField
  44. kScalarField
  45. template getBigInt*(Name: static Algebra, kind: static FieldKind): untyped =
  46. # Workaround:
  47. # in `ptr UncheckedArray[BigInt[EC.getScalarField().bits()]]
  48. # EC.getScalarField is not accepted by the compiler
  49. #
  50. # and `ptr UncheckedArray[BigInt[Fr[EC.F.Name].bits]]` gets undeclared field: 'Name'
  51. #
  52. # but `ptr UncheckedArray[getBigInt(EC.getName(), kScalarField)]` works fine
  53. when kind == kBaseField:
  54. Name.baseFieldModulus().typeof()
  55. else:
  56. Name.scalarFieldModulus().typeof()
  57. # ------------------------------------------------------------------------------
  58. type BenchMultiexpContext*[GT] = object
  59. elems: seq[GT]
  60. exponents: seq[getBigInt(GT.Name, kScalarField)]
  61. proc createBenchMultiExpContext*(GT: typedesc, inputSizes: openArray[int]): BenchMultiexpContext[GT] =
  62. discard
  63. # ------------------------------------------------------------------------------
  64. proc main() =
  65. let ctx = createBenchMultiExpContext(Fp12[BLS12_381], [2, 4, 8, 16])
  66. main()