From 97ef52e3c129d6f1168d3751025557167dc84513 Mon Sep 17 00:00:00 2001 From: Asti1982 <65121113+Asti1982@users.noreply.github.com> Date: Tue, 12 May 2026 20:08:45 +0200 Subject: [PATCH] Fix multi-arch oracle HMAC import --- .../src/mutator_oracle/multi_arch_oracles.py | 4 +- tests/test_multi_arch_oracles_hmac.py | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/test_multi_arch_oracles_hmac.py diff --git a/rips/rustchain-core/src/mutator_oracle/multi_arch_oracles.py b/rips/rustchain-core/src/mutator_oracle/multi_arch_oracles.py index 5f51f3a15..887357d36 100644 --- a/rips/rustchain-core/src/mutator_oracle/multi_arch_oracles.py +++ b/rips/rustchain-core/src/mutator_oracle/multi_arch_oracles.py @@ -49,6 +49,7 @@ from typing import Dict, List, Optional, Tuple, Set from enum import Enum, auto import hashlib +import hmac import struct import secrets import time @@ -289,7 +290,7 @@ def generate_mutation_seed(self, block_height: int) -> Optional[MultiArchMutatio final_seed, b''.join(a.encode() for a in sorted(contributions.keys())), hashlib.sha256 - ).digest() if 'hmac' in dir() else hashlib.sha256(final_seed).digest() + ).digest() seed = MultiArchMutationSeed( seed=final_seed, @@ -478,5 +479,4 @@ def demo_multi_arch_network(): if __name__ == "__main__": - import hmac # Import for ring signature demo_multi_arch_network() diff --git a/tests/test_multi_arch_oracles_hmac.py b/tests/test_multi_arch_oracles_hmac.py new file mode 100644 index 000000000..bb26b2019 --- /dev/null +++ b/tests/test_multi_arch_oracles_hmac.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""Regression tests for multi-architecture oracle ring signatures.""" + +import contextlib +import hashlib +import hmac +import io +import sys +from pathlib import Path + + +MODULE_DIR = ( + Path(__file__).resolve().parents[1] + / "rips" + / "rustchain-core" + / "src" + / "mutator_oracle" +) +sys.path.insert(0, str(MODULE_DIR)) + +from multi_arch_oracles import ( # noqa: E402 + ArchitectureOracle, + CPUArchitecture, + MultiArchOracleRing, +) + + +def test_library_import_uses_hmac_for_ring_signature(): + ring = MultiArchOracleRing() + + with contextlib.redirect_stdout(io.StringIO()): + assert ring.register_oracle( + ArchitectureOracle( + node_id="ppc", + hostname="ppc.local", + ip_address="127.0.0.1", + architecture=CPUArchitecture.POWERPC_G4, + cpu_model="PowerMac G4", + simd_enabled=True, + ) + ) + assert ring.register_oracle( + ArchitectureOracle( + node_id="x86", + hostname="x86.local", + ip_address="127.0.0.2", + architecture=CPUArchitecture.INTEL_X86_64, + cpu_model="x86_64", + simd_enabled=True, + ) + ) + seed = ring.generate_mutation_seed(block_height=123) + + assert seed is not None + arch_message = b"".join( + arch.encode() for arch in sorted(seed.architecture_contributions) + ) + expected_hmac = hmac.new(seed.seed, arch_message, hashlib.sha256).digest() + fallback_sha256 = hashlib.sha256(seed.seed).digest() + + assert seed.ring_signature == expected_hmac + assert seed.ring_signature != fallback_sha256