From e7bec288ca62ee250179c1363c765c827336ed26 Mon Sep 17 00:00:00 2001 From: luc10921 Date: Thu, 23 May 2024 12:29:02 -0300 Subject: [PATCH] CU-86dthgzj6 - Update CryptoLib native contract --- boa3/builtin/interop/crypto/__init__.py | 10 +- boa3/builtin/interop/crypto/namedcurve.py | 4 +- boa3/builtin/nativecontract/cryptolib.py | 10 +- .../model/builtin/interop/crypto/__init__.py | 6 +- .../builtin/interop/crypto/keccak256method.py | 12 +++ ...amedcurvetype.py => namedcurvehashtype.py} | 18 ++-- .../builtin/interop/crypto/verifywithecdsa.py | 4 +- .../internal/model/builtin/interop/interop.py | 5 +- .../model/builtin/native/cryptolibclass.py | 1 + .../model/builtin/native/nativecontract.py | 2 +- boa3/internal/model/sc/__init__.py | 2 +- boa3/internal/neo3/contracts/namedcurve.py | 24 ----- .../internal/neo3/contracts/namedcurvehash.py | 38 ++++++++ boa3/sc/contracts/cryptolib.py | 23 ++++- boa3/sc/types/__init__.py | 4 +- .../interop_test/crypto/VerifyWithECDsa.py | 4 +- .../VerifyWithECDsaSecp256k1Sha256Bool.py} | 4 +- .../VerifyWithECDsaSecp256k1Sha256Bytes.py} | 4 +- ...y => VerifyWithECDsaSecp256k1Sha256Int.py} | 4 +- ...WithECDsaSecp256k1Sha256MismatchedType.py} | 4 +- ...y => VerifyWithECDsaSecp256k1Sha256Str.py} | 4 +- .../VerifyWithECDsaSecp256r1Sha256Bool.py} | 4 +- ...=> VerifyWithECDsaSecp256r1Sha256Bytes.py} | 4 +- ...y => VerifyWithECDsaSecp256r1Sha256Int.py} | 4 +- ...WithECDsaSecp256r1Sha256MismatchedType.py} | 4 +- .../VerifyWithECDsaSecp256r1Sha256Str.py | 6 ++ .../crypto/VerifyWithECDsaSecp256r1Str.py | 6 -- .../native_test/cryptolib/Keccak256.py | 7 ++ .../native_test/cryptolib/VerifyWithECDsa.py | 4 +- .../VerifyWithECDsaSecp256k1Keccak256.py | 8 ++ .../VerifyWithECDsaSecp256k1Sha256Bool.py} | 4 +- ...=> VerifyWithECDsaSecp256k1Sha256Bytes.py} | 4 +- ...y => VerifyWithECDsaSecp256k1Sha256Int.py} | 4 +- ...WithECDsaSecp256k1Sha256MismatchedType.py} | 4 +- .../VerifyWithECDsaSecp256k1Sha256Str.py | 6 ++ .../cryptolib/VerifyWithECDsaSecp256k1Str.py | 6 -- .../VerifyWithECDsaSecp256r1Keccak256.py | 8 ++ .../VerifyWithECDsaSecp256r1Sha256Bool.py} | 4 +- .../VerifyWithECDsaSecp256r1Sha256Bytes.py} | 4 +- ...y => VerifyWithECDsaSecp256r1Sha256Int.py} | 4 +- ...WithECDsaSecp256r1Sha256MismatchedType.py} | 4 +- .../VerifyWithECDsaSecp256r1Sha256Str.py | 6 ++ .../cryptolib/VerifyWithECDsaSecp256r1Str.py | 6 -- .../test_interop/test_crypto.py | 24 ++--- .../test_native/test_cryptolib.py | 95 ++++++++++++++++--- 45 files changed, 275 insertions(+), 142 deletions(-) create mode 100644 boa3/internal/model/builtin/interop/crypto/keccak256method.py rename boa3/internal/model/builtin/interop/crypto/{namedcurvetype.py => namedcurvehashtype.py} (72%) delete mode 100644 boa3/internal/neo3/contracts/namedcurve.py create mode 100644 boa3/internal/neo3/contracts/namedcurvehash.py rename boa3_test/test_sc/{native_test/cryptolib/VerifyWithECDsaSecp256k1Bool.py => interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Bool.py} (50%) rename boa3_test/test_sc/{native_test/cryptolib/VerifyWithECDsaSecp256r1Bytes.py => interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Bytes.py} (56%) rename boa3_test/test_sc/interop_test/crypto/{VerifyWithECDsaSecp256k1Int.py => VerifyWithECDsaSecp256k1Sha256Int.py} (50%) rename boa3_test/test_sc/{native_test/cryptolib/VerifyWithECDsaSecp256k1MismatchedType.py => interop_test/crypto/VerifyWithECDsaSecp256k1Sha256MismatchedType.py} (62%) rename boa3_test/test_sc/interop_test/crypto/{VerifyWithECDsaSecp256k1Str.py => VerifyWithECDsaSecp256k1Sha256Str.py} (57%) rename boa3_test/test_sc/{native_test/cryptolib/VerifyWithECDsaSecp256r1Bool.py => interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Bool.py} (50%) rename boa3_test/test_sc/interop_test/crypto/{VerifyWithECDsaSecp256r1Bytes.py => VerifyWithECDsaSecp256r1Sha256Bytes.py} (56%) rename boa3_test/test_sc/interop_test/crypto/{VerifyWithECDsaSecp256r1Int.py => VerifyWithECDsaSecp256r1Sha256Int.py} (50%) rename boa3_test/test_sc/interop_test/crypto/{VerifyWithECDsaSecp256k1MismatchedType.py => VerifyWithECDsaSecp256r1Sha256MismatchedType.py} (62%) create mode 100644 boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Str.py delete mode 100644 boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Str.py create mode 100644 boa3_test/test_sc/native_test/cryptolib/Keccak256.py create mode 100644 boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Keccak256.py rename boa3_test/test_sc/{interop_test/crypto/VerifyWithECDsaSecp256k1Bool.py => native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Bool.py} (50%) rename boa3_test/test_sc/native_test/cryptolib/{VerifyWithECDsaSecp256k1Bytes.py => VerifyWithECDsaSecp256k1Sha256Bytes.py} (56%) rename boa3_test/test_sc/native_test/cryptolib/{VerifyWithECDsaSecp256k1Int.py => VerifyWithECDsaSecp256k1Sha256Int.py} (50%) rename boa3_test/test_sc/native_test/cryptolib/{VerifyWithECDsaSecp256r1MismatchedType.py => VerifyWithECDsaSecp256k1Sha256MismatchedType.py} (62%) create mode 100644 boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Str.py delete mode 100644 boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Str.py create mode 100644 boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Keccak256.py rename boa3_test/test_sc/{interop_test/crypto/VerifyWithECDsaSecp256r1Bool.py => native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Bool.py} (50%) rename boa3_test/test_sc/{interop_test/crypto/VerifyWithECDsaSecp256k1Bytes.py => native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Bytes.py} (56%) rename boa3_test/test_sc/native_test/cryptolib/{VerifyWithECDsaSecp256r1Int.py => VerifyWithECDsaSecp256r1Sha256Int.py} (50%) rename boa3_test/test_sc/{interop_test/crypto/VerifyWithECDsaSecp256r1MismatchedType.py => native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256MismatchedType.py} (62%) create mode 100644 boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Str.py delete mode 100644 boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Str.py diff --git a/boa3/builtin/interop/crypto/__init__.py b/boa3/builtin/interop/crypto/__init__.py index cfa1d3104..e4a960d4c 100644 --- a/boa3/builtin/interop/crypto/__init__.py +++ b/boa3/builtin/interop/crypto/__init__.py @@ -1,5 +1,5 @@ __all__ = [ - 'NamedCurve', + 'NamedCurveHash', 'IBls12381', 'sha256', 'ripemd160', @@ -22,7 +22,7 @@ from deprecation import deprecated from boa3.builtin.interop.crypto.ibls12381 import IBls12381 -from boa3.builtin.interop.crypto.namedcurve import NamedCurve +from boa3.builtin.interop.crypto.namedcurve import NamedCurveHash from boa3.builtin.type import ECPoint @@ -142,12 +142,12 @@ def check_multisig(pubkeys: list[ECPoint], signatures: list[bytes]) -> bool: @deprecated(details='This module is deprecated. Use CryptoLib from boa3.sc.contracts instead') -def verify_with_ecdsa(message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurve) -> bool: +def verify_with_ecdsa(message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurveHash) -> bool: """ Using the elliptic curve, it checks if the signature of the message was originally produced by the public key. >>> verify_with_ecdsa(b'unit test', ECPoint(b'\\x03\\x5a\\x92\\x8f\\x20\\x16\\x39\\x20\\x4e\\x06\\xb4\\x36\\x8b\\x1a\\x93\\x36\\x54\\x62\\xa8\\xeb\\xbf\\xf0\\xb8\\x81\\x81\\x51\\xb7\\x4f\\xaa\\xb3\\xa2\\xb6\\x1a'), - ... b'wrong_signature', NamedCurve.SECP256R1) + ... b'wrong_signature', NamedCurveHash.SECP256R1SHA256) False :param message: the encrypted message @@ -157,7 +157,7 @@ def verify_with_ecdsa(message: bytes, pubkey: ECPoint, signature: bytes, curve: :param signature: the signature of the item :type signature: bytes :param curve: the curve that will be used by the ecdsa - :type curve: NamedCurve + :type curve: NamedCurveHash :return: a boolean value that represents whether the signature is valid :rtype: bool """ diff --git a/boa3/builtin/interop/crypto/namedcurve.py b/boa3/builtin/interop/crypto/namedcurve.py index b29a68c8e..6140b3688 100644 --- a/boa3/builtin/interop/crypto/namedcurve.py +++ b/boa3/builtin/interop/crypto/namedcurve.py @@ -1,3 +1,3 @@ -from boa3.internal.neo3.contracts.namedcurve import NamedCurve +from boa3.internal.neo3.contracts.namedcurvehash import NamedCurveHash -__all__ = ['NamedCurve'] +__all__ = ['NamedCurveHash'] diff --git a/boa3/builtin/nativecontract/cryptolib.py b/boa3/builtin/nativecontract/cryptolib.py index 28d43015a..d5cd8c6cd 100644 --- a/boa3/builtin/nativecontract/cryptolib.py +++ b/boa3/builtin/nativecontract/cryptolib.py @@ -1,6 +1,6 @@ __all__ = [ 'CryptoLib', - 'NamedCurve', + 'NamedCurveHash', 'IBls12381' ] @@ -8,7 +8,7 @@ from deprecation import deprecated -from boa3.builtin.interop.crypto import NamedCurve, IBls12381 +from boa3.builtin.interop.crypto import NamedCurveHash, IBls12381 from boa3.builtin.type import ECPoint, UInt160 @@ -77,12 +77,12 @@ def ripemd160(cls, key: Any) -> bytes: pass @classmethod - def verify_with_ecdsa(cls, message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurve) -> bool: + def verify_with_ecdsa(cls, message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurveHash) -> bool: """ Using the elliptic curve, it checks if the signature of the message was originally produced by the public key. >>> CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'\\x03\\x5a\\x92\\x8f\\x20\\x16\\x39\\x20\\x4e\\x06\\xb4\\x36\\x8b\\x1a\\x93\\x36\\x54\\x62\\xa8\\xeb\\xbf\\xf0\\xb8\\x81\\x81\\x51\\xb7\\x4f\\xaa\\xb3\\xa2\\xb6\\x1a'), - ... b'wrong_signature', NamedCurve.SECP256R1) + ... b'wrong_signature', NamedCurveHash.SECP256R1SHA256) False :param message: the encrypted message @@ -92,7 +92,7 @@ def verify_with_ecdsa(cls, message: bytes, pubkey: ECPoint, signature: bytes, cu :param signature: the signature of the item :type signature: bytes :param curve: the curve that will be used by the ecdsa - :type curve: NamedCurve + :type curve: NamedCurveHash :return: a boolean value that represents whether the signature is valid :rtype: bool """ diff --git a/boa3/internal/model/builtin/interop/crypto/__init__.py b/boa3/internal/model/builtin/interop/crypto/__init__.py index 79dc0df17..5402d21db 100644 --- a/boa3/internal/model/builtin/interop/crypto/__init__.py +++ b/boa3/internal/model/builtin/interop/crypto/__init__.py @@ -10,8 +10,9 @@ 'CheckSigMethod', 'Hash160Method', 'Hash256Method', + 'Keccak256Method', 'Murmur32Method', - 'NamedCurveType', + 'NamedCurveHashType', 'Ripemd160Method', 'Sha256Method', 'VerifyWithECDsaMethod', @@ -28,8 +29,9 @@ from boa3.internal.model.builtin.interop.crypto.checksigmethod import CheckSigMethod from boa3.internal.model.builtin.interop.crypto.hash160method import Hash160Method from boa3.internal.model.builtin.interop.crypto.hash256method import Hash256Method +from boa3.internal.model.builtin.interop.crypto.keccak256method import Keccak256Method from boa3.internal.model.builtin.interop.crypto.murmur32method import Murmur32Method -from boa3.internal.model.builtin.interop.crypto.namedcurvetype import NamedCurveType +from boa3.internal.model.builtin.interop.crypto.namedcurvehashtype import NamedCurveHashType from boa3.internal.model.builtin.interop.crypto.ripemd160method import Ripemd160Method from boa3.internal.model.builtin.interop.crypto.sha256method import Sha256Method from boa3.internal.model.builtin.interop.crypto.verifywithecdsa import VerifyWithECDsaMethod diff --git a/boa3/internal/model/builtin/interop/crypto/keccak256method.py b/boa3/internal/model/builtin/interop/crypto/keccak256method.py new file mode 100644 index 000000000..eef691b52 --- /dev/null +++ b/boa3/internal/model/builtin/interop/crypto/keccak256method.py @@ -0,0 +1,12 @@ +from boa3.internal.model.builtin.interop.nativecontract import CryptoLibMethod +from boa3.internal.model.variable import Variable + + +class Keccak256Method(CryptoLibMethod): + + def __init__(self): + from boa3.internal.model.type.type import Type + identifier = 'keccak256' + native_identifier = 'keccak256' + args: dict[str, Variable] = {'key': Variable(Type.bytes)} + super().__init__(identifier, native_identifier, args, return_type=Type.bytes) diff --git a/boa3/internal/model/builtin/interop/crypto/namedcurvetype.py b/boa3/internal/model/builtin/interop/crypto/namedcurvehashtype.py similarity index 72% rename from boa3/internal/model/builtin/interop/crypto/namedcurvetype.py rename to boa3/internal/model/builtin/interop/crypto/namedcurvehashtype.py index c2f23d4c8..e0c925cd1 100644 --- a/boa3/internal/model/builtin/interop/crypto/namedcurvetype.py +++ b/boa3/internal/model/builtin/interop/crypto/namedcurvehashtype.py @@ -3,17 +3,17 @@ from boa3.internal.model.symbol import ISymbol from boa3.internal.model.type.itype import IType from boa3.internal.model.type.primitive.inttype import IntType -from boa3.internal.neo3.contracts.namedcurve import NamedCurve +from boa3.internal.neo3.contracts.namedcurvehash import NamedCurveHash -class NamedCurveType(IntType): +class NamedCurveHashType(IntType): """ - A class used to represent Neo NamedCurve type + A class used to represent Neo NamedCurveHash type """ def __init__(self): super().__init__() - self._identifier = 'NamedCurve' + self._identifier = 'NamedCurveHash' @classmethod def build(cls, value: Any = None) -> IType: @@ -22,7 +22,7 @@ def build(cls, value: Any = None) -> IType: @classmethod def _is_type_of(cls, value: Any): - return isinstance(value, (NamedCurve, NamedCurveType)) + return isinstance(value, (NamedCurveHash, NamedCurveHashType)) @property def symbols(self) -> dict[str, ISymbol]: @@ -34,7 +34,7 @@ def symbols(self) -> dict[str, ISymbol]: from boa3.internal.model.variable import Variable _symbols = super().symbols - _symbols.update({name: Variable(self) for name in NamedCurve.__members__.keys()}) + _symbols.update({name: Variable(self) for name in NamedCurveHash.__members__.keys()}) return _symbols @@ -44,10 +44,10 @@ def get_value(self, symbol_id) -> Any: :return: the value if this type has this symbol. None otherwise. """ - if symbol_id in self.symbols and symbol_id in NamedCurve.__members__: - return NamedCurve.__members__[symbol_id] + if symbol_id in self.symbols and symbol_id in NamedCurveHash.__members__: + return NamedCurveHash.__members__[symbol_id] return None -_NamedCurve = NamedCurveType() +_NamedCurve = NamedCurveHashType() diff --git a/boa3/internal/model/builtin/interop/crypto/verifywithecdsa.py b/boa3/internal/model/builtin/interop/crypto/verifywithecdsa.py index e6f1a9964..423efdceb 100644 --- a/boa3/internal/model/builtin/interop/crypto/verifywithecdsa.py +++ b/boa3/internal/model/builtin/interop/crypto/verifywithecdsa.py @@ -7,7 +7,7 @@ class VerifyWithECDsaMethod(CryptoLibMethod): def __init__(self): from boa3.internal.model.type.type import Type from boa3.internal.model.type.collection.sequence.ecpointtype import ECPointType - from boa3.internal.model.builtin.interop.crypto.namedcurvetype import NamedCurveType + from boa3.internal.model.builtin.interop.crypto.namedcurvehashtype import NamedCurveHashType identifier = 'verify_with_ecdsa' native_identifier = 'verifyWithECDsa' @@ -15,6 +15,6 @@ def __init__(self): 'data': Variable(Type.bytes), 'pubkey': Variable(ECPointType.build()), 'signature': Variable(Type.bytes), - 'curve': Variable(NamedCurveType.build()) + 'curve': Variable(NamedCurveHashType.build()) } super().__init__(identifier, native_identifier, args, return_type=Type.bool) diff --git a/boa3/internal/model/builtin/interop/interop.py b/boa3/internal/model/builtin/interop/interop.py index b7001588e..d2dfb9f9a 100644 --- a/boa3/internal/model/builtin/interop/interop.py +++ b/boa3/internal/model/builtin/interop/interop.py @@ -72,7 +72,7 @@ def interop_events(cls) -> list[Event]: ContractType = ContractType.build() FindOptionsType = FindOptionsType() Iterator = IteratorType.build() - NamedCurveType = NamedCurveType() + NamedCurveHashType = NamedCurveHashType() NotificationType = NotificationType.build() OracleResponseCode = OracleResponseCodeType.build() OracleType = OracleClass.build() @@ -130,6 +130,7 @@ def interop_events(cls) -> list[Event]: CheckSig = CheckSigMethod() Hash160 = Hash160Method() Hash256 = Hash256Method() + Keccak256 = Keccak256Method() Murmur32 = Murmur32Method() Ripemd160 = Ripemd160Method() Sha256 = Sha256Method() @@ -337,7 +338,7 @@ def interop_events(cls) -> list[Event]: CryptoPackage = Package(deprecated=True, new_location='boa3.sc.contracts, boa3.sc.utils and boa3.sc.types', identifier=InteropPackage.Crypto, - types=[NamedCurveType, + types=[NamedCurveHashType, Bls12381Type ], methods=[Bls12381Add, diff --git a/boa3/internal/model/builtin/native/cryptolibclass.py b/boa3/internal/model/builtin/native/cryptolibclass.py index 9e27373b2..dd263d9bd 100644 --- a/boa3/internal/model/builtin/native/cryptolibclass.py +++ b/boa3/internal/model/builtin/native/cryptolibclass.py @@ -23,6 +23,7 @@ def class_methods(self) -> dict[str, Method]: 'murmur32': Interop.Murmur32, 'sha256': Interop.Sha256, 'ripemd160': Interop.Ripemd160, + 'keccak256': Interop.Keccak256, 'verify_with_ecdsa': Interop.VerifyWithECDsa, 'bls12_381_add': Interop.Bls12381Add, 'bls12_381_deserialize': Interop.Bls12381Deserialize, diff --git a/boa3/internal/model/builtin/native/nativecontract.py b/boa3/internal/model/builtin/native/nativecontract.py index f6e6647f8..10261f492 100644 --- a/boa3/internal/model/builtin/native/nativecontract.py +++ b/boa3/internal/model/builtin/native/nativecontract.py @@ -37,7 +37,7 @@ class NativeContract: new_location='boa3.sc.contracts', identifier=CryptoLib.identifier.lower(), types=[CryptoLib, - Interop.NamedCurveType, + Interop.NamedCurveHashType, Interop.Bls12381Type]) GasModule = Package(deprecated=True, diff --git a/boa3/internal/model/sc/__init__.py b/boa3/internal/model/sc/__init__.py index 8bc0391af..79e5ebb45 100644 --- a/boa3/internal/model/sc/__init__.py +++ b/boa3/internal/model/sc/__init__.py @@ -269,7 +269,7 @@ def package_symbols(cls, package: str = None) -> Package | None: Builtin.Nep17Contract, Builtin.Opcode, Interop.FindOptionsType, - Interop.NamedCurveType, + Interop.NamedCurveHashType, Interop.Bls12381Type, Interop.RoleType, Interop.NotificationType, diff --git a/boa3/internal/neo3/contracts/namedcurve.py b/boa3/internal/neo3/contracts/namedcurve.py deleted file mode 100644 index 257063be2..000000000 --- a/boa3/internal/neo3/contracts/namedcurve.py +++ /dev/null @@ -1,24 +0,0 @@ -from enum import IntFlag - - -class NamedCurve(IntFlag): - """ - Represents the named curve used in ECDSA. - - Check out `Neo's Documentation `__ - to learn more about ECDSA signing. - """ - - SECP256K1 = 22 - """ - The secp256k1 curve. - - :meta hide-value: - """ - - SECP256R1 = 23 - """ - The secp256r1 curve, which known as prime256v1 or nistP-256. - - :meta hide-value: - """ diff --git a/boa3/internal/neo3/contracts/namedcurvehash.py b/boa3/internal/neo3/contracts/namedcurvehash.py new file mode 100644 index 000000000..30f81b58f --- /dev/null +++ b/boa3/internal/neo3/contracts/namedcurvehash.py @@ -0,0 +1,38 @@ +from enum import IntFlag + + +class NamedCurveHash(IntFlag): + """ + Represents the named curve used in ECDSA. + + Check out `Neo's Documentation `__ + to learn more about ECDSA signing. + """ + + SECP256K1SHA256 = 22 + """ + The secp256k1 curve and SHA256 hash algorithm. + + :meta hide-value: + """ + + SECP256R1SHA256 = 23 + """ + The secp256r1 curve, which known as prime256v1 or nistP-256, and SHA256 hash algorithm. + + :meta hide-value: + """ + + SECP256K1KECCAK256 = 122 + """ + The secp256k1 curve and Keccak256 hash algorithm. + + :meta hide-value: + """ + + SECP256R1KECCAK256 = 123 + """ + The secp256r1 curve, which known as prime256v1 or nistP-256, and Keccak256 hash algorithm. + + :meta hide-value: + """ diff --git a/boa3/sc/contracts/cryptolib.py b/boa3/sc/contracts/cryptolib.py index 07aae09f8..f623b7250 100644 --- a/boa3/sc/contracts/cryptolib.py +++ b/boa3/sc/contracts/cryptolib.py @@ -4,7 +4,7 @@ from typing import Any -from boa3.sc.types import ECPoint, UInt160, NamedCurve, IBls12381 +from boa3.sc.types import ECPoint, UInt160, NamedCurveHash, IBls12381 class CryptoLib: @@ -71,12 +71,27 @@ def ripemd160(cls, key: Any) -> bytes: pass @classmethod - def verify_with_ecdsa(cls, message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurve) -> bool: + def keccak256(cls, data: bytes) -> bytes: + """ + Computes the hash value for the specified byte array using the keccak256 algorithm. + + >>> CryptoLib.keccak256(b'unit test') + b'\\xe5\\x26\\x91\\x5a\\xff\\x6f\\x5e\\x35\\x9d\\x64\\xa3\\xce\\xf0\\x6e\\xf3\\xdb\\x9f\\x4a\\x89\\x0e\\x20\\xd1\\xa5\\x19\\x5e\\x3a\\x24\\x29\\x78\\x7e\\xec\\xb7' + + :param data: the input to compute the hash code for + :type data: bytes + :return: computed hash + :rtype: bytes + """ + pass + + @classmethod + def verify_with_ecdsa(cls, message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurveHash) -> bool: """ Using the elliptic curve, it checks if the signature of the message was originally produced by the public key. >>> CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'\\x03\\x5a\\x92\\x8f\\x20\\x16\\x39\\x20\\x4e\\x06\\xb4\\x36\\x8b\\x1a\\x93\\x36\\x54\\x62\\xa8\\xeb\\xbf\\xf0\\xb8\\x81\\x81\\x51\\xb7\\x4f\\xaa\\xb3\\xa2\\xb6\\x1a'), - ... b'wrong_signature', NamedCurve.SECP256R1) + ... b'wrong_signature', NamedCurveHash.SECP256R1SHA256) False :param message: the encrypted message @@ -86,7 +101,7 @@ def verify_with_ecdsa(cls, message: bytes, pubkey: ECPoint, signature: bytes, cu :param signature: the signature of the item :type signature: bytes :param curve: the curve that will be used by the ecdsa - :type curve: NamedCurve + :type curve: NamedCurveHash :return: a boolean value that represents whether the signature is valid :rtype: bool """ diff --git a/boa3/sc/types/__init__.py b/boa3/sc/types/__init__.py index e575a2736..2e588cd2f 100644 --- a/boa3/sc/types/__init__.py +++ b/boa3/sc/types/__init__.py @@ -32,7 +32,7 @@ 'Nep17Contract', 'Opcode', 'FindOptions', - 'NamedCurve', + 'NamedCurveHash', 'IBls12381', 'Role', 'Notification', @@ -50,7 +50,7 @@ from boa3.internal.neo3.contracts import CallFlags from boa3.internal.neo3.contracts import TriggerType from boa3.internal.neo3.contracts.findoptions import FindOptions -from boa3.internal.neo3.contracts.namedcurve import NamedCurve +from boa3.internal.neo3.contracts.namedcurvehash import NamedCurveHash from boa3.internal.neo3.contracts.native import Role from boa3.internal.neo3.network.payloads import OracleResponseCode from boa3.internal.neo3.network.payloads.verification import WitnessScope, WitnessRuleAction, WitnessConditionType diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsa.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsa.py index 994e632ec..7325fd197 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsa.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsa.py @@ -1,8 +1,8 @@ from boa3.sc.compiletime import public from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash @public -def Main(message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurve) -> bool: +def Main(message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurveHash) -> bool: return CryptoLib.verify_with_ecdsa(message, pubkey, signature, curve) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Bool.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Bool.py similarity index 50% rename from boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Bool.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Bool.py index c50ab5860..bd63e4f8b 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Bool.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Bool.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa(False, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa(False, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Bytes.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Bytes.py similarity index 56% rename from boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Bytes.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Bytes.py index 74e466152..4c0b99061 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Bytes.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Bytes.py @@ -1,8 +1,8 @@ from boa3.sc.compiletime import public from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash @public def Main(): - CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256R1) + CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Int.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Int.py similarity index 50% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Int.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Int.py index 1401b380a..bef15f6bf 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Int.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Int.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa(10, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa(10, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1MismatchedType.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256MismatchedType.py similarity index 62% rename from boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1MismatchedType.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256MismatchedType.py index f60096ffc..a002b6f54 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1MismatchedType.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256MismatchedType.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import NamedCurve +from boa3.sc.types import NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa('unit test', 10, b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa('unit test', 10, b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Str.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Str.py similarity index 57% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Str.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Str.py index b23fd3295..06b29491f 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Str.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Sha256Str.py @@ -1,8 +1,8 @@ from boa3.sc.compiletime import public from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash @public def Main(): - CryptoLib.verify_with_ecdsa('unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa('unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Bool.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Bool.py similarity index 50% rename from boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Bool.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Bool.py index d1e0e85aa..15ce2592d 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Bool.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Bool.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa(False, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256R1) + CryptoLib.verify_with_ecdsa(False, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Bytes.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Bytes.py similarity index 56% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Bytes.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Bytes.py index 74e466152..4d4a3aa4c 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Bytes.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Bytes.py @@ -1,8 +1,8 @@ from boa3.sc.compiletime import public from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash @public def Main(): - CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256R1) + CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Int.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Int.py similarity index 50% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Int.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Int.py index 94f17398e..11071afb1 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Int.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Int.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa(10, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256R1) + CryptoLib.verify_with_ecdsa(10, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1MismatchedType.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256MismatchedType.py similarity index 62% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1MismatchedType.py rename to boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256MismatchedType.py index f60096ffc..2e281450c 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1MismatchedType.py +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256MismatchedType.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import NamedCurve +from boa3.sc.types import NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa('unit test', 10, b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa('unit test', 10, b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Str.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Str.py new file mode 100644 index 000000000..43f7f0a90 --- /dev/null +++ b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Sha256Str.py @@ -0,0 +1,6 @@ +from boa3.sc.contracts import CryptoLib +from boa3.sc.types import ECPoint, NamedCurveHash + + +def Main(): + CryptoLib.verify_with_ecdsa('unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Str.py b/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Str.py deleted file mode 100644 index e0a3ae2a1..000000000 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Str.py +++ /dev/null @@ -1,6 +0,0 @@ -from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve - - -def Main(): - CryptoLib.verify_with_ecdsa('unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256R1) diff --git a/boa3_test/test_sc/native_test/cryptolib/Keccak256.py b/boa3_test/test_sc/native_test/cryptolib/Keccak256.py new file mode 100644 index 000000000..6321f893f --- /dev/null +++ b/boa3_test/test_sc/native_test/cryptolib/Keccak256.py @@ -0,0 +1,7 @@ +from boa3.sc.compiletime import public +from boa3.sc.contracts import CryptoLib + + +@public +def main(data: bytes) -> bytes: + return CryptoLib.keccak256(data) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsa.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsa.py index 994e632ec..7325fd197 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsa.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsa.py @@ -1,8 +1,8 @@ from boa3.sc.compiletime import public from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash @public -def Main(message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurve) -> bool: +def Main(message: bytes, pubkey: ECPoint, signature: bytes, curve: NamedCurveHash) -> bool: return CryptoLib.verify_with_ecdsa(message, pubkey, signature, curve) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Keccak256.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Keccak256.py new file mode 100644 index 000000000..d14db5a19 --- /dev/null +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Keccak256.py @@ -0,0 +1,8 @@ +from boa3.sc.compiletime import public +from boa3.sc.contracts import CryptoLib +from boa3.sc.types import ECPoint, NamedCurveHash + + +@public +def Main(): + CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1KECCAK256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Bool.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Bool.py similarity index 50% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Bool.py rename to boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Bool.py index c50ab5860..bd63e4f8b 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Bool.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Bool.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa(False, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa(False, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Bytes.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Bytes.py similarity index 56% rename from boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Bytes.py rename to boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Bytes.py index c2dcca37a..4c0b99061 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Bytes.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Bytes.py @@ -1,8 +1,8 @@ from boa3.sc.compiletime import public from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash @public def Main(): - CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Int.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Int.py similarity index 50% rename from boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Int.py rename to boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Int.py index 1401b380a..bef15f6bf 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Int.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Int.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa(10, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa(10, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1MismatchedType.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256MismatchedType.py similarity index 62% rename from boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1MismatchedType.py rename to boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256MismatchedType.py index 7ee40e5e8..a002b6f54 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1MismatchedType.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256MismatchedType.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import NamedCurve +from boa3.sc.types import NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa('unit test', 10, b'signature', NamedCurve.SECP256R1) + CryptoLib.verify_with_ecdsa('unit test', 10, b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Str.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Str.py new file mode 100644 index 000000000..c6b7d96ca --- /dev/null +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Sha256Str.py @@ -0,0 +1,6 @@ +from boa3.sc.contracts import CryptoLib +from boa3.sc.types import ECPoint, NamedCurveHash + + +def Main(): + CryptoLib.verify_with_ecdsa('unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256K1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Str.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Str.py deleted file mode 100644 index a9687ac01..000000000 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256k1Str.py +++ /dev/null @@ -1,6 +0,0 @@ -from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve - - -def Main(): - CryptoLib.verify_with_ecdsa('unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256K1) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Keccak256.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Keccak256.py new file mode 100644 index 000000000..3aa1ebcf0 --- /dev/null +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Keccak256.py @@ -0,0 +1,8 @@ +from boa3.sc.compiletime import public +from boa3.sc.contracts import CryptoLib +from boa3.sc.types import ECPoint, NamedCurveHash + + +@public +def Main(): + CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1KECCAK256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Bool.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Bool.py similarity index 50% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Bool.py rename to boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Bool.py index d1e0e85aa..15ce2592d 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1Bool.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Bool.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa(False, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256R1) + CryptoLib.verify_with_ecdsa(False, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Bytes.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Bytes.py similarity index 56% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Bytes.py rename to boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Bytes.py index c2dcca37a..4d4a3aa4c 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256k1Bytes.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Bytes.py @@ -1,8 +1,8 @@ from boa3.sc.compiletime import public from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash @public def Main(): - CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256K1) + CryptoLib.verify_with_ecdsa(b'unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Int.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Int.py similarity index 50% rename from boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Int.py rename to boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Int.py index 94f17398e..11071afb1 100644 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Int.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Int.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve +from boa3.sc.types import ECPoint, NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa(10, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256R1) + CryptoLib.verify_with_ecdsa(10, ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1MismatchedType.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256MismatchedType.py similarity index 62% rename from boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1MismatchedType.py rename to boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256MismatchedType.py index 7ee40e5e8..2e281450c 100644 --- a/boa3_test/test_sc/interop_test/crypto/VerifyWithECDsaSecp256r1MismatchedType.py +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256MismatchedType.py @@ -1,6 +1,6 @@ from boa3.sc.contracts import CryptoLib -from boa3.sc.types import NamedCurve +from boa3.sc.types import NamedCurveHash def Main(): - CryptoLib.verify_with_ecdsa('unit test', 10, b'signature', NamedCurve.SECP256R1) + CryptoLib.verify_with_ecdsa('unit test', 10, b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Str.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Str.py new file mode 100644 index 000000000..43f7f0a90 --- /dev/null +++ b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Sha256Str.py @@ -0,0 +1,6 @@ +from boa3.sc.contracts import CryptoLib +from boa3.sc.types import ECPoint, NamedCurveHash + + +def Main(): + CryptoLib.verify_with_ecdsa('unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurveHash.SECP256R1SHA256) diff --git a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Str.py b/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Str.py deleted file mode 100644 index e0a3ae2a1..000000000 --- a/boa3_test/test_sc/native_test/cryptolib/VerifyWithECDsaSecp256r1Str.py +++ /dev/null @@ -1,6 +0,0 @@ -from boa3.sc.contracts import CryptoLib -from boa3.sc.types import ECPoint, NamedCurve - - -def Main(): - CryptoLib.verify_with_ecdsa('unit test', ECPoint(b'0123456789ABCDEFGHIJKLMNOPQRSTUVW'), b'signature', NamedCurve.SECP256R1) diff --git a/boa3_test/tests/compiler_tests/test_interop/test_crypto.py b/boa3_test/tests/compiler_tests/test_interop/test_crypto.py index 47bb27007..e530ed9b4 100644 --- a/boa3_test/tests/compiler_tests/test_interop/test_crypto.py +++ b/boa3_test/tests/compiler_tests/test_interop/test_crypto.py @@ -4,7 +4,7 @@ from boa3.internal.model.type.type import Type from boa3.internal.neo.vm.opcode.Opcode import Opcode from boa3.internal.neo.vm.type.Integer import Integer -from boa3.internal.neo3.contracts.namedcurve import NamedCurve +from boa3.internal.neo3.contracts.namedcurvehash import NamedCurveHash from boa3_test.tests import boatestcase @@ -247,34 +247,34 @@ def test_verify_with_ecdsa(self): self.compile(path) def test_verify_with_ecdsa_secp256r1_str(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Str.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Sha256Str.py') def test_verify_with_ecdsa_secp256r1_bool(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Bool.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Sha256Bool.py') def test_verify_with_ecdsa_secp256r1_int(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Int.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Sha256Int.py') def test_verify_with_ecdsa_secp256r1_bytes(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Bool.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256Bool.py') def test_verify_with_ecdsa_secp256r1_mismatched_type(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1MismatchedType.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Sha256MismatchedType.py') def test_verify_with_ecdsa_secp256k1_str(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Str.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256Str.py') def test_verify_with_ecdsa_secp256k1_bool(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Bool.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256Bool.py') def test_verify_with_ecdsa_secp256k1_int(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Int.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256Int.py') def test_verify_with_ecdsa_secp256k1_bytes(self): byte_input1 = b'0123456789ABCDEFGHIJKLMNOPQRSTUVW' byte_input2 = b'signature' string = b'unit test' - named_curve = Integer(NamedCurve.SECP256K1).to_byte_array(signed=True, min_length=1) + named_curve = Integer(NamedCurveHash.SECP256K1SHA256).to_byte_array(signed=True, min_length=1) expected_output = ( Opcode.PUSHINT8 + named_curve @@ -293,11 +293,11 @@ def test_verify_with_ecdsa_secp256k1_bytes(self): + Opcode.RET ) - output, _ = self.assertCompile('VerifyWithECDsaSecp256k1Bytes.py') + output, _ = self.assertCompile('VerifyWithECDsaSecp256k1Sha256Bytes.py') self.assertEqual(expected_output, output) def test_verify_with_ecdsa_secp256k1_mismatched_type(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1MismatchedType.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256MismatchedType.py') async def test_import_crypto(self): self.assertCompilerLogs(CompilerWarning.DeprecatedSymbol, 'ImportCrypto.py') diff --git a/boa3_test/tests/compiler_tests/test_native/test_cryptolib.py b/boa3_test/tests/compiler_tests/test_native/test_cryptolib.py index 80b1383f4..bc93afa67 100644 --- a/boa3_test/tests/compiler_tests/test_native/test_cryptolib.py +++ b/boa3_test/tests/compiler_tests/test_native/test_cryptolib.py @@ -5,7 +5,7 @@ from boa3.internal.model.type.type import Type from boa3.internal.neo.vm.opcode.Opcode import Opcode from boa3.internal.neo.vm.type.Integer import Integer -from boa3.internal.neo3.contracts.namedcurve import NamedCurve +from boa3.internal.neo3.contracts.namedcurvehash import NamedCurveHash from boa3_test.tests import boatestcase @@ -120,19 +120,19 @@ def test_verify_with_ecdsa(self): self.compile(path) def test_verify_with_ecdsa_secp256r1_str(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Str.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Sha256Str.py') def test_verify_with_ecdsa_secp256r1_bool(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Bool.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Sha256Bool.py') def test_verify_with_ecdsa_secp256r1_int(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Int.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Sha256Int.py') - def test_verify_with_ecdsa_secp256r1_bytes(self): + def test_verify_with_ecdsa_secp256r1sha256_bytes(self): byte_input1 = b'0123456789ABCDEFGHIJKLMNOPQRSTUVW' byte_input2 = b'signature' string = b'unit test' - named_curve = Integer(NamedCurve.SECP256R1).to_byte_array(signed=True, min_length=1) + named_curve = Integer(NamedCurveHash.SECP256R1SHA256).to_byte_array(signed=True, min_length=1) expected_output = ( Opcode.PUSHINT8 + named_curve @@ -151,26 +151,52 @@ def test_verify_with_ecdsa_secp256r1_bytes(self): + Opcode.RET ) - output, _ = self.assertCompile('VerifyWithECDsaSecp256r1Bytes.py') + output, _ = self.assertCompile('VerifyWithECDsaSecp256r1Sha256Bytes.py') + self.assertEqual(expected_output, output) + + def test_verify_with_ecdsa_secp256r1keccak256(self): + byte_input1 = b'0123456789ABCDEFGHIJKLMNOPQRSTUVW' + byte_input2 = b'signature' + string = b'unit test' + named_curve = Integer(NamedCurveHash.SECP256R1KECCAK256).to_byte_array(signed=True, min_length=1) + + expected_output = ( + Opcode.PUSHINT8 + named_curve + + Opcode.PUSHDATA1 + + Integer(len(byte_input2)).to_byte_array(min_length=1) + + byte_input2 + + Opcode.PUSHDATA1 + + Integer(len(byte_input1)).to_byte_array(min_length=1) + + byte_input1 + + self.ecpoint_init + + Opcode.PUSHDATA1 + + Integer(len(string)).to_byte_array(min_length=1) + + string + + Opcode.CALLT + b'\x00\x00' + + Opcode.DROP + + Opcode.RET + ) + + output, _ = self.assertCompile('VerifyWithECDsaSecp256r1Keccak256.py') self.assertEqual(expected_output, output) def test_verify_with_ecdsa_secp256r1_mismatched_type(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1MismatchedType.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256r1Sha256MismatchedType.py') def test_verify_with_ecdsa_secp256k1_str(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Str.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256Str.py') def test_verify_with_ecdsa_secp256k1_bool(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Bool.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256Bool.py') def test_verify_with_ecdsa_secp256k1_int(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Int.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256Int.py') - def test_verify_with_ecdsa_secp256k1_bytes(self): + def test_verify_with_ecdsa_secp256k1sha256_bytes(self): byte_input1 = b'0123456789ABCDEFGHIJKLMNOPQRSTUVW' byte_input2 = b'signature' string = b'unit test' - named_curve = Integer(NamedCurve.SECP256K1).to_byte_array(signed=True, min_length=1) + named_curve = Integer(NamedCurveHash.SECP256K1SHA256).to_byte_array(signed=True, min_length=1) expected_output = ( Opcode.PUSHINT8 + named_curve @@ -189,11 +215,37 @@ def test_verify_with_ecdsa_secp256k1_bytes(self): + Opcode.RET ) - output, _ = self.assertCompile('VerifyWithECDsaSecp256k1Bytes.py') + output, _ = self.assertCompile('VerifyWithECDsaSecp256k1Sha256Bytes.py') + self.assertEqual(expected_output, output) + + def test_verify_with_ecdsa_secp256k1keccak256(self): + byte_input1 = b'0123456789ABCDEFGHIJKLMNOPQRSTUVW' + byte_input2 = b'signature' + string = b'unit test' + named_curve = Integer(NamedCurveHash.SECP256K1KECCAK256).to_byte_array(signed=True, min_length=1) + + expected_output = ( + Opcode.PUSHINT8 + named_curve + + Opcode.PUSHDATA1 + + Integer(len(byte_input2)).to_byte_array(min_length=1) + + byte_input2 + + Opcode.PUSHDATA1 + + Integer(len(byte_input1)).to_byte_array(min_length=1) + + byte_input1 + + self.ecpoint_init + + Opcode.PUSHDATA1 + + Integer(len(string)).to_byte_array(min_length=1) + + string + + Opcode.CALLT + b'\x00\x00' + + Opcode.DROP + + Opcode.RET + ) + + output, _ = self.assertCompile('VerifyWithECDsaSecp256k1Keccak256.py') self.assertEqual(expected_output, output) def test_verify_with_ecdsa_secp256k1_mismatched_type(self): - self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1MismatchedType.py') + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'VerifyWithECDsaSecp256k1Sha256MismatchedType.py') def test_murmur32(self): expected_output = ( @@ -209,6 +261,19 @@ def test_murmur32(self): output, _ = self.assertCompile('Murmur32.py') self.assertEqual(expected_output, output) + async def test_keccak256(self): + expected_output = ( + Opcode.INITSLOT + + b'\x00' + + b'\x01' + + Opcode.LDARG0 + + Opcode.CALLT + b'\x00\x00' + + Opcode.RET + ) + + output, _ = self.assertCompile('Keccak256.py') + self.assertEqual(expected_output, output) + def test_bls12_381_add(self): expected_output = ( Opcode.INITSLOT