tests: add unit tests for SHA hashes, etc. (#1853)

This tests each arm referenced in
https://urbit.org/docs/reference/library/3d/, based on official NIST
test vectors wherever possible.

Squashed commit of the following:

commit 3162b29f4d4d25460647be6ffb18214e602c0f7c
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Wed Nov 6 16:51:38 2019 +0000

    Update hoon.hoon

commit fe31ab286ad221147c1b6d5d733338bef2a6d39c
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Wed Nov 6 16:50:19 2019 +0000

    Create hoon.hoon

commit 28051f248694f6280b9803b37c388aab398c2976
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Wed Nov 6 16:45:38 2019 +0000

    Delete hoon.hoon

commit 186aaa89a1549ae14ab3faaf7c5867ffc84a6eaa
Merge: d0c8ae043 ed57763f1
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Wed Nov 6 11:43:51 2019 -0500

    Merge pull request #1 from ken-meier/master

    Add files via upload

commit ed57763f1e47c28bebe4f99d1be1cdea151c3d40
Merge: 225987e10 d0c8ae043
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Wed Nov 6 11:43:18 2019 -0500

    Merge branch 'add-sha-unit-tests' into master

commit 225987e100fe207a599738655d5377c10a171359
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Wed Nov 6 11:28:05 2019 -0500

    Add files via upload

commit d0c8ae04358fa76bd6a3a654eb5f9f38c3fba502
Author: Ken Meier <55196189+ken-meier@users.noreply.github.com>
Date:   Fri Oct 25 19:20:50 2019 +0000

    Added containers for shad, shas, shaf

commit 1c9ac5b89c619b9005f6691aaf271a2992c5660e
Merge: 62f438bf5 7f124db8d
Author: Ken Meier <55196189+ken-meier@users.noreply.github.com>
Date:   Fri Oct 25 18:28:32 2019 +0000

    Merge branch 'master' into add-sha-unit-tests

commit 62f438bf5f754903779a69eb2fec48c1bdb5aae5
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Fri Oct 25 11:03:38 2019 -0400

    Add files via upload

commit 5090ea40a8100197cee415463b9b331920b7dfa3
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Fri Oct 25 11:02:59 2019 -0400

    Add files via upload

commit 9084b1598984476c4fd4ce1e66edbea42166ca72
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Wed Oct 23 20:41:45 2019 -0400

    Add files via upload

commit 4575827736361b687551582a7842951226f38eac
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Thu Oct 24 00:17:07 2019 +0000

    Delete sha.hoon

commit 833e4d6fda5befdbe2a39c92d6971774f476483c
Author: ken-meier <55196189+ken-meier@users.noreply.github.com>
Date:   Fri Oct 18 17:56:41 2019 +0000

    Add files via upload

Signed-off-by: Jared Tobin <jared@tlon.io>
This commit is contained in:
Jared Tobin 2019-11-07 17:27:23 +08:00
parent f183a5f669
commit 080cd38f0a
No known key found for this signature in database
GPG Key ID: 0E4647D58F8A69E4

View File

@ -53,4 +53,281 @@
!> 0x2ad3.9968
!> (mug [(dec (bex 128)) 1])
==
:: SHA tests
:: For references see FIPS180-4 and related test vectors
:: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
:: https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program
::
++ test-sha-1
=/ a ''
=/ b 'abc'
=/ c 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
=/ d 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn\
/hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'
;: weld
%+ expect-eq
!> 0xda39.a3ee.5e6b.4b0d.3255.bfef.9560.1890.afd8.0709
!> `@ux`(sha-1:sha (swp 3 a))
::
%+ expect-eq
!> 0xa999.3e36.4706.816a.ba3e.2571.7850.c26c.9cd0.d89d
!> `@ux`(sha-1:sha (swp 3 b))
::
%+ expect-eq
!> 0x8498.3e44.1c3b.d26e.baae.4aa1.f951.29e5.e546.70f1
!> `@ux`(sha-1:sha (swp 3 c))
::
%+ expect-eq
!> 0xa49b.2446.a02c.645b.f419.f995.b670.9125.3a04.a259
!> `@ux`(sha-1:sha (swp 3 d))
::
==
::
++ test-sha-1l
=/ a 0x0
=/ b 0x549e.959e
=/ c 0x7c.9c67.323a.1df1.adbf.e5ce.b415.eaef.0155.ece2.820f.4d50.c1ec.22cb.
a492.8ac6.56c8.3fe5.85db.6a78.ce40.bc42.757a.ba7e.5a3f.5824.28d6.ca68.
d0c3.9783.36a6.efb7.2961.3e8d.9979.0162.04bf.d921.322f.dd52.2218.3554.
447d.e5e6.e9bb.e6ed.f76d.7b71.e18d.c2e8.d6dc.89b7.3983.64f6.52fa.fc73.
4329.aafa.3dcd.45d4.f31e.388e.4faf.d7fc.6495.f37c.a5cb.ab7f.54d5.8646.
3da4.bfea.a3ba.e09f.7b8e.9239.d832.b4f0.a733.aa60.9cc1.f8d4
=/ d 0x938.f2e2.ebb6.4f8a.f8bb.fc91
;: weld
:: Empty string
::
%+ expect-eq
!> 0xda39.a3ee.5e6b.4b0d.3255.bfef.9560.1890.afd8.0709
!> `@ux`(sha-1l:sha [(met 3 a) a])
:: Short Message
::
%+ expect-eq
!> 0xb78b.ae6d.1433.8ffc.cfd5.d5b5.674a.275f.6ef9.c717
!> `@ux`(sha-1l:sha [(met 3 b) b])
:: Long Message
::
%+ expect-eq
!> 0xd8fd.6a91.ef3b.6ced.05b9.8358.a991.07c1.fac8.c807
!> `@ux`(sha-1l:sha [(met 3 c) c])
:: Leading-zero byte
::
%+ expect-eq
!> 0x9f4e.66b6.ceea.40dc.f4b9.166c.28f1.c884.7414.1da9
!> `@ux`(sha-1l:sha [(met 3 d) d])
::
==
::
++ test-shay
=/ a 0x0
=/ b 0xb4.190e
=/ c 0x45.1101.250e.c6f2.6652.249d.59dc.974b.7361.d571.a810.1cdf.d36a.ba3b.
5854.d3ae.086b.5fdd.4597.721b.66e3.c0dc.5d8c.606d.9657.d0e3.2328.3a52.
17d1.f53f.2f28.4f57.b85c.8a61.ac89.2471.1f89.5c5e.d90e.f177.45ed.2d72.
8abd.22a5.f7a1.3479.a462.d71b.56c1.9a74.a40b.655c.58ed.fe0a.188a.d2cf.
46cb.f305.24f6.5d42.3c83.7dd1.ff2b.f462.ac41.9800.7345.bb44.dbb7.b1c8.
6129.8cdf.6198.2a83.3afc.728f.ae1e.da2f.87aa.2c94.8085.8bec
=/ d 0x777.fc1e.1ca4.7304.c2e2.6569.2838.109e.26aa.b9e5.c4ae.4e86.00df.4b1f
;: weld
:: Empty string
::
%+ expect-eq
!> 0xe3b0.c442.98fc.1c14.9afb.f4c8.996f.b924.27ae.41e4.649b.934c.a495.
991b.7852.b855
!> `@ux`(sha-256l:sha [(met 3 a) a])
:: Short Message
::
%+ expect-eq
!> 0xdff2.e730.91f6.c05e.5288.96c4.c831.b944.8653.dc2f.f043.528f.6769.
437b.c7b9.75c2
!> `@ux`(sha-256l:sha [(met 3 b) b])
:: Long Message
::
%+ expect-eq
!> 0x3c59.3aa5.39fd.cdae.516c.df2f.1500.0f66.3418.5c88.f505.b397.75fb.
9ab1.37a1.0aa2
!> `@ux`(sha-256l:sha [(met 3 c) c])
:: Leading-zero byte
::
%+ expect-eq
!> 0xffb4.fc03.e054.f8ec.bc31.470f.c023.bedc.d4a4.06b9.dd56.c71d.a1b6.
60dc.c484.2c65
!> `@ux`(sha-256l:sha [(met 3 d) d])
==
++ test-shal
=/ a 0x0
=/ b 0x23be.86d5
=/ c 0x4f.0560.0950.664d.5190.a2eb.c29c.9edb.89c2.0079.a4d3.e6bc.3b27.d75e.
34e2.fa3d.0276.8502.bd69.7900.7859.8d5f.cf3d.6779.bfed.1284.bbe5.ad72.
fb45.6015.181d.9587.d6e8.64c9.4056.4eaa.fb4f.2fea.d434.6ea0.9b68.77d9.
340f.6b82.eb15.1588.0872.213d.a3ad.88fe.ba9f.4f13.817a.71d6.f90a.1a17.
c43a.15c0.38d9.88b5.b29e.dffe.2d6a.0628.13ce.dbe8.52cd.e302.b3e3.3b69.
6846.d2a8.e36b.d680.efcc.6cd3.f9e9.a4c1.ae8c.ac10.cc52.44d1.3167.7140.
3991.76ed.4670.0019.a004.a163.806f.7fa4.67fc.4e17.b461.7bbd.7641.aaff.
7ff5.6396.ba8c.08a8.be10.0b33.a20b.5daf.134a.2aef.a5e1.c349.6770.dcf6.
baa4.f7bb
=/ d 0xa.55db
;: weld
:: Empty string
::
%+ expect-eq
!> 0xcf83.e135.7eef.b8bd.f154.2850.d66d.8007.d620.e405.0b57.15dc.83f4.
a921.d36c.e9ce.47d0.d13c.5d85.f2b0.ff83.18d2.877e.ec2f.63b9.31bd.
4741.7a81.a538.327a.f927.da3e
!> `@ux`(sha-512l:sha [(met 3 a) a])
:: Short message
::
%+ expect-eq
!> 0x76d4.2c8e.adea.35a6.9990.c63a.762f.3306.14a4.6999.77f0.58ad.b988.
f406.fb0b.e8f2.ea3d.ce3a.2bbd.1d82.7b70.b9b2.99ae.6f9e.5058.ee97.
b50b.d492.2d6d.37dd.c761.f8eb
!> `@ux`(sha-512l:sha [(met 3 b) b])
:: Long message
::
%+ expect-eq
!> 0xa9db.490c.708c.c725.48d7.8635.aa7d.a79b.b253.f945.d710.e5cb.677a.
474e.fc7c.65a2.aab4.5bc7.ca11.13c8.ce0f.3c32.e139.9de9.c459.535e.
8816.521a.b714.b2a6.cd20.0525
!> `@ux`(sha-512l:sha [(met 3 c) c])
:: Leading-zero byte
::
%+ expect-eq
!> 0x7952.585e.5330.cb24.7d72.bae6.96fc.8a6b.0f7d.0804.577e.347d.99bc.
1b11.e52f.3849.85a4.2844.9382.306a.8926.1ae1.43c2.f3fb.6138.04ab.
20b4.2dc0.97e5.bf4a.96ef.919b
!> `@ux`(sha-512l:sha [(met 3 d) d])
::
==
::
++ test-shax
=/ a ''
=/ b 'abc'
=/ c 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
;: weld
:: Empty string
::
%+ expect-eq
!> 0xe3b0.c442.98fc.1c14.9afb.f4c8.996f.b924.27ae.41e4.649b.934c.a495.
991b.7852.b855
!> `@ux`(sha-256:sha (swp 3 a))
:: Short Message
::
%+ expect-eq
!> 0xba78.16bf.8f01.cfea.4141.40de.5dae.2223.b003.61a3.9617.7a9c.b410.
ff61.f200.15ad
!> `@ux`(sha-256:sha (swp 3 b))
:: Long Message
::
%+ expect-eq
!> 0x248d.6a61.d206.38b8.e5c0.2693.0c3e.6039.a33c.e459.64ff.2167.f6ec.
edd4.19db.06c1
!> `@ux`(sha-256:sha (swp 3 c))
::
==
::
++ test-shaz
=/ a ''
=/ b 'abc'
=/ c 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijkl\
/mnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'
;: weld
:: Empty string
::
%+ expect-eq
!> 0xcf83.e135.7eef.b8bd.f154.2850.d66d.8007.d620.e405.0b57.15dc.83f4.
a921.d36c.e9ce.47d0.d13c.5d85.f2b0.ff83.18d2.877e.ec2f.63b9.31bd.
4741.7a81.a538.327a.f927.da3e
!> `@ux`(sha-512:sha (swp 3 a))
:: Short Message
::
%+ expect-eq
!> 0xddaf.35a1.9361.7aba.cc41.7349.ae20.4131.12e6.fa4e.89a9.7ea2.0a9e.
eee6.4b55.d39a.2192.992a.274f.c1a8.36ba.3c23.a3fe.ebbd.454d.4423.
643c.e80e.2a9a.c94f.a54c.a49f
!> `@ux`(sha-512:sha (swp 3 b))
:: Long Message
::
%+ expect-eq
!> 0x8e95.9b75.dae3.13da.8cf4.f728.14fc.143f.8f77.79c6.eb9f.7fa1.7299.
aead.b688.9018.501d.289e.4900.f7e4.331b.99de.c4b5.433a.c7d3.29ee.
b6dd.2654.5e96.e55b.874b.e909
!> `@ux`(sha-512:sha (swp 3 c))
::
==
::
++ test-sham
;: weld
%+ expect-eq
!> 0v3.71s52.4bqnp.ki2b8.9hhsp.2ufgg
!> (sham [2 4])
::
%+ expect-eq
!> 0v1.hg8mv.t7s3f.u4f8a.q5noe.dvqvh
!> (sham "hello")
::
==
::
++ test-raw
;: weld
%+ expect-eq
!> 0b1001
!> `@ub`(~(raw og 27) 4)
::
%+ expect-eq
!> 0b0
!> `@ub`(~(raw og 27) 3)
::
%+ expect-eq
!> 0b1111
!> `@ub`(~(raw og 11) 4)
::
%+ expect-eq
!> 0b100
!> `@ub`(~(raw og 11) 3)
::
==
::
++ test-raws
=/ rng ~(. og 7)
=^ a rng (rads:rng 4)
=^ b rng (rads:rng 4)
%+ expect-eq
!> [0b10 0b1]
!> [`@ub`a `@ub`b]
::
++ test-rad
;: weld
%+ expect-eq
!> 4
!> (~(rad og 5) 11)
::
%+ expect-eq
!> 2
!> (~(rad og 758.716.593) 11)
::
%+ expect-eq
!> 71.499
!> (~(rad og 1) 100.000)
::
==
::
++ test-rads
=/ rng ~(. og 7)
=^ a rng (rads:rng 10)
=^ b rng (rads:rng 10)
%+ expect-eq
!> [2 8]
!> [a b]
::
++ test-shaw
;: weld
%+ expect-eq
!> 0b11.0111
!> `@ub`(shaw 3 6 98)
::
%+ expect-eq
!> 0b11
!> `@ub`(shaw 2 6 98)
::
==
:: End SHA tests
--