status-im / status-protocol-go

Status Protocol implementation in Go
Mozilla Public License 2.0
0 stars 1 forks source link

Porting username generationg code #71

Closed jakubgs closed 4 years ago

jakubgs commented 5 years ago

Turns out we haven't ported the code that generates the 3 word usernames from JavaScript yet. This code lives here: https://github.com/status-im/status-js-api/blob/master/src/utils.ts And uses this JS package for its deterministic randomness: https://github.com/chancejs/chancejs

This package implements it's own version of Mersenne Twister algorithm for generating 32bit ints: https://github.com/chancejs/chancejs/blob/220cb36f/chance.js#L7406-L7440

There is a Go version of the Chance library but it doesn't generate the same usernames. I also found a similar implementation of the Mersenne Twister called mt19937 but that produces only random int64: https://github.com/seehuhn/mt19937/blob/cc7708819/mt19937.go#L107-L132

We could try to reproduce the version of randomness from the JS library, which would probably be a real pain in the ass. Or we could just say "fuck it" - since there's no continuity between Beta and V1 accounts - and just use an existing solution like that mt19937 library.

jakubgs commented 5 years ago

This is the C implementation of Mersenne Twister which the Chance library uses: https://github.com/ESultanik/mtwister So in theory we could try using that to get the same results in Go.

jakubgs commented 5 years ago

I tried comparing different implementations of Mersenne Twister from different languages with the same seed(123) to see if I can get the same thing, and I was unable to:

Library Language Method Result
ESultanik/mtwister C genRandLong() -297355222
seehuhn/mt19937 Go Int31() 1345184524
seehuhn/mt19937 Go Uint32() 2690369049
seehuhn/mt19937 Go Int63() 5777523539921853504
seehuhn/mt19937 Go Uint64() 5777523539921853504
mersenne-twister JS random_int() 2991312382
mersenne-twister JS random_int31() 1495656191
mersenne-twister JS random_long() 0.6964691855978616
chance JS integer() 3539274234331136

So I'm really confused as how this is supposed to be deterministic....

jakubgs commented 5 years ago

Igor did make an implementation already for a bot in Go(which I was told about by Andrea) right here: https://github.com/status-im/status-go-bots/blob/ethdenver-bot/vendor/github.com/status-im/status-go/sdk/three_word_name.go I'm gonna try adding this to the repo in a PR.

cammellos commented 5 years ago

I am thinking about dropping Mersenne Twister in favor of something much simpler, fibonacci lfsr https://en.wikipedia.org/wiki/Linear-feedback_shift_register , we don't need anything sophisticated for this I believe, and it should be trivial to port in other languages.

here's for the initial implementation https://github.com/status-im/status-protocol-go/compare/feature/gfycat?expand=1

Also because v1 will be breaking, there's no issue in changing username scheme.

jakubgs commented 4 years ago

I agree, as I said in OP, it's a good idea to take this opportunity and to switch to something simpler. I was talking about this with @oskarth and @jarradh yesterday and they had some mild complaints, but if we double team them it should be fine. Wish you were here with us.

adambabik commented 4 years ago

What are these mild complaints? I would totally go for a simpler version. What @cammellos proposed seems pretty neat.

jakubgs commented 4 years ago

Well, we can ask them directly:

@oskarth & @jarradh, do you have any specific objections to using V1 as an opportunity to get rid of the weird JS library for 3 word username generation and going with Fibonacci LFSR (for example)?

oskarth commented 4 years ago

No specific objections! As long as we can implement the same algo in different code bases. All for simplifying while we have a clean break

oskarth commented 4 years ago

One proposal sounded like same seed would generate different names in JS and Go land, which would be bad. That's all.

jakubgs commented 4 years ago

This is being handled by https://github.com/status-im/status-protocol-go/pull/74 which awaits testing with status-react in https://github.com/status-im/status-react/pull/9006.

jakubgs commented 4 years ago

Closing since https://github.com/status-im/status-protocol-go/pull/74 was merged.