canontra-0.4.0.0: src/Canontra/Fingerprint/ControlFlow.hs
{- |
Module : Canontra.Fingerprint.ControlFlow
Description : Cryptographic fingerprint tier for Control-Flow Graphs (F_CF).
Computes F_CF by hashing the deterministic canonical binary representation
of all basic blocks and branching topology in the module after normalization.
-}
module Canontra.Fingerprint.ControlFlow
( computeFCF
, computeFCFNormalized
) where
import Canontra.Analysis.CFG (buildCFGs)
import Canontra.Analysis.CFGCanonical (canonicalizeCFGRPO)
import Canontra.Canonical.Serialize (canonicalizeCFGs)
import Canontra.Fingerprint.Source (hashBytes)
import Canontra.IR.Program (Program)
import Canontra.Normalize.Normalize (CanonicalProgram (..), toCanonicalProgram)
import Canontra.Types (Fingerprint)
-- | Compute the F_CF control-flow fingerprint for a Program.
computeFCF :: Program -> Fingerprint
computeFCF = computeFCFNormalized . toCanonicalProgram
-- | Compute F_CF directly from an already-normalized 'CanonicalProgram'.
computeFCFNormalized :: CanonicalProgram -> Fingerprint
computeFCFNormalized (CanonicalProgram normProg) =
let cfgs = map canonicalizeCFGRPO (buildCFGs normProg)
canonicalBytes = canonicalizeCFGs cfgs
in hashBytes canonicalBytes