on master help sign in
Update 2e
d66ab34rmariani
docs/hoon/library/2e.md+176−71
@@ -6,33 +6,32 @@ navhome: /docs/
6# 2e: insecure hashing
6# 2e: insecure hashing
7### `++fnv`
7### `++fnv`
8
8
9−Hashes an atom with the 32-bit FNV non-cryptographic hash algorithm.
9+FNV scrambler
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.
12
10
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+
13−Accepts
15+#### Accepts
14−-------
15
16
16`a` is an atom.
17`a` is an atom.
17
18
18−Produces
19+#### Produces
19−--------
20
20
21An atom.
21An atom.
22
22
23+#### Source
23
24
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)))
28
26
29−Examples
27+#### Examples
30−--------
31
28
32 > (fnv 10.000)
29 > (fnv 10.000)
33 272.465.456
30 272.465.456
31+
34 > (fnv 10.001)
32 > (fnv 10.001)
35 289.243.075
33 289.243.075
34+
36 > (fnv 1)
35 > (fnv 1)
37 16.777.619
36 16.777.619
38
37
@@ -41,31 +40,30 @@ Examples
41***
40***
42### `++mug`
41### `++mug`
43
42
43+FNV-1a scrambler
44+
44−Hashes `a` with the 31-bit nonzero FNV-1a non-cryptographic hash
45+Hashes `a` with the 31-bit nonzero FNV-1a non-cryptographic hash algorithm,
45−algorithm, producing an atom.
46+producing an atom.
46
47
47−Accepts
48+#### Accepts
48−-------
49
49
50−A is a noun.
50+`a` is a noun.
51
51
52−Produces
52+#### Produces
53−--------
54
53
55An atom.
54An atom.
56
55
57−Source
56+#### Source
58−------
59
57
60− ++ mug :: 31bit nonzero FNV1a
58+ ++ mug
61 ~/ %mug
59 ~/ %mug
62− |= a/*
60+ |= a=*
63 ?^ a
61 ?^ a
64 =+ b=[p=$(a -.a) q=$(a +.a)]
62 =+ b=[p=$(a -.a) q=$(a +.a)]
65 |- ^- @
63 |- ^- @
66 =+ c=(fnv (mix p.b (fnv q.b)))
64 =+ c=(fnv (mix p.b (fnv q.b)))
67 =+ d=(mix (rsh 0 31 c) (end 0 31 c))
65 =+ d=(mix (rsh 0 31 c) (end 0 31 c))
68− ?. =(0 c) c
66+ ?. =(0 d) d
69 $(q.b +(q.b))
67 $(q.b +(q.b))
70 =+ b=2.166.136.261
68 =+ b=2.166.136.261
71 |- ^- @
69 |- ^- @
@@ -79,81 +77,188 @@ Source
79 $(c (fnv (mix c (cut 3 [d 1] a))), d +(d))
77 $(c (fnv (mix c (cut 3 [d 1] a))), d +(d))
80
78
81
79
82−Examples
80+#### Examples
83−--------
84
81
85 > (mug 10.000)
82 > (mug 10.000)
86 178.152.889
83 178.152.889
84+
87 > (mug 10.001)
85 > (mug 10.001)
88 714.838.017
86 714.838.017
87+
89 > (mug 1)
88 > (mug 1)
90 67.918.732
89 67.918.732
90+
91 > (mug (some 10))
91 > (mug (some 10))
92 1.872.403.737
92 1.872.403.737
93+
93− > (mug (limo [1 2 3 4 5 ~]))
94+ > (mug [1 2 3 4 5 ~])
94 1.067.931.605
95 1.067.931.605
95
96
96
97
97
98
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+
98***
213***
99### `++mum`
214### `++mum`
100
215
101−XX document
216+31-bit MurmurHash3 scrambler
102
217
103−Accepts
218+Hashes `a` with the 31-bit MurmurHash3 non-cryptographic hash algorithm,
104−-------
219+producing an atom.
105
220
106−Produces
221+#### Accepts
107−--------
108
222
109−Source
223+`a` is a noun.
110−------
111
224
112− ++ mum :: mug with murmur3
225+#### Produces
226+
227+An atom.
228+
229+#### Source
230+
231+ ++ mum
113 ~/ %mum
232 ~/ %mum
114− |= a/*
233+ |= a=*
115 |^ (trim ?@(a a (mix $(a -.a) (mix 0x7fff.ffff $(a +.a)))))
234 |^ (trim ?@(a a (mix $(a -.a) (mix 0x7fff.ffff $(a +.a)))))
116− ++ spec :: standard murmur3
235+ ++ trim
117− |= {syd/@ key/@}
236+ |= key=@
118− ?> (lte (met 5 syd) 1)
237+ =+ syd=0xcafe.babe
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]
132 |- ^- @
238 |- ^- @
133− ?: =(inx len) goc
239+ =+ haz=(muk syd (met 3 key) key)
134− =+ kop=(cut 5 [inx 1] key)
240+ =+ ham=(mix (rsh 0 31 haz) (end 0 31 haz))
135− =. kop (mow kop 0xcc9e.2d51)
241+ ?.(=(0 ham) ham $(syd +(syd)))
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))
142
242
143−
144−Examples
243+#### Examples
145−--------
146
244
147 > (mum 10.000)
245 > (mum 10.000)
148− 1.232.632.901
246+ 795.713.195
247+
149 > (mum 10.001)
248 > (mum 10.001)
150− 658.093.079
249+ 420.521.697
250+
251+ > (mum 'jerry')
252+ 1.540.161.753
253+
151 > (mum 1)
254 > (mum 1)
152− 818.387.364
255+ 1.901.865.568
256+
153 > (mum (some 10))
257 > (mum (some 10))
154− 1.177.215.703
258+ 2.099.076.946
259+
155− > (mum ~[1 2 3 4 5])
260+ > (mum [1 2 3 4 5 ~])
156− 1.517.902.092
261+ 1.719.833.228
157
262
158
263
159
264