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

[question] custom crypto #202

Closed
florianlenz opened this issue Jun 16, 2018 · 8 comments
Closed

[question] custom crypto #202

florianlenz opened this issue Jun 16, 2018 · 8 comments
Labels
discussion Questions, feedback and general information.

Comments

@florianlenz
Copy link

Hi,

we would like to run ether.js from inside of this vm. Is there a way to override the crypto functions used by ether.js with out own functions? Since we are using golang it's gonna be faster to calculate hashes etc outside of the VM based on given parameters.

Thanks.

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Jun 17, 2018
@ricmoo
Copy link
Member

ricmoo commented Jun 17, 2018

If you make it part of the build process, it is quite easy to modify the Gruntfile (in v4, the gulpfile) to put the hooks into the VM at that level. It might be a bit easier on the new v4 (coming out very soon). Does that VM allow synchronous hooks though? That has been one of the bigger problems with trying to hook into the VM, in Ethers Wallet. Since a call to sha2 (for example) is expected to be synchronous, but the VM doesn't actually allow that.

Which functions are you trying to hook? For the most part, the cryptographic functions are actually not all that expensive unless you are trying to mine vanity contract addresses or such things. HDNode calculation can be slow (as you can see by running the test cases), so the HMAC and SHA512 could benefit, as well as JSON wallet decrypting/encrypting, since they use scrypt, which would gain the most being done at the native layer.

@florianlenz
Copy link
Author

florianlenz commented Jun 17, 2018

If you make it part of the build process, it is quite easy to modify the Gruntfile (in v4, the gulpfile) to put the hooks into the VM at that level

Perfect.

Does that VM allow synchronous hooks though?

Yes. They are synchronous by default.

Which functions are you trying to hook?

Everything that will help us to decrease unnecessary memory usage. We plan to run multiple vm instances and would like to keep the executed JS as small as possible.

so the HMAC and SHA512 could benefit

Thanks. That sounds very good. I will wait for the V4 release :)

@florianlenz
Copy link
Author

florianlenz commented Jun 21, 2018

Hi. I just tried to run this in otto vm and sadly this regex and be parsed since the VM (and golang) don't support the regex expression. I guess we have to fork then :/ (we will leave a note to the origin). whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",") is this regular expression just about to insert a , every three characters? We will need to build a workaround.

@ricmoo
Copy link
Member

ricmoo commented Jun 21, 2018

Which part exactly is is having issues with? I don’t mind unrolling the brace, but if it doesn’t support standard regular expressions, might be worth fixing it:

https://github.com/robertkrimen/otto/blob/master/type_regexp.go

Try adjusting the regex to see what is causing the hiccups... :)

It could be the backtracking or forward tracking as well...

@florianlenz
Copy link
Author

florianlenz commented Jun 21, 2018

@ricmoo i am not sure if I get what you mean. Lookahead regex is not supported by golang and can't be fixed therefore. So in order to use this library in the VM i need to change this line. However, I don't want you to change ether.js just because it doesn't work in otto VM. So I am curiose what whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",") does. For me it looks like it's inserting a , every three characters. But I am not 100% sure. If I know what it does I can replace it in our fork.

@ricmoo
Copy link
Member

ricmoo commented Jun 21, 2018

Oh yes, that is all it does is “commify” a string. So, add a comma every 3 digits, from the right. In the future I would like to support arbitrary formatting, some countries use comma vs space vs decimal point, some group by 3 vs 4... But this has been an issue in ethers.objc, since parsing becomes ambiguous...

I just worry about forking, since it means you will have to merge bug fixes and such manually...

I just meant that there is a bridge for regex in the Otto already, and it would make sense for JavaScript compatible regex to be implemented correctly in Otto... Perhaps if the Go regex fails, fallback onto a JavaScript shim. Just an idea. :)

@ricmoo ricmoo added the v4.0 label Jun 29, 2018
ricmoo added a commit that referenced this issue Sep 26, 2018
@ricmoo
Copy link
Member

ricmoo commented Sep 26, 2018

The new commify operation in v4 no longer uses back-tracking, so should be easier to use in this environment. :)

@florianlenz
Copy link
Author

Thanks. We switched to another VM since ether.js took four minutes to load in otto. I guess it's a problem related to otto since it only takes three seconds to load ether.js in duktape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

2 participants