liveBook · Manning

Greatest common divisor

As explained in Elliptic Curve Cryptography for Developers

The greatest common divisor of polynomials is a highest-degree polynomial that divides each given polynomial, defined up to multiplication by a nonzero constant.

The greatest common divisor (gcd) of two polynomials is a highest-degree polynomial dividing both, with equivalent answers differing by a nonzero constant. Over a finite field, compute it with the remainders from Euclid’s polynomial division. The result supports irreducible-polynomial searches and inversion-related elliptic-curve arithmetic. section 10.0 section 10.8

Implement the routine with this execution flow:

  • Return the other polynomial when either input is zero. section 10.1 section 10.4
  • Assign the higher-degree input to the working numerator aw and the lower-degree input to bw. section 10.1 section 10.4
  • Repeatedly divide aw by bw, discard the quotient, replace aw with bw, and replace bw with the remainder. section 10.1 section 10.8
  • Stop when bw has degree zero; return aw if that constant is zero, otherwise return bw. section 10.4

Remainder-driven computation

For gcd(a, b), initialize aw and bw in descending degree order. Compute r = aw / bw, where r denotes the remainder, then iterate with (aw, bw) ← (bw, r). When the final remainder is zero, the preceding working polynomial is the gcd; otherwise the final constant remainder is returned. If the inputs share no nonconstant factor, that result is 1 or another nonzero constant. section 10.1 section 10.4

Listing 10.3 Gcd main loop
  poly_init(&q);
  poly_init(&r);
  while(bw.deg > 0)  #1
  {
    poly_euclid(&q, &r, aw, bw);  #2
    poly_copy(&aw, bw);  #3
    poly_copy(&bw, r);   #4
  }
  if(!mpz_cmp_ui(bw.coef[0], 0))  #5
    poly_copy(d, aw);  #5
  else  #6
    poly_copy(d, bw);  #6
  poly_clear(&r);
  poly_clear(&q);
  poly_clear(&bw);
  poly_clear(&aw);
}

The main-loop listing maps the mathematical recurrence directly to initialized remainder storage, repeated polynomial division, working-value updates, termination, and cleanup. section 10.4

How Elliptic Curve Cryptography for Developers covers it

How deep it goes
Explains zero cases, degree ordering, remainder iteration, termination, and C-style implementation, stopping short of a numeric worked polynomial.
Examples
Uses a simple Euclidean remainder progression and C listings for initialization, the main loop, termination, and cleanup.
What it assumes
Assumes polynomial addition and multiplication, while defining degree, leading coefficient, and division behavior for the algorithm.
What you can do after
Build it from scratch using polynomial division remainders and handle zero inputs, degree ordering, constants, and resource cleanup.
How it connects
Connects polynomial division, Euclidean algorithm, finite fields, irreducible polynomials, elliptic curves, and extended Euclidean algorithm.

Also explained in

Compare how 2 books explain greatest common divisor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elliptic Curve Cryptography for Developers. The section links point to the parts of the book it draws on.