s LearnSqueak.com

SMALLTALK LAB

Extended Euclid in Squeak: Find Modular Inverses

Carry two coefficients through Euclid’s remainders, prove a Bezout identity, and use it to solve modular equations with exact integers.

8 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Open Workspace and Print it on one complete checkpoint at a time. These independent examples use nonnegative integer inputs; at least one must be positive. You need no packages. The visual download shows the evolving coefficient rows for 99 and 78.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.

01 - Read a remainder as an equation

Integer division gives a quotient and remainder: a = q*b + r. We will preserve not only each remainder but also how it was made from the original inputs. Parentheses are important because Smalltalk binary messages have equal precedence and associate left to right.

Independent Workspace expression - select all, then Print it
| a b q remainder |
a := 99. b := 78.
q := a // b.
remainder := a - (q * b).
{q. remainder. a = ((q * b) + remainder)}

Expected: #(1 21 true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Read a remainder as an equation
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try 78 and 21: the quotient is 3 and remainder is 15.

Download this checkpoint

02 - Start with two coefficient rows

A row #(r s t) means r = s*a + t*b. The initial rows are trivial: a = 1*a + 0*b, and b = 0*a + 1*b. Keeping this identity true is the whole algorithm.

Independent Workspace expression - select all, then Print it
| a b rows |
a := 99. b := 78.
rows := #((99 1 0) (78 0 1)).
rows collect: [:row | row first = (((row at: 2) * a) + ((row at: 3) * b))]

Expected: #(true true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Start with two coefficient rows
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The coefficient columns refer to the original a and b, not to the changing remainders.

Download this checkpoint

03 - Subtract the same multiple in all three columns

The next row is oldRow minus q times currentRow. Subtracting identities yields a new identity. For 99 and 78 the first new row is #(21 1 -1), because 21 = 99 - 78.

Independent Workspace expression - select all, then Print it
| previous current q next |
previous := #(99 1 0). current := #(78 0 1).
q := previous first // current first.
next := (1 to: 3) collect: [:i |
 (previous at: i) - (q * (current at: i))].
next asArray

Expected: #(21 1 -1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Subtract the same multiple in all three columns
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Apply the same operation to #(78 0 1) and #(21 1 -1); expect #(15 -3 4).

Download this checkpoint

04 - Loop until the current remainder is zero

Keep old and current values for the remainder and both coefficients. A temporary next value prevents overwriting a value before it is used. When r reaches zero, oldR is the gcd and oldS, oldT are its Bezout coefficients. The final zero row is not the answer.

Independent Workspace expression - select all, then Print it
| a b oldR r oldS s oldT t |
a := 99. b := 78.
oldR := a. r := b.
oldS := 1. s := 0. oldT := 0. t := 1.
[r ~= 0] whileTrue: [ | q next |
 q := oldR // r.
 next := oldR - (q * r). oldR := r. r := next.
 next := oldS - (q * s). oldS := s. s := next.
 next := oldT - (q * t). oldT := t. t := next].
{oldR. oldS. oldT}

Expected: #(3 -11 14)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Loop until the current remainder is zero
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Check -11*99 + 14*78 by hand: both terms may be much larger than their sum.

Download this checkpoint

05 - Verify the identity independently

Instead of trusting the printed coefficients, substitute them into the original equation. Also verify divisibility of both inputs. Exact integer arithmetic avoids rounding error, and Squeak grows integers beyond machine-word size as needed.

Independent Workspace expression - select all, then Print it
| a b oldR r oldS s oldT t |
a := 99. b := 78.
oldR := a. r := b.
oldS := 1. s := 0. oldT := 0. t := 1.
[r ~= 0] whileTrue: [ | q next |
 q := oldR // r.
 next := oldR - (q * r). oldR := r. r := next.
 next := oldS - (q * s). oldS := s. s := next.
 next := oldT - (q * t). oldT := t. t := next].
{((oldS * a) + (oldT * b)) = oldR.
 (a \\ oldR) = 0. (b \\ oldR) = 0}

Expected: #(true true true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Verify the identity independently
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change the inputs to 240 and 46. The gcd is 2 and this update order returns coefficients -9 and 47.

Download this checkpoint

06 - Turn a coefficient into a modular inverse

For a modulus m greater than one, a has an inverse only when gcd(a,m) is 1. Then s*a + t*m = 1 implies s*a is congruent to 1 modulo m. Squeak remainder with a positive modulus normalizes a negative coefficient into the range 0 through m-1.

