mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-13 11:45:45 +03:00
working initial tests and BTC byt take function
This commit is contained in:
parent
457542001c
commit
86202a80fe
27
BIP158.md
27
BIP158.md
@ -1,6 +1,6 @@
|
||||
# BIP158 - Neutrino Filters
|
||||
|
||||
## GCS Decodijng
|
||||
## GCS Decoding
|
||||
Below should yield `769.941` as the `delta`, with 3 bits of padding at the end.
|
||||
```
|
||||
=b158 -build-file %/lib/bip158/hoon
|
||||
@ -46,6 +46,9 @@ Genesis block on testnet, N=1
|
||||
Last should return `769.741`, same as `delta` in GCS above.
|
||||
|
||||
## Hash Set of Script Pubkeys
|
||||
From https://blog.trezor.io/bip158-compact-block-filters-9b813b07a878
|
||||
|
||||
### Just Hashing
|
||||
```
|
||||
=b158 -build-file %/lib/bip158/hoon
|
||||
=block-filter 0x9.027a.cea6.1b6c.c3fb.33f5.d52f.7d08.8a6b.2f75.d234.e89c.a800
|
||||
@ -61,7 +64,25 @@ Last should return `769.741`, same as `delta` in GCS above.
|
||||
Yields for the hashed set:
|
||||
~[176.536 2.341.508 3.078.625]
|
||||
|
||||
https://blog.trezor.io/bip158-compact-block-filters-9b813b07a878
|
||||
### Yes/No Match
|
||||
```
|
||||
(match:b158 block-filter k ~[in1 in2 out1])
|
||||
(match:b158 block-filter k ~[out1])
|
||||
```
|
||||
Yields `%y.` and `%.n`, respectively.
|
||||
|
||||
### Return All Matches
|
||||
```
|
||||
`(list [@ @ux])`(all-match:b158 block-filter k ~[in1 in2 out1])
|
||||
`(list [@ @ux])`(all-match:b158 block-filter k ~[out1])
|
||||
```
|
||||
Yields
|
||||
~[
|
||||
[25 0x76.a914.5033.3046.115e.aa0a.c9e0.2165.65f9.4507.0e44.5739.88ac]
|
||||
[25 0x76.a914.3ebc.40e4.11ed.3c76.f867.1150.7ab9.5230.0890.3972.88ac]
|
||||
]
|
||||
and
|
||||
~
|
||||
|
||||
|
||||
|
||||
0xfd.9721.d01c.f654.63f9.9af6
|
||||
|
14
TEST.md
Normal file
14
TEST.md
Normal file
@ -0,0 +1,14 @@
|
||||
# Tests
|
||||
|
||||
## BTC libs
|
||||
```
|
||||
|commit %home
|
||||
=test -build-file %/tests/btc/hoon
|
||||
```
|
||||
|
||||
## BIP158
|
||||
```
|
||||
|commit %home
|
||||
=test -build-file %/tests/bip158/hoon
|
||||
run:test
|
||||
```
|
@ -1,3 +1,4 @@
|
||||
/+ btc
|
||||
|%
|
||||
+$ bits [wid=@ dat=@ub]
|
||||
++ params
|
||||
@ -5,6 +6,7 @@
|
||||
++ p 19
|
||||
++ m 784.931
|
||||
--
|
||||
::
|
||||
++ siphash
|
||||
|= [k=byts m=byts]
|
||||
^- byts
|
||||
@ -180,12 +182,21 @@
|
||||
?: =(n 0xfe) (cut 3 [(sub start 4) 4] filter)
|
||||
?: =(n 0xff) (cut 3 [(sub start 8) 8] filter)
|
||||
n
|
||||
::
|
||||
++ parse-filter
|
||||
|= [filter=@ux]
|
||||
^- [n=@ux gcs-set=bits]
|
||||
=+ n=(get-n filter)
|
||||
=+ gcs-size=(sub (met 3 filter) (met 3 n))
|
||||
[n [(mul 8 gcs-size) `@ub`(end [3 gcs-size] filter)]]
|
||||
:: +to-key: blockhash (little endian) to key for siphash
|
||||
::
|
||||
++ to-key
|
||||
|= blockhash=tape
|
||||
^- byts
|
||||
%+ take:byt:btc 16
|
||||
%- flip:byt:btc
|
||||
[32 (to-hex:btc (crip blockhash))]
|
||||
:: +match: whether block filter matches *any* target scriptpubkeys
|
||||
:: - filter: full block filter, with leading N
|
||||
:: - k: key for siphash (end of blockhash, reversed)
|
||||
@ -197,12 +208,51 @@
|
||||
=/ [p=@ m=@] [p:params m:params]
|
||||
=/ [n=@ux gcs-set=bits] (parse-filter filter)
|
||||
=+ target-hs=(set-construct:hsh targets k (mul n m))
|
||||
=| last-val=@
|
||||
=+ last-val=0
|
||||
|-
|
||||
?: (lth wid.gcs-set p) %.n
|
||||
?~ target-hs %.n
|
||||
=^ delta gcs-set
|
||||
(de:gol gcs-set p)
|
||||
:: TODO fill in w algo
|
||||
:: TODO decode all the items in a loop
|
||||
%.y
|
||||
=. last-val
|
||||
(add delta last-val)
|
||||
?: =(last-val i.target-hs)
|
||||
%.y
|
||||
?: (lth last-val i.target-hs)
|
||||
$
|
||||
$(target-hs t.target-hs)
|
||||
:: +all-match: returns all target byts that match
|
||||
:: - filter: full block filter, with leading N
|
||||
:: - k: key for siphash (end of blockhash, reversed)
|
||||
:: - targets: scriptpubkeys to match
|
||||
::
|
||||
++ all-match
|
||||
|= [filter=@ux k=byts targets=(list byts)]
|
||||
^- (list byts)
|
||||
=/ [p=@ m=@] [p:params m:params]
|
||||
=/ [n=@ux gcs-set=bits] (parse-filter filter)
|
||||
=/ target-map=(map @ byts)
|
||||
%- ~(gas by *(map @ byts))
|
||||
%+ turn targets
|
||||
|=(t=byts [(to-range:hsh t (mul n m) k) t])
|
||||
=+ target-hs=~(tap in ~(key by target-map))
|
||||
=+ last-val=0
|
||||
=| matches=(list @)
|
||||
|-
|
||||
?: (lth wid.gcs-set p)
|
||||
(murn matches ~(get by target-map))
|
||||
?~ target-hs
|
||||
(murn matches ~(get by target-map))
|
||||
=^ delta gcs-set
|
||||
(de:gol gcs-set p)
|
||||
=. last-val
|
||||
(add delta last-val)
|
||||
?: =(last-val i.target-hs)
|
||||
%= $
|
||||
target-hs t.target-hs
|
||||
matches [last-val matches]
|
||||
==
|
||||
?: (lth last-val i.target-hs)
|
||||
$
|
||||
$(target-hs t.target-hs)
|
||||
--
|
||||
|
21
lib/btc.hoon
21
lib/btc.hoon
@ -3,6 +3,19 @@
|
||||
=< [sur .]
|
||||
=, sur
|
||||
|%
|
||||
++ to-hex
|
||||
|= h=@t
|
||||
^- @ux
|
||||
?: =('' h) 0x0
|
||||
:: Add leading 00
|
||||
::
|
||||
=+ (lsh [3 2] h)
|
||||
:: Group by 4-size block
|
||||
::
|
||||
=+ (rsh [3 2] -)
|
||||
:: Parse hex to atom
|
||||
::
|
||||
`@ux`(rash - hex)
|
||||
++ xpub-type
|
||||
|= =xpub
|
||||
^- bipt
|
||||
@ -399,6 +412,14 @@
|
||||
|= b=byts
|
||||
^- byts
|
||||
[wid.b (rev 3 b)]
|
||||
:: +take: take n bytes from front of byts
|
||||
:: pads front with extra zeroes if n is longer than byts
|
||||
::
|
||||
++ take
|
||||
|= [n=@ b=byts]
|
||||
?: (gth n wid.b)
|
||||
[n dat.b]
|
||||
[n (rsh [3 (sub wid.b n)] dat.b)]
|
||||
:: Converts a list of bits to a list of n-bit numbers
|
||||
:: input-bits should be big-endian
|
||||
::
|
||||
|
58
tests/bip158.hoon
Normal file
58
tests/bip158.hoon
Normal file
@ -0,0 +1,58 @@
|
||||
/+ *test, *bip158
|
||||
|%
|
||||
+$ filter [blockhash=tape fil=@ux]
|
||||
::
|
||||
++ block-filters
|
||||
|%
|
||||
++ test-genesis
|
||||
^- filter
|
||||
:* "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"
|
||||
0x19d.fca8
|
||||
==
|
||||
++ test-926485
|
||||
^- filter
|
||||
:* "000000000000015d6077a411a8f5cc95caf775ccf11c54e27df75ce58d187313"
|
||||
0x9.027a.cea6.1b6c.c3fb.33f5.d52f.7d08.8a6b.2f75.d234.e89c.a800
|
||||
==
|
||||
--
|
||||
++ run
|
||||
=/ [p=@ m=@] [p:params m:params]
|
||||
:: Filter parsing and GCS decoding
|
||||
::
|
||||
=/ bfil=filter test-genesis:block-filters
|
||||
=/ [n=@ux gcs-set=bits] (parse-filter fil.bfil)
|
||||
=/ result
|
||||
[delta=769.941 [3 0b0]]
|
||||
?. =(result (de:gol gcs-set p))
|
||||
~|("Filter Parse/GCS Decoding Failed" !!)
|
||||
:: Filter parsing for large N
|
||||
:: TODO: tests for N when prefixed with 0xfd/0xfe/0xff
|
||||
:: TODO:
|
||||
::
|
||||
:: Siphash
|
||||
::
|
||||
=/ bfil=filter test-genesis:block-filters
|
||||
=/ [n=@ux gcs-set=bits] (parse-filter fil.bfil)
|
||||
=+ f=(mul n m:params)
|
||||
=+ k=(to-key blockhash.bfil)
|
||||
=/ item
|
||||
[67 0x41.0467.8afd.b0fe.5548.2719.67f1.a671.30b7.105c.d6a8.28e0.3909.a679.62e0.ea1f.61de.b649.f6bc.3f4c.ef38.c4f3.5504.e51e.c112.de5c.384d.f7ba.0b8d.578a.4c70.2b6b.f11d.5fac]
|
||||
=/ result (rsh [0 64] (mul f (swp 3 dat:(siphash k item))))
|
||||
?. =(769.941 result)
|
||||
~|("Siphash and mod F failed" !!)
|
||||
:: Hashing and matching scriptpubkeys
|
||||
::
|
||||
=/ bfil=filter test-926485:block-filters
|
||||
=/ [n=@ux gcs-set=bits] (parse-filter fil.bfil)
|
||||
=+ k=(to-key blockhash.bfil)
|
||||
=+ in1=[25 0x76.a914.3ebc.40e4.11ed.3c76.f867.1150.7ab9.5230.0890.3972.88ac]
|
||||
=+ in2=[25 0x76.a914.5033.3046.115e.aa0a.c9e0.2165.65f9.4507.0e44.5739.88ac]
|
||||
=+ out1=[21 0x14.7e69.a44c.1a94.2139.c8ab.4127.8325.5e1e.46d0.f0da]
|
||||
=/ result
|
||||
(set-construct:hsh ~[in1 in2 out1] k (mul n m:params))
|
||||
?. =(~[176.536 2.341.508 3.078.625] result)
|
||||
~|("Hashing scriptpubkeys failed" !!)
|
||||
?. =([%.y %.n] [(match fil.bfil k ~[in1 in2 out1]) (match fil.bfil k ~[out1])])
|
||||
~|("Matching scriptpubkeys to block failed" !!)
|
||||
"Tests passed"
|
||||
--
|
0
tests/btc.hoon
Normal file
0
tests/btc.hoon
Normal file
Loading…
Reference in New Issue
Block a user