🏳️
Bag of Flags
  • Home
  • 2023
    • 🅿️picoCTF 2023
      • money-ware
      • repetitions
      • two-sum
      • ReadMyCert
      • rotation
      • hideme
      • PcapPoisoning
      • who is it
      • Reverse
      • timer
      • Safe Opener 2
      • findme
      • MatchTheRegex
      • SOAP
    • 🐦magpieCTF 2023
      • Space Plan
      • Space Exploration
      • So Meta
      • There is no flag
      • Momma says to play fair
      • Rubis
      • What is the password?
      • Eavesdropper
      • Shredded
      • Missing Flag
      • This outta be large enough right?
      • No Password Here
      • Chocolate Chips with Zero-G
      • Education Comes First
    • 🌴ISSessions CTF 2023
      • Basic Permissions
      • Crack Me
      • File Detective
      • Word Vomit
      • Fileception
      • Coding Time
      • Ghost File
      • CryptoTools1
      • CryptoTools2
      • 1337
      • ROT++
      • RunedMyDay
      • RSA_2
      • The Man Who Sold the World
      • VaultChallenge
      • Lost Media
      • Decontamination
      • Decade Capsule
      • Password in A Haystack
  • 2022
    • 🏁UW CTF S22
      • 0s and 1s
      • simple image
      • Helikopter
      • Meow
      • Google Form
      • Strings, literally
      • WASM
      • Audio
      • Pwn0
      • YATD
      • steg
      • Passwords
      • Vitalik
  • Practice
    • 🧠CryptoHack
      • Introduction
        • Finding Flags
        • Great Snakes
      • General
        • ASCII
        • Hex
        • Base64
        • Bytes and Big Integers
        • XOR Starter
        • XOR Properties
        • Favourite byte
        • You either know, XOR you don't
        • Greatest Common Divisor
Powered by GitBook
On this page
  • Description
  • Mr. Euclid, Sir
  • Euclidean Algorithm
  • Implementation
  • Flag
  1. Practice
  2. CryptoHack
  3. General

Greatest Common Divisor

PreviousYou either know, XOR you don't

Last updated 2 years ago

Description

The Greatest Common Divisor (GCD), sometimes known as the highest common factor, is the largest number which divides two positive integers (a,b). For a = 12, b = 8 we can calculate the divisors of a: {1,2,3,4,6,12} and the divisors of b: {1,2,4,8}. Comparing these two, we see that gcd(a,b) = 4. Now imagine we take a = 11, b = 17. Both a and b are prime numbers. As a prime number has only itself and 1 as divisors, gcd(a,b) = 1. We say that for any two integers a,b, if gcd(a,b) = 1 then a and b are coprime integers. If a and b are prime, they are also coprime. If a is prime and b < a then a and b are coprime. Think about the case for a prime and b > a, why are these not necessarily coprime? There are many tools to calculate the GCD of two integers, but for this task we recommend looking up . Try coding it up; it's only a couple of lines. Use a = 12, b = 8 to test it. Now calculate gcd(a,b) for a = 66528, b = 52920 and enter it below.

Mr. Euclid, Sir

To answer the first quiz question, if b > a and a is prime, then it is possible that b is a multiple of a, so the GCD of these two numbers is a and they are not coprime.

Let's take a look at Euclid's Algorithm

Euclidean Algorithm

We are given two positive integers a and b, assume that a > b

  1. Divide a / b to get the remainder r and if r = 0, b is the GCD of a, b

  2. Else, replace a with b and b with r, then repeat

Proof

We can prove this by induction, but intuitively this algorithm will eventually terminate as the remainder r will inevitably decrease and eventually reach 0

Equivalently, we have a=q0b+r0→b=q1r0+r1→r0=q2r1+r2⋯a = q_0b+r_0 \rightarrow b = q_1r_0 +r_1 \rightarrow r_0=q_2r_1+r_2 \cdotsa=q0​b+r0​→b=q1​r0​+r1​→r0​=q2​r1​+r2​⋯

Eventually, we get rn=0r_n=0rn​=0 which implies rn−1=gcd(a,b)r_{n-1}=gcd(a,b)rn−1​=gcd(a,b)

We wish to prove rn−1=gcd(a,b)=gr_{n-1}=gcd(a,b)=grn−1​=gcd(a,b)=g

  1. rn−1≤gr_{n-1}\leq grn−1​≤g

    Since rn=0r_n=0rn​=0 we know rn−2=qnrn−1r_{n-2}=q_nr_{n-1}rn−2​=qn​rn−1​ and rn−1r_{n-1}rn−1​ divides rn−2r_{n-2}rn−2​

    Thus we can also get rn−3=qn−1rn−2+rn−1=qn−1qnrn−1+rn−1r_{n-3}=q_{n-1}r_{n-2}+r_{n-1}=q_{n-1}q_nr_{n-1}+r_{n-1}rn−3​=qn−1​rn−2​+rn−1​=qn−1​qn​rn−1​+rn−1​ and rn−1r_{n-1}rn−1​ also divides rn−3r_{n-3}rn−3​

    We can repeat this to get that rn−1r_{n-1}rn−1​ divides aaa and bbb, and thus is a common divisor It follows that rn−1≤gr_{n-1} \leq grn−1​≤g, the greatest common divisor

  2. rn−1≥gr_{n-1}\geq grn−1​≥g

    For any common divisor ccc we have a=mca=mca=mc and b=ncb=ncb=nc

    Then ccc divides r0r_0r0​ since r0=a−q0b=mc−q0nc=(m−q0n)cr_0=a-q_0b=mc-q_0nc=(m-q_0n)cr0​=a−q0​b=mc−q0​nc=(m−q0​n)c

    We can repeat this argument to get that ccc divides rn−1r_{n-1}rn−1​, so it follows that ggg divides rn−1r_{n-1}rn−1​ as it is also a common divisor It follows that rn−1≥gr_{n-1}\geq grn−1​≥g

From both of these statements, we conclude rn−1=gr_{n-1}=grn−1​=g

Implementation

def gcd(a, b):
    # Only works if a > b
    if a < b:
        return gcd(b, a)

    # Euclidean algorithm
    while b != 0:
        # Calculate r = a % b
        # Set a = b
        # Set b = r
        a, b = b, a % b
    
    return a
    
print(gcd(66528, 52920))

Though of course, if you're lazy you can use an online tool

Flag

1512

🧠
🔆
Euclid's Algorithm