Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static uint64 at compileTime: 'intVal' is not accessible using discriminant 'kind' of type 'TFullReg' [FieldDefect] #14585

Closed
mratsim opened this issue Jun 6, 2020 · 3 comments · Fixed by #17590
Labels
Compiler Crash Static[T] VM see also `const` label

Comments

@mratsim
Copy link
Collaborator

mratsim commented Jun 6, 2020

static uint64 parameter cannot be accessed in a VM context they raise the error

'intVal' is not accessible using discriminant 'kind' of type 'TFullReg' [FieldDefect]

Test case:

const foo_m0ninv = 0x1234'u64

proc foo(m0ninv: static uint64) =
  echo m0ninv

static: # Execute in a VM context
  foo(foo_m0ninv)

Tracing the bug, the crash happens line 450 of vm.nim:

Nim/compiler/vm.nim

Lines 441 to 453 in 32083c7

of tyUInt..tyUInt64:
dest.ensureKind(rkInt)
case skipTypes(srctyp, abstractRange).kind
of tyFloat..tyFloat64:
dest.intVal = int(src.floatVal)
else:
let srcDist = (sizeof(src.intVal) - srctyp.size) * 8
let destDist = (sizeof(dest.intVal) - desttyp.size) * 8
var value = cast[BiggestUInt](src.intVal)
value = (value shl srcDist) shr srcDist
value = (value shl destDist) shr destDist
dest.intVal = cast[BiggestInt](value)

At that point we have a rkNode instead of a rkInt and we crash.

This prevents me from using the same code for both runtime and compile-time (i.e. I need to dispatch with when nimvm)

I also suspect that it's linked to the nimsuggest crashed mentioned by @alaviss here alaviss/nim.nvim#18 (comment)

@mratsim mratsim added VM see also `const` label Static[T] Compiler Crash labels Jun 6, 2020
mratsim added a commit to mratsim/constantine that referenced this issue Jun 7, 2020
mratsim added a commit to mratsim/constantine that referenced this issue Jun 7, 2020
mratsim added a commit to mratsim/constantine that referenced this issue Jun 7, 2020
mratsim added a commit to mratsim/constantine that referenced this issue Jun 14, 2020
* Add MultiScalar recoding from "Efficient and Secure Algorithms for GLV-Based Scalar Multiplication" by Faz et al

* precompute cube root of unity - Add VM precomputation of Fp - workaround upstream bug nim-lang/Nim#14585

* Add the φ-accelerated lookup table builder

* Add a dedicated bithacks file

* cosmetic import consistency

* Build the φ precompute table with n-1 EC additions instead of 2^(n-1) additions

* remove binary

* Add the GLV precomputations to the sage scripts

* You can't avoid it, bigint multiplication is needed at one point

* Add bigint multiplication discarding some low words

* Implement the lattice decomposition in sage

* Proper decomposition for BN254

* Prepare the code for a new scalar mul

* We compile, and now debugging hunt

* More helpers to debug GLV scalar Mul

* Fix conditional negation

* Endomorphism accelerated scalar mul working for BN254 curve

* Implement endomorphism acceleration for BLS12-381 (needed cofactor clearing of the point)

* fix nimble test script after bench rename
@cooldome
Copy link
Member

cooldome commented Nov 4, 2020

works as expected now

@narimiran
Copy link
Member

works as expected now

For me it produces the same thing as reported in the original report:

Error: unhandled exception: 'intVal' is not accessible using discriminant 'kind' of type 'TFullReg' [FieldDefect]

@timotheecour
Copy link
Member

@cooldome please always write git hash instead of now, works as expected now is very vague.

I'm fixing this here: #17590

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compiler Crash Static[T] VM see also `const` label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants