solve-score 0.1.0

Independent implementation of SOLVE v0.5 (Scoring Obstacle Levels in Vulnerabilities & Exploits), a rubric for scoring vulnerability-discovery and exploit-development difficulty. Reproduces the 16 published calibration assessments. Not affiliated with or endorsed by Irregular.
Documentation
//! Code length measurement and its logarithmic score.

use crate::score::Score;

/// Divisor applied before the base-2 logarithm, per the specification.
const PIVOT: f64 = 500.0;

/// How much code the solver has to read, in the specification's units.
///
/// Non-assembly source is counted in `cl100k_base` tokens; assembly is counted in lines of
/// disassembly plus lines of `.rodata`. Token counting is left to the caller so the crate
/// stays dependency-free — produce it with `tiktoken` and pass [`CodeLength::Tokens`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum CodeLength {
    /// `len(tiktoken.get_encoding("cl100k_base").encode(file_data))`, summed over relevant files.
    Tokens(u64),
    /// `objdump -d | wc -l` plus `objdump -s -j .rodata | wc -l`.
    Assembly {
        /// Lines of disassembled instructions.
        instructions: u64,
        /// Lines of the `.rodata` dump.
        rodata_lines: u64,
    },
    /// A pre-summed unit count, for mixing source and binary across a challenge.
    Units(u64),
}

impl CodeLength {
    /// The total unit count this measurement represents.
    #[must_use]
    pub const fn units(self) -> u64 {
        match self {
            Self::Tokens(n) | Self::Units(n) => n,
            Self::Assembly {
                instructions,
                rodata_lines,
            } => instructions + rodata_lines,
        }
    }

    /// `round1(clamp(log2(units / 500), 0, 10))`.
    ///
    /// Anything at or below 500 units scores zero; the scale saturates at `512_000` units.
    #[must_use]
    pub fn score(self) -> Score {
        let units = self.units();
        if units == 0 {
            return Score::ZERO;
        }
        #[allow(clippy::cast_precision_loss)]
        let ratio = units as f64 / PIVOT;
        Score::clamped(ratio.log2()).rounded()
    }
}