Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CU-86dthgzj6 - Update CryptoLib native contract #1257

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions boa3/builtin/interop/crypto/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = [
'NamedCurve',
'NamedCurveHash',
'IBls12381',
'sha256',
'ripemd160',
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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
"""
Expand Down
4 changes: 2 additions & 2 deletions boa3/builtin/interop/crypto/namedcurve.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from boa3.internal.neo3.contracts.namedcurve import NamedCurve
from boa3.internal.neo3.contracts.namedcurvehash import NamedCurveHash

__all__ = ['NamedCurve']
__all__ = ['NamedCurveHash']
10 changes: 5 additions & 5 deletions boa3/builtin/nativecontract/cryptolib.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
__all__ = [
'CryptoLib',
'NamedCurve',
'NamedCurveHash',
'IBls12381'
]

from typing import Any

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


Expand Down Expand Up @@ -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
Expand All @@ -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
"""
Expand Down
6 changes: 4 additions & 2 deletions boa3/internal/model/builtin/interop/crypto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
'CheckSigMethod',
'Hash160Method',
'Hash256Method',
'Keccak256Method',
'Murmur32Method',
'NamedCurveType',
'NamedCurveHashType',
'Ripemd160Method',
'Sha256Method',
'VerifyWithECDsaMethod',
Expand All @@ -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
12 changes: 12 additions & 0 deletions boa3/internal/model/builtin/interop/crypto/keccak256method.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]:
Expand All @@ -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

Expand All @@ -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()
4 changes: 2 additions & 2 deletions boa3/internal/model/builtin/interop/crypto/verifywithecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ 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'
args: dict[str, Variable] = {
'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)
5 changes: 3 additions & 2 deletions boa3/internal/model/builtin/interop/interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -130,6 +130,7 @@ def interop_events(cls) -> list[Event]:
CheckSig = CheckSigMethod()
Hash160 = Hash160Method()
Hash256 = Hash256Method()
Keccak256 = Keccak256Method()
Murmur32 = Murmur32Method()
Ripemd160 = Ripemd160Method()
Sha256 = Sha256Method()
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions boa3/internal/model/builtin/native/cryptolibclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion boa3/internal/model/builtin/native/nativecontract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion boa3/internal/model/sc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 0 additions & 24 deletions boa3/internal/neo3/contracts/namedcurve.py

This file was deleted.

38 changes: 38 additions & 0 deletions boa3/internal/neo3/contracts/namedcurvehash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from enum import IntFlag


class NamedCurveHash(IntFlag):
"""
Represents the named curve used in ECDSA.

Check out `Neo's Documentation <https://developers.neo.org/docs/n3/foundation/Cryptography/encryption_algorithm#ecdsa-signing>`__
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:
"""
23 changes: 19 additions & 4 deletions boa3/sc/contracts/cryptolib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
"""
Expand Down
4 changes: 2 additions & 2 deletions boa3/sc/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
'Nep17Contract',
'Opcode',
'FindOptions',
'NamedCurve',
'NamedCurveHash',
'IBls12381',
'Role',
'Notification',
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions boa3_test/test_sc/interop_test/crypto/VerifyWithECDsa.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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)
Loading
Loading