From d4e202ead5a41dc8a841497086a7bb51956b7b28 Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Sat, 17 Sep 2022 18:52:52 +0200 Subject: [PATCH] Don't use array[^1], it can throw and cannot be locally turn off --- constantine/blssig_pop_on_bls12381_g2.nim | 4 ++-- constantine/ethereum_evm_precompiles.nim | 4 ++-- constantine/math/config/precompute.nim | 6 +++--- helpers/prng_unsafe.nim | 2 +- research/kzg_poly_commit/fft_fr.nim | 4 ++-- research/kzg_poly_commit/fft_g1.nim | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/constantine/blssig_pop_on_bls12381_g2.nim b/constantine/blssig_pop_on_bls12381_g2.nim index d9de9ad..9cf99de 100644 --- a/constantine/blssig_pop_on_bls12381_g2.nim +++ b/constantine/blssig_pop_on_bls12381_g2.nim @@ -242,7 +242,7 @@ func deserialize_public_key_compressed_unchecked*(dst: var PublicKey, src: array # General case var t{.noInit.}: matchingBigInt(BLS12_381) t.unmarshal(src, bigEndian) - t.limbs[^1] = t.limbs[^1] and (MaxWord shr 3) # The first 3 bytes contain metadata to mask out + t.limbs[t.len-1] = t.limbs[t.len-1] and (MaxWord shr 3) # The first 3 bytes contain metadata to mask out if bool(t >= BLS12_381.Mod()): return cttBLS_CoordinateGreaterOrEqualThanModulus @@ -296,7 +296,7 @@ func deserialize_signature_compressed_unchecked*(dst: var Signature, src: array[ # General case var t{.noInit.}: matchingBigInt(BLS12_381) t.unmarshal(src.toOpenArray(0, 48-1), bigEndian) - t.limbs[^1] = t.limbs[^1] and (MaxWord shr 3) # The first 3 bytes contain metadata to mask out + t.limbs[t.limbs.len-1] = t.limbs[t.limbs.len-1] and (MaxWord shr 3) # The first 3 bytes contain metadata to mask out if bool(t >= BLS12_381.Mod()): return cttBLS_CoordinateGreaterOrEqualThanModulus diff --git a/constantine/ethereum_evm_precompiles.nim b/constantine/ethereum_evm_precompiles.nim index 5c374fc..8d9aa50 100644 --- a/constantine/ethereum_evm_precompiles.nim +++ b/constantine/ethereum_evm_precompiles.nim @@ -322,7 +322,7 @@ func eth_evm_ecpairing*( if N == 0: # Spec: "Empty input is valid and results in returning one." zeroMem(r.addr, r.sizeof()) - r[^1] = byte 1 + r[r.len-1] = byte 1 return var gt0{.noInit.}, gt1{.noInit.}: Fp12[BN254_Snarks] @@ -361,4 +361,4 @@ func eth_evm_ecpairing*( zeroMem(r.addr, r.sizeof()) if gt0.isOne().bool: - r[^1] = byte 1 + r[r.len-1] = byte 1 diff --git a/constantine/math/config/precompute.nim b/constantine/math/config/precompute.nim index e09026d..9c86edb 100644 --- a/constantine/math/config/precompute.nim +++ b/constantine/math/config/precompute.nim @@ -236,7 +236,7 @@ func checkOdd(M: BigInt) = func checkValidModulus(M: BigInt) = const expectedMsb = M.bits-1 - WordBitWidth * (M.limbs.len - 1) - let msb = log2_vartime(BaseType(M.limbs[^1])) + let msb = log2_vartime(BaseType(M.limbs[M.limbs.len-1])) doAssert msb == expectedMsb, "Internal Error: the modulus must use all declared bits and only those:\n" & " Modulus '" & M.toHex() & "' is declared with " & $M.bits & @@ -254,7 +254,7 @@ func countSpareBits*(M: BigInt): int = ## - [0, 8p) if 3 bits are available ## - ... checkValidModulus(M) - let msb = log2_vartime(BaseType(M.limbs[^1])) + let msb = log2_vartime(BaseType(M.limbs[M.limbs.len-1])) result = WordBitWidth - 1 - msb.int func invModBitwidth[T: SomeUnsignedInt](a: T): T = @@ -336,7 +336,7 @@ func r_powmod(n: static int, M: BigInt): BigInt = start = (w-1)*WordBitWidth + msb stop = n*WordBitWidth*w - result.limbs[^1] = SecretWord(BaseType(1) shl msb) # C0 = 2^(wn-1), the power of 2 immediatly less than the modulus + result.limbs[M.limbs.len-1] = SecretWord(BaseType(1) shl msb) # C0 = 2^(wn-1), the power of 2 immediatly less than the modulus for _ in start ..< stop: result.doubleMod(M) diff --git a/helpers/prng_unsafe.nim b/helpers/prng_unsafe.nim index 211b629..7b19267 100644 --- a/helpers/prng_unsafe.nim +++ b/helpers/prng_unsafe.nim @@ -165,7 +165,7 @@ template clearExtraBitsOverMSB(a: var BigInt) = when a.bits != a.limbs.len * WordBitWidth: const posExtraBits = a.bits - (a.limbs.len-1) * WordBitWidth const mask = (One shl posExtraBits) - One - a.limbs[^1] = a.limbs[^1] and mask + a.limbs[a.limbs.len-1] = a.limbs[a.limbs.len-1] and mask func random_unsafe(rng: var RngState, a: var BigInt) = ## Initialize a standalone BigInt diff --git a/research/kzg_poly_commit/fft_fr.nim b/research/kzg_poly_commit/fft_fr.nim index 6ceffee..b6a11b9 100644 --- a/research/kzg_poly_commit/fft_fr.nim +++ b/research/kzg_poly_commit/fft_fr.nim @@ -78,9 +78,9 @@ func expandRootOfUnity[F](rootOfUnity: F): seq[F] = result.setLen(2) result[0].setOne() result[1] = rootOfUnity - while not result[^1].isOne().bool: + while not result[result.len-1].isOne().bool: result.setLen(result.len + 1) - result[^1].prod(result[^2], rootOfUnity) + result[result.len-1].prod(result[result.len-2], rootOfUnity) # FFT Algorithm # ---------------------------------------------------------------- diff --git a/research/kzg_poly_commit/fft_g1.nim b/research/kzg_poly_commit/fft_g1.nim index e47af00..70ffce5 100644 --- a/research/kzg_poly_commit/fft_g1.nim +++ b/research/kzg_poly_commit/fft_g1.nim @@ -82,10 +82,10 @@ func expandRootOfUnity[F](rootOfUnity: F): auto {.noInit.} = r[1] = rootOfUnity.toBig() var cur = rootOfUnity - while not r[^1].isOne().bool: + while not r[r.len-1].isOne().bool: cur *= rootOfUnity r.setLen(r.len + 1) - r[^1] = cur.toBig() + r[r.len-1] = cur.toBig() return r