Independent Workspace expression - select all, then Print it
| a b oldR r oldS s oldT t |
a := 17. b := 43.
oldR := a. r := b.
oldS := 1. s := 0. oldT := 0. t := 1.
[r ~= 0] whileTrue: [ | q next |
 q := oldR // r.
 next := oldR - (q * r). oldR := r. r := next.
 next := oldS - (q * s). oldS := s. s := next.
 next := oldT - (q * t). oldT := t. t := next].
{oldR. oldS. oldS \\ b. ((oldS \\ b) * a) \\ b}

Expected: #(1 -5 38 1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Turn a coefficient into a modular inverse
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: 38 is an inverse of 17 modulo 43. The coefficient -5 represents the same residue.

Download this checkpoint

07 - Detect when no inverse exists

Do not normalize and advertise a coefficient unless the gcd is one. For 12 modulo 18 every product remains divisible by 6 modulo 18, so it cannot equal 1. A symbol is an explicit failure result in this small demonstration.

Independent Workspace expression - select all, then Print it
| a b oldR r oldS s oldT t |
a := 12. b := 18.
oldR := a. r := b.
oldS := 1. s := 0. oldT := 0. t := 1.
[r ~= 0] whileTrue: [ | q next |
 q := oldR // r.
 next := oldR - (q * r). oldR := r. r := next.
 next := oldS - (q * s). oldS := s. s := next.
 next := oldT - (q * t). oldT := t. t := next].
oldR = 1 ifTrue: [oldS \\ b] ifFalse: [#noInverse]

Expected: #noInverse

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Detect when no inverse exists
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The identity still exists: the coefficients explain gcd 6, not an inverse of 12.

Download this checkpoint

08 - Solve a modular linear equation

To solve 17*x = 5 modulo 43, multiply 5 by the inverse of 17 and normalize. The resulting x=18 is the unique residue in 0..42 because the gcd is one. This example checks its answer by substitution.

Independent Workspace expression - select all, then Print it
| a b oldR r oldS s oldT t |
a := 17. b := 43.
oldR := a. r := b.
oldS := 1. s := 0. oldT := 0. t := 1.
[r ~= 0] whileTrue: [ | q next |
 q := oldR // r.
 next := oldR - (q * r). oldR := r. r := next.
 next := oldS - (q * s). oldS := s. s := next.
 next := oldT - (q * t). oldT := t. t := next].
{(5 * (oldS \\ b)) \\ b.
 (a * ((5 * (oldS \\ b)) \\ b)) \\ b}

Expected: #(18 5)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 8: Solve a modular linear equation
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Replace 5 with 9: the solution is 41. Always substitute the result into the original congruence.

Download this checkpoint

Watch the computed states

NATIVE SQUEAK CAPTURE
Extended Euclid in Squeak: Find Modular Inverses native visual trace
Replay of actual Morphic window captures at successive computed stages; not mouse footage.

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.

Reason about correctness and limits

These checkpoints require nonnegative integers and at least one positive input. The modular examples additionally require b > 1. Reject other inputs or deliberately normalize signs in a reusable API.

Squeak uses // for integer quotient and \\ for remainder. / would introduce Fractions, breaking the intended remainder recurrence.

Update old/current pairs using temporary values. Reassigning oldR before calculating the new remainder changes the algorithm.

Bezout coefficients need not be positive or unique. Normalize only the inverse residue, not the identity coefficients.

The algorithm performs O(log min(a,b)) Euclidean divisions for positive inputs; this is a count of arithmetic operations, not constant-time big-integer bit complexity. It uses a constant number of growing integer variables.

This educational implementation is not constant-time cryptographic code. Do not infer secure key handling or resistance to timing attacks.

Three practice challenges

A new inverse

Find the inverse of 7 modulo 26 and check it.

Show solution

The normalized inverse is 15: 7*15 = 105 = 4*26 + 1.

A zero second input

What does the coefficient loop return for a=9 and b=0?

Show solution

The loop performs no iterations and returns #(9 1 0). The gcd identity holds, but zero is not a valid modulus for the inverse examples.

Non-coprime equations

Why can 12*x = 6 modulo 18 have solutions even though 12 has no inverse?

Show solution

The gcd 6 divides the right side. Divide the equation and modulus by 6: 2*x = 1 modulo 3, so x=2 modulo 3. In 0..17 the solutions are 2,5,8,11,14,17. A general solver must check gcd divisibility and enumerate residue classes.

Continue learning

Euclidean Gcd | Fractions | Fast Power

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.