fiat-crypto icon indicating copy to clipboard operation
fiat-crypto copied to clipboard

Can we generate genuine 64-bit code for Java?

Open JasonGross opened this issue 4 years ago • 5 comments

Java has neither unsigned types nor uint128. Currently, we are generating 32-bit-targeted code for Java, but if I understand correctly, all of the operations (+, -, *, <<, >>>, &, |) commute with modulo/casting, so I think we should just be able to use signed integers as if they were unsigned, and use wrap-around behavior. Presumably if we do this the pass inserting the casts for this should be verified.

cc @andres-erbsen @peterdettman @dghgit for thoughts / sanity-checking on this strategy.

JasonGross avatar Mar 11 '20 20:03 JasonGross

I think the main question is how to get the high part of a 64-by-64 multiplication. Does Java have int128?

andres-erbsen avatar Mar 12 '20 14:03 andres-erbsen

Ah, right, yeah, Java has neither int128 nor mulx. So I guess the alternative to using 32-bit synthesis is to do the same thing we're doing for bedrock2

JasonGross avatar Mar 12 '20 15:03 JasonGross

Java 9 has Math.multiplyHigh (also Math.multiplyFull) which I assume gets JITed to the sensible thing, though it's all signed integers and (a - 2^64) * (b - 2^64) != a * b mod (2^128). (Maybe you can fix it at the cost of a couple conditional additions? Or limit it to unsaturated limbs?)

I'm not familiar enough with the Java ecosystem to know whether requiring Java 9 is a reasonable baseline these days.

davidben avatar Mar 12 '20 15:03 davidben

what we do for bedrock2 is mulHigh.

andres-erbsen avatar Mar 12 '20 16:03 andres-erbsen

Ooh, neat! I can hopefully use that.

JasonGross avatar Mar 12 '20 17:03 JasonGross