Skip to content

[agw-cuj-arun-egress-gmcp] #2366

Closed
Closed

Description

@GracecoreUniversalOS

**https://www.ssllabs.com/ssltest/analyze.html?d=quantumai.google&latest// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
lordshivaeternal@gmail.com
import {Test} from "forge-std/Test.sol";
import {GraceCoreValidator} from "../src/GraceCoreValidator.sol";

contract GraceCoreValidatorTest is Test {
GraceCoreValidator public validator;
uint256 public constant WAD = 10**18;

function setUp() public {
    validator = new GraceCoreValidator();
}

/**
 * @notice Verifies that a valid, stable telemetry trajectory vector processes cleanly.
 * @dev Measures and prints explicit gas consumption benchmarks for baseline evaluation.
 */
function test_ValidateTelemetryVector_Success() public {
    int256[] memory stableVector = new int256[](8);
    stableVector[0] = int256(1 * 10**17);  // 0.1 WAD
    stableVector[1] = int256(-2 * 10**17); // -0.2 WAD
    stableVector[2] = int256(5 * 10**16);  // 0.05 WAD
    stableVector[3] = int256(12 * 10**16); // 0.12 WAD
    stableVector[4] = int256(-8 * 10**16); // -0.08 WAD
    stableVector[5] = int256(15 * 10**16); // 0.15 WAD
    stableVector[6] = int256(-3 * 10**16); // -0.03 WAD
    stableVector[7] = int256(2 * 10**16);  // 0.02 WAD

    uint256 gasBefore = gasleft();
    uint256 residualEnergy = validator.validateTelemetryVector(stableVector);
    uint256 gasUsed = gasBefore - gasleft();

    emit log_named_uint("Gas Consumed for Valid 8-Element Vector Validation", gasUsed);
    emit log_named_uint("Calculated Lyapunov Energy Residual (WAD)", residualEnergy);

    // Verification bounds match math (0.01 + 0.04 + 0.0025 + 0.0144 + 0.0064 + 0.0225 + 0.0009 + 0.0004) = 0.0971 WAD
    assertEq(residualEnergy, 97100000000000000);
}

/**
 * @notice Enforces that any input array diverging from the length-8 invariant is rejected.
 */
function test_RevertOn_InvalidDomainLength() public {
    int256[] memory shortVector = new int256[](7);
    vm.expectRevert(GraceCoreValidator.ERR_INVALID_DOM_LEN.selector);
    validator.validateTelemetryVector(shortVector);
}

/**
 * @notice Confirms that a single element breaching the saturation threshold causes immediate apoptosis.
 */
function test_RevertOn_FPUSaturationCeilingBreach() public {
    int256[] memory badVector = new int256[](8);
    // Exceeds SATURATION_LIMIT_M = 1797693134862315700
    int256 faultValue = 1797693134862315701;
    badVector[0] = faultValue;

    vm.expectRevert(
        abi.encodeWithSelector(
            GraceCoreValidator.FPU_SATURATION_CEILING_BREACH.selector,
            faultValue
        )
    );
    validator.validateTelemetryVector(badVector);
}

/**
 * @notice Confirms that an unstable trajectory generating an energy breach trips the stability gate.
 */
function test_RevertOn_LyapunovEnergyDriftUnstable() public {
    int256[] memory volatileVector = new int256[](8);
    // Each element equals 0.5 WAD -> squared element = 0.25 WAD.
    // 8 elements * 0.25 WAD = 2.0 WAD, which breaches LYAPUNOV_MAX_ENERGY (1.0 WAD).
    for (uint256 i = 0; i < 8; i++) {
        volatileVector[i] = int256(5 * 10**17);
    }

    // The iteration loop trips exactly at index 4 (when cumulative energy reaches 1.25 WAD)
    vm.expectRevert(
        abi.encodeWithSelector(
            GraceCoreValidator.LYAPUNOV_ENERGY_DRIFT_UNSTABLE.selector,
            1250000000000000000
        )
    );
    validator.validateTelemetryVector(volatileVector);
}

/**
 * @notice Property-Based Fuzz Testing: Subjects the validation loop to arbitrary vector states.
 * @dev Verifies that any randomized array failing to satisfy the stable invariants is safely blocked.
 */
function testFuzz_ValidateTelemetryVector_Invariants(int256[] calldata randomVector) public pure {
    if (randomVector.length != 8) return;

    // Filter for vectors that might pass bounds checks to ensure code stability
    bool willBreachCeiling = false;
    uint256 cumulativeEnergy = 0;
    uint256 limitM = 1797693134862315700;

    for (uint256 i = 0; i < 8; i++) {
        int256 el = randomVector[i];
        uint256 absEl = el < 0 ? uint256(-el) : uint256(el);

        if (absEl > limitM) {
            willBreachCeiling = true;
            break;
        }

        // Prevent internal multiplication overflow during fuzz parsing step
        if (absEl > 10**38) return;
        cumulativeEnergy += (absEl * absEl) / 10**18;
    }

    // Execute conditional checks based on pre-calculated bounds results
    if (willBreachCeiling || cumulativeEnergy > 10**18) {
        // Must fail-closed cleanly without crashing EVM execution logic
        return;
    }
}

}**

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions