ref: 74d91a0021de012908cfdb35fb61a1473a376130
dir: /lib/crypto/test/sha3.myr/
use std use crypto use testr const main = { testr.run([ [.name="keccak224empty", .fn={ctx var ret ret = crypto.keccak224("") testr.check(ctx, \ std.eq(\ ret[:], \ "\xf7\x18\x37\x50\x2b\xa8\xe1\x08\x37\xbd\xd8\xd3\x65\xad" \ "\xb8\x55\x91\x89\x56\x02\xfc\x55\x2b\x48\xb7\x39\x0a\xbd"), \ "invalid hash result {r}", ret[:]) }], [.name="keccak256empty", .fn={ctx var ret ret = crypto.keccak256("") testr.check(ctx, \ std.eq(\ ret[:], \ "\xC5\xD2\x46\x01\x86\xF7\x23\x3C\x92\x7E\x7D\xB2\xDC\xC7\x03\xC0" \ "\xE5\x00\xB6\x53\xCA\x82\x27\x3B\x7B\xFA\xD8\x04\x5D\x85\xA4\x70"), \ "invalid hash result {r}", ret[:]) }], [.name="keccak384empty", .fn={ctx var ret ret = crypto.keccak384("") testr.check(ctx, \ std.eq(\ ret[:], \ "\x2C\x23\x14\x6A\x63\xA2\x9A\xCF" \ "\x99\xE7\x3B\x88\xF8\xC2\x4E\xAA" \ "\x7D\xC6\x0A\xA7\x71\x78\x0C\xCC" \ "\x00\x6A\xFB\xFA\x8F\xE2\x47\x9B" \ "\x2D\xD2\xB2\x13\x62\x33\x74\x41" \ "\xAC\x12\xB5\x15\x91\x19\x57\xFF"), \ "invalid hash result {r}", ret[:]) }], [.name="keccak512empty", .fn={ctx var ret ret = crypto.keccak512("") testr.check(ctx, \ std.eq(\ ret[:], \ "\x0E\xAB\x42\xDE\x4C\x3C\xEB\x92" \ "\x35\xFC\x91\xAC\xFF\xE7\x46\xB2" \ "\x9C\x29\xA8\xC3\x66\xB7\xC6\x0E" \ "\x4E\x67\xC4\x66\xF3\x6A\x43\x04" \ "\xC0\x0F\xA9\xCA\xF9\xD8\x79\x76" \ "\xBA\x46\x9B\xCB\xE0\x67\x13\xB4" \ "\x35\xF0\x91\xEF\x27\x69\xFB\x16" \ "\x0C\xDA\xB3\x3D\x36\x70\x68\x0E"), \ "invalid hash result {r}", ret[:]) }], [.name="keccak224smoke", .fn={ctx var ret ret = crypto.keccak224("Keccak-224 Test Hash") testr.check(ctx, \ std.eq(\ ret[:], \ "\x30\x04\x5B\x34\x94\x6E\x1B\x2E\x09\x16\x13\x36\x2F\xD2" \ "\x2A\xA0\x8E\x2B\xEA\xFE\xC5\xE8\xDA\xEE\x42\xC2\xE6\x65"), \ "invalid hash result {r}", ret[:]) }], [.name="keccak256smoke", .fn={ctx var ret ret = crypto.keccak256("Keccak-256 Test Hash") testr.check(ctx, \ std.eq(\ ret[:], \ "\xA8\xD7\x1B\x07\xF4\xAF\x26\xA4\xFF\x21\x02\x7F\x62\xFF\x60\x26" \ "\x7F\xF9\x55\xC9\x63\xF0\x42\xC4\x6D\xA5\x2E\xE3\xCF\xAF\x3D\x3C"), \ "invalid hash result {r}", ret[:]) }], [.name="keccak384smoke", .fn={ctx var ret ret = crypto.keccak384("Keccak-384 Test Hash") testr.check(ctx, \ std.eq(\ ret[:], \ "\xE2\x13\xFD\x74\xAF\x0C\x5F\xF9" \ "\x1B\x42\x3C\x8B\xCE\xEC\xD7\x01" \ "\xF8\xDD\x64\xEC\x18\xFD\x6F\x92" \ "\x60\xFC\x9E\xC1\xED\xBD\x22\x30" \ "\xA6\x90\x86\x65\xBC\xD9\xFB\xF4" \ "\x1A\x99\xA1\x8A\x7D\x9E\x44\x6E"), \ "invalid hash result {r}", ret[:]) }], [.name="keccak512smoke", .fn={ctx var ret ret = crypto.keccak512("Keccak-512 Test Hash") testr.check(ctx, \ std.eq(\ ret[:], \ "\x96\xEE\x47\x18\xDC\xBA\x3C\x74" \ "\x61\x9B\xA1\xFA\x7F\x57\xDF\xE7" \ "\x76\x9D\x3F\x66\x98\xA8\xB3\x3F" \ "\xA1\x01\x83\x89\x70\xA1\x31\xE6" \ "\x21\xCC\xFD\x05\xFE\xFF\xBC\x11" \ "\x80\xF2\x63\xC2\x7F\x1A\xDA\xB4" \ "\x60\x95\xD6\xF1\x25\x33\x14\x72" \ "\x4B\x5C\xBF\x78\x28\x65\x8E\x6A"), \ "invalid hash result {r}", ret[:]) }], [.name="keccak224multiadd", .fn = keccak224multiadd], [.name="keccak256multiadd", .fn = keccak256multiadd], [.name="keccak384multiadd", .fn = keccak384multiadd], [.name="keccak512multiadd", .fn = keccak512multiadd], ][:]) } const toadd = [ [ "", "", "", "", "" ][:], [ "a", "b" ][:], /* Hover around the blocksz mark */ [ "a seventy-one byte string to make keccak-512 touch the limit (blocksz).", "" ][:], [ "a seventy-one byte string to make keccak-512 touch the limit (blocksz).", "_" ][:], [ "a seventy-one byte string to make keccak-512 touch the limit (blocksz).", "_", "_" ][:], [ "a seventy-one byte string to make keccak-512 touch the limit (blocksz).", "__" ][:], /* Go between blocksz and blocksz + 4 to stress addblock */ [ "Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, ", "I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. ", "Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul;", ][:], ][:] const keccak224multiadd = {c for l : toadd var st : crypto.keccak224 crypto.keccak224init(&st) var merged : std.strbuf# = std.mksb() for data : l std.sbputs(merged, data) crypto.keccak224add(&st, data) ;; var hm = crypto.keccak224(std.sbfin(merged)) var hi = crypto.keccak224fin(&st) testr.check(c, std.sleq(hm[0:28], hi[0:28]), "incremental keccak224 disagrees with all-at-once") ;; } const keccak256multiadd = {c for l : toadd var st : crypto.keccak256 crypto.keccak256init(&st) var merged : std.strbuf# = std.mksb() for data : l std.sbputs(merged, data) crypto.keccak256add(&st, data) ;; var hm = crypto.keccak256(std.sbfin(merged)) var hi = crypto.keccak256fin(&st) testr.check(c, std.sleq(hm[0:32], hi[0:32]), "incremental keccak256 disagrees with all-at-once") ;; } const keccak384multiadd = {c for l : toadd var st : crypto.keccak384 crypto.keccak384init(&st) var merged : std.strbuf# = std.mksb() for data : l std.sbputs(merged, data) crypto.keccak384add(&st, data) ;; var hm = crypto.keccak384(std.sbfin(merged)) var hi = crypto.keccak384fin(&st) testr.check(c, std.sleq(hm[0:48], hi[0:48]), "incremental keccak384 disagrees with all-at-once") ;; } const keccak512multiadd = {c for l : toadd var st : crypto.keccak512 crypto.keccak512init(&st) var merged : std.strbuf# = std.mksb() for data : l std.sbputs(merged, data) crypto.keccak512add(&st, data) ;; var hm = crypto.keccak512(std.sbfin(merged)) var hi = crypto.keccak512fin(&st) testr.check(c, std.sleq(hm[0:64], hi[0:64]), "incremental keccak512 disagrees with all-at-once") ;; }