File: t23853.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (91 lines) | stat: -rw-r--r-- 2,330 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# issue #23853

block simplified:
  type QuadraticExt[F] = object
    coords: array[2, F]
  template Name(E: type QuadraticExt): int = 123
  template getBigInt(Name: static int): untyped = int
  type Foo[GT] = object
    a: getBigInt(GT.Name)
  var x: Foo[QuadraticExt[int]]
  
import std/macros

type
  Algebra* = enum
    BN254_Snarks
    BLS12_381

  Fp*[Name: static Algebra] = object
    limbs*: array[4, uint64]

  QuadraticExt*[F] = object
    ## Quadratic Extension field
    coords*: array[2, F]

  CubicExt*[F] = object
    ## Cubic Extension field
    coords*: array[3, F]

  ExtensionField*[F] = QuadraticExt[F] or CubicExt[F]

  Fp2*[Name: static Algebra] =
    QuadraticExt[Fp[Name]]

  Fp4*[Name: static Algebra] =
    QuadraticExt[Fp2[Name]]

  Fp6*[Name: static Algebra] =
    CubicExt[Fp2[Name]]

  Fp12*[Name: static Algebra] =
    CubicExt[Fp4[Name]]
    # QuadraticExt[Fp6[Name]]

template Name*(E: type ExtensionField): Algebra =
  E.F.Name

const BLS12_381_Order = [uint64 0x1, 0x2, 0x3, 0x4]
const BLS12_381_Modulus = [uint64 0x5, 0x6, 0x7, 0x8]


{.experimental: "dynamicBindSym".}

macro baseFieldModulus*(Name: static Algebra): untyped =
  result = bindSym($Name & "_Modulus")

macro scalarFieldModulus*(Name: static Algebra): untyped =
  result = bindSym($Name & "_Order")

type FieldKind* = enum
  kBaseField
  kScalarField

template getBigInt*(Name: static Algebra, kind: static FieldKind): untyped =
  # Workaround:
  # in `ptr UncheckedArray[BigInt[EC.getScalarField().bits()]]
  # EC.getScalarField is not accepted by the compiler
  #
  # and `ptr UncheckedArray[BigInt[Fr[EC.F.Name].bits]]` gets undeclared field: 'Name'
  #
  # but `ptr UncheckedArray[getBigInt(EC.getName(), kScalarField)]` works fine
  when kind == kBaseField:
    Name.baseFieldModulus().typeof()
  else:
    Name.scalarFieldModulus().typeof()

# ------------------------------------------------------------------------------

type BenchMultiexpContext*[GT] = object
  elems: seq[GT]
  exponents: seq[getBigInt(GT.Name, kScalarField)]

proc createBenchMultiExpContext*(GT: typedesc, inputSizes: openArray[int]): BenchMultiexpContext[GT] =
  discard

# ------------------------------------------------------------------------------

proc main() =
  let ctx = createBenchMultiExpContext(Fp12[BLS12_381], [2, 4, 8, 16])

main()