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

Hi. I Made extension for BigInt to Data and reverse. #54

Closed
OleksiiShulzhenko opened this issue Dec 24, 2018 · 10 comments
Closed

Hi. I Made extension for BigInt to Data and reverse. #54

OleksiiShulzhenko opened this issue Dec 24, 2018 · 10 comments

Comments

@OleksiiShulzhenko
Copy link

OleksiiShulzhenko commented Dec 24, 2018

extension BigInt {
    
    public func serialize() -> Data {
        var array = Array(BigUInt.init(self.magnitude).serialize())
        
        if array.count > 0 {
            if self.sign == BigInt.Sign.plus {
                if array[0] >= 128 {
                    array.insert(0, at: 0)
                }
            } else if self.sign == BigInt.Sign.minus {
                if array[0] <= 127 {
                    array.insert(255, at: 0)
                }
            }
        }
        
        return Data.init(bytes: array)
    }
    
    public init(_ data: Data) {
        var dataArray = Array(data)
        var sign: BigInt.Sign = BigInt.Sign.plus
        
        if dataArray.count > 0 {
            if dataArray[0] >= 128 {
                sign = BigInt.Sign.minus
                
                if dataArray.count > 1 {
                    if dataArray[0] == 255, dataArray.count > 1 {
                        dataArray.remove(at: 0)
                    } else {
                        dataArray[0] = UInt8(256 - Int(dataArray[0]))
                    }
                }
            }
        }
        
        let magnitude = BigUInt.init(Data.init(bytes: dataArray))
        
        self .init(sign: sign, magnitude: magnitude)
    }
}
@HaydenMcCabe
Copy link
Contributor

I created a pull request with a slightly different implementation of this idea, which you can take a look at. It's PR #61.

@tgymnich
Copy link
Collaborator

merged HaydenMcCabe approach

@sanjaykmwt94222
Copy link

@OleksiiShulzhenko's approach is more correct if we see what android's function toByteArray gives I was struggling for two days because the solution of @HaydenMcCabe Gives wrong output.

Please replace the serialize method of this library with @OleksiiShulzhenko's solution

@tgymnich
Copy link
Collaborator

tgymnich commented Oct 9, 2020

I'll look into it. Do you have some examples for wrong output?

@sanjaykmwt94222
Copy link

You yourself can compare by giving the same input to the android and iOS and you will get different results

@koraykoska
Copy link
Collaborator

koraykoska commented Oct 9, 2020 via email

@koraykoska
Copy link
Collaborator

koraykoska commented Oct 9, 2020 via email

@tgymnich
Copy link
Collaborator

tgymnich commented Oct 9, 2020

I think what @sanjaykmwt94222 wants to say is, that our implementation of serialize() uses a sign byte and an unsigned integer, when javas BigInteger uses 2s complement to serialize big integers and no sign byte.

In my opinion the output is not wrong and the documentation clearly states the fact, that we are using a sign byte.

@sanjaykmwt94222
Copy link

@tgymnich Okay in that case there should be a function which gives output in 2s complement format same as android's BigInteger class

@koraykoska
Copy link
Collaborator

koraykoska commented Oct 12, 2020 via email

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

Successfully merging a pull request may close this issue.

5 participants