{"id":138,"date":"2026-11-13T12:32:27","date_gmt":"2026-11-13T04:32:27","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-baby-step-giant-step-solves-a-discrete-log\/"},"modified":"2026-11-13T21:51:07","modified_gmt":"2026-11-13T13:51:07","slug":"how-baby-step-giant-step-solves-a-discrete-log","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-baby-step-giant-step-solves-a-discrete-log\/","title":{"rendered":"How baby-step giant-step solves a discrete log"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Baby-step giant-step<\/strong> solves g^x \u2261 h (mod p) for a small x by writing x = i\u00b7m \u2212 j with m = ceil(sqrt(N)) and x in a range of length N. It stores the baby steps g^j for j = 0..m\u22121 in a hash table, then walks giant steps h\u00b7(g^{\u2212m})^i and looks each one up. A hit gives x = i\u00b7m \u2212 j. Time and memory are <strong>O(\u221aN)<\/strong> multiplications and table entries. The algorithm is deterministic and exponential in the bit length of the group order.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A discrete logarithm has no known polynomial-time classical algorithm in a general prime field. Brute force tries every exponent. Baby-step giant-step is the meet-in-the-middle improvement: the unknown exponent is a high part times m minus a low part, and one side of that equation can be tabulated. Any collision between a stored baby step and a computed giant step solves for x.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The group does not have to be the multiplicative group modulo a prime. Any cyclic group in which elements can be multiplied and hashed works. The range N must be known. Usually N is the group order.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">If a solution exists in the searched range, the algorithm finds the smallest nonnegative one, or any one, depending on the order of the search. Time is \u0398(\u221aN) group operations and memory is \u0398(\u221aN) group elements. That memory is the limit long before the time is. Pollard&#8217;s rho for discrete logs uses negligible memory and has a similar expected operation count. It is the practical replacement when \u221aN elements will not fit.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The algorithm is not an index-calculus method and does not exploit a special prime. On a cryptographic group whose order is a large prime, \u221aN is still infeasible. The existence of this algorithm is why a discrete-log system needs a group large enough that \u221aN operations are impossible.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Let m = ceil(sqrt(N)). Compute baby steps b[j] = g^j for j = 0, 1, \u2026, m\u22121, and store a map from the group element to j.<\/li>\n<li>Compute factor = g^{\u2212m}, the inverse of g^m. Set gamma = h.<\/li>\n<li>For i = 0, 1, \u2026, m\u22121, look up gamma in the map. If gamma = g^j is found, return x = i\u00b7m \u2212 j, adjusted into range if the sign is negative.<\/li>\n<li>Replace gamma by gamma\u00b7factor and continue.<\/li>\n<li>If the loop ends with no hit, no exponent in the range solves the equation.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The square-root bound is time and memory. Pollard&#8217;s rho for logarithms drops the memory and keeps a similar expected time. Neither algorithm is a reason to use a small group. Index calculus and number-field sieve variants attack discrete logs in some groups faster and do not apply unchanged to a generic elliptic-curve group, where baby-step giant-step and rho remain the generic attacks.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the table stores only the exponent<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Store j and not the group element g^j<\/td>\n<td>The lookup is by the element the giant step just computed. The exponent is the value retrieved after the element hits.<\/td>\n<\/tr>\n<tr>\n<td>Use m much smaller than sqrt(N) and stop at i &lt; m<\/td>\n<td>The pairs (i, j) then cover fewer than N exponents. A solution outside the covered rectangle is missed.<\/td>\n<\/tr>\n<tr>\n<td>Ignore the memory and quote only the time<\/td>\n<td>\u0398(\u221aN) group elements is the reason a large instance cannot run this algorithm even when the operation count looks acceptable on paper.<\/td>\n<\/tr>\n<tr>\n<td>Apply the result as a polynomial-time break of a standard-size discrete-log key<\/td>\n<td>Square root of a 256-bit group order is 128 bits of work. The algorithm is exponential in the security parameter.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A low-memory generic discrete log is Pollard&#8217;s rho in the group. A prime-field log at cryptographic size, when it is attacked at all, uses index calculus, not this table. Baby-step giant-step is the deterministic meet-in-the-middle log for a moderate range.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment stores baby steps g^j for j &lt; m, with m = ceil(sqrt(order)), then takes giant steps of g^{\u2212m}. In the group modulo 23, base 5, target 8, the discrete log is <code>6<\/code> because 5^6 \u2261 8.<\/p>\n\n<pre><code class=\"language-python\">p, g, target = 23, 5, 8\norder = p - 1\nm = int(order ** 0.5 + 0.999)\nbaby = {}\nvalue = 1\nfor j in range(m):\n    baby[value] = j\n    value = value * g % p\nfactor = pow(pow(g, m, p), -1, p)\ngamma = target\nfor i in range(m):\n    if gamma in baby:\n        answer = i * m + baby[gamma]\n        break\n    gamma = gamma * factor % p\nassert pow(g, answer, p) == target\nprint(answer)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Baby-step giant-step computes a discrete logarithm in a group small enough to store the baby steps. It is a cryptanalytic and a parameter-checking tool, not a handshake. Pollard&#8217;s rho is the method used once the group no longer fits in memory.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the table when \u221aN elements do not fit. Use a rho-style random walk. Keep baby-step giant-step when the range is small enough to store and a deterministic answer is wanted.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for baby-step giant-step returns the \u221aN table of powers and the giant-step lookup that recovers the exponent. <strong>Baby-step giant-step solves a discrete logarithm in a known range of length N with O(\u221aN) time and O(\u221aN) memory by matching tabulated powers against giant steps.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Baby-step giant-step solves g^x \u2261 h (mod p) for a small x by writing x = i\u00b7m \u2212 j with m = ceil(sqrt(N)) and x in a range of length N. It stores the baby steps g^j for j = 0..m\u22121 in a hash table, then walks&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,42],"tags":[],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-number-theory"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":537,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions\/537"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}