From cee0a38278de07ff853c4071bde67362436fd069 Mon Sep 17 00:00:00 2001 From: andri lim Date: Thu, 25 Apr 2019 22:33:26 +0700 Subject: [PATCH] add some comment --- nimbus/vm/interpreter/opcodes_impl.nim | 5 +++++ nimbus/vm/precompiles.nim | 21 +++++++++++++++++++++ tests/test_generalstate_failing.nim | 3 +++ 3 files changed, 29 insertions(+) diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index 76e5e9b31..0861b0bc9 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -197,6 +197,11 @@ op sha3, inline = true, startPos, length: ## 0x20, Compute Keccak-256 hash. let (pos, len) = (startPos.toInt, length.toInt) + # TODO: + # "randomStatetest14.json", # SHA3 offset + # "sha3_deja.json", # SHA3 startPos + # both test require Uint256 to calculate startpos/offset + if pos < 0 or len < 0 or pos > 2147483648: raise newException(OutOfBoundsRead, "Out of bounds memory access") diff --git a/nimbus/vm/precompiles.nim b/nimbus/vm/precompiles.nim index 467ab1137..c69c59ff2 100644 --- a/nimbus/vm/precompiles.nim +++ b/nimbus/vm/precompiles.nim @@ -56,6 +56,24 @@ proc getPoint[T: G1|G2](t: typedesc[T], data: openarray[byte]): Point[T] = raise newException(ValidationError, "Could not get point value") if not py.fromBytes2(data.toOpenArray(nextOffset, nextOffset * 2 - 1)): raise newException(ValidationError, "Could not get point value") + + # "ecpairing_perturb_g2_by_field_modulus_again.json", + # "ecpairing_perturb_zeropoint_by_field_modulus.json", + # "ecpairing_perturb_g2_by_field_modulus.json", + # modulus comparion in FQ2.fromBytes produce different result + const + modulus = Uint256.fromHex("30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47") + let a = Uint256.fromBytesBE(data.toOpenArray(0, 31), false) + let b = Uint256.fromBytesBE(data.toOpenArray(32, 63), false) + when T is G2: + let c = Uint256.fromBytesBE(data.toOpenArray(64, 95), false) + let d = Uint256.fromBytesBE(data.toOpenArray(96, 127), false) + if a >= modulus or b >= modulus or c >= modulus or d >= modulus: + raise newException(ValidationError, "value greater than field modulus") + else: + if a >= modulus or b >= modulus: + raise newException(ValidationError, "value greater than field modulus") + if px.isZero() and py.isZero(): result = T.zero() else: @@ -138,6 +156,7 @@ proc modExpInternal(computation: BaseComputation, base_len, exp_len, mod_len: in else: log2(exp) # highest-bit in exponent else: let first32 = rawMsg.rangeToPadded[:Uint256](96 + base_len, 95 + base_len + exp_len) + # TODO: `modexpRandomInput.json` require Uint256 arithmetic for this code below if not first32.isZero: 8 * (exp_len - 32) + first32.log2 else: @@ -176,6 +195,8 @@ proc modExpInternal(computation: BaseComputation, base_len, exp_len, mod_len: in else: powmod(base, exp, modulo).toByteArrayBE + # maximum output len is the same as mod_len + # if it less than mod_len, it will be zero padded at left if output.len >= mod_len: computation.rawOutput = @(output[^mod_len..^1]) else: diff --git a/tests/test_generalstate_failing.nim b/tests/test_generalstate_failing.nim index 4b4b60790..239eb02e5 100644 --- a/tests/test_generalstate_failing.nim +++ b/tests/test_generalstate_failing.nim @@ -25,10 +25,13 @@ func allowedFailingGeneralStateTest*(folder, name: string): bool = "RevertInCreateInInit.json", "modexp.json", + # see precompiles getPoint[G2] "ecpairing_perturb_g2_by_field_modulus_again.json", "ecpairing_perturb_zeropoint_by_field_modulus.json", "ecpairing_perturb_g2_by_field_modulus.json", + # all these tests below actually pass + # but they are very slow # byzantium slow "LoopCallsDepthThenRevert3.json", "LoopCallsDepthThenRevert2.json",