-
Notifications
You must be signed in to change notification settings - Fork 2
Сonvert functions for u64 #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d5f807a
added convert functions for u64
aritkulova e519d70
typo
aritkulova 58d0dbe
returned u32 typo back, it will be fixed in upcoming pr;
aritkulova 1bc9a6c
added missing split_u64_into_u8
aritkulova aeb1539
Merge branch 'dev' into feat/u64-convert
aritkulova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /// Widening uint conversions | ||
|
|
||
| /// Converts u64 to u128 | ||
| pub fn u64_to_u128(a: u64) -> u128 { | ||
| <(u64, u64)>::into((0, a)) | ||
| } | ||
|
|
||
| /// Converts u64 to u256 | ||
| pub fn u64_to_u256(a: u64) -> u256 { | ||
| let a_u128: u128 = u64_to_u128(a); | ||
|
|
||
| <(u128, u128)>::into((0, a_u128)) | ||
| } | ||
|
|
||
| /// Splitting uint conversions | ||
|
|
||
| /// Splits u64 into eight u8 | ||
| pub fn split_u64_into_u8(a: u64) -> (u8, u8, u8, u8, u8, u8, u8, u8) { | ||
| <u64>::into(a) | ||
| } | ||
|
|
||
| /// Splits u64 into four u16 | ||
| pub fn split_u64_into_u16(a: u64) -> (u16, u16, u16, u16) { | ||
| <u64>::into(a) | ||
| } | ||
|
|
||
| /// Splits u64 into two u32 | ||
| pub fn split_u64_into_u32(a: u64) -> (u32, u32) { | ||
| <u64>::into(a) | ||
| } | ||
|
|
||
| /// Narrowing uint conversions | ||
|
|
||
| /// Converts u64 into u1. | ||
| /// Panics if the value does not fit in u1 | ||
| pub fn safe_u64_to_u1(a: u64) -> u1 { | ||
| let u1_max: u64 = jet::left_pad_low_1_64(jet::high_1()); | ||
|
|
||
| assert!(jet::le_64(a, u1_max)); | ||
|
|
||
| jet::rightmost_64_1(a) | ||
| } | ||
|
|
||
| /// Converts u64 into u8. | ||
| /// Panics if the value does not fit in u8 | ||
| pub fn safe_u64_to_u8(a: u64) -> u8 { | ||
| let u8_max: u64 = jet::left_pad_low_8_64(jet::high_8()); | ||
|
|
||
| assert!(jet::le_64(a, u8_max)); | ||
|
|
||
| jet::rightmost_64_8(a) | ||
| } | ||
|
|
||
| /// Converts u64 into u16. | ||
| /// Panics if the value does not fit in u16 | ||
| pub fn safe_u64_to_u16(a: u64) -> u16 { | ||
| let u16_max: u64 = jet::left_pad_low_16_64(jet::high_16()); | ||
|
|
||
| assert!(jet::le_64(a, u16_max)); | ||
|
|
||
| jet::rightmost_64_16(a) | ||
| } | ||
|
|
||
| /// Converts u64 into u32. | ||
| /// Panics if the value does not fit in u32 | ||
| pub fn safe_u64_to_u32(a: u64) -> u32 { | ||
| let u32_max: u64 = jet::left_pad_low_32_64(jet::high_32()); | ||
|
|
||
| assert!(jet::le_64(a, u32_max)); | ||
|
|
||
| jet::rightmost_64_32(a) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| use crate::lib::u64::convert::{ | ||
| u64_to_u128, | ||
| u64_to_u256, | ||
| split_u64_into_u8, | ||
| split_u64_into_u16, | ||
| split_u64_into_u32, | ||
| safe_u64_to_u1, | ||
| safe_u64_to_u8, | ||
| safe_u64_to_u16, | ||
| safe_u64_to_u32 | ||
| }; | ||
| use crate::lib::asserts::{ | ||
| assert_eq_1, | ||
| assert_eq_8, | ||
| assert_eq_16, | ||
| assert_eq_32, | ||
| assert_eq_128, | ||
| assert_eq_256 | ||
| }; | ||
| use crate::helper::if_test_this_function; | ||
|
|
||
| fn main() { | ||
| let fn_idx: u8 = witness::FUNCTION_INDEX; | ||
|
|
||
| let a: u64 = witness::FIRST_ARG; | ||
|
|
||
| let expected: u256 = witness::EXPECTED; | ||
|
|
||
| match if_test_this_function(0, fn_idx) { | ||
| true => { | ||
| let (_, expected): (u128, u128) = <u256>::into(expected); | ||
|
|
||
| assert_eq_128(u64_to_u128(a), expected); | ||
| }, | ||
| false => {}, | ||
| }; | ||
|
|
||
| match if_test_this_function(1, fn_idx) { | ||
| true => { | ||
| assert_eq_256(u64_to_u256(a), expected); | ||
| }, | ||
| false => {}, | ||
| }; | ||
|
|
||
| match if_test_this_function(2, fn_idx) { | ||
| true => { | ||
| let ( | ||
| a7, | ||
| a6, | ||
| a5, | ||
| a4, | ||
| a3, | ||
| a2, | ||
| a1, | ||
| a0 | ||
| ): (u8, u8, u8, u8, u8, u8, u8, u8) = split_u64_into_u8(a); | ||
|
|
||
| let (_, _, _, expected): (u64, u64, u64, u64) = <u256>::into(expected); | ||
| let ( | ||
| expected7, | ||
| expected6, | ||
| expected5, | ||
| expected4, | ||
| expected3, | ||
| expected2, | ||
| expected1, | ||
| expected0 | ||
| ): (u8, u8, u8, u8, u8, u8, u8, u8) = <u64>::into(expected); | ||
|
|
||
| assert_eq_8(a7, expected7); | ||
| assert_eq_8(a6, expected6); | ||
| assert_eq_8(a5, expected5); | ||
| assert_eq_8(a4, expected4); | ||
| assert_eq_8(a3, expected3); | ||
| assert_eq_8(a2, expected2); | ||
| assert_eq_8(a1, expected1); | ||
| assert_eq_8(a0, expected0); | ||
| }, | ||
| false => {}, | ||
| }; | ||
|
|
||
| match if_test_this_function(3, fn_idx) { | ||
| true => { | ||
| let (a3, a2, a1, a0): (u16, u16, u16, u16) = split_u64_into_u16(a); | ||
|
|
||
| let (_, _, _, expected): (u64, u64, u64, u64) = <u256>::into(expected); | ||
| let ( | ||
| expected3, | ||
| expected2, | ||
| expected1, | ||
| expected0 | ||
| ): (u16, u16, u16, u16) = <u64>::into(expected); | ||
|
|
||
| assert_eq_16(a3, expected3); | ||
| assert_eq_16(a2, expected2); | ||
| assert_eq_16(a1, expected1); | ||
| assert_eq_16(a0, expected0); | ||
| }, | ||
| false => {}, | ||
| }; | ||
|
|
||
| match if_test_this_function(4, fn_idx) { | ||
| true => { | ||
| let (a1, a0): (u32, u32) = split_u64_into_u32(a); | ||
|
|
||
| let (_, _, _, expected): (u64, u64, u64, u64) = <u256>::into(expected); | ||
| let (expected1, expected0): (u32, u32) = <u64>::into(expected); | ||
|
|
||
| assert_eq_32(a1, expected1); | ||
| assert_eq_32(a0, expected0); | ||
| }, | ||
| false => {}, | ||
| }; | ||
|
|
||
| match if_test_this_function(5, fn_idx) { | ||
| true => { | ||
| let (_, _, _, expected): (u64, u64, u64, u64) = <u256>::into(expected); | ||
| let expected: u1 = jet::rightmost_64_1(expected); | ||
|
|
||
| assert_eq_1(safe_u64_to_u1(a), expected); | ||
| }, | ||
| false => {}, | ||
| }; | ||
|
|
||
| match if_test_this_function(6, fn_idx) { | ||
| true => { | ||
| let (_, _, _, expected): (u64, u64, u64, u64) = <u256>::into(expected); | ||
| let expected: u8 = jet::rightmost_64_8(expected); | ||
|
|
||
| assert_eq_8(safe_u64_to_u8(a), expected); | ||
| }, | ||
| false => {}, | ||
| }; | ||
|
|
||
| match if_test_this_function(7, fn_idx) { | ||
| true => { | ||
| let (_, _, _, expected): (u64, u64, u64, u64) = <u256>::into(expected); | ||
| let expected: u16 = jet::rightmost_64_16(expected); | ||
|
|
||
| assert_eq_16(safe_u64_to_u16(a), expected); | ||
| }, | ||
| false => {}, | ||
| }; | ||
|
|
||
| match if_test_this_function(8, fn_idx) { | ||
| true => { | ||
| let (_, _, _, expected): (u64, u64, u64, u64) = <u256>::into(expected); | ||
| let expected: u32 = jet::rightmost_64_32(expected); | ||
|
|
||
| assert_eq_32(safe_u64_to_u32(a), expected); | ||
| }, | ||
| false => {}, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.