r/programming • u/walen • Jun 12 '17
TIL the current hash function for Java strings is of unknown author. In 2004 Joshua Bloch "went so far as to call up Dennis Ritchie, who said that he did not know where the hash function came from. He walked across the hall and asked Brian Kernighan, who also had no recollection." [x-post /r/java]
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=40456221.6k
u/namekuseijin Jun 12 '17 edited Jun 12 '17
Fascinating bit of quite recent (1997) historic trivia going back even further ('70s). That's good digital archeology, a hacking art itself.
If neither Steele, Ritchie, Kernighan and many of those serious computer scientists from the '70s know its origins, we can be pretty sure it's alien tech, probably first implemented in Lisp. ;-)
edited to appease friendly grammar nazis, thanks all. :)
451
u/TargetBoy Jun 12 '17
Probably the NSA like IBM's encryption algorithms from the '70's.
53
u/rmxz Jun 12 '17
... like IBM's encryption algorithms from the '70's.
Or like BitCoin's software - which also has unknown (to almost everyone) authorship.
16
u/winlifeat Jun 12 '17
Not everyone ;)
→ More replies (3)5
u/HoJo3030 Jun 13 '17
I would love for your comment to have ~78000 upvotes sometime in the mid-2020s.
→ More replies (15)167
u/namekuseijin Jun 12 '17
BTW, reflections on past security reflections, or, On trusting trust, by Ken Thompson:
104
u/YeenaGreustInATree Jun 12 '17
Someone who used Ken Thompson's Trojan Horse to great effect appears in Mick Stute's worst nightmare. https://www.quora.com/What-is-a-coders-worst-nightmare
→ More replies (24)552
u/LeifCarrotson Jun 12 '17
That was hard to get to (had to create a temporary Quora profile and log in),so I'm backing it up here:
*This was mine:
I was hired by a psychologist to fix a program that seemed to have "strange output" written by one of his ex-grad students. It was a program that reads a data file, asks about 50 questions, does some calculations, and comes up with some score based on this PhD's research. It's on a research 3B2 at the university. He demonstrates the program and sure enough there seemed to be strange flashing words on the screen when it moves from question to question, and they don't seem nice. I agree to do it, should be pretty straightforward, so he'll pay me by the hour to determine how big the fix is and then we'll agree to a fee.
Day 1 I sit down at the 3B2 and login to the ex-grad student's account that has been given to me. This is where the code resides. I examine the C code. It is written to be hard to read. All the code is squished on one line. It's spread over 15 files with about 3 functions per file -- all on one line. All variable names are just three, seemingly random, letters. I talk to the guy and agree to go with hourly on this (great decision). I untangle all the code and format it nicely so I can see it.
It was done on purpose. It used the curses library to move to a point on the screen, print a question and the answers, and wait for a response. But it first went to the first line of the question, printed some white supremacy message, waited 1/2 a second, and then overwrote it with the question. This ought to be simple. There are only about five places it could output anything, and all of them had this subliminal flash of a message. Each one was hard coded. No problem. Delete the offending mvprintw() and all is well. Or should be. I compile, thinking I'm done. But when I ran it, there it is again -- the subliminal messages. This time with different text still the same subject, just different messages.
I check my code and believe it or not it's back to the initial state I found it. 15 files, mangled, 3-letter variables -- the whole thing right back where I started. I want shoot myself for not making a copy of my code. I unmangle again, this time putting it in three files, named differently. I make a copy of the whole directory, and I mark the files readable only. I compiled it. All looks good. I run the program. There's now a copy of the original 15 files in the directory along with mine and the subliminal messages are back.
Okay, so somewhere on the disk is the source code necessary to keep doing this and he's set the program up to pull in that code when you compile it. I do a full disk search in the include areas (/usr/include) and since this is a research version we have source for just about everything but the kernel itself. That's a lot of header files and this takes some time on the 3B2, so that's day 1.
Day 2 The disk search showed up nothing. The strings were apparently either encrypted or they are buried in a library somewhere. Because I don't have check sums of all the original executable objects, I decide to search all libraries for the text. This is even longer than before, so day two is over.
Day 3 No results. The strings are encrypted. That means I'm going to have to follow all the header files from each #include and each one they #include to find where this is. And that will, take some time. We do alert the campus computing department that we believe someone has gained root level access to Dr. Phelps research computer, which is just a shared lab computer in the science building. They're understandably not convinced.
I start unwinding the #include files. I do that, nowhere do I find the code. So now I know it's compiled in a library. No problem at all. Why not just recompile all those libraries, we do have the source after all.
Days 4-6 The hardest part, convincing the campus nerds they have an issue. But we finally do and Mark, the Unix admin who was hired because he married the Dean's daughter, gets busy learning how to do this. In the end, he agrees to allow me to handle it, because he just doesn't really know how to get all that stuff compiled. End of Day 6, all standard libraries are recompiled. Woo hoo!
I whip out my modified, cleaned up source and start the compile. All looks good. I run it. O M G. It did it again. 15 messed up source files and the subliminal messages are back. This is suddenly like magic. I investigate very very carefully though I am stumped. This code doesn't exist in source code. I think I might be beaten. Dr. Phelps isn't happy with the hours involved and thinks maybe we ought to just rewrite the program from scratch. "Sure", I say staring at the terminal like a lost puppy too deep in my thoughts to put out of my thinking mode, "I think you're right. That will be quicker." "Good," he says, "we can start tomorrow."
Day 7 To hell with that. This guy isn't beating me. We are compiling it from his stinking code or not at all! "You don't have to pay me anymore, Dr. Phelps, I just want lab time." This is nerd war.
Days 8-14 I get smart, I'm thinking he somehow modified the curses library. I compile the curses code to assembly and though I don't know 3B2 assembly (yet!), I start learning. I read manuals for 6 days, piecing together that assembly code. Waste of time, nothing seems unusual.
Day 15 I suddenly realize it's in the compiler. It was the compiler. And every time you compile the original code and run it puts in the subliminal message code into the source code. I'd heard of this before.
Ah ah! I've got him!!!! We have the source code for the compiler as well. I search through it looking for a reference. Lo and behold, I find it. Indeed. There is source code in the compiler/linker that does this:
it examines any call to fopen(), searches the file opened looking for Dr. Phelp's questions; if it finds them then
it rewrites the 15 files to the current directory when compiling that specific program.
It then compiles Dr. Phelps program using the 15 files and outputs to the -o name in the link phase.
The compiler was modified to put that code in Dr. Phelps program was written by the man that modified the compiler.
Several days later, an AT&T tech shows up with a disk and loads the proper compile and linker source and we recompile the compiler from the source. That solves it. All the bad source in the compiler is gone and we've got a new clean copy of the compiler.
Except it didn't. Because the compiler was poisoned with other source code that we didn't have. And that source code, that now existed only in the executable compiler, put those changes back into the compiler source before it compiled it. But this time it didn't modify the /usr/src copy, it copied it to a hidden directory, modified the compiler source, compiled itself from there, and deleted the hidden directory. It took an AT&T tech to find this. The ex-grad student had poisoned the compiler to poison itself when it was recompiled. We had to put a new binary version of the compiler on disk from another 3B2 running the same revision before the problem went away.
We also found that if /sbin/login is compiled it puts in a backdoor allowing anyone who uses a specific password to login in as the root user. This computer is accessible by modem and Tymnet. Finally, this gets the computing center's attention.
Genius! But put to a horrible cause.*
70
u/LasseF-H Jun 12 '17 edited Jun 12 '17
For a while there i thought i was on /r/nosleep edited because of danish autocorrect
→ More replies (1)62
u/unkz Jun 12 '17
Back when an AT&T tech was someone the regular experts called when they needed serious expertise.
→ More replies (1)104
33
u/CaptainMurphy111 Jun 12 '17
Reminds me of a story my dad told me about a guy he use to work with.
This guy wrote the operating system for their mainframe, and it was pretty successful so the company started selling it to other companies. But then they started getting complaints from the female operators about rude messages and jokes, also the printers would occasionally print dirty songs out. So they asked the guy to fix it but he outright refused.
They tried to get the other programmers to fix it, I don't know how the mainframes worked back then (I'm guessing they had to modify the program while it was running) but my dad said it was impossible to remove the stuff because the machine was constantly moving the memory pointers and data about, so they would locate the rude words but it would move before they could fix it.
→ More replies (2)53
u/sirin3 Jun 12 '17
The ex-grad student had poisoned the compiler to poison itself when it was recompiled.
Of course it was a grad student
Only a grad student has the time and the skills for that.
21
u/industry7 Jun 12 '17
Assuming this is all true, which if I'm being honest is very hard to believe. But assuming it's true, it shouldn't be too hard to track down the name of the person who was originally hired to write that program.
→ More replies (1)21
u/za4h Jun 12 '17
Gripping drama.
I wonder why Hollywood uses shitty "hacking" scenes when they could adapt something like this instead, where the dev is navigating the maze of an evil geniuses mind.
→ More replies (5)17
u/alexbuzzbee Jun 12 '17 edited Jun 13 '17
I think the /sbin/login thing was actually done by
either Ritchie or Kernighan[it was Thompson]. I heard a story once that he modified the compiler to compile itself to compile /sbin/login to add a backdoor, without it showing up in source. Then he recompiled the compiler using the modified compiler from the original source, and bam invisible backdoor-dropper-dropper.→ More replies (2)13
u/slash213 Jun 12 '17
Thanks. It was impossible to find it on mobile. I wonder if the ex-grad ever read this story.
→ More replies (6)12
44
u/alreadyburnt Jun 12 '17
Reflections on security reflections on this subject of trusting trust! https://www.dwheeler.com/trusting-trust/
12
→ More replies (1)7
Jun 12 '17 edited Sep 14 '17
deleted 0.9229 What is ^^^this?
3
u/NOX_QS Jun 12 '17
Hey fellow TU/e person! I had the same fuzzy feeling inside, seeing that win.tue.nl link 😁 ah, memories
32
u/russellbeattie Jun 12 '17
I love that story!! IBM comes up with DES for the US government, and then NSA appears and says "Ummm... you need to tweak the algorithm a bit like this, oh and use a shorter key." The former actually made the algorithm stronger (a fact not discovered for 20 years) helping protect US secrets, while the latter made it weaker to brute-force attacks. But since the NSA was the only org capable of such an attack at the time, they were OK with that.
I wonder how far the ahead the NSA still is, if at all? Already, China's top supercomputer is literally 10x faster than than the US's top publicly known one... and they have more total supercomputers in the Top500 than the US as well. Plus all of our chips are made there. It makes you wonder.
26
u/gimpwiz Jun 12 '17
Our big chips are not made in China. Intel manufactures in the US, Ireland, Israel. Everyone else manufactures in the US, Taiwan, and a couple other places.
China's Tihane 2, or however its called, is a big fucker made of Intel CPUs and AMD/Nvidia GPUs (can't be arsed to confirm which). It's big, but not as big as you claim.
→ More replies (3)6
u/ttul Jun 13 '17
Guaranteed the NSA has special purpose hardware for codebreaking that is utilizing the latest process technology, and is absolutely top secret. They used to make their own chips at Fort Meade but now outsource that. Nevertheless, custom Silicon has always been a big part of what they do.
→ More replies (4)4
u/Throwaway_bicycling Jun 13 '17
China's top supercomputer is literally 10x faster than the US's top publicly known one...
Where to start...
Any nation-state with real things to accomplish would never publicize anything much about their key computing resources.
China know this as well.
There might still be some reason why anybody would care about the Top500 list these days, but it's not obvious why.
The massive OPM hack didn't require anything very fancy on the part of China, to the extent where I kind of wonder whether there was a deeper story going on here. But probably not.
→ More replies (13)6
u/The_fartocle Jun 12 '17 edited May 29 '24
zesty chubby oil snobbish correct dam possessive tie brave office
This post was mass deleted and anonymized with Redact
63
Jun 12 '17
They got it from Stackoverflow, obviously.
14
69
u/vtable Jun 12 '17
If it were first implemented in Lisp, Guy Steele would surely know. The only conclusion is that Steele is covering up the alien tech.
8
22
u/leeringHobbit Jun 12 '17
I don't know much about programming. Does the headline imply Java uses an algorithm that was first written in C?
4
u/kanuut Jun 13 '17
I think it is.
It's not surprising tho, this sort of algorithm is based on some pretty solid maths and doesn't need to have the latest//greatest hashing, this one works fine
→ More replies (3)8
u/namekuseijin Jun 12 '17
Does the headline imply Java uses an algorithm that was first written in C?
what's so surprising? many algorithms from the '70s still run today. quicksort? string-comparison algorithms? There hasn't been much better algorithms than the ones compsci gods created in the '50s, '60s, ''70s already...
not sure, but also guessing first java compilers and toolset may have been originally written in C/C++ like so many system tools those days...
31
30
Jun 12 '17
No one wanting to claim some bit of code is usually because it is really badly written, not because it is divine.
/code review revue -- chorus and verse
8
u/modernbenoni Jun 12 '17
"Farther" only refers to distance, here "further" would be right! Not to detract from the humour of your comment though :)
→ More replies (2)3
u/namekuseijin Jun 12 '17
thanks, not native english speaker. Although decades into this weird german-fathered language, it can still surprise me in nooks and crannies (always wanted to use that expression)
→ More replies (5)36
u/Konkurrenz1 Jun 12 '17
70's
70's
70's what?
72
u/The_fartocle Jun 12 '17 edited May 29 '24
quaint yoke concerned simplistic toothbrush encouraging chop amusing point aspiring
This post was mass deleted and anonymized with Redact
18
u/rspeed Jun 12 '17
I think "70s" is acceptable, even when referring to the 1970s.
→ More replies (6)→ More replies (1)10
u/aztecraingod Jun 12 '17
Having flashbacks to when I was a reporter for my high school paper and I couldn't convince the editor to care about this error.
→ More replies (4)5
u/eythian Jun 12 '17
This is not how apostrophes work with numbers. You are committing incorrect over prescriptive apostrophe abuse.
For numbers, with or without are both quite OK.
→ More replies (4)8
u/The_fartocle Jun 12 '17 edited May 29 '24
groovy wasteful tie illegal rotten worry combative touch retire cats
This post was mass deleted and anonymized with Redact
2
→ More replies (2)2
382
u/Tumors Jun 12 '17 edited Jun 12 '17
This type of string hash function is sometimes referred to as djb2, named after Daniel Bernstein although he only claims to adding an initial value of 5381 to an existing algorithm. In the same usenet discussion, Chris Torek (mentioned in the op) also talks about the hash function a great deal.
edit: here Torek claims he stole the hash function from Gosling Emacs which was created by James Gosling, the creator of Java
146
u/goin_home Jun 12 '17 edited Jun 12 '17
Gosling Emacs source code can be found in a tarball here on GitHub.
This features the following code in abbrev.c:
static unsigned hash (s) /* hash an abbrev string */ register char *s; { register unsigned h = 0; while (*s) h = h * 31 + *s++; return h; }The header of this file states:
/* Unix Emacs Abbrev mode */ /* Copyright (c) 1981,1980 James Gosling */edit: More info about the source code can be found in the README.md, as can be seen in this commit.
About publishing the files:
Brian Reid: "Fine with me. I consider them to be James' property and not my own."
James Gosling: "It’s fine with me too. Archaeology is a good thing :-)"
38
u/za4h Jun 12 '17
I am used to ANSII C, so this function signature looked like alien madness to me. But it led to me learning about K & R C, where one would put stuff like "register char *s;" between the function signature and the function body.
So if I ever find a time machine and wish to become a 70's C developer, I will be better prepared thanks to this code snippet. Thanks!
9
4
u/SmartassComment Jun 12 '17
You don't have to travel back so far. I was working for a company that used the pre-ANSI form of function declarations in 1988.
13
u/uptotwentycharacters Jun 12 '17
NetHack is still being actively developed today and it uses K&R style syntax, probably because it came out in 1987 (and being text based, portability to a variety of platforms was considered important), and changing it to the modern style would be too much work for no real gain.
→ More replies (1)2
u/uptotwentycharacters Jun 12 '17
So K&R C allowed you to use the
registerspecifier on function arguments? That's not legal these days, is it? Did it alter the calling convention somehow, or did it copy the value of the argument into some kind of implicit register-specified local variable?6
Jun 13 '17
I don't entirely understand your question, and I hope this isn't cringefully pedantic, but in both K&R C and the first ANSI C, "register" is merely a directive to the compiler suggesting that it store the value in one of the processor's actual, physical registers, rather than in RAM, simply because the code will access it so frequently that doing so will speed up execution. Of course, if a spare register wasn't available, the compiler could choose to store the value in RAM or wherever. Compilers have gotten so good at optimizing, I guess this isn't done anymore. I apologize if this is all well-known to you.
→ More replies (1)27
u/manuscelerdei Jun 12 '17
Still my go-to hash function when doing any systems programming. Simple, fast, good enough for dictionary bucketing.
34
u/gprime311 Jun 12 '17 edited Jun 12 '17
Can you explain how it works? I don't understand "while (*s)"
Spez: He gets it now, thank you everyone.
34
u/manuscelerdei Jun 12 '17
That just means “while the character pointed to by this pointer is not 0”. C strings end with a null terminator character, which is the ASCII value of 0 (not the character 0). So it tells the function when to stop.
15
u/Rangsk Jun 12 '17
Strings in C always end in a 0 character, so this loops until the end of the string.
Pseudocode would be something like:
H = 0 For each character C in string S: H = H * 31 + (ascii value of C) Result is HMore modern versions of this function will start H at a different initial value, which gives it better properties.
→ More replies (1)42
u/Vystril Jun 12 '17
Strings in C always end in a 0 character
If you're lucky. :)
→ More replies (11)24
→ More replies (3)2
u/haminacup Jun 12 '17
The variable s increments along the string, pointing to the next character each loop. The while loop will continue as long as s points to something non-zero. When s points to the end of the string (the null character, ASCII value 0) then the loop terminates.
3
u/gprime311 Jun 12 '17
When s points to the end of the string (the null character, ASCII value 0) then the loop terminates.
That's what I was missing. Thank you.
113
u/zeekar Jun 12 '17
The JLS 20.12.10 (page 535) describes the java.lang.String.hashCode method in great detail. Unfortunately, it doesn't describe what the code actually does.
The perils of maintaining documentation separate from the code..
57
u/PM_ME_YOUR_SMlLE Jun 12 '17 edited Jun 12 '17
Unfortunately, it doesn't describe what the code actually does.
every software utility's website
29
u/caboosetp Jun 12 '17 edited Jun 13 '17
Our software, "Extremely fast quantum super optimized efficient" is the best of its class.
Whether you need the latest industry standard or blazing fast execution, we guarantee our software will fulfill all your business needs.
Installation is quick and easy. Simply and with a few clicks of the mouse, you'll be up and running in no time!
Get it now!
4
538
u/mariopenna Jun 12 '17
That's a dumb question. Of course it was Satoshi Nakamoto!
216
u/yawaramin Jun 12 '17
Wrong. It was definitely the NSA trying to backdoor Java!
148
Jun 12 '17
Goddamn NASA always in my emails
→ More replies (1)257
u/JoseJimeniz Jun 12 '17
NASA created ISS
86
13
10
u/Pastrami Jun 12 '17
I assume you are making a joke, but serious question: Could a back door in this code really be used for anything? What is the purpose of this code? I hear "string hashing" and I assume it's the hashing algorithm used to generate the hash key for storing strings in hash tables.
12
u/mirhagk Jun 12 '17
Hash functions are used for security, however only a very incompetent developer would ever use this hash function for anything security related.
6
u/nerd4code Jun 12 '17
Something like this is mostly used for indexing in a hash table, so you could come up with a DoS attack by pumping in strings whose hashes all collide. E.g., if the hash table uses chaining you might be able to create a huge bucket, so that every search into that bucket takes a large amount of time. This kind of attack is made a little more complicated in the specific hash-table sense by Java resizing the hash table behind-the-scenes, since whatever the hash is will usually be modded down to fit into the table.
However, there’s other stuff you can do with the hash that’s more or less susceptible to fuckery. E.g., if the hash is kept along with the string, then you can use it to short-circuit an entire string comparison when two hashes differ; with long strings, you could force the serve to only use the character-by-character comparison, which with long strings might significantly slow it down.
11
u/TinyBreadBigMouth Jun 12 '17
That is correct. I guess theoretically the NSA could be using it to get every string value in your programs, but I think someone would notice if hashing went from being near-instantaneous to depending on your internet speed.
→ More replies (1)4
Jun 12 '17
Well, a poorly chosen hash function could have performance implications, either by taking too much time to compute or having too many collisions for real world input (the post above links Chris Torek's remarks to that effect). I don't think there are security implications beyond the possibility of denial-of-service by spamming things with identical hashes, but making something unnecessarily slow is also a quality of service issue.
→ More replies (2)2
u/user_82650 Jun 12 '17
Not in any direct way... however, computer backdoors can be incredibly subtle and complicated. You can leak data by simply making a function take longer on certain inputs, for example, or you could return values that triggered a bug in a different library, etc.
So probably not, but it's not impossible.
2
u/narwi Jun 12 '17
Could a back door in this code really be used for anything?
Yes. If you has user supplied input then they can at least dos you by supplying strings that always hash to the same bucket. By extension you can do various analyses depending on response times after you have fucked with the hash table. This is only somewhat far fetched.
7
u/user_82650 Jun 12 '17
Trick question. "Satoshi Nakamoto" is just an alias for a secret alliance of the world's top 20 intelligence agencies working to covertly protect humanity from the Martian infiltration.
→ More replies (1)7
Jun 12 '17
Double trick question, "the worlds top 20 intelligence agencies" are just an alias for machinations of the giant Satoshi Nakamoto brain at the center of the Earth.
11
3
20
u/CMDR_welder Jun 12 '17
Is there also amateurgramming?
2
u/iamonlyoneman Jun 12 '17
http://www.etymonline.com/index.php?term=program The "pro" in programming comes from the wrong root word for that to make sense.
9
u/CMDR_welder Jun 12 '17
I dont speak english daily, it just seemed like funny wordplay to me
9
u/iamonlyoneman Jun 12 '17
a) the joke almost works
b) never feel bad about your English because it's probably 1000% better than the (insert your native language here) of most redditors!
→ More replies (1)14
3
66
u/L0d0vic0_Settembr1n1 Jun 12 '17
I find it pretty cool that there is stuff in IT with origins already lost in history. Like the "what the fuck" constant in the fast inverse square root calculation.
72
u/Askolei Jun 12 '17
The fact mathematicians could not get the exact same constant via calculus makes it even better.
He first computed the optimal constant for the linear approximation step as 0x5f37642f, close to 0x5f3759df, but this new constant gave slightly less accuracy after one iteration of Newton's method. [He then found another, more optimal constant.]
This whole function is legendary, I can almost smell coffee in the comments.
30
u/epicwisdom Jun 12 '17
Hardware always wins:
The algorithm generates reasonably accurate results using a unique first approximation for Newton's method; however, it is much slower and less accurate than using the SSE instruction rsqrtss on x86 processors also released in 1999.
→ More replies (4)3
u/jms_nh Jun 13 '17
Unless it's wrong or the spec changes. Or the manufacturer doesn't have the resources to design and test a hardware implementation.
4
u/epicwisdom Jun 13 '17
True, there are disadvantages. Otherwise, of course, programming as a profession wouldn't exist. But generally these sorts of micro-optimizations are obsoleted by hardware improvements, compiler improvements, standard library additions, etc.
12
u/mxzf Jun 12 '17
Yeah, magic numbers pop up from time to time in programming. We try to avoid them when at all possible, but sometimes it's just not practical.
Currently, I've got a bit of a magic numbers table in my code that's just a lookup table for converting 0.01-3.00 standard deviations to percentile probabilities. As it turns out, there's no good way to calculate that directly, the only technique I found was to just calculate a couple thousand values for the normal distribution at that std and then count the values to find percentages; so I just used the lookup table and called it a day.
10
u/Zetho Jun 12 '17
Will you include documentation in code, explaining what the table is, to future archeologists?
→ More replies (1)4
u/mxzf Jun 12 '17
Well, I should. If it'll actually happen is somewhat up for debate. I can say that it will probably happen eventually, but that's about as much as I'm comfortable stating ATM.
9
u/pdp10 Jun 12 '17
You make it sound like pasting your comment above into a code comment is a major and painful undertaking.
→ More replies (2)2
u/ReversedGif Jun 12 '17
Currently, I've got a bit of a magic numbers table in my code that's just a lookup table for converting 0.01-3.00 standard deviations to percentile probabilities. As it turns out, there's no good way to calculate that directly, the only technique I found was to just calculate a couple thousand values for the normal distribution at that std and then count the values to find percentages; so I just used the lookup table and called it a day.
Sounds like you should be using the CDF of the nornal distribution... (which, admittedly, uses the error function, which has no closed form, but most languages have it, and failing that, you can copy-paste an implementation). It will be much more accurate than approximating by sampling ever could be.
2
u/mxzf Jun 12 '17
If I remember correctly, I looked into CDFs when I was implementing it in the first place (a year or so ago). If I remember correctly, it was unreasonably either complex or slow for what I'm doing (and with minimal accuracy benefit over the lookup table I method I'm using).
For context, I'm getting probability values for ~3k ~2000x4000 element numpy arrays, and trying to get the calculations done in a reasonable time frame. A lookup table that's accurate to 0.01 standard deviations is good enough for what I need without taking painfully long to process.
79
u/reini_urban Jun 12 '17
Even in 2004 they didn't get the purpose of prime table sizes:
"Also, the performance of this class of hash functions seems largely unaffected by whether the size of the hash table is prime or not. This is important because Java's auto-growing hash table is typically does not contain a prime number of buckets."
prime sizes are mostly important for hash table security, not performance and not quality. Of course power of 2 sizes offer the best performance, whilst prime sizes guarantee that all bits of the hash are used, not just the last n. Which makes it much harder to brute-force hash table chaining DOS attacks. Which was fixed in java some years later properly, unlike in most other implementations which solely rely on a private seed.
22
u/nupogodi Jun 12 '17 edited Jun 13 '17
Collisions are decreased if the hash table size is co-prime with the constant used in certain types of hash functions. If we can't necessarily control the hash function, by picking a hash table size that is prime, we guarantee it will be co-prime with any [edit: smaller] constant.
More here: https://stackoverflow.com/questions/1145217/why-should-hash-functions-use-a-prime-number-modulus
→ More replies (2)7
u/mxzf Jun 12 '17
by picking a hash table size that is prime, we guarantee it will be co-prime with any constant.
You can't even guarantee that; any prime number
p(such as one picked for a hash table size) has some other number that's equal to2*p(and other larger numbers) that it's not co-prime with. A prime number is only co-prime with smaller numbers, you can't make that same claim for all numbers.A large enough prime number will make the chance of it being co-prime very small, but you can't guarantee that it's never co-prime with another number, just that it's not co-prime with any smaller number.
4
8
u/boink_that Jun 12 '17 edited Jan 14 '20
Facepalm just reading this and all the responses so far. If you've ever actually written an open addressing hash table with double hashing collision resolution you'd know that the operative question is: how many times do you probe in collision resolution before giving up? The answer is n times, i.e. the size of the hash table. Your collision resolution code is for i=0, i<n; i++. Since your probe increment is a secondary hash that can be any size between 1 and n, the only way you can guarantee NOT reprobing a previously probed location, is by ensuring that the probe increment is not a factor of the size of the hash table. Thus the size of the table needs to be prime. Prime has nothing to do with performance.
A note that may not be obvious, every location in the hash table should have the opportunity to host a value. So just work it forward starting with a probe increment of 1, then 2, and so on. Look at the case where the table size is even and the probe increment is 2... Boom half the table is inaccessible. Not a problem if the table is prime.
→ More replies (4)0
Jun 12 '17
[deleted]
5
u/narwi Jun 12 '17
For any hash function and prime, input sets that behave worst with the prime (and not with a power of 2 size) can be found just as easily.
→ More replies (1)9
u/smokeyrobot Jun 12 '17
I think the redditor /u/reini_urban was saying that it wasn't done for the purpose of performance gains and quality. Rather the performance gains were a pleasant secondary effect.
3
u/EsperSpirit Jun 12 '17
You can read a good write up on prime vs powers of 2 here: https://probablydance.com/2017/02/26/i-wrote-the-fastest-hashtable/
0
94
u/Arandur Jun 12 '17
Man, I thought K&R were supposed to be smart. Didn't they know about git blame?
69
u/boba-fett-life Jun 12 '17
Don't be silly, git didn't exist back then. Everyone used mercurial.
39
u/Arandur Jun 12 '17
Truly a dark and wretched time.
→ More replies (23)5
u/Nvrnight Jun 12 '17
Am I the only one who prefers mercurial to git? git seems to be way over-complicated to me. Plus with the TortoiseHg GUI which works on Windows and Linux, everything is just intuitive to me.
→ More replies (1)9
Jun 12 '17
Nah, I've heard stories that they used beta versions of IBM Rational ClearCaseTM, which, legend has it, was written by Satan himself.
9
u/DragonMoose Jun 12 '17
No, Satan devoted all of his time and effort to SourceSafe.
→ More replies (1)→ More replies (2)3
Jun 12 '17
I was still using clearcase at work until about a year ago. Gorgeous lesstif UI (in pepto-bismol pink) and kernel compatibility for up to Linux 2.6.x!
We're finally on git for everything now.
4
Jun 12 '17
We used it up until a few years ago. We're on Subversion now and you know what, I can't complain. It does the job. Could be worse. I work with RF engineers that don't know what revision control is, let alone how to do a proper merge or branch. Git would be asking too much from these smart yet stubborn engineers.
I still laugh every time I think about how many IBM products we used to use, and how we've dumped all of them. Lotus Notes, other inferior IBM products such as ClearQuest, DOORS, and ClearCase. Good fucking riddance. What horrible fucking software.
→ More replies (1)3
u/agumonkey Jun 12 '17
They do, just in an alternate universe because they messed up their DeLoreane and can't come back (at least until Episode III)
9
u/kranker Jun 12 '17
Risk Assessment: Surprisingly small. Serialization of Hashtables does not depend on consistent hash values. It is possible that some customer has implemented some persistent data structure that relies on the String hash function not changing, but it is highly unlikely that more than a handful of customers have done so.
I am also surprised by this assessment. I would have thought that the hash would been persisted enough outside of the built-in hashtable to cause issues.
11
u/bwainfweeze Jun 12 '17
In java 5 they swapped the hash table implementation, and this resulted in a new ordering of values returned from the reflection API.
And this is how a bunch of people, including my team, discovered that they had unintended dependencies between their tests, because they started failing as soon as you upgraded.
1
u/yawkat Jun 13 '17
In Java 8 hashmap collision handling changed and this did break a bunch of code, mostly people depending on iteration order in unit tests.
118
86
u/jdgordon Jun 12 '17
This might be a dumb question, but why was he asking K&R about java internals?
108
u/silmeth Jun 12 '17 edited Jun 12 '17
Not about Java internals. About a hash function that has since found its place inside Java internals.
From the cited Bloch’s text:
While this class of hash function is recommended in The Dragon Book (P(65599)) and Kernighan and Ritchie's "The C Programming Language, 2 Ed." (P(31)), it is not attributed in either of these books.
So – Java copied an algorithm described in a few different sources, among others, their book, and Bloch tried to find the original author of the algorithm.
→ More replies (6)132
Jun 12 '17
This might be a dumb question
It is, because the answer is in the article you're supposedly reacting to. But also it isn't, because it's triggering me to give you the answer anyway.
The algorithm is mentioned in their book, so the author of the article called them up to ask where it came from, but they didn't know either.
→ More replies (1)29
u/jdgordon Jun 12 '17
well you know the line about the easiest way to get an answer on the internet is to write something obviously wrong and have a million people spam you with the correct answer! :)
55
u/karmabaiter Jun 12 '17
Ah, ok. So...
I came up with the string hashing function that is used in Java
42
Jun 12 '17
Neat. That along with 10 years of experience and you might be stand out enough to be interviewed for an entry level position. I'm looking for a new job and i'm dying inside
→ More replies (1)10
u/TheSpoom Jun 12 '17
Look, we just need 10 years of experience with HTML 5 because
that's what we'll tell the H1-B to claimwe need a very experienced developer.→ More replies (4)→ More replies (1)8
5
6
11
u/blackmist Jun 12 '17
It's not uncommon.
The infamous fast inverse square root function used in Quake 3 has no clear origins. It had simply been passed down for years as the best way to do something, and nobody remembers where it actually originated.
6
u/K3wp Jun 12 '17
https://en.wikipedia.org/wiki/Fast_inverse_square_root#History_and_investigation
The original author is Isaac Newton!
4
Jun 12 '17
That's nowhere near as commonly used as this hash function. Can you find another algorithm used in multiple languages with no known author?
3
5
u/peacepeace23 Jun 12 '17
According to Robert Sedgewick (Knuth's doctoral student) its based on Horner's Method
4
u/Taxtro1 Jun 12 '17
They say he is somewhere in the mesopotamian wasteland writing assembler code for ancient machines.
1
Jun 12 '17
Does anyone else find it really heartwarming that in 2004, K&R were still working in the same building? Wiki says that bwk has been at Princeton since 2000, but it seems that dmr was still at Bell Labs (later Lucent Technologies), so I'm not sure where they were that they could be "across the hall" from each other.
2
u/walen Jun 13 '17
This happened in 1997, not 2004. I got confused by the date that appears to the right of the bug report and messed up the title, sorry.
3
u/palordrolap Jun 12 '17
I guess the hash function operates in reverse on the string because alphabetically sorted words as strings and sorted numbers inside of strings change most often at the end of the string.
In reverse, the last character of the string is convolved with all the other characters. Done forwards, the hash of "1231" is literally one power of the base away from the hash of "1232" (modulo the size of int h).
The same problem exists at the other end (i.e. beginning) of the string, sure, but it ensures that similar strings that differ only at the end are given less predictable / more random / better hashes. (When taken with a strong pinch of subjectivity anyway.)
4
u/jms_nh Jun 12 '17
It doesn't operate in reverse.
It may look like it "operates in reverse" because of the specification in the Javadocs:
Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]but in reality the code looks like this:
public int hashCode() { int h = hash; if (h == 0 && count > 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; }When you start from index 0 you end up multiplying its contribution to the hash N-1 times, and index 1's contribution to the hash N-2 times, and so on.
→ More replies (2)
7
u/OriginalPostSearcher Jun 12 '17
X-Post referenced from /r/java by /u/walen
TIL the current hash function for Java strings is of unknown author. In 2004 Joshua Bloch "went so far as to call up Dennis Ritchie, who said that he did not know where the hash function came from. He walked across the hall and asked Brian Kernighan, who also had no recollection."
I am a bot. I delete my negative comments. Contact | Code | FAQ
6
2
2
Jun 12 '17
It has since been changed. Now, it iterates through the entire string to avoid collisions with similar strings.
4
u/escherbach Jun 12 '17 edited Jun 12 '17
That's the point of the change request in the OP link above! It suggest exactly that change, so the code is now
public static int hashCode(byte[] value) { int h = 0; int length = value.length >> 1; for (int i = 0; i < length; i++) { h = 31 * h + getChar(value, i); } return h; }The point is, that no one seems to know who originally came up with this function, eg a 33 multiplier would work almost as well as 31 (but 33, being composite, may have some hard to detect sub-cycles for example)
2
u/acerbicwidow Jun 12 '17
Can anyone Eli5 for me?
4
u/escherbach Jun 12 '17 edited Jun 12 '17
The Java string hash code (which maps an arbitrary string to a number) was very badly implemented originally. But, back in 1997 a change request was made (linked to by the OP) to correct it to a better function, which is very simple and works really well, it takes the ascii value of each character in the string, multiplies it by increasing powers of 31 and sums all the values:
public static int hashCode(byte[] value) { int h = 0; int length = value.length >> 1; for (int i = 0; i < length; i++) { h = 31 * h + getChar(value, i); } return h; }But nobody knows the origin of the algorithm! In the OP link there is discussion about it, even asking well-known experts, but none seem to know where the algorithm originated. There are plenty of alternative, equally simple candidates, eg just use 33 instead of 31 as the multiplication factor! So no real proof that this is an optimal algorithm (it's nice that 31 is prime though, since with a composite multiplier like 33 you will get complicated sub-cycles which could lead to issues in certain types of data sets), it could have been a hack by some student back in the 60s that just became popular because it was simple, or just gained popularity by accident
2
u/nohimn Jun 13 '17
Similarly, Floyd's Cycle Detection algorithm has no known author, and is considered an example of a "folk algorithm". Knuth attributed the algorithm to Floyd, but there's no evidence that he wrote it.
*Edit: I originally said it was the first such example, but I'm not really 100% sure of that.
2
2
u/DialSquare84 Jun 13 '17
Guys, guys...I don't wanna toot my own horn or nothin', but...I think it was me.
1
u/albeva Jun 13 '17
there is only blame - no glory or money. Still think it's you?
→ More replies (1)
3
4
Jun 12 '17
Can someone explain to a non coder why this is interesting? Who are these people? Is it normal to know who made something work in a programming language?
25
u/lordcirth Jun 12 '17
It's not actually who wrote it into Java. The person who wrote it in Java copied the algorithm from 2 famous books, which listed it is a standard & recommended solution, but neither book said where it came from, which is unusual. Normally you would give credit to the author. So nobody knows who first designed it. They asked the authors of one book (K&R), and they didn't know either.
11
u/spirolateral Jun 12 '17
The people they are talking about (Ritchie and Kernighan) are major developers from back in the beginnings of Unix days. They designed the C programming language, among other things, and developed a lot of algorithms. Apparently they had something to do with a string hashing algorithm that was eventually brought over and implemented in Java. It turned out to not be implemented according to the spec and someone wanted to know where the algorithm came from.
It's interesting to me because of who they're asking, major players in computer science history. And what they're asking them about, a simple string hashing algorithm, of all the things you can ask them about!
4
u/iamonlyoneman Jun 12 '17
Something built-in to something that zillions of people use, and that works well enough to continue using for years and years . . . and nobody knows where it came from/who wrote it.
2
1
1
1
1
1
1
u/gatlin Jun 12 '17
I'm having a hard time with reading comprehension right now I think. In the article the author assesses various hash function performances and then says:
Looking at this table, it's clear that all of the functions except for the current Java function and the two broken versions of Weinberger's function offer excellent, nearly indistinguishable performance.
However, only one version of Weinberger's functions appears to be broken in the table.
Am I misunderstanding something?
1
u/escherbach Jun 12 '17
Fn(24) is particularly poor for URLs, not as bad as Fn(MatPak) but obviously worse than the other good functions (also quite a bit worse in the other two categories)
1
u/pdp10 Jun 12 '17
This whole post reminds me of Dasgupta's Algorithms, which starts with modular arithmetic, and would make for an excellent lesson on both comparative hash implementations and programming in the real world.
1
u/MorrisMeyer Jun 12 '17
Lee Boynton (formerly of NeXT) wrote the first Java String class. It is possible he would know where the hash method originated.
1
1
u/zynasis Jun 12 '17
Love this kind of stuff. It's like something out of good will hunting. This and the fast inverse square were probably written by a passing janitor.
1
1
1
u/ismaelc Jun 12 '17 edited Jun 12 '17
If Reddit has taught me anything, I'd say one of these guys wrote it, had carbon monoxide poisoning, and totally forgot he wrote the hash function
1
0
853
u/roerd Jun 12 '17
The title is very misleading. It suggests the author of the implementation within Java is unknown, whereas it's actually the author of the algorithm that this implementation is based on who is unknown.