Skip to content

Commit

Permalink
v0.1.4-pre
Browse files Browse the repository at this point in the history
- Added `chunk32` and `chunks32` to kwl32
- Added `PartialEq` to `Bytes` and `Word`
- Added `remove` and `__remove` to `View`
  • Loading branch information
0xMaka authored Sep 30, 2024
2 parents 31d2a3a + a3e7ef4 commit 5aaf963
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ See tests and examples for inspiration.

| [version history](misc/kawalas_log.md) |
|-------------------------------------------|
| => [v0.1.4](misc/kawalas_log.md#v014) |
| => [v0.1.3](misc/kawalas_log.md#v013) |
| => [v0.1.2](misc/kawalas_log.md#v012) |
| => [v0.1.1](misc/kawalas_log.md#v011) |
| => -------- |
| => [v0.1.1](misc/kawalas_log.md#v011) |
| <img width=100/> |

</td>
Expand Down
29 changes: 15 additions & 14 deletions misc/kawalas_log.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@

## Version logs

##### v0.1.1
- Initial commit
- - Porting over some of the python tools I wrote, as a single module
- - Data dump of initial working library { lib.rs, kwl32.rs, bai.rs }
##### v0.1.4-pre
- Added `chunk32` and `chunks32` to kwl32
- Added `PartialEq` to `Bytes` and `Word`
- Added `remove` and `__remove` to `View`

##### v0.1.3
- Added edge case handlers to `pad32l` and `pad32r`
- - Functions needed to be more robust as the ones accepting arbitrary length
- Tests added to cover most of bai and kwl32
- Modules are now public

##### v0.1.2
- Removed traits
- - Codebase wasn't written with enough respect for them, so they aren't being used

- Modified `View` method `summary()` (still too crude)
- - Now prints each k,v to a single line if less than `SUMMARY_COUNT`
- - Changed summary key _"Signature"_ to _"sig"_

- Fixed printing error in example `basic_stream`

- Added `data()` and `len()` wrappers to `Signature` and `Word` types for convenience
- Added more common `pop` to`View`, returns the hex `String` as opposed to `Word` type.
- Added more common `pop` to`View`, returns the hex `String` as opposed to `Word` type
- Swapped some magic numbers for constants

##### v0.1.3
- Added edge case handlers to `pad32l` and `pad32r`
- - Functions needed to be more robust as the ones accepting arbitrary length
- Tests added to cover most of bai and kwl32
- Modules are now public
##### v0.1.1
- Initial commit
- - Porting over some of the python tools I wrote, as a single module
- - Data dump of initial working library { lib.rs, kwl32.rs, bai.rs }

13 changes: 9 additions & 4 deletions misc/kawalas_todo.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@

## Current to do

- [ ] Deque => Give option to pop from the top of the page
- [ ] Masks => View methods for simplified masking
- We can already build a mask, but can make the process feel more intuitive

- [ ] Replace => View method to replace from word, consuming replacement
- There is already replace, and long winded ways to do above, just add an abstraction

- [ ] Deque => View method to pop from the top of the page
- Not an issue just undecided on how I want to handle empty, if at all

- [ ] Remove => Give option to remove an element from within the middle of the page
- [x] Remove => View method to remove an element from within the middle of the page
- Not something I'd use over clearing, but very reasonable to expect to have the option

- [ ] Chunk32 => Give kwl32 a dedicated chunk function
- [x] Chunk => kwl32 dedicated chunk function
- Unlikley to be utilised by Kawala
- - Kawalas implementation is more forgiving for its less performance concerned `View`.
- - Gives kwl32 viability as its own module, though Kawala will remain dependancy free.
Expand Down
15 changes: 14 additions & 1 deletion src/kwl32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
pub mod util {
use std::cmp::{ min, max };

// outout the result of right padding input with zeros
// output the result of right padding input with zeros
pub fn pad32r(bytes: &[u8]) -> [u8;32] {
let ost = min(32, bytes.len() as i32) as usize;
let mut padded = [0u8;32]; padded[..ost] . copy_from_slice(&bytes[..ost]);
Expand Down Expand Up @@ -44,6 +44,19 @@ use std::cmp::{ min, max };
buf[..32 - shift] . copy_from_slice(&bytes[shift..]); buf
}

//-----------------------------------------------------------------------------

// takes an arbitrary lengthed slice, returns a 32 byte slice
pub fn chunk32(bytes: &[u8]) -> [u8;32] {
if bytes.len() < 32 { pad32r(bytes) }
else { let mut buf = [0u8;32]; buf . copy_from_slice(&bytes[..32]); buf }
}
// takes an arbitrary lengthed slice, returns an array of 32 byte slice(s)
pub fn chunks32(bytes: &[u8]) -> Vec<[u8;32]> {
(0..bytes.len()) . step_by(32) . map(|x| { chunk32(&bytes[x..]) })
. collect::<Vec<[u8;32]>>()
}

//-----------------------------------------------------------------------------

/* simd : map can be better for small arrays and we are working on 32 bytes
Expand Down
33 changes: 31 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ impl<I: std::slice::SliceIndex<[u8]>> std::ops::Index<I> for Bytes {
}
}

impl PartialEq for Bytes {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
( Bytes::Bytes4(x) , Bytes::Bytes4(y) ) => x == y,
( Bytes::Bytes32(x), Bytes::Bytes32(y)) => x == y,
( Bytes::Array(x) , Bytes::Array(y) ) => x == y,
_ => false
}
}
}

/* ----------------------------------------------------------------------------
Calldata structure
-----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -196,6 +207,13 @@ impl Word {
pub fn hex_0x(&self) -> String {
"0x".to_owned() + &self.data.hex()
}

}

impl PartialEq for Word {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}

/* ----------------------------------------------------------------------------
Expand Down Expand Up @@ -416,7 +434,15 @@ View cont.. exposed functions that take or return Kawala types
pub fn __pop(&mut self) -> Word {
self.page.pop() . unwrap_or(Word::from_bytes(&EMPTY_BYTES32))
}

// remove an element
pub fn __remove(&mut self, index : usize) -> Word {
Some(self.page.remove(index)) . unwrap_or(Word::from_bytes(&EMPTY_BYTES32))
}

/*
To do: Profile pseudo deque vs `VecDeque` refactor. Consider how infrequent.
*/

/* -----------------------*NOTE*: end of destructive ------------------------ */

// returns a ref to all 32 byte Words
Expand Down Expand Up @@ -499,8 +525,11 @@ use bai::con::{ bytes_to_hex, hex_to_bytes };
use kwl32::util::{ pad32l, pad32r };
use kwl32::util::{ xor32, and32, not32, or32 };
use kwl32::util::{ roll32l, roll32r };
#[allow(unused_imports)]
use kwl32::util::{ chunk32, chunks32 };

/*
End of core.
End of core.
//////////////////////////////////////////////////////////////////////// */
/* ----------------------------------------------------------------------------
MIT License 2024 Maka */
66 changes: 61 additions & 5 deletions tests/kwl32_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ mod kwl32_util {
. into_iter()
. chain([1u8; 16])
. collect::<Vec<u8>>();

assert_eq!(util::roll32r(&input, 16), expected . as_slice());
}

Expand All @@ -117,7 +116,6 @@ mod kwl32_util {
. into_iter()
. chain([0u8; 16])
. collect::<Vec<u8>>();

assert_eq!(util::roll32l(&input, 16), expected . as_slice());
}

Expand All @@ -127,18 +125,76 @@ mod kwl32_util {
fn roll32r_shift_exceeding_32() {
let input = [1u8; 32];
let expected = input;

assert_eq!(util::roll32r(&input, 64), expected);
}

#[test]
fn roll32l_shift_exceeding_32() {
let input = [1u8; 32];
let expected = input;

assert_eq!(util::roll32l(&input, 64), expected);
}


//-------- -------- CHUNK 32 -------- --------//

// less input
#[test]
fn chunk32_less_than_32_bytes() {
let input = [0u8; 16];
let expected = util::pad32r(&input);
assert_eq!(util::chunk32(&input), expected);
}

// exact input
#[test]
fn chunk32_exact_32_bytes() {
let input = [0u8; 32];
assert_eq!(util::chunk32(&input), input);
}

// more input
#[test]
fn chunk32_more_than_32_bytes() {
let input = [0u8; 64];
let expected = [0u8; 32];
assert_eq!(util::chunk32(&input), expected);
}

//-------- -------- CHUNKS 32 -------- --------//

// empty input
#[test]
fn chunks32_empty() { assert_eq!(util::chunks32(&[]), Vec::<[u8;32]>::new()) }

// exact input
#[test]
fn chunks32_exact_32_bytes() {
let input = [0u8; 32];
assert_eq!(util::chunks32(&input), vec![input]);
}

// regular multi input
#[test]
fn chunks32_multiple_32_bytes() {
let input = [0u8; 96];
assert_eq!(util::chunks32(&input), vec![
[0u8;32],
[0u8;32],
[0u8;32]
]);
}

// irregular multi (will trunk)
#[test]
fn chunks32_not_multiple_32_bytes() {
let input = [0u8; 65];
assert_eq!(util::chunks32(&input), vec![
[0u8;32],
[0u8;32],
[0u8;32]
]);
}

//-------- -------- XOR32 -------- --------//


Expand Down
15 changes: 14 additions & 1 deletion tests/view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod view {

use kawala::{ View, Calldata, WithSig };
use kawala::{ View, Calldata, WithSig, Word };
#[test]
fn view() -> (){

Expand Down Expand Up @@ -105,5 +105,18 @@ mod view {
view.replace(0,word2);
assert_eq!(view.word(0), word2);
}


#[test]
fn __remove_word() -> (){
let mut view = View::new(Calldata::from_bytes(&[[0u8;32],[1u8;32]].concat()), WithSig::False);
let expected = Word::from_bytes(&[1u8;32]);
assert_eq!(view.word_count(), 2);
let input = view.__remove(1);
assert_eq!(input, expected);
assert_eq!(view.word_count(), 1);
}


}

0 comments on commit 5aaf963

Please sign in to comment.