Skip to content

Instantly share code, notes, and snippets.

@jaekwon
Last active December 24, 2015 23:39
Show Gist options
  • Save jaekwon/6881680 to your computer and use it in GitHub Desktop.
Save jaekwon/6881680 to your computer and use it in GitHub Desktop.
// Get http://acme.com/software/bigint/
// Then change Makefile to point CC to emcc.
// make
// Emscripten compile with the following line:
// emcc bigint.a -O2 -o bigint.html -s EXPORTED_FUNCTIONS="['_str_to_bi_base', '_bi_mod_power', '_bi_int_mod', '_bi_copy', '_bi_initialize', '_bi_int_divide', '_bi_compare', '_int_to_bi', '_digit_to_char']"
// Now go to bigint.html & run the following code:
var bi_initialize = Module.cwrap('bi_initialize', null, [])
var str_to_bi_base = Module.cwrap('str_to_bi_base', 'number', ['string', 'number'])
var bi_int_mod = Module.cwrap('bi_int_mod', 'number', ['number', 'number'])
var bi_int_divide = Module.cwrap('bi_int_divide', 'number', ['number', 'number'])
var bi_mod_power = Module.cwrap('bi_mod_power', 'number', ['number', 'number', 'number'])
var bi_compare = Module.cwrap('bi_compare', 'number', ['number', 'number'])
var bi_copy = Module.cwrap('bi_copy', 'number', ['number'])
var int_to_bi = Module.cwrap('int_to_bi', 'number', ['number'])
bi_initialize()
function digit_to_char(d) {
return "0123456789ABCDEF"[d];
}
function bi_to_str (bi, base) {
var last = digit_to_char(bi_int_mod(bi_copy(bi), base));
if (bi_compare(bi_copy(bi), int_to_bi(base)) > 0) {
return bi_to_str(bi_copy(bi_int_divide(bi_copy(bi), base)), base)+last;
} else {
return last;
}
}
function DO_MOD_POW(x, e, m) {
x = str_to_bi_base(x, 16);
e = str_to_bi_base(e, 16);
m = str_to_bi_base(m, 16);
var it = bi_mod_power(x, e, m);
return bi_to_str(it, 16);
}
// This needs to return in under 30ms
DO_MOD_POW("3df28cffa0e831ac233b758092b31d46dd8fb95aeac04aee96e70e13e58c1196d5fa458baaf0115a438cf943da69f8f6f2ee0af6202c1b0fdd61950bebac908b59143104ccd0fa1ec495754dfe3610e6a071818b863968c4cdd0e844ed0afb3a4fd3df9b8b4cd339d21176174724b5e73466aaa5c322a510ceb7c1bb630db7b2", "9d023a7ca70950d1699b0162a75671404670ea7924a89ff3bf8fdc011ee58bb0d5499b044cd5382f424d5c9af48434ce8ef088ce9749ee1e071c491c9cc98b315b0ac8e982acd752ada03f009a8e73678bbaea6053b09dd62dcc38e15f3de276b57b9593fd95735c8cb4bf0e08e2d47d24f348f61094db85fd4e21493adec361", "9e061980b8b500d00fc64c52e0b1bc2f758e0df8fb9e94c878692816c03a09ce77b6d848bf3335077ac20538e272ad775b915229ece126cb9b450d8310edee55eb3201ff92e356fa3775ea14e3d4d726fb1666dbfeff4c60194be734eaf0b49ce9612e7fdcefea8ed262754b3757a9318ebe9ea5a1478571c77f6ba3f52630e9")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment