File: t23855.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 (61 lines) | stat: -rw-r--r-- 1,656 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
# issue #23855, not entirely fixed

import std/bitops

const WordBitWidth = sizeof(pointer) * 8

func wordsRequired*(bits: int): int {.inline.} =
  const divShiftor = fastLog2(uint32(WordBitWidth))
  result = (bits + WordBitWidth - 1) shr divShiftor

type
  Algebra* = enum
    BLS12_381

  BigInt*[bits: static int] = object
    limbs*: array[wordsRequired(bits), uint]

  Fr*[Name: static Algebra] = object
    residue_form*: BigInt[255]

  Fp*[Name: static Algebra] = object
    residue_form*: BigInt[381]

  FF*[Name: static Algebra] = Fp[Name] or Fr[Name]

template getBigInt*[Name: static Algebra](T: type FF[Name]): untyped =
  ## Get the underlying BigInt type.
  typeof(default(T).residue_form)

type
  EC_ShortW_Aff*[F] = object
    ## Elliptic curve point for a curve in Short Weierstrass form
    ##   y² = x³ + a x + b
    ##
    ## over a field F
    x*, y*: F

func bits*[Name: static Algebra](T: type FF[Name]): static int =
  T.getBigInt().bits

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

type
  ECFFT_Descriptor*[EC] = object
    ## Metadata for FFT on Elliptic Curve
    order*: int
    rootsOfUnity*: ptr UncheckedArray[BigInt[Fr[EC.F.Name].bits()]] # Undeclared identifier `Name`

func new*(T: type ECFFT_Descriptor): T =
  discard

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

template getBits[bits: static int](x: ptr UncheckedArray[BigInt[bits]]): int = bits

proc main() =
  let ctx = ECFFT_Descriptor[EC_ShortW_Aff[Fp[BLS12_381]]].new()
  doAssert getBits(ctx.rootsOfUnity) == 255
  doAssert ctx.rootsOfUnity[0].limbs.len == wordsRequired(255)

main()