Skip to content

Commit

Permalink
[+] v0.1.5
Browse files Browse the repository at this point in the history
- Added `replace_with` to `View`
- Removed simd feature
  • Loading branch information
0xMaka authored Oct 12, 2024
2 parents 849bf42 + 2632b2d commit cd7004b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ------------------------------------------------------------------------------
[package] #|
name = "kawala"
version = "0.1.4"
version = "0.1.5"
authors = ["Maka <makagucci@gmx.com>"]
edition = "2021"
license = "MIT"
Expand All @@ -20,6 +20,5 @@ path = "src/lib.rs"

[dependencies]
[features]
simd = []
##|
# ------------------------------------------------------------------------------
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ See tests and examples for inspiration.

| [version history](misc/kawalas_log.md) |
|-------------------------------------------|
| => [v0.1.5](misc/kawalas_log.md#v015) |
| => [v0.1.4](misc/kawalas_log.md#v014) |
| => [v0.1.3](misc/kawalas_log.md#v013) |
| => [v0.1.2](misc/kawalas_log.md#v012) |
Expand All @@ -64,7 +65,7 @@ See tests and examples for inspiration.
/* Run in dir: */ use kawala::{ View, Calldata, WithSig };
cargo add kawala //
// or add to Cargo.toml: //* init *///------------------------------------------------------->
kawala = "0.1.4" let mut view = View::new(Calldata::from_hex("ff"), WithSig::False);
kawala = "0.1.5" let mut view = View::new(Calldata::from_hex("ff"), WithSig::False);
```
</td>
</tr>
Expand All @@ -79,4 +80,4 @@ kawala = "0.1.4" let mut view = View::new(Calldata::from_hex("ff"),
_..416c6c204920736565206973206172726179206f66667365742c2075696e7432353620616e6420616464726573732e_ ![image](misc/assets/glider.png)
<!-- All I see is array offset, uint256 and address. -->
<!-- say free <3 -->


6 changes: 6 additions & 0 deletions misc/kawalas_log.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Version logs

##### v0.1.5
- Added `replace_with` to `View`
- Removed simd feature
- - Abstracting the f applied to a,b had ruined the simd implementations.
- - Juice seems not worth squeeze in current fix, so just removing for now.

##### v0.1.4
- Added `chunk32` and `chunks32` to kwl32
- Added `PartialEq` to `Bytes` and `Word`
Expand Down
2 changes: 1 addition & 1 deletion misc/kawalas_todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- [ ] 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
- [x] 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
Expand Down
12 changes: 0 additions & 12 deletions src/kwl32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,8 @@ use std::cmp::{ min, max };

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

/* simd : map can be better for small arrays and we are working on 32 bytes
so profile these! */
#[cfg(feature = "simd")]
use std::simd::{ u8x16, SimdFloat };
// h/o abstraction to remove repetition
fn _fab(f: &dyn Fn(u8,u8) -> u8, a: &[u8;32], b: &[u8;32]) -> [u8;32] {
#[cfg(feature = "simd")]
{
let a_simd = u8x16::from_slice(a);
let b_simd = u8x16::from_slice(b);
let result_simd = f(a_simd, b_simd);
return result_simd.to_array;
}
// fallback
let mut buf = [0u8;32]; (0..32) . for_each(|i|buf[i] = f(a[i], b[i]));
buf
}
Expand Down
29 changes: 20 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ View cont.. destructive functions that mutate state
pub fn remove(&mut self, index : usize) -> String {
self.__remove(index).hex()
}
// replace a word with a word, consume replacement
pub fn replace_with(&mut self, to : usize, from : usize) -> () {
let word = Some(self.page.remove(self._id(from)))
. unwrap_or(Word::from_bytes(&EMPTY_BYTES32));
self._replace_word(self._id(to), &word.bytes())
}
// replace word with left padded equivalent
pub fn left_pad(&mut self, index : usize) -> () {
let word = self.__word(index);
Expand Down Expand Up @@ -418,7 +424,7 @@ View cont.. destructive functions that mutate state
}
// *private* perform xor on the 2 tail elements, consume the tail
fn _fold (&mut self) -> () {
let buf = self.__pop(); self.xor_into(self.page.len() - ONE, buf.bytes())
let buf = self.__pop(); self.xor_into(self.page.len() - ONE, buf.bytes())
}
// public caller for above
pub fn xor_fold(&mut self) -> () {
Expand All @@ -432,15 +438,19 @@ View cont.. destructive functions that mutate state
}
// wrap the word at index around to the right by the given shift
pub fn right_shift(&mut self, index : usize, shift : usize) -> () {
let word = self.__word(index);
self.replace_from_bytes(index, self.__right_shift(word, shift).bytes())
let id = self._id(index); let word = self.__word(id);
self.replace_from_bytes(id, self.__right_shift(word, shift).bytes())
}
// wrap around the word at index around to the left by the given shift
// wrap the word at index around to the left by the given shift
pub fn left_shift(&mut self, index : usize, shift : usize) -> () {
let word = self.__word(index);
self.replace_from_bytes(index, self.__left_shift(word, shift).bytes())
let id = self._id(index); let word = self.__word(id);
self.replace_from_bytes(id, self.__left_shift(word, shift).bytes())
}

// returns an index within bounds
fn _id(&self, x: usize) -> usize {
let y = self.page.len() -ZERO_OFFSET; std::cmp::max(0, std::cmp::min(x, y))
}

/* ----------------------------------------------------------------------------
View cont.. exposed functions that take or return Kawala types
-----------------------------------------------------------------------------*/
Expand All @@ -454,9 +464,10 @@ 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
// remove a word, remove last if out of bounds
pub fn __remove(&mut self, index : usize) -> Word {
Some(self.page.remove(index)) . unwrap_or(Word::from_bytes(&EMPTY_BYTES32))
Some(self.page.remove(self._id(index)))
. unwrap_or(Word::from_bytes(&EMPTY_BYTES32))
}

/*
Expand Down
53 changes: 46 additions & 7 deletions tests/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod view {
use kawala::bai;

#[test]
fn view() -> (){
fn view() -> () {
/* Create a blank View */
let view = View::new(Calldata::from_bytes(&[0u8;32]), WithSig::False);
/* Run some sanity checks */
Expand Down Expand Up @@ -108,7 +108,7 @@ mod view {

// remove word and get Word
#[test]
fn __remove_word_ideal_state() -> (){
fn __remove_word_ideal_state() -> () {
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);
Expand All @@ -117,7 +117,7 @@ mod view {

// remove word get String
#[test]
fn remove_word_ideal_state() -> (){
fn remove_word_ideal_state() -> () {
let mut view = View::new(Calldata::from_bytes(&[[0u8;32],[1u8;32]].concat()), WithSig::False);
let expected = Word::from_bytes(&[1u8;32]).hex();
assert_eq!(view.word_count(), 2); let input = view.remove(1);
Expand All @@ -126,7 +126,7 @@ mod view {

// pop Word get Word
#[test]
fn __pop_word_ideal_state() -> (){
fn __pop_word_ideal_state() -> () {
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.__pop();
Expand All @@ -135,7 +135,7 @@ mod view {

// pop Word get get String
#[test]
fn pop_word_ideal_state() -> (){
fn pop_word_ideal_state() -> () {
let mut view = View::new(Calldata::from_bytes(&[[0u8;32],[1u8;32]].concat()), WithSig::False);
let expected = Word::from_bytes(&[1u8;32]).hex();
assert_eq!(view.word_count(), 2); let input = view.pop();
Expand All @@ -144,7 +144,7 @@ mod view {

// append from Word
#[test]
fn __append_word_ideal_state() -> (){
fn __append_word_ideal_state() -> () {
let mut view = View::new(Calldata::from_bytes(&[0u8;32]), WithSig::False);
let input = Word::from_bytes(&[1u8;32]);
let expected = &Word::from_bytes(&[1u8;32]);
Expand All @@ -156,7 +156,7 @@ mod view {

// append from hex
#[test]
fn append_word_ideal_state() -> (){
fn append_word_ideal_state() -> () {
let mut view = View::new(Calldata::from_bytes(&[0u8;32]), WithSig::False);
let input = &bai::con::bytes_to_hex(&[1u8;32]);
let expected = bai::con::bytes_to_hex(&[1u8;32]);
Expand All @@ -166,6 +166,45 @@ mod view {
assert_eq!(view.word_count(), 2);
}

// replace with word
#[test]
fn replace_with_word_ideal_state() -> () {
let mut view = View::new(Calldata::from_bytes(&[0u8;32]), WithSig::False);

let input = Word::from_bytes(&[1u8;32]);
let expected = &Word::from_bytes(&[1u8;32]);
assert_eq!(view.word_count(), 1);

view.__append(input);

assert_eq!(view.word_count(), 2);
assert_ne!(view.__word(0), expected);

view.replace_with(0,1);

assert_eq!(view.__word(0), expected);
assert_eq!(view.word_count(), 1);
}

// replace with word from out of bounds
#[test]
fn replace_with_word_out_of_bounds() -> () {
let mut view = View::new(Calldata::from_bytes(&[1u8;32]), WithSig::False);

let input = Word::from_bytes(&[0u8;32]);
let expected = &Word::from_bytes(&[0u8;32]);
assert_eq!(view.word_count(), 1);

view.__append(input);

assert_eq!(view.word_count(), 2);
assert_ne!(view.__word(0), expected);

view.replace_with(0,99);

assert_eq!(view.__word(0), expected);
assert_eq!(view.word_count(), 1);
}
}


0 comments on commit cd7004b

Please sign in to comment.