mratsim / constantine

Constantine: modular, high-performance, zero-dependency cryptography stack for verifiable computation, proof systems and blockchain protocols.
Other
391 stars 43 forks source link

Nim 2.0.x devel: static generic typeclass type mismatch #373

Closed mratsim closed 2 months ago

mratsim commented 5 months ago

From https://github.com/mratsim/constantine/pull/370#issuecomment-2081212632

Overview

Command:

nim c  -d:CTT_ASM=false  --tlsEmulation=off  -d:danger  --panics:on -d:noSignalHandler  --mm:arc -d:useMalloc  --verbosity:0 --hints:off --warnings:off  --passC:-fno-semantic-interposition  --passC:-falign-functions=64  --passC:-fmerge-all-constants --threads:on  --noMain --app:staticlib  --nimMainPrefix:ctt_init_  --out:libconstantine.a --outdir:lib  --nimcache:nimcache/libconstantine_static bindings/lib_constantine.nim

It seems like there is a regression in Nim 2.0.x devel branch that breaks type-checking:

image

No issue in Nim 2.0.2 or 2.04

image

Investigation

Error

Error: type mismatch
Expression: isOne((coords: [(mres: (limbs: [0'u, 0'u, 0'u, 0'u, 0'u, 0'u])), (mres: (limbs: [
    16517514583386313282'u, 74322656156451461'u, 16683759486841714365'u,
    815493829203396097'u, 204518332920448171'u, 1306242806803223655'u]))]))
  [1] (coords: [(mres: (limbs: [0'u, 0'u, 0'u, 0'u, 0'u, 0'u])), (mres: (limbs: [
    16517514583386313282'u, 74322656156451461'u, 16683759486841714365'u,
    815493829203396097'u, 204518332920448171'u, 1306242806803223655'u]))]): QuadraticExt

Expected one of (first mismatch at [position]):
[1] func isOne(a: BigInt): SecretBool
[1] func isOne(a: ExtensionField): SecretBool
[1] func isOne(a: FF): SecretBool

The input should match:

https://github.com/mratsim/constantine/blob/976c8bb215a3f0b21ce3d05f894eb506072a6285/constantine/math/extension_fields/towers.nim#L124-L129

and ExtensionField is just a generic typeclass

https://github.com/mratsim/constantine/blob/976c8bb215a3f0b21ce3d05f894eb506072a6285/constantine/math/extension_fields/towers.nim#L34-L48

The only potential buggy part is that it's called in a compile-time context: https://github.com/mratsim/constantine/blob/976c8bb215a3f0b21ce3d05f894eb506072a6285/constantine/math/extension_fields/towers.nim#L1442-L1447

mratsim commented 5 months ago

From https://github.com/mratsim/constantine/actions/runs/8629836320/job/23654839440 in PR https://github.com/mratsim/constantine/pull/368

v2.0.4 with git hash b47747d31844c6bd9af4322efe55e24fefea544c was working fine

image

mratsim commented 5 months ago

Reproduced locally, the compiler loses the concrete type here: image

Some possibly interesting points:

mratsim commented 5 months ago

image

mratsim commented 2 months ago

Fixed upstream in https://github.com/nim-lang/Nim/pull/23852 And workaround in https://github.com/mratsim/constantine/pull/434