extend ssz hash type to arbitrary lengths (#116)

* refactor hash in simple serialize to support arbitrary byte length

* make comment clearer

* add deserialization of hashN

* formatting

* remove specific hash examples
This commit is contained in:
Danny Ryan 2018-11-09 11:39:03 +01:00 committed by GitHub
parent fa9c29d522
commit b76275482d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 79 deletions

View File

@ -16,9 +16,6 @@ deserializing objects and data types.
- [uint: 8/16/24/32/64/256](#uint-816243264256) - [uint: 8/16/24/32/64/256](#uint-816243264256)
- [Address](#address) - [Address](#address)
- [Hash](#hash) - [Hash](#hash)
* [Hash32](#hash32)
* [Hash96](#hash96)
* [Hash97](#hash97)
- [Bytes](#bytes) - [Bytes](#bytes)
- [List/Vectors](#listvectors) - [List/Vectors](#listvectors)
- [Container](#container) - [Container](#container)
@ -26,9 +23,6 @@ deserializing objects and data types.
- [uint: 8/16/24/32/64/256](#uint-816243264256-1) - [uint: 8/16/24/32/64/256](#uint-816243264256-1)
- [Address](#address-1) - [Address](#address-1)
- [Hash](#hash-1) - [Hash](#hash-1)
* [Hash32](#hash32-1)
* [Hash96](#hash96-1)
* [Hash97](#hash97-1)
- [Bytes](#bytes-1) - [Bytes](#bytes-1)
- [List/Vectors](#listvectors-1) - [List/Vectors](#listvectors-1)
- [Container](#container-1) - [Container](#container-1)
@ -97,7 +91,7 @@ return b'\x01' if value is True else b'\x00'
#### Address #### Address
The address should already come as a hash/byte format. Ensure that length is The `address` should already come as a hash/byte format. Ensure that length is
**20**. **20**.
| Check to perform | Code | | Check to perform | Code |
@ -113,63 +107,24 @@ return value
| Hash Type | Usage | | Hash Type | Usage |
|:---------:|:------------------------------------------------| |:---------:|:------------------------------------------------|
| `hash32` | Hash size of ``keccak`` or `blake2b[0.. < 32]`. | | `hashN` | Hash of arbitrary byte length `N`. |
| `hash96` | BLS Public Key Size. |
| `hash97` | BLS Public Key Size with recovery bit. |
| Checks to perform | Code | | Checks to perform | Code |
|:-----------------------------------|:---------------------| |:---------------------------------------|:---------------------|
| Length is correct (32) if `hash32` | ``len(value) == 32`` | | Length in bytes is correct for `hashN` | ``len(value) == N`` |
| Length is correct (96) if `hash96` | ``len(value) == 96`` |
| Length is correct (97) if `hash97` | ``len(value) == 97`` |
##### hashN
**Example all together**
```python ```python
if (type(value) == 'hash32'): assert(len(value) == N)
assert(len(value) == 32)
elif (type(value) == 'hash96'):
assert(len(value) == 96)
elif (type(value) == 'hash97'):
assert(len(value) == 97)
else:
raise TypeError('Invalid hash type supplied')
return value return value
``` ```
##### Hash32
Ensure 32 byte length and return the bytes.
```python
assert(len(value) == 32)
return value
```
##### Hash96
Ensure 96 byte length and return the bytes.
```python
assert(len(value) == 96)
return value
```
##### Hash97
Ensure 97 byte length and return the bytes.
```python
assert(len(value) == 97)
return value
```
#### Bytes #### Bytes
For general `byte` type: For general `bytes` type:
1. Get the length/number of bytes; Encode into a `4-byte` integer. 1. Get the length/number of bytes; Encode into a `4-byte` integer.
2. Append the value to the length and return: ``[ length_bytes ] + [ 2. Append the value to the length and return: ``[ length_bytes ] + [
value_bytes ]`` value_bytes ]``
@ -313,37 +268,16 @@ return rawbytes[current_index:current_index+20], new_index
#### Hash #### Hash
##### Hash32 ##### hashN
Return the 32 bytes. Return the `N` bytes.
```python ```python
assert(len(rawbytes) >= current_index + 32) assert(len(rawbytes) >= current_index + N)
new_index = current_index + 32 new_index = current_index + N
return rawbytes[current_index:current_index+32], new_index return rawbytes[current_index:current_index+N], new_index
``` ```
##### Hash96
Return the 96 bytes.
```python
assert(len(rawbytes) >= current_index + 96)
new_index = current_index + 96
return rawbytes[current_index:current_index+96], new_index
```
##### Hash97
Return the 97 bytes.
```python
assert(len(rawbytes) >= current_index + 97)
new_index = current_index + 97
return rawbytes[current_index:current_index+97], new_index
```
#### Bytes #### Bytes
Get the length of the bytes, return the bytes. Get the length of the bytes, return the bytes.