Don't use array[^1], it can throw and cannot be locally turn off
This commit is contained in:
parent
df048112c3
commit
d4e202ead5
|
@ -242,7 +242,7 @@ func deserialize_public_key_compressed_unchecked*(dst: var PublicKey, src: array
|
||||||
# General case
|
# General case
|
||||||
var t{.noInit.}: matchingBigInt(BLS12_381)
|
var t{.noInit.}: matchingBigInt(BLS12_381)
|
||||||
t.unmarshal(src, bigEndian)
|
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()):
|
if bool(t >= BLS12_381.Mod()):
|
||||||
return cttBLS_CoordinateGreaterOrEqualThanModulus
|
return cttBLS_CoordinateGreaterOrEqualThanModulus
|
||||||
|
@ -296,7 +296,7 @@ func deserialize_signature_compressed_unchecked*(dst: var Signature, src: array[
|
||||||
# General case
|
# General case
|
||||||
var t{.noInit.}: matchingBigInt(BLS12_381)
|
var t{.noInit.}: matchingBigInt(BLS12_381)
|
||||||
t.unmarshal(src.toOpenArray(0, 48-1), bigEndian)
|
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()):
|
if bool(t >= BLS12_381.Mod()):
|
||||||
return cttBLS_CoordinateGreaterOrEqualThanModulus
|
return cttBLS_CoordinateGreaterOrEqualThanModulus
|
||||||
|
|
|
@ -322,7 +322,7 @@ func eth_evm_ecpairing*(
|
||||||
if N == 0:
|
if N == 0:
|
||||||
# Spec: "Empty input is valid and results in returning one."
|
# Spec: "Empty input is valid and results in returning one."
|
||||||
zeroMem(r.addr, r.sizeof())
|
zeroMem(r.addr, r.sizeof())
|
||||||
r[^1] = byte 1
|
r[r.len-1] = byte 1
|
||||||
return
|
return
|
||||||
|
|
||||||
var gt0{.noInit.}, gt1{.noInit.}: Fp12[BN254_Snarks]
|
var gt0{.noInit.}, gt1{.noInit.}: Fp12[BN254_Snarks]
|
||||||
|
@ -361,4 +361,4 @@ func eth_evm_ecpairing*(
|
||||||
|
|
||||||
zeroMem(r.addr, r.sizeof())
|
zeroMem(r.addr, r.sizeof())
|
||||||
if gt0.isOne().bool:
|
if gt0.isOne().bool:
|
||||||
r[^1] = byte 1
|
r[r.len-1] = byte 1
|
||||||
|
|
|
@ -236,7 +236,7 @@ func checkOdd(M: BigInt) =
|
||||||
|
|
||||||
func checkValidModulus(M: BigInt) =
|
func checkValidModulus(M: BigInt) =
|
||||||
const expectedMsb = M.bits-1 - WordBitWidth * (M.limbs.len - 1)
|
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" &
|
doAssert msb == expectedMsb, "Internal Error: the modulus must use all declared bits and only those:\n" &
|
||||||
" Modulus '" & M.toHex() & "' is declared with " & $M.bits &
|
" Modulus '" & M.toHex() & "' is declared with " & $M.bits &
|
||||||
|
@ -254,7 +254,7 @@ func countSpareBits*(M: BigInt): int =
|
||||||
## - [0, 8p) if 3 bits are available
|
## - [0, 8p) if 3 bits are available
|
||||||
## - ...
|
## - ...
|
||||||
checkValidModulus(M)
|
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
|
result = WordBitWidth - 1 - msb.int
|
||||||
|
|
||||||
func invModBitwidth[T: SomeUnsignedInt](a: T): T =
|
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
|
start = (w-1)*WordBitWidth + msb
|
||||||
stop = n*WordBitWidth*w
|
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:
|
for _ in start ..< stop:
|
||||||
result.doubleMod(M)
|
result.doubleMod(M)
|
||||||
|
|
||||||
|
|
|
@ -165,7 +165,7 @@ template clearExtraBitsOverMSB(a: var BigInt) =
|
||||||
when a.bits != a.limbs.len * WordBitWidth:
|
when a.bits != a.limbs.len * WordBitWidth:
|
||||||
const posExtraBits = a.bits - (a.limbs.len-1) * WordBitWidth
|
const posExtraBits = a.bits - (a.limbs.len-1) * WordBitWidth
|
||||||
const mask = (One shl posExtraBits) - One
|
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) =
|
func random_unsafe(rng: var RngState, a: var BigInt) =
|
||||||
## Initialize a standalone BigInt
|
## Initialize a standalone BigInt
|
||||||
|
|
|
@ -78,9 +78,9 @@ func expandRootOfUnity[F](rootOfUnity: F): seq[F] =
|
||||||
result.setLen(2)
|
result.setLen(2)
|
||||||
result[0].setOne()
|
result[0].setOne()
|
||||||
result[1] = rootOfUnity
|
result[1] = rootOfUnity
|
||||||
while not result[^1].isOne().bool:
|
while not result[result.len-1].isOne().bool:
|
||||||
result.setLen(result.len + 1)
|
result.setLen(result.len + 1)
|
||||||
result[^1].prod(result[^2], rootOfUnity)
|
result[result.len-1].prod(result[result.len-2], rootOfUnity)
|
||||||
|
|
||||||
# FFT Algorithm
|
# FFT Algorithm
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
|
@ -82,10 +82,10 @@ func expandRootOfUnity[F](rootOfUnity: F): auto {.noInit.} =
|
||||||
r[1] = rootOfUnity.toBig()
|
r[1] = rootOfUnity.toBig()
|
||||||
|
|
||||||
var cur = rootOfUnity
|
var cur = rootOfUnity
|
||||||
while not r[^1].isOne().bool:
|
while not r[r.len-1].isOne().bool:
|
||||||
cur *= rootOfUnity
|
cur *= rootOfUnity
|
||||||
r.setLen(r.len + 1)
|
r.setLen(r.len + 1)
|
||||||
r[^1] = cur.toBig()
|
r[r.len-1] = cur.toBig()
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue