r/learnprogramming • • Apr 28 '22

If everything is ultimately binary, does't that just make programs a specific number?

I did the CS50 course a while ago and they covered binary in there. I did binary back in 2006 when I was at college too so understood the logic behind it already. It's either on or off!

When a C program compiles, it goes through it's phases and compiles to assembly and then ultimately machine code i.e binary.

Let's say our program is 5,000,000 pages of 1's and 0's. All of those 1's and 0's make up a single number....albeit a super long number.

Does that mean that the entire Linux operating system, for example, can be represented by an actual number?

If so, why are we not storing programs as a number because that would take up less space than the 5,000 pages of 1's and 0's.

110101000101011111011011111110111100 = 57000443836 in decimal. So why don't we store programs in decimal and convert that to 1's and 0's in real time?

Storage would be much less. Is that how compression works?

These may be crazy questions but it just entered my head earlier when thinking about what the binary of a program can represent.

Even shorter than decimal is HEX.... So why don't we store programs in Hex?

Any help appreciated on this one so I can wrap my head around it.

1.7k Upvotes

346 comments sorted by

View all comments

Show parent comments

3

u/twbluenaxela Apr 29 '22

What? How is that possible? How did the source code for a program be in a prime number

61

u/captainAwesomePants Apr 29 '22 edited Apr 29 '22

Here is a prime number with 1401 digits:

48565078965 7397829309 8418946942 8613770744 2087351357 9240196520 7366869851 3401047237 4469687974 3992611751 0973777701 0274475280 4905883138 4037549709 9879096539 5522701171 2157025974 6669932402 2683459661 9606034851 7424977358 4685188556 7457025712 5474999648 2194184655 7100841190 8625971694 7970799152 0048667099 7592359606 1320725973 7979936188 6063169144 7358830024 5336972781 8139147979 5551339994 9394882899 8469178361 0018259789 0103160196 1835034344 8956870538 4520853804 5842415654 8248893338 0474758711 2833959896 8522325446 0840897111 9771276941 2079586244 0547161321 0050064598 2017696177 1809478113 6220027234 4827224932 3259547234 6880029277 7649790614 8129840428 3457201463 4896854716 9082354737 8356619721 8622496943 1622716663 9390554302 4156473292 4855248991 2257394665 4862714048 2117138124 3882177176 0298412552 4464744505 5834628144 8833563190 2725319590 4392838737 6407391689 1257924055 0156208897 8716337599 9107887084 9081590975 4801928576 8451988596 3053238234 9055809203 2999603234 4711407760 1984716353 1161713078 5760848622 3637028357 0104961259 5681846785 9653331007 7017991614 6744725492 7283348691 6000647585 9174627812 1269007351 8309241530 1063028932 9566584366 2000800476 7789679843 8209079761 9859493646 3093805863 3672146969 5975027968 7712057249 9666698056 1453382074 1203159337 7030994915 2746918356 5937621022 2006812679 8273445760 9380203044 7912277498 0917955938 3871210005 8876668925 8448700470 7725524970 6044465212 7130404321 1826101035 9118647666 2963858495 0874484973 7347686142 0880529443

Consider this as a number. This is its decimal representation, but you could also write it in binary as a number with 4500ish bits. You could save those bits in order as a file. If you were to do that, you would in this case find that it is a gzip file, and if you opened that gzip, you would find the source code to a program that breaks the encryption on DVDs.

22

u/twbluenaxela Apr 29 '22

So you're telling me, if I take this number, throw it into an online binary converter, then take this number, throw it into notepad, save it, then it will convert to a gzip file automatically? And when I open that zip file, it will have the source code to break the encryption on DVDs?

51

u/captainAwesomePants Apr 29 '22

Yes.

I'll take it that you completely understand that files are just bits and bytes, and bits and bytes can represent anything, including one giant number. So you understand that a large number can just be a representation of a file, and it follows naturally that one could make a gzip file containing the source code and convert that into a number like the one above.

Your confusion then is how the hell somebody could specifically aim to make that number be prime. That's a great thing to wonder about, and the answer is very clever. You see, gzip is special because if you add a few bytes to the end of it, nothing changes. It still decodes to the same thing. The bytes at the end are completely ignored. So, let's say you start with the gzip file you want, and then you add 2 bytes of zeroes at the end. Now turn that into a number. Now do this:

  1. Check if the number is prime. If so, we did it! Stop.

  2. Increase number by 1.

  3. Goto 1

Pretty good chance that one of the next 65536 or so numbers is gonna be prime.

Here's the creator's explanation of how he worked it out: http://fatphil.org/maths/illegal1.html

30

u/twbluenaxela Apr 29 '22 edited Apr 29 '22

That is absolutely insane. Thank you so much for taking the time to write this up.

So like, there is a prime number out there with every password I will ever have, every comment I will ever write, every picture I've ever taken.

That is crazy.

26

u/captainAwesomePants Apr 29 '22

There is a prime number out there containing a photo of your true love and the greatest poem that the greatest poet could ever write about them.

4

u/drake588 Apr 29 '22 edited Apr 29 '22

Why do you keep talking about prime numbers? What is the significance of prime? Why is that important? Can't a regular number divisible by 2 or 3 be just as good? Why are you so certain the photo is a prime number? Is there some computing law that says only prime numbers can become code?

2

u/erevos33 Apr 29 '22

Not computing law. Mathematical law. A prime number is divisible by 1 and itself, only. Thats it. So a prime number is basically unique. You cant divide it by any other number. The number 24 is divisible by 1,2,3,4,6,8,12,24. The number 23 only by 1 and 23. So if i encode a message into a number, which one do you think you can tell with certainty that you have found to be correct? The one with many divisors or the one with the unique divisor?

And i am not an expert on primes and math and programming. But keep in mind that programming is basically math (thats how the computer understands it).

4

u/drake588 Apr 29 '22

So if i encode a message into a number, which one do you think you can tell with certainty that you have found to be correct? The one with many divisors or the one with the unique divisor?

You're gonna have to explain that better because I still don't understand why that code NEEDS to be prime. I understand what a prime number is lol.. but not why code has to be prime. Why are you even looking at divisors in the first place? None of this makes sense. Why can't a picture represent the number 6500?

2

u/WatTheFork Apr 29 '22

I'm just formulating an hypothesis, the answer could be a problem with ambiguity. Almost all programs are deterministic (I say almost because you can create algorithms that do not necessarily will work 100%, unfortunately, I don't remember the name right now).

Say that you create a program A and translate this program to a number, imagine the number 8. A third person could take this number and have 3 different programs out of it corresponding to the numbers 2, 4, and 8 Which one of those are your program? Which one of those programs does what your program do? This person wouldn't know.

→ More replies (0)

1

u/erevos33 Apr 29 '22

I am not a programmer but think of the unique aspect mentioned. and combine it with mathematical operations.

A picture that can be identified by a unique number is better than a picture that can be identified by many. We are looking at divisors, since you asked, because everything is math. A picture that uses a prime number as a key for encoding has only that key as a point of attack. Thats the cyber security aspect.

As to why a program or a picture cannot be an even? Uniqueness.

I am afraid i am not knowledgeable enough to explain to you the why, i understand it enough but cant convey the why in a simple way. Maybe this article will help a bit:

https://codecoda.com/en/blog/entry/prime-numbers-and-the-importance-to-modern-life

→ More replies (0)

0

u/[deleted] Apr 29 '22

[deleted]

→ More replies (3)

8

u/Dranks Apr 29 '22

Can i just say i love when people are happy about things? Like, the amazement you have at this makes me happy

3

u/[deleted] Apr 29 '22

so why did he use gzip, when the source code could just be a text file? because of this property (being able to append other data which will be ignored so it can become a prime number and get archived on that list)?

7

u/TheMcDucky Apr 29 '22

Compression -> Smaller file -> Easier to find in a prime.
You wouldn't need to gzip it to pad it with garbage data.

→ More replies (1)
→ More replies (5)

13

u/AromaticIce9 Apr 29 '22

Yes*

* but not exactly, I don't think you could use notepad for example. You would need a program that writes those digits to the file directly, without any converting or adding anything to the file. Probably convert the number to hexadecimal and use a hex editor.

You'd also probably need to name the file appropriately by adding the fine extension on Windows.

Also I'm assuming that number actually does the thing he says it does.

12

u/Sol33t303 Apr 29 '22 edited Apr 29 '22

It'd be easier to convert it to hexadecimal and throw it a hex editor, hex editors are made specifically for editing the raw binary in files (usually used in reverse engineering, applying hacks to closed source and old games, data recovery, etc. It's a more common thing to do then some might think), so they shouldn't give any formatting troubles.

1

u/[deleted] Apr 29 '22

thank you this is really helpful.

so do i understand it correctly that the only reason he needed it to be a prime number was to be able to archive it on this website which proves primality of large numbers?

→ More replies (3)
→ More replies (1)

10

u/Triple96 Apr 29 '22 edited Apr 29 '22

Same as what the OP of this post is talking about. Take a program, compile it into binary and convert that binary to decimal. In this case apparently that decimal value is 1401 some number 1401 digits long.

Edit: 1401 digits long, not 1401

10

u/acamann Apr 29 '22

not equal to 1401, equal to a number that is 1401 digits long. Way mind blowinger

5

u/Ruhnie Apr 29 '22

I certainly just had my mind blowinged!

4

u/Triple96 Apr 29 '22

Oh damn, yes good catch. WAAAYYY bigger

2

u/[deleted] Apr 29 '22

I think he's saying 1401 is the amount of decimal placeholders of the decoder program, not the decimal number itself (which would be very large)

2

u/Triple96 Apr 29 '22

Yep, I just corrected my comment good catch!

1

u/protienbudspromax Apr 29 '22

You are getting it in reverse. First ofcourse there can be many different implementation for the same output right? Can write a program in many different ways.

When you write and finally compile it into a binary executable what do you get?? A long binary string. It is just a bunch of ones and zeros when compiled. And any combination of a 1's and 0's is just a binary number.

In this case what must've happened is that the prime number when taken in binary form ended up doing the same thing as what the gzip program did after being compiled for that specific architecture.