From 39562d6938af1e12c09ce1d5f95af61004087c60 Mon Sep 17 00:00:00 2001 From: Advaita Saha Date: Wed, 7 Feb 2024 01:50:29 +0530 Subject: [PATCH] sqrt generalized precomp --- .../finite_fields_precomp_square_root.nim | 28 ++++--------------- .../arithmetic/finite_fields_square_root.nim | 3 ++ .../elliptic/ec_twistededwards_affine.nim | 4 +-- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/constantine/math/arithmetic/finite_fields_precomp_square_root.nim b/constantine/math/arithmetic/finite_fields_precomp_square_root.nim index 253b1b640..784c83934 100644 --- a/constantine/math/arithmetic/finite_fields_precomp_square_root.nim +++ b/constantine/math/arithmetic/finite_fields_precomp_square_root.nim @@ -8,7 +8,6 @@ import std/tables, - ../config/curves, ../../platforms/abstractions, ../constants/zoo_square_roots, ./bigints, ./finite_fields @@ -27,9 +26,8 @@ import # NOTE: If x is not a root of unity as asserted, the behaviour is undefined. func sqrtAlg_NegDlogInSmallDyadicSubgroup(x: Fp): int = let key = cast[int](x.mres.limbs[0] and SecretWord 0xFFFF) - if key in Fp.C.sqrtDlog(dlogLUT): - return Fp.C.sqrtDlog(dlogLUT)[key] - return 0 + return Fp.C.sqrtDlog(dlogLUT).getOrDefault(key, 0) + # sqrtAlg_GetPrecomputedRootOfUnity sets target to g^(multiplier << (order * sqrtParam_BlockSize)), where g is the fixed primitive 2^32th root of unity. # @@ -136,7 +134,7 @@ func sqrtAlg_ComputeRelevantPowers(z: Fp, squareRootCandidate: var Fp, rootOfUni squareRootCandidate.prod(acc, z) -func invSqrtEqDyadic(z: var Fp): SecretBool = +func invSqrtEqDyadic(z: var Fp) = ## The algorithm works by essentially computing the dlog of z and then halving it. ## negExponent is intended to hold the negative of the dlog of z. ## We determine this 32-bit value (usually) _sqrtBlockSize many bits at a time, starting with the least-significant bits. @@ -165,7 +163,7 @@ func invSqrtEqDyadic(z: var Fp): SecretBool = # if (negExponent and 1) == 1: # return false - result = SecretBool((negExponent and 1) != 1) + # result = SecretBool((negExponent and 1) != 1) for i in 1..