snapkitty
cryptography
python
marlborg-worm / hardware /clash /EntropyAdderTree.hs
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/marlborg-worm
75619b0 verified
Raw History Blame Contribute Delete
3.05 kB
--
-- Copyright (c) 2026 BEL ESPRIT D ACCORD TRUST HOLDINGS INC
-- All rights reserved.
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module MarlborgWorm.Hardware.EntropyAdderTree
( entropyAdderTree
, topEntity
) where
import Clash.Prelude
-- | Fixed-point parameters (scaling by 10^4)
-- H_BASE = 0.1000 nats -> 1000
-- H_HASH = 0.0400 nats -> 400 (when hash removed)
-- H_MARLBORG = 0.0005 nats/rule -> 5 per rule
-- epsilon = 168/10088352 nats/op -> (1680000 * N) / 10088352 in fixed-point
type HBaseFixed = 1000
type HHashFixed = 400
type HMarlborgPerRule = 5
data AdderState = AdderState
{ hTruncAccum :: Unsigned 32
, lastOpCount :: Unsigned 32
} deriving (Show, Eq, Generic, NFDataX)
initialAdderState :: AdderState
initialAdderState = AdderState 0 0
-- | Mealy transition: compute total cognitive entropy in fixed-point
adderStep :: AdderState
-> (Bit, Unsigned 12, Unsigned 32, Bool, Unsigned 16)
-> (AdderState, Unsigned 32)
adderStep st (truncValid, _thetaFixed, opCount, hashRemoved, deltaRules) =
let deltaN = if opCount >= lastOpCount st
then opCount - lastOpCount st
else 0
hTruncAdd = if deltaN == 0
then 0
else (1680000 * resize deltaN) `div` 10088352
hTruncNext = hTruncAccum st + hTruncAdd
hHash = if hashRemoved then HHashFixed else 0
hMarlborg = resize deltaRules * HMarlborgPerRule
hCogFixed = HBaseFixed + hTruncNext + hHash + hMarlborg
in (AdderState hTruncNext opCount, hCogFixed)
-- | Entropy adder tree: sums all entropy components
entropyAdderTree
:: Clock System
-> Reset System
-> Enable System
-> Signal System Bit
-> Signal System (Unsigned 12)
-> Signal System (Unsigned 32)
-> Signal System Bool
-> Signal System (Unsigned 16)
-> Signal System (Unsigned 32)
entropyAdderTree clk rst en truncValid thetaFixed opCount hashRemoved deltaRules =
mealy clk rst en adderStep initialAdderState
(bundle (truncValid, thetaFixed, opCount, hashRemoved, deltaRules))
topEntity
:: Clock System
-> Reset System
-> Enable System
-> Signal System Bit
-> Signal System (Unsigned 12)
-> Signal System (Unsigned 32)
-> Signal System Bool
-> Signal System (Unsigned 16)
-> Signal System (Unsigned 32)
topEntity = entropyAdderTree
{-# ANN topEntity
(Synthesize
{ t_name = "EntropyAdderTree"
, t_inputs = [ PortName "clk"
, PortName "rst"
, PortName "en"
, PortName "truncatorValid"
, PortName "thetaFixed"
, PortName "operationCount"
, PortName "hashRemoved"
, PortName "deltaRules"
]
, t_output = PortName "hCogFixed"
}) #-}