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

draft: Final touches (closes #147, #135, #133, #132, #124, #111, #100, #92, #82, #81, #57, #54) #154

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2480131
chore: remove deprecated classes
recanman May 2, 2024
2c8311e
chore: remove old example.php
recanman May 2, 2024
5e417b6
style: format files
recanman May 2, 2024
ac1513f
feat: rename library, bump php, add phpunit test support
recanman May 2, 2024
612c909
feat: add new Mnemonic class
recanman May 2, 2024
ea70d6d
ci: add security and test workflows
recanman May 2, 2024
32702a5
feat: partially-refactored varint class and tests
recanman May 2, 2024
dc74c82
chore: delete old base58 class
recanman May 9, 2024
4eead4b
feat: implement base58 encode and tests
recanman May 9, 2024
95d72fc
docs: block comments for Base58
recanman May 9, 2024
1a699c7
feat: implement Monero base58 decode
recanman May 9, 2024
d374296
fix: use base58 static methods in Cryptonote, set namespace
recanman May 9, 2024
0b269e5
feat: add some Cryptonote tests
recanman May 16, 2024
793f73e
feat: BigInt, use it in base58
recanman May 16, 2024
1c89c99
chore: improve composer.json gmp suggestion msg
recanman May 16, 2024
5f5eecc
docs: grammatical error in comment
recanman May 16, 2024
6bbbac1
feat: add tests for BigInteger class
recanman May 16, 2024
69b9a6f
ci: add release action
recanman May 25, 2024
461773f
docs: add documentation on publishing
recanman May 25, 2024
2d5d318
ci: change main to master
recanman May 25, 2024
873fdb0
fix: allow mixed input to various BigInteger functions
recanman May 27, 2024
9b05ea7
chore: remove ext-curl dependency
recanman May 27, 2024
cfb4d06
feat: BigInteger bitshift left and right
recanman May 27, 2024
8b9bf27
feat: add bitshift left/right to BigInteger
recanman Jun 1, 2024
51a0bf9
feat: partially implement new ed25519 class
recanman Jun 1, 2024
c9273b7
chore: delete redundant subaddress class
recanman Jun 1, 2024
5d9a8f4
chore: delete old ed25519 class
recanman Jun 1, 2024
457ab44
feat: MoneroNetwork enum in Cryptonote, declare strict types
recanman Jun 1, 2024
95b081c
fix: declare Cryptonote type in test
recanman Jun 1, 2024
8db754c
chore: comment out failing Cryptonote tests until functions fixed (te…
recanman Jun 1, 2024
4945605
feat: accept MoneroNetwork enum in Cryptonote constructor
recanman Jun 1, 2024
87497ea
feat: implement Ed25519 point encoding
recanman Jun 2, 2024
a7b9059
test: add another point test
recanman Jun 2, 2024
a76b1ab
feat: implement bit and decodepoint
recanman Jun 2, 2024
f0a0e6a
chore: remove unused parameter
recanman Jun 2, 2024
54f8d73
chore: fix wordset docstrings
recanman Jun 2, 2024
b4db214
chore: fix many phpstan errors
recanman Jun 2, 2024
40d5f0a
test: add negative base58 test
recanman Jun 2, 2024
04e86f1
test: edwards and scalarmult test, formatting
recanman Jun 5, 2024
2e1d826
fix: edwards implementation
recanman Jun 5, 2024
b6f49d2
fix: don't convert to int in bit
recanman Jun 5, 2024
f732277
fix: decodepoint
recanman Jun 5, 2024
002394c
test: ed25519 public key
recanman Jun 5, 2024
114fbaa
test: sc_reduce
recanman Jun 5, 2024
43f11ad
fix: duplicate declaration of network_prefixes
recanman Aug 3, 2024
b2831a5
style: add editorconfig and format files
recanman Aug 3, 2024
8611a82
refactor: use new classes and clean up cryptonote code
recanman Aug 3, 2024
b2d91ab
chore: remove unneeded extension
recanman Aug 3, 2024
40031d0
docs: rewrite readme
recanman Aug 3, 2024
d3d8223
docs: add mnemonic.md
recanman Aug 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add tests for BigInteger class
  • Loading branch information
recanman committed May 16, 2024
commit 6bbbac1bebd1f02c0492657dc42cfabbb92c8973
119 changes: 119 additions & 0 deletions tests/unit/BCMATH_BigIntegerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

use MoneroIntegrations\MoneroCrypto\BigInteger;
use PHPUnit\Framework\TestCase;

class BCMATH_BigIntegerTest extends TestCase
{
public static function setUpBeforeClass(): void
{
unset($GLOBALS['S_MATH_BIGINTEGER_MODE']);
define('S_MATH_BIGINTEGER_MODE', "bcmath");
}

public function testCreate(): void
{
$values = [
["1010000", 2],
[hex2bin("50"), 256],
["50", 16],
["80", 10],
["80", 10],
[80, 10],
]; // [value, base]

for ($i = 0; $i < count($values); $i++) {
$value = $values[$i];
$bi = new BigInteger($value[0], $value[1]);

$this->assertEquals($bi->toBits(), "1010000");
$this->assertEquals($bi->toBytes(), hex2bin("50"));
$this->assertEquals($bi->toHex(), "50");
$this->assertEquals($bi->toDec(), "80");
$this->assertEquals($bi->toNumber(), 80);
$this->assertEquals($bi->toBase(58), "1M");
}
}

public function testCreateSafe(): void
{
$this->expectException(\ValueError::class);
new BigInteger("zz", 2);

$this->expectException(\ValueError::class);
new BigInteger("zz", 10);

$this->expectException(\ValueError::class);
new BigInteger("zz", 16);
}

public function testSpaces(): void
{
$this->assertEquals((new BigInteger("11 0 1", 2))->toBits(), "1101");
$this->assertEquals((new BigInteger("6 2 0 6", 10))->toDec(), "6206");
$this->assertEquals((new BigInteger("f3 5 12 ac 0", 16))->toHex(), "f3512ac0");
}

public function testOp(): void
{
$this->assertEquals((new BigInteger(20))->add(34)->toString(), "54");
$this->assertEquals((new BigInteger(20))->sub(14)->toString(), "6");
$this->assertEquals((new BigInteger(20))->mul(12)->toString(), "240");
$this->assertEquals((new BigInteger(20))->div(4)->toString(), "5");
$this->assertEquals((new BigInteger(20))->divR(7)->toString(), "6");

$qr = (new BigInteger(20))->divQR(6);
$this->assertEquals($qr[0]->toString(), "3");
$this->assertEquals($qr[1]->toString(), "2");

$this->assertEquals((new BigInteger(20))->mod(3)->toString(), "2");
$this->assertEquals((new BigInteger(54))->gcd(81)->toString(), "27");
$this->assertEquals((new BigInteger(3))->modInverse(10)->toString(), "7");
$this->assertEquals((new BigInteger(3))->pow(4)->toString(), "81");
$this->assertEquals((new BigInteger(3))->powMod(4, 10)->toString(), "1");
$this->assertEquals((new BigInteger(20))->abs()->toString(), "20");
$this->assertEquals((new BigInteger(20))->neg()->toString(), "-20");
$this->assertEquals((new BigInteger(20))->binaryAnd(18)->toString(), "16");
$this->assertEquals((new BigInteger(20))->binaryOr(18)->toString(), "22");
$this->assertEquals((new BigInteger(20))->binaryXor(18)->toString(), "6");
$this->assertEquals((new BigInteger(20))->setbit(3)->toString(), "28");
$this->assertEquals((new BigInteger(20))->testbit(4), true);
$this->assertEquals((new BigInteger(20))->testbit(3), false);
$this->assertEquals((new BigInteger(5))->testbit(0), true);
$this->assertEquals((new BigInteger(6))->testbit(0), false);
$this->assertEquals((new BigInteger(6))->testbit(1), true);
$this->assertEquals((new BigInteger(5))->testbit(1), false);
$this->assertEquals((new BigInteger(132))->testbit(7), true);
$this->assertEquals((new BigInteger(81))->testbit(7), false);
$this->assertEquals((new BigInteger(258))->testbit(8), true);
$this->assertEquals((new BigInteger(253))->testbit(8), false);
$this->assertEquals((new BigInteger(20))->scan0(2), 3);
$this->assertEquals((new BigInteger(20))->scan1(3), 4);
$this->assertEquals((new BigInteger(20))->cmp(22), -1);
$this->assertEquals((new BigInteger(20))->cmp(20), 0);
$this->assertEquals((new BigInteger(20))->cmp(18), 1);
$this->assertEquals((new BigInteger(20))->equals(20), true);
$this->assertEquals((new BigInteger(20))->equals(21), false);
$this->assertEquals((new BigInteger(-20))->sign(), -1);
$this->assertEquals((new BigInteger(0))->sign(), 0);
$this->assertEquals((new BigInteger(20))->sign(), 1);
$this->assertEquals((new BigInteger("-20"))->toString(), "-20");
$this->assertEquals((new BigInteger("-14", 16))->toString(), "-20");
$this->assertEquals((new BigInteger("-10100", 2))->toString(), "-20");
}

public function testBig(): void
{
$bits = "1001010111010010100001000101110110100001000101101000110101010101001";
$hex = "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3";
$dec = "436529472098746319073192837123683467019263172846";
$bytes = hex2bin($hex);

$this->assertEquals((new BigInteger($bits, 2))->toBits(), $bits);
$this->assertEquals((new BigInteger($dec, 10))->toDec(), $dec);
$this->assertEquals((new BigInteger($hex, 16))->toHex(), $hex);
$this->assertEquals((new BigInteger($bytes, 256))->toBytes(), $bytes);
}
}
119 changes: 119 additions & 0 deletions tests/unit/GMP_BigIntegerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

use MoneroIntegrations\MoneroCrypto\BigInteger;
use PHPUnit\Framework\TestCase;

class GMP_BigIntegerTest extends TestCase
{
public static function setUpBeforeClass(): void
{
unset($GLOBALS['S_MATH_BIGINTEGER_MODE']);
define('S_MATH_BIGINTEGER_MODE', "gmp");
}

public function testCreate(): void
{
$values = [
["1010000", 2],
[hex2bin("50"), 256],
["50", 16],
["80", 10],
["80", 10],
[80, 10],
]; // [value, base]

for ($i = 0; $i < count($values); $i++) {
$value = $values[$i];
$bi = new BigInteger($value[0], $value[1]);

$this->assertEquals($bi->toBits(), "1010000");
$this->assertEquals($bi->toBytes(), hex2bin("50"));
$this->assertEquals($bi->toHex(), "50");
$this->assertEquals($bi->toDec(), "80");
$this->assertEquals($bi->toNumber(), 80);
$this->assertEquals($bi->toBase(58), "1M");
}
}

public function testCreateSafe(): void
{
$this->expectException(\ValueError::class);
new BigInteger("zz", 2);

$this->expectException(\ValueError::class);
new BigInteger("zz", 10);

$this->expectException(\ValueError::class);
new BigInteger("zz", 16);
}

public function testSpaces(): void
{
$this->assertEquals((new BigInteger("11 0 1", 2))->toBits(), "1101");
$this->assertEquals((new BigInteger("6 2 0 6", 10))->toDec(), "6206");
$this->assertEquals((new BigInteger("f3 5 12 ac 0", 16))->toHex(), "f3512ac0");
}

public function testOp(): void
{
$this->assertEquals((new BigInteger(20))->add(34)->toString(), "54");
$this->assertEquals((new BigInteger(20))->sub(14)->toString(), "6");
$this->assertEquals((new BigInteger(20))->mul(12)->toString(), "240");
$this->assertEquals((new BigInteger(20))->div(4)->toString(), "5");
$this->assertEquals((new BigInteger(20))->divR(7)->toString(), "6");

$qr = (new BigInteger(20))->divQR(6);
$this->assertEquals($qr[0]->toString(), "3");
$this->assertEquals($qr[1]->toString(), "2");

$this->assertEquals((new BigInteger(20))->mod(3)->toString(), "2");
$this->assertEquals((new BigInteger(54))->gcd(81)->toString(), "27");
$this->assertEquals((new BigInteger(3))->modInverse(10)->toString(), "7");
$this->assertEquals((new BigInteger(3))->pow(4)->toString(), "81");
$this->assertEquals((new BigInteger(3))->powMod(4, 10)->toString(), "1");
$this->assertEquals((new BigInteger(20))->abs()->toString(), "20");
$this->assertEquals((new BigInteger(20))->neg()->toString(), "-20");
$this->assertEquals((new BigInteger(20))->binaryAnd(18)->toString(), "16");
$this->assertEquals((new BigInteger(20))->binaryOr(18)->toString(), "22");
$this->assertEquals((new BigInteger(20))->binaryXor(18)->toString(), "6");
$this->assertEquals((new BigInteger(20))->setbit(3)->toString(), "28");
$this->assertEquals((new BigInteger(20))->testbit(4), true);
$this->assertEquals((new BigInteger(20))->testbit(3), false);
$this->assertEquals((new BigInteger(5))->testbit(0), true);
$this->assertEquals((new BigInteger(6))->testbit(0), false);
$this->assertEquals((new BigInteger(6))->testbit(1), true);
$this->assertEquals((new BigInteger(5))->testbit(1), false);
$this->assertEquals((new BigInteger(132))->testbit(7), true);
$this->assertEquals((new BigInteger(81))->testbit(7), false);
$this->assertEquals((new BigInteger(258))->testbit(8), true);
$this->assertEquals((new BigInteger(253))->testbit(8), false);
$this->assertEquals((new BigInteger(20))->scan0(2), 3);
$this->assertEquals((new BigInteger(20))->scan1(3), 4);
$this->assertEquals((new BigInteger(20))->cmp(22), -1);
$this->assertEquals((new BigInteger(20))->cmp(20), 0);
$this->assertEquals((new BigInteger(20))->cmp(18), 1);
$this->assertEquals((new BigInteger(20))->equals(20), true);
$this->assertEquals((new BigInteger(20))->equals(21), false);
$this->assertEquals((new BigInteger(-20))->sign(), -1);
$this->assertEquals((new BigInteger(0))->sign(), 0);
$this->assertEquals((new BigInteger(20))->sign(), 1);
$this->assertEquals((new BigInteger("-20"))->toString(), "-20");
$this->assertEquals((new BigInteger("-14", 16))->toString(), "-20");
$this->assertEquals((new BigInteger("-10100", 2))->toString(), "-20");
}

public function testBig(): void
{
$bits = "1001010111010010100001000101110110100001000101101000110101010101001";
$hex = "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3";
$dec = "436529472098746319073192837123683467019263172846";
$bytes = hex2bin($hex);

$this->assertEquals((new BigInteger($bits, 2))->toBits(), $bits);
$this->assertEquals((new BigInteger($dec, 10))->toDec(), $dec);
$this->assertEquals((new BigInteger($hex, 16))->toHex(), $hex);
$this->assertEquals((new BigInteger($bytes, 256))->toBytes(), $bytes);
}
}
Loading