Class: MlDsa::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ml_dsa/config.rb

Overview

Thread-safe configuration holder for instrumentation subscribers and pluggable RNG. Each Ractor or test context can have its own Config instance.

Examples:

Custom config for testing

cfg = MlDsa::Config.new
cfg.random_source = proc { |n| "\x42" * n }
pk, sk = MlDsa.keygen(MlDsa::ML_DSA_65, config: cfg)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
# File 'lib/ml_dsa/config.rb', line 13

def initialize
  @subscribers = []
  @mutex = Mutex.new
  @random_source = nil
end

Instance Attribute Details

#random_source ⇒ Proc?

Returns the current random source, or nil for OS CSPRNG.

Returns:

  • (Proc, nil) —

    the current random source, or nil for OS CSPRNG



38
39
40
# File 'lib/ml_dsa/config.rb', line 38

def random_source
  @random_source
end

Instance Method Details

#notify(operation, param_set, count, duration_ns) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ml_dsa/config.rb', line 52

def notify(operation, param_set, count, duration_ns)
  # In non-main Ractors, skip instrumentation to avoid isolation errors
  if defined?(Ractor) && Ractor.respond_to?(:main?) && !Ractor.main?
    return nil
  end
  subs = @mutex.synchronize { @subscribers.dup }
  return if subs.empty?
  event = {
    operation: operation,
    param_set: param_set,
    count: count,
    duration_ns: duration_ns
  }.freeze
  subs.each { |s| s.call(event) }
  nil
end

#subscribe {|Hash| ... } ⇒ Proc

Subscribe to instrumentation events. The block receives a frozen Hash with keys :operation, :param_set, :count, and :duration_ns.

Yields:

  • (Hash) —

    event payload

Returns:

Raises:

  • (ArgumentError)


24
25
26
27
28
# File 'lib/ml_dsa/config.rb', line 24

def subscribe(&block)
  raise ArgumentError, "subscribe requires a block" unless block
  @mutex.synchronize { @subscribers << block }
  block
end

#unsubscribe(subscriber) ⇒ Proc?

Remove a previously registered subscriber.

Parameters:

  • subscriber (Proc) —

    the block returned by #subscribe

Returns:

  • (Proc, nil) —

    the removed subscriber, or nil if not found



33
34
35
# File 'lib/ml_dsa/config.rb', line 33

def unsubscribe(subscriber)
  @mutex.synchronize { @subscribers.delete(subscriber) }
end