on master help sign in
Update 2e

Updated syntax and examples. Also added documentation for `++muk` and `++mum`.

commitd66ab3481d8bb53a02894392e028749446f29bf8 authorrmariani <robert@tlon.io> dateTue 29 May 2018, 14:49 (8 years ago) parentbc8ad33
1 file changed+176−71
docs/hoon/library/2e.md+176−71
docs/hoon/library/2e.md+176−71
@@ -6,33 +6,32 @@ navhome: /docs/
66# 2e: insecure hashing
77### `++fnv`
88
9−Hashes an atom with the 32-bit FNV non-cryptographic hash algorithm.
10−Multiplies `a` by the prime number 16,777,619 and then takes the block
11−of size 5 off the product's end, producing an atom.
9+FNV scrambler
1210
13−Accepts
14−-------
11+Hashes an atom with the 32-bit FNV non-cryptographic hash algorithm. Multiplies
12+`a` by the prime number 16,777,619 and then takes the [block](../2c.md) of size
13+5 off the product's end, producing an atom.
14+
15+#### Accepts
1516
1617`a` is an atom.
1718
18−Produces
19−--------
19+#### Produces
2020
2121An atom.
2222
23+#### Source
2324
24−Source
25−------
26−
27− ++ fnv |=(a/@ (end 5 1 (mul 16.777.619 a))) :: FNV scrambler
25+ ++ fnv |=(a=@ (end 5 1 (mul 16.777.619 a)))
2826
29−Examples
30−--------
27+#### Examples
3128
3229 > (fnv 10.000)
3330 272.465.456
31+
3432 > (fnv 10.001)
3533 289.243.075
34+
3635 > (fnv 1)
3736 16.777.619
3837
@@ -41,31 +40,30 @@ Examples
4140***
4241### `++mug`
4342
44−Hashes `a` with the 31-bit nonzero FNV-1a non-cryptographic hash
45−algorithm, producing an atom.
43+FNV-1a scrambler
44+
45+Hashes `a` with the 31-bit nonzero FNV-1a non-cryptographic hash algorithm,
46+producing an atom.
4647
47−Accepts
48−-------
48+#### Accepts
4949
50−A is a noun.
50+`a` is a noun.
5151
52−Produces
53−--------
52+#### Produces
5453
5554An atom.
5655
57−Source
58−------
56+#### Source
5957
60− ++ mug :: 31bit nonzero FNV1a
58+ ++ mug
6159 ~/ %mug
62− |= a/*
60+ |= a=*
6361 ?^ a
6462 =+ b=[p=$(a -.a) q=$(a +.a)]
6563 |- ^- @
6664 =+ c=(fnv (mix p.b (fnv q.b)))
6765 =+ d=(mix (rsh 0 31 c) (end 0 31 c))
68− ?. =(0 c) c
66+ ?. =(0 d) d
6967 $(q.b +(q.b))
7068 =+ b=2.166.136.261
7169 |- ^- @
@@ -79,81 +77,188 @@ Source
7977 $(c (fnv (mix c (cut 3 [d 1] a))), d +(d))
8078
8179
82−Examples
83−--------
80+#### Examples
8481
8582 > (mug 10.000)
8683 178.152.889
84+
8785 > (mug 10.001)
8886 714.838.017
87+
8988 > (mug 1)
9089 67.918.732
90+
9191 > (mug (some 10))
9292 1.872.403.737
93− > (mug (limo [1 2 3 4 5 ~]))
93+
94+ > (mug [1 2 3 4 5 ~])
9495 1.067.931.605
9596
9697
9798
99+***
100+### `++muk`
101+
102+Standard MurmurHash3
103+
104+Implementation of the 32-bit
105+[MurmurHash3](https://en.wikipedia.org/wiki/MurmurHash#Algorithm)
106+non-cryptographic hash algorithm. Takes `a` as the seed, `b` as the key length
107+in [blocks](../2c) of size 3, and `c` as the key, producing an atom.
108+
109+#### Accepts
110+
111+`a` is an atom.
112+
113+`b` is an atom.
114+
115+`c` is an atom.
116+
117+#### Produces
118+
119+An atom.
120+
121+#### Source
122+ ++ muk
123+ ~% %muk ..muk ~
124+ =+ ~(. fe 5)
125+ |= [syd=@ len=@ key=@]
126+ ?> &((lte (met 5 syd) 1) (lte (met 0 len) 31))
127+ =/ pad (sub len (met 3 key))
128+ =/ data (weld (rip 3 key) (reap pad 0))
129+ =/ nblocks (div len 4) :: intentionally off-by-one
130+ =/ h1 syd
131+ =+ [c1=0xcc9e.2d51 c2=0x1b87.3593]
132+ =/ blocks (rip 5 key)
133+ =/ i nblocks
134+ =. h1 =/ hi h1 |-
135+ ?: =(0 i) hi :: negative array index
136+ =/ k1 (snag (sub nblocks i) blocks)
137+ =. k1 (sit (mul k1 c1))
138+ =. k1 (rol 0 15 k1)
139+ =. k1 (sit (mul k1 c2))
140+ =. hi (mix hi k1)
141+ =. hi (rol 0 13 hi)
142+ =. hi (sum (sit (mul hi 5)) 0xe654.6b64)
143+ $(i (dec i))
144+ =/ tail (slag (mul 4 nblocks) data)
145+ =/ k1 0
146+ =/ tlen (dis len 3)
147+ =. h1
148+ ?+ tlen h1 :: fallthrough switch
149+ $3 =. k1 (mix k1 (lsh 0 16 (snag 2 tail)))
150+ =. k1 (mix k1 (lsh 0 8 (snag 1 tail)))
151+ =. k1 (mix k1 (snag 0 tail))
152+ =. k1 (sit (mul k1 c1))
153+ =. k1 (rol 0 15 k1)
154+ =. k1 (sit (mul k1 c2))
155+ (mix h1 k1)
156+ $2 =. k1 (mix k1 (lsh 0 8 (snag 1 tail)))
157+ =. k1 (mix k1 (snag 0 tail))
158+ =. k1 (sit (mul k1 c1))
159+ =. k1 (rol 0 15 k1)
160+ =. k1 (sit (mul k1 c2))
161+ (mix h1 k1)
162+ $1 =. k1 (mix k1 (snag 0 tail))
163+ =. k1 (sit (mul k1 c1))
164+ =. k1 (rol 0 15 k1)
165+ =. k1 (sit (mul k1 c2))
166+ (mix h1 k1)
167+ ==
168+ =. h1 (mix h1 len)
169+ |^ (fmix32 h1)
170+ ++ fmix32
171+ |= h=@
172+ =. h (mix h (rsh 0 16 h))
173+ =. h (sit (mul h 0x85eb.ca6b))
174+ =. h (mix h (rsh 0 13 h))
175+ =. h (sit (mul h 0xc2b2.ae35))
176+ =. h (mix h (rsh 0 16 h))
177+ h
178+ --
179+
180+
181+#### Examples
182+
183+ > (muk 6 1 3)
184+ 3.427.677.118
185+
186+ > (muk 6 2 'jerry')
187+ ! exit
188+
189+ > (muk 6 2 'je')
190+ 3.602.081.716
191+
192+ > (met 3 'jerry')
193+ 5
194+
195+ > (met 3 'je')
196+ 2
197+
198+ > (muk 6 5 'jerry')
199+ 1.276.447.489
200+
201+ > (muk 6 15 'jerry')
202+ 2.881.503.571
203+
204+ > =e (cut 3 [0 2] eny) :: `eny` is 256 bits of entropy (a random number).
205+ > e
206+ 44.228
207+
208+ > (muk e 11 10.000)
209+ 246.077.549
210+
211+
212+
98213***
99214### `++mum`
100215
101−XX document
216+31-bit MurmurHash3 scrambler
102217
103−Accepts
104−-------
218+Hashes `a` with the 31-bit MurmurHash3 non-cryptographic hash algorithm,
219+producing an atom.
105220
106−Produces
107−--------
221+#### Accepts
108222
109−Source
110−------
223+`a` is a noun.
111224
112− ++ mum :: mug with murmur3
225+#### Produces
226+
227+An atom.
228+
229+#### Source
230+
231+ ++ mum
113232 ~/ %mum
114− |= a/*
233+ |= a=*
115234 |^ (trim ?@(a a (mix $(a -.a) (mix 0x7fff.ffff $(a +.a)))))
116− ++ spec :: standard murmur3
117− |= {syd/@ key/@}
118− ?> (lte (met 5 syd) 1)
119− =+ ^= row
120− |= {a/@ b/@}
121− (con (end 5 1 (lsh 0 a b)) (rsh 0 (sub 32 a) b))
122− =+ mow=|=({a/@ b/@} (end 5 1 (mul a b)))
123− =+ len=(met 5 key)
124− =- =. goc (mix goc len)
125− =. goc (mix goc (rsh 4 1 goc))
126− =. goc (mow goc 0x85eb.ca6b)
127− =. goc (mix goc (rsh 0 13 goc))
128− =. goc (mow goc 0xc2b2.ae35)
129− (mix goc (rsh 4 1 goc))
130− ^= goc
131− =+ [inx=0 goc=syd]
235+ ++ trim
236+ |= key=@
237+ =+ syd=0xcafe.babe
132238 |- ^- @
133− ?: =(inx len) goc
134− =+ kop=(cut 5 [inx 1] key)
135− =. kop (mow kop 0xcc9e.2d51)
136− =. kop (row 15 kop)
137− =. kop (mow kop 0x1b87.3593)
138− =. goc (mix kop goc)
139− =. goc (row 13 goc)
140− =. goc (end 5 1 (add 0xe654.6b64 (mul 5 goc)))
141− $(inx +(inx))
239+ =+ haz=(muk syd (met 3 key) key)
240+ =+ ham=(mix (rsh 0 31 haz) (end 0 31 haz))
241+ ?.(=(0 ham) ham $(syd +(syd)))
142242
143−
144−Examples
145−--------
243+#### Examples
146244
147245 > (mum 10.000)
148− 1.232.632.901
246+ 795.713.195
247+
149248 > (mum 10.001)
150− 658.093.079
249+ 420.521.697
250+
251+ > (mum 'jerry')
252+ 1.540.161.753
253+
151254 > (mum 1)
152− 818.387.364
255+ 1.901.865.568
256+
153257 > (mum (some 10))
154− 1.177.215.703
155− > (mum ~[1 2 3 4 5])
156− 1.517.902.092
258+ 2.099.076.946
259+
260+ > (mum [1 2 3 4 5 ~])
261+ 1.719.833.228
157262
158263
159264