The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Remove the cryptography package dependency. (#16988)
comfyanonymous06c67806bcb3
diff --git a/app/governance.py b/app/governance.pyindex 247099806..2172818ed 100644--- a/app/governance.py+++ b/app/governance.py@@ -14,6 +14,8 @@ import unicodedataimport yaml+from utils.ed25519 import Ed25519PublicKey+GOVERNANCE_REQUIRED = FalseGOVERNANCE_BUILD_IDENTITY = ""@@ -254,8 +256,6 @@ def apply_disabled_nodes(disabled: set[str]) -> None:def verify_and_load(envelope_bytes: bytes) -> dict:- from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey-if len(envelope_bytes) > _MAX_ENVELOPE_BYTES:raise ValueError("governance envelope exceeds maximum size")diff --git a/requirements.txt b/requirements.txtindex 7276a8d2a..af0421951 100644--- a/requirements.txt+++ b/requirements.txt@@ -26,7 +26,6 @@ comfy-aimdo==0.5.6requestssimpleeval>=1.0.0blake3-cryptography#non essential dependencies:kornia>=0.7.1diff --git a/tests-unit/governance_test/test_verify.py b/tests-unit/governance_test/test_verify.pyindex d9de32c04..fb703b430 100644--- a/tests-unit/governance_test/test_verify.py+++ b/tests-unit/governance_test/test_verify.py@@ -3,12 +3,12 @@ import jsonfrom pathlib import Pathimport sys-from cryptography.exceptions import InvalidSignaturefrom cryptography.hazmat.primitives import serializationfrom cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKeyimport pytestfrom app import governance+from utils.ed25519 import InvalidSignatureDOMAIN_SEPARATOR = b"comfyui-governance-v1\x00"diff --git a/tests-unit/requirements.txt b/tests-unit/requirements.txtindex 3a6790ee0..117e0f10a 100644--- a/tests-unit/requirements.txt+++ b/tests-unit/requirements.txt@@ -2,3 +2,4 @@ pytest>=7.8.0pytest-aiohttppytest-asynciowebsocket-client+cryptographydiff --git a/utils/ed25519.py b/utils/ed25519.pynew file mode 100644index 000000000..0052ed736--- /dev/null+++ b/utils/ed25519.py@@ -0,0 +1,81 @@+"""Pure-Python Ed25519 verification using the RFC 8032 group equation."""++import hashlib+++_P = 2**255 - 19+_L = 2**252 + 27742317777372353535851937790883648493+_D = -121665 * pow(121666, _P - 2, _P) % _P+_SQRT_M1 = pow(2, (_P - 1) // 4, _P)+_IDENTITY = (0, 1, 1, 0)+++class InvalidSignature(Exception):+ pass+++def _add(p, q):+ # Extended Edwards coordinates: x = X/Z, y = Y/Z, xy = T/Z.+ a = (p[1] - p[0]) * (q[1] - q[0]) % _P+ b = (p[1] + p[0]) * (q[1] + q[0]) % _P+ c = 2 * _D * p[3] * q[3] % _P+ d = 2 * p[2] * q[2] % _P+ e, f, g, h = b - a, d - c, d + c, b + a+ return e * f % _P, g * h % _P, f * g % _P, e * h % _P+++def _multiply(point, scalar):+ result = _IDENTITY+ while scalar:+ if scalar & 1:+ result = _add(result, point)+ point = _add(point, point)+ scalar >>= 1+ return result+++def _decode(data):+ encoded = int.from_bytes(data, "little")+ y, sign = encoded & ((1 << 255) - 1), encoded >> 255+ if y >= _P:+ raise InvalidSignature+ x_squared = (y * y - 1) * pow(_D * y * y + 1, _P - 2, _P) % _P+ x = pow(x_squared, (_P + 3) // 8, _P)+ if x * x % _P != x_squared:+ x = x * _SQRT_M1 % _P+ if x * x % _P != x_squared or (x == 0 and sign):+ raise InvalidSignature+ if x & 1 != sign:+ x = _P - x+ return x, y, 1, x * y % _P+++_BASE = _decode(bytes.fromhex("5866666666666666666666666666666666666666666666666666666666666666"))+++class Ed25519PublicKey:+ def __init__(self, data):+ self._data = memoryview(data).tobytes()+ if len(self._data) != 32:+ raise ValueError("An Ed25519 public key is 32 bytes long")++ @classmethod+ def from_public_bytes(cls, data):+ return cls(data)++ def verify(self, signature, data):+ signature = memoryview(signature).tobytes()+ if len(signature) != 64:+ raise InvalidSignature+ scalar = int.from_bytes(signature[32:], "little")+ if scalar >= _L:+ raise InvalidSignature+ public_point = _decode(self._data)+ r = _decode(signature[:32])+ digest = hashlib.sha512(signature[:32] + self._data)+ digest.update(data)+ challenge = int.from_bytes(digest.digest(), "little") % _L+ left = _multiply(_BASE, scalar)+ right = _add(r, _multiply(public_point, challenge))+ if (left[0] * right[2] - right[0] * left[2]) % _P or (left[1] * right[2] - right[1] * left[2]) % _P:+ raise InvalidSignature