Skip to content
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

UInt256 #19

Closed
zackshapiro opened this issue Dec 4, 2017 · 11 comments
Closed

UInt256 #19

zackshapiro opened this issue Dec 4, 2017 · 11 comments
Assignees

Comments

@zackshapiro
Copy link

Hey Joel,

If I wanted to implement UInt256, would I essentially just duplicate your work with a project with UInt128 in it and double all the 64 values to 128?

@Jitsusama
Copy link
Owner

Hello @zackshapiro,

That method would work for a lot of the logic, but once you get to multiplication it would get harder. I do have the double width operations defined, so you could just depend on this project and wrap around those operations to get what you want; assuming that you don’t want to be able to perform double width 256-bit arithmetic.

@Jitsusama Jitsusama self-assigned this Dec 4, 2017
@zackshapiro
Copy link
Author

Thanks for getting back to me so fast! I don't need to do any arithmetic, I just need to represent numbers in UInt128 and public/private key pairs as UInt256

@Jitsusama
Copy link
Owner

Oh, then you could probably just compose a struct from this struct having an upper and lower value like mine.

Are you wanting to store as 256bit numbers and present as a string?

BTW; my double width operations didn’t do what I thought, so it would take a bit of work to make a similar 256bit library.

@zackshapiro
Copy link
Author

Are you wanting to store as 256bit numbers and present as a string?

Yeah, exactly. In my project I need to have a 256 bit seed, run it through a hashing algorithm to get a key pair and then do another hashing operation with the public key. Eventually I'll need to do a string operation on one of them but that's all I'm using them for

@Jitsusama
Copy link
Owner

You’d probably want to create a new struct, using a higher and lower value of 128 bit UInts and include your hashing algorithm as a method.

Take a look at my string methods when you’re ready to represent the numbers as strings, as I believe my method will work on any sized integer count. You might need to introduce your own bit shifting methods as well, as I don’t recall off the top of my head what my string method relies on.

@zackshapiro
Copy link
Author

Got it. Thanks @Jitsusama.

I've never worked at the bit level before so I'm a bit intimidated by all this but I'll figure it out.

Working on the struct now

@Jitsusama
Copy link
Owner

_valueToString is the method for my String conversion.

It will require a division method instead of a bit shift, but that probably depends on a bit shift method. I’d have to dig to remember.

It can be a bit hairy. I started writing this library when I wanted to natively store IPv6 addresses. I soon found myself down a rabbit hole 😀

@zackshapiro
Copy link
Author

@Jitsusama Here's my implementation,

https://gist.github.com/zackshapiro/848a2c1efeda9a34b89b8b5648a7c24c

I took all of the UInt128s and turned them into UInt256s, all the 64s and doubled them, all the 127s and replaced them with 255s

Would you mind taking a look and seeing how that looks since you're much more familiar with the implementation? I'm still digesting :)

I left 2 comments at the bottom of my gist with open questions, if I needed to do anything more in two spots.

The multipliedFullWidth is a doozy of a method, but sounds like I may be able to cut it - in needed - since it doesn't fit my project's needs

@Jitsusama
Copy link
Owner

The multipliedFillWidth method will need significant changes in order to handle 256 bits of math. It’s basically performing long multiplication as you might do on paper, but instead of handling each decimal digit, it’s doing it in chunks of digits. I don’t have my MacBook with me (I’m on a business trip), so I can’t play much, but I would strongly recommend writing out some XCTestCases for scenarios you need to be able to handle and then use breakpoints to start figuring out where things break down. Unfortunately there is a lot of interdependency on arithmetic methods to perform tasks as the only way to do more “advanced” arithmetic is by composing simpler arithmetic, which all has to be handled by your library.

@zackshapiro
Copy link
Author

Got it working!

Thanks for your help Joel

@Jitsusama
Copy link
Owner

That’s great to hear, and you are most welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants