shift-reality / php-crypto

A Pure PHP (without any external PHP-extension) Implementation of BLAKE-256, GROESTL-256, JH-256, SKEIN-256 cryptography algorithms.
Apache License 2.0
12 stars 6 forks source link

cool project #1

Closed wyattzheng closed 6 years ago

wyattzheng commented 6 years ago

are you going to build a php-miner of Monero coin? they are all algos that CryptoNote mentioned

thanks to your contributions!

wyattzheng commented 6 years ago

i run the perfm tests,and i found many many many perfm problems we can avoid. for example, intval() is super slow, 600% slower than (int), and it is called very often. and many details waiting us to work with.i made 40seconds a hash up to 25seconds a hash,and keep trying

shift-reality commented 6 years ago

You can await until i upload new lib with 4 hash functions? blake(your last variant) jh gro skein Now it's complete 50/50... So, other parts like oaes and CN goes to your phpminer project and purged from lib. u64 class goes to lib into 'utils' namespace.

I think intval is fn call + new zval created, where (int) is just syntax cast and same zendval used

wyattzheng commented 6 years ago

sure,we work for more effiency,i desire new library to include.You know a lot about the core of php like zval

wyattzheng commented 6 years ago

blake jh gro skein they are called one time ,and cost few times

shift-reality commented 6 years ago

Yes, we place it into library And i help you with main code

wyattzheng commented 6 years ago

ok

shift-reality commented 6 years ago

skein-256-256 or skein-512-256 used in CN?

wyattzheng commented 6 years ago

don't know,https://cryptonote.org/cns/cns008.txt said it's skein-256

shift-reality commented 6 years ago

can you research about this problem? I have no ideas Implementation is diff and hashes too

shift-reality commented 6 years ago

Currenty is skein 512-256, i can implement 256-256

wyattzheng commented 6 years ago

skein 512-256 is used for what.i think it's difficult for me ,so i use your algo libs

shift-reality commented 6 years ago

Ok, i uploaded new lib into new branch.

Tests fine (run test.php on your's machine to compare): test JH256#0 is: OK test BLAKE256#1 is: OK test BLAKE256#2 is: OK test BLAKE256#3 is: OK test BLAKE256#4 is: OK test BLAKE256#5 is: OK test BLAKE256#6 is: OK test BLAKE256#7 is: OK test BLAKE256#8 is: OK test GROESTL256#9 is: OK test GROESTL256#10 is: OK test GROESTL256#11 is: OK test GROESTL256#12 is: OK test SKEIN256#13 is: OK test SKEIN256#14 is: OK test SKEIN256#15 is: OK

You can modify file fixtures.json to add some test cases(JH256 has only one test). Skein 512-256 already implemented. Usage is like that: $hash = Hasher::doHash($algo, InputDataSupplier::forHex($dataHex)); Please Look to file InputDataSupplier and IHash to more info (input/output is actually set from hex/binary string and bytes array)

Final code is: replace from: //√ switch($chosen){ case 0: echo "blake256\n"; $ret=blake256($data_ret);break; case 1: echo "groestl256\n"; $ret=groestl256($data_ret);break; case 2: echo "jh\n"; $ret=jh($data_ret);break; case 3: echo "skein\n"; $ret=skein($data_ret);break; } TO: $hashes = ['BLAKE256','GROESTL256','JH256','SKEIN256']; $algo = $hashes[$chosen]; echo "$algo\n"; $ret = Hasher::doHash($algo, InputDataSupplier::forByteArray($inputDataArray))->byteArray();

wyattzheng commented 6 years ago

nice!you pack them into one lib!i will run the test.php and follow this

wyattzheng commented 6 years ago

don't find a test.php in your project folder, can you upload yours?

shift-reality commented 6 years ago

https://github.com/shift-reality/php-crypto/tree/newlib composer.json:

{ "name": "shift196/php-project3-example", "description": "Description of project PhpProject3-example.", "authors": [ { "name": "bogdan", "email": "shift196@yandex.ru" } ], "repositories": [ { "url": "https://github.com/shift-reality/php-crypto.git", "type": "git", "reference": "newlib" } ], "require": { "shift196/lib-akash": "dev-newlib" } }

wyattzheng commented 6 years ago

ok,they are in new branch

shift-reality commented 6 years ago

I submit new stable version, so you can use 1.0.*

shift-reality commented 6 years ago

Useful feature: Hasher::registerAlgo, can be used for register KECCAK and cryptonight into lib. So usage is become easy.. Like that: Hasher::registerAlgo('CRYPTONIGHT', new CryptonightImpl()); CryptonightImpl should implement interface IHashFunction

wyattzheng commented 6 years ago

test JH256#0 is: OK test BLAKE256#1 is: OK test BLAKE256#2 is: OK test BLAKE256#3 is: OK test BLAKE256#4 is: OK test BLAKE256#5 is: OK test BLAKE256#6 is: OK test BLAKE256#7 is: OK test BLAKE256#8 is: OK test GROESTL256#9 is: OK test GROESTL256#10 is: OK test GROESTL256#11 is: OK test GROESTL256#12 is: OK test SKEIN256#13 is: OK test SKEIN256#14 is: OK test SKEIN256#15 is: OK

work well

wyattzheng commented 6 years ago

You made this become structured.More algos!

shift-reality commented 6 years ago

I think need add adapter to openssl digest algos if it's available, for biggest support list

wyattzheng commented 6 years ago

Hahaha,you are good at and interested in implementing algos in pure PHP.That makes those who made algos-extensions embarrassed.If you add more COMPLEX algos in pure PHP,i think this project will be famous.More people like to import it

shift-reality commented 6 years ago

$longstate=new splFixedArray(2097152);//Thanks to god,we have this;

Is that really faster than array?

wyattzheng commented 6 years ago

really,about 10% faster,but it cost half memory usage. i have to use this

wyattzheng commented 6 years ago

it can replace ARRAY immediately without changing any code

wyattzheng commented 6 years ago

default php settings included 30 seconds running time limit and 64M memory limit .I try my best finally the limits are both unreachable

wyattzheng commented 6 years ago

https://github.com/deepwn/phpminer Added phpcryptolib !

shift-reality commented 6 years ago

any problem with keccak or cryptonight?

wyattzheng commented 6 years ago

when you pack keccak into Hash class,and i use it.CryptoNight.php it's written by me,and i want keep this.How fast your cn.php behave?

shift-reality commented 6 years ago

Oaes and CN is 100% part of phpminer, but i not sure about keccak functions.. If i add it into lib, only 1 method from interface IHashFunction is available-doHash.. But in CN used keccakf and keccak1600...

Is keccak called one time as other functions or should be optimized?

I not sure about my cn.php) probably extremely slow

shift-reality commented 6 years ago

Why you changed encodeLELong to encodeBELong?

shift-reality commented 6 years ago

test JH256#0 is: OK test BLAKE256#1 is: OK test BLAKE256#2 is: OK test BLAKE256#3 is: OK test BLAKE256#4 is: OK test BLAKE256#5 is: OK test BLAKE256#6 is: OK test BLAKE256#7 is: OK test BLAKE256#8 is: OK test GROESTL256#9 is: OK test GROESTL256#10 is: OK test GROESTL256#11 is: OK test GROESTL256#12 is: OK test SKEIN256#13 is: OK test SKEIN256#14 is: OK test SKEIN256#15 is: OK test KECCAK256#16 is: OK test KECCAK256#17 is: OK test KECCAK256#18 is: OK

Download 1.0.1 version for keccak, usage is: `$keccak = Keccak::getInstance();

//round-function: $keccak->keccakf(array $state, int $rounds);

//256-bit hash: $keccak->keccak1600(array $inputData, array &$hash); or $hash=$keccak->doHash($inputData); or $hashObject = Hasher::doHash('KECCAK256', ...);`

shift-reality commented 6 years ago

i spent lots of time to search out the BOTTLENECK of performance。And now i have a valued >report.Bottleneck is DEFINITELY NOT about Math Calculating.

new o_u64(a,b); this cost so much time;You can run this line 500000 times to test it running in LOOPS.This is one of >MAIN reason why being so low efficiency.

Why I can sure that this is not because of Math Calculating,i was running many tests about Math >Calculating like XOR bitShift,it cost little even in 500000 times.

How to solve this problem about "new o_u64(a,b);"???, new a PHP Class cost too much,and every >time we do a bit-calc like ShiftRight,it will NEW a new o_64 to as a return value.

Look to Object Pool pattern, and create Uint64Poll class with methods: -getObject -returnObject This allow to reuse same object and not need to create new instances every time

shift-reality commented 6 years ago

or just make all o_u64 methods static and add zero-arg: array $data where 0 index = hi, 1 = lo likelist($hi,$lo)=$data; so used new array with 2 elements instead of object creation or just use 8-bytes binary string (need alot of changes)

shift-reality commented 6 years ago

pool it's right way

shift-reality commented 6 years ago

I create pool for that, please await about 10 min

shift-reality commented 6 years ago

https://github.com/shift-reality/php-crypto/releases

shift-reality commented 6 years ago

object instantiation is hidden into getObject method So when u64 object not needed more you should call UInt64Poll::returnObject($obj)

shift-reality commented 6 years ago

If both operands for the &, | and ^ operators are strings, then the operation will be performed on the ASCII values of the characters that make up the strings and the result will be a string. In all other cases, both operands will be converted to integers and the result will be an integer.

If the operand for the ~ operator is a string, the operation will be performed on the ASCII values of the characters that make up the string and the result will be a string, otherwise the operand and the result will be treated as integers.

We need implement this, so u64 class become to 8-byte string XOR easiest $str0^$str1 Faster that manipulate each byte in N times, i checked.. where N is count of iters(array elements)

shift-reality commented 6 years ago

UInt64String works fine Need profile performance of this

wyattzheng commented 6 years ago

oh,i am coding to make mul_sum_xor_dst() and xor_blocks() into an extension.I found that Binary ("8bytes string") worked well when passing data into it.So,it's neccessary to implement UInt64String class.PHPMiner will use extension_loaded to detect if extension loaded.If not,using UInt64String to calculate.That is perfect solution I think

wyattzheng commented 6 years ago

wow , you had implemented That !!!!!! I download this utils library right now

shift-reality commented 6 years ago

ext is not pure php and not work on freehost

wyattzheng commented 6 years ago

it's hard believe that the price of my host is 0.1$/1month,and support php.ini and extension.And pure PHP is IMPOSSIBLE to earn XMR

shift-reality commented 6 years ago

what you think about UInt64String?

shift-reality commented 6 years ago

i read some articles of subject -object pooling Statistics said that method will increase performance +4000% in PHP with object pool size = 1024objects

wyattzheng commented 6 years ago

lots of functions need to change into a strange codes like SubAndShiftAndMixAddRound(),.I am trying to use this to rewrite my cryptonight().Your statistics data is gratifying

wyattzheng commented 6 years ago

almost all the codes need to rewrite if use UInt64String,so hard

shift-reality commented 6 years ago

don't rewrite code, use pooled u64 version instead but not forget return objects to pool