25_combo_breaker.rb
# https://en.wikipedia.org/wiki/Discrete_logarithm
# there are many algorithms; for now I picked a simple-to-understand one.
# https://en.wikipedia.org/wiki/Baby-step_giant-step
en.wikipedia.org/wiki/Discrete_logarithm
there are many algorithms; for now I picked a simple-to-understand one.
en . wikipedia . org / wiki / Discrete_logarithm there are many algorithms ; for now I picked a simple - to - understand one .
def discrete_log(g, h, mod)
  m = (mod ** 0.5).ceil
  j_of_aj = {1 => 0}
  current = 1
  (1...m).each { |j|
    j_of_aj[current = (current * g) % mod] = j
  }
  am = g.pow(mod - m - 1, mod)
  gamma = h
  m.times { |i|
    if (j = j_of_aj[gamma])
      return i * m + j
    end
    gamma = (gamma * am) % mod
  }
  raise 'not found'
end
def discrete_log ( g , h , mod ) m = ( mod ** 0.5 ) . ceil j_of_aj = { 1 => 0 } current = 1 ( 1 ... m ) . each { | j | j_of_aj [ current = ( current * g ) % mod ] = j } am = g . pow ( mod - m - 1 , mod ) gamma = h m . times { | i | if ( j = j_of_aj [ gamma ] ) return i * m + j end gamma = ( gamma * am ) % mod } raise 'not found' end
https://en.wikipedia.org/wiki/Discrete_logarithm
there are many algorithms; for now I picked a simple-to-understand one.
https : // en . wikipedia . org / wiki / Discrete_logarithm there are many algorithms ; for now I picked a simple - to - understand one .
g
<not_specific>
h
<not_specific>
mod
<not_specific>