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

Feat/u512 #128

Merged
merged 42 commits into from
Jan 28, 2025
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
f3f6b79
init
dovgopoly Jan 9, 2025
9d6d09c
rm comments
dovgopoly Jan 9, 2025
c8c57db
wip
dovgopoly Jan 15, 2025
ba239ee
wip
dovgopoly Jan 15, 2025
3720814
fixed add & added test
dovgopoly Jan 16, 2025
a8a79a5
wip (passed ecdsa check) 17.8kk
dovgopoly Jan 16, 2025
c698a36
fix
dovgopoly Jan 16, 2025
85a2c36
opt
dovgopoly Jan 17, 2025
de6382b
-500k
Arvolear Jan 17, 2025
31a36eb
15.8kk 2p
dovgopoly Jan 17, 2025
4e00dc7
15.3kk
dovgopoly Jan 17, 2025
7facbab
rm shl 15.48kk
dovgopoly Jan 17, 2025
32a9716
wip
dovgopoly Jan 20, 2025
9efa52b
wip
dovgopoly Jan 20, 2025
2c5dd2d
added test vectors, tests are failed
dovgopoly Jan 20, 2025
ce83623
fixed ecdsa512 impl 22.2kk
dovgopoly Jan 21, 2025
8f7deb4
20.4kk
dovgopoly Jan 21, 2025
007dc1c
added assert
dovgopoly Jan 21, 2025
e650ce3
small fixes
dovgopoly Jan 21, 2025
85684f1
refactored
dovgopoly Jan 21, 2025
57f0f8d
typo
dovgopoly Jan 21, 2025
6db5141
added crazy optimization with bit skipping 20.1kk
dovgopoly Jan 21, 2025
b9462a7
13.86kk & typos
dovgopoly Jan 21, 2025
288f0c1
remove opt 384 libs
mllwchrry Jan 23, 2025
b3cffd6
add tests for U512
mllwchrry Jan 23, 2025
5df79f0
add natspec
mllwchrry Jan 24, 2025
82a88c0
add operator overloading
mllwchrry Jan 24, 2025
936c966
modify moddiv test
mllwchrry Jan 24, 2025
5b188af
rm ops and fixed tests
dovgopoly Jan 26, 2025
cfc730b
added assign & call & bitwise ops
dovgopoly Jan 26, 2025
c1c7cd8
added modexpU256 & tested gas
dovgopoly Jan 26, 2025
0aaa9fe
typo
dovgopoly Jan 26, 2025
52672ed
small adjustments
dovgopoly Jan 26, 2025
d87784e
add U512 usage example and fix tests
mllwchrry Jan 27, 2025
bbca148
fix natspec
mllwchrry Jan 27, 2025
4b65a75
fixed comment
dovgopoly Jan 27, 2025
bf130be
add toBytes to natspec
mllwchrry Jan 27, 2025
66ba3e1
typos
dovgopoly Jan 27, 2025
d5a813c
typos
dovgopoly Jan 27, 2025
0a427dc
typos
dovgopoly Jan 27, 2025
8dd88d9
small adjustments
dovgopoly Jan 28, 2025
d053dd7
update readme
Arvolear Jan 28, 2025
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
rm shl 15.48kk
  • Loading branch information
dovgopoly committed Jan 17, 2025
commit 7facbabbfb03e97e3df429ef880f260974a2efc9
2 changes: 1 addition & 1 deletion contracts/libs/crypto/ECDSA384.sol
Original file line number Diff line number Diff line change
@@ -86,9 +86,9 @@
/// accept s only from the lower part of the curve
if (
U512.eqUint256(inputs_.r, 0) ||
U512.cmp(inputs_.r, params_.n) >= 0 ||
U512.eqUint256(inputs_.s, 0) ||
U512.cmp(inputs_.s, params_.lowSmax) > 0

Check warning on line 91 in contracts/libs/crypto/ECDSA384.sol

Codecov / codecov/patch

contracts/libs/crypto/ECDSA384.sol#L89-L91

Added lines #L89 - L91 were not covered by tests
) {
return false;
}
@@ -151,9 +151,9 @@
unchecked {
if (
U512.eqUint256(x_, 0) ||
U512.eq(x_, p_) ||
U512.eqUint256(y_, 0) ||
U512.eq(y_, p_)

Check warning on line 156 in contracts/libs/crypto/ECDSA384.sol

Codecov / codecov/patch

contracts/libs/crypto/ECDSA384.sol#L154-L156

Added lines #L154 - L156 were not covered by tests
) {
return false;
}
@@ -256,14 +256,14 @@
}

if (U512.eqUint256(y1_, 0)) {
return (x2_, y2_);

Check warning on line 259 in contracts/libs/crypto/ECDSA384.sol

Codecov / codecov/patch

contracts/libs/crypto/ECDSA384.sol#L259

Added line #L259 was not covered by tests
}

uint512 m1_ = U512.modexp(call_, x1_, two_, p_);
U512.modmulAssign(call_, m1_, three_, p_);
U512.modaddAssign(call_, m1_, a_, p_);

uint512 m2_ = U512.shl(call_, y1_, p_);
uint512 m2_ = U512.modmul(call_, y1_, two_, p_);
U512.moddivAssign(call_, m1_, m2_, p_);

x2_ = U512.modexp(call_, m1_, two_, p_);
@@ -293,7 +293,7 @@
unchecked {
if (U512.isNull(x1_) || U512.isNull(x2_)) {
if (U512.isNull(x1_) && U512.isNull(x2_)) {
return (x3, y3);

Check warning on line 296 in contracts/libs/crypto/ECDSA384.sol

Codecov / codecov/patch

contracts/libs/crypto/ECDSA384.sol#L296

Added line #L296 was not covered by tests
}

return
@@ -307,7 +307,7 @@
return _twiceAffine(call_, p_, two_, three_, a_, x1_, y1_);
}

return (x3, y3);

Check warning on line 310 in contracts/libs/crypto/ECDSA384.sol

Codecov / codecov/patch

contracts/libs/crypto/ECDSA384.sol#L310

Added line #L310 was not covered by tests
}

uint512 m1_ = U512.modsub(call_, y1_, y2_, p_);
21 changes: 0 additions & 21 deletions contracts/libs/crypto/bn/U512.sol
Original file line number Diff line number Diff line change
@@ -124,28 +124,28 @@
return -1;
}

assembly {

Check warning on line 127 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L127

Added line #L127 was not covered by tests
aWord_ := mload(add(a_, 0x20))
bWord_ := mload(add(b_, 0x20))
}

if (aWord_ > bWord_) {
return 1;

Check warning on line 133 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L133

Added line #L133 was not covered by tests
}

if (aWord_ < bWord_) {
return -1;

Check warning on line 137 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L137

Added line #L137 was not covered by tests
}

return 0;

Check warning on line 140 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L140

Added line #L140 was not covered by tests
}
}

function mod(call call_, uint512 a_, uint512 m_) internal view returns (uint512 r_) {
unchecked {
r_ = uint512.wrap(_allocate(_UINT512_ALLOCATION));

Check warning on line 146 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L144-L146

Added lines #L144 - L146 were not covered by tests

_mod(call_, a_, m_, r_);

Check warning on line 148 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L148

Added line #L148 was not covered by tests
}
}

@@ -155,29 +155,29 @@
}
}

function modAssignTo(call call_, uint512 a_, uint512 m_, uint512 to_) internal view {
unchecked {
_mod(call_, a_, m_, to_);

Check warning on line 160 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L158-L160

Added lines #L158 - L160 were not covered by tests
}
}

function modinv(call call_, uint512 a_, uint512 m_) internal view returns (uint512 r_) {
unchecked {
r_ = uint512.wrap(_allocate(_UINT512_ALLOCATION));

Check warning on line 166 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L164-L166

Added lines #L164 - L166 were not covered by tests

_modinv(call_, a_, m_, r_);

Check warning on line 168 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L168

Added line #L168 was not covered by tests
}
}

function modinvAssign(call call_, uint512 a_, uint512 m_) internal view {
unchecked {
_modinv(call_, a_, m_, a_);

Check warning on line 174 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L172-L174

Added lines #L172 - L174 were not covered by tests
}
}

function modinvAssignTo(call call_, uint512 a_, uint512 m_, uint512 to_) internal view {
unchecked {
_modinv(call_, a_, m_, to_);

Check warning on line 180 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L178-L180

Added lines #L178 - L180 were not covered by tests
}
}

@@ -194,21 +194,21 @@
}
}

function modexpAssign(call call_, uint512 b_, uint512 e_, uint512 m_) internal view {
unchecked {
_modexp(call_, b_, e_, m_, b_);

Check warning on line 199 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L197-L199

Added lines #L197 - L199 were not covered by tests
}
}

function modexpAssignTo(

Check warning on line 203 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L203

Added line #L203 was not covered by tests
call call_,
uint512 b_,
uint512 e_,
uint512 m_,
uint512 to_
) internal view {
unchecked {
_modexp(call_, b_, e_, m_, to_);

Check warning on line 211 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L210-L211

Added lines #L210 - L211 were not covered by tests
}
}

@@ -231,35 +231,35 @@
}
}

function modaddAssignTo(

Check warning on line 234 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L234

Added line #L234 was not covered by tests
call call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal view {
unchecked {
_modadd(call_, a_, b_, m_, to_);

Check warning on line 242 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L241-L242

Added lines #L241 - L242 were not covered by tests
}
}

function add(uint512 a_, uint512 b_) internal pure returns (uint512 r_) {
unchecked {
r_ = uint512.wrap(_allocate(_UINT512_ALLOCATION));

Check warning on line 248 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L246-L248

Added lines #L246 - L248 were not covered by tests

_add(a_, b_, r_);

Check warning on line 250 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L250

Added line #L250 was not covered by tests
}
}

function addAssign(uint512 a_, uint512 b_) internal pure {
unchecked {
_add(a_, b_, a_);

Check warning on line 256 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L254-L256

Added lines #L254 - L256 were not covered by tests
}
}

function addAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure {
unchecked {
_add(a_, b_, to_);

Check warning on line 262 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L260-L262

Added lines #L260 - L262 were not covered by tests
}
}

@@ -282,35 +282,35 @@
}
}

function modsubAssignTo(

Check warning on line 285 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L285

Added line #L285 was not covered by tests
call call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal view {
unchecked {
_modsub(call_, a_, b_, m_, to_);

Check warning on line 293 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L292-L293

Added lines #L292 - L293 were not covered by tests
}
}

function sub(uint512 a_, uint512 b_) internal pure returns (uint512 r_) {
unchecked {
r_ = uint512.wrap(_allocate(_UINT512_ALLOCATION));

Check warning on line 299 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L297-L299

Added lines #L297 - L299 were not covered by tests

_sub(a_, b_, r_);

Check warning on line 301 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L301

Added line #L301 was not covered by tests
}
}

function subAssign(uint512 a_, uint512 b_) internal pure {
unchecked {
_sub(a_, b_, a_);

Check warning on line 307 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L305-L307

Added lines #L305 - L307 were not covered by tests
}
}

function subAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure {
unchecked {
_sub(a_, b_, to_);

Check warning on line 313 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L311-L313

Added lines #L311 - L313 were not covered by tests
}
}

@@ -333,35 +333,35 @@
}
}

function modmulAssignTo(

Check warning on line 336 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L336

Added line #L336 was not covered by tests
call call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal view {
unchecked {
_modmul(call_, a_, b_, m_, to_);

Check warning on line 344 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L343-L344

Added lines #L343 - L344 were not covered by tests
}
}

function mul(uint512 a_, uint512 b_) internal pure returns (uint512 r_) {
unchecked {
r_ = uint512.wrap(_allocate(_UINT512_ALLOCATION));

Check warning on line 350 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L348-L350

Added lines #L348 - L350 were not covered by tests

_mul(a_, b_, r_);

Check warning on line 352 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L352

Added line #L352 was not covered by tests
}
}

function mulAssign(uint512 a_, uint512 b_) internal pure {
unchecked {
_mul(a_, b_, a_);

Check warning on line 358 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L356-L358

Added lines #L356 - L358 were not covered by tests
}
}

function mulAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure {
unchecked {
_mul(a_, b_, to_);

Check warning on line 364 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L362-L364

Added lines #L362 - L364 were not covered by tests
}
}

@@ -384,39 +384,18 @@
}
}

function moddivAssignTo(

Check warning on line 387 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L387

Added line #L387 was not covered by tests
call call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal view {
unchecked {
_moddiv(call_, a_, b_, m_, to_);

Check warning on line 395 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L394-L395

Added lines #L394 - L395 were not covered by tests
}
}

function shl(call call_, uint512 a_, uint512 p_) internal view returns (uint512 r_) {
unchecked {
r_ = uint512.wrap(_allocate(_UINT512_ALLOCATION));

_shl(call_, a_, p_, r_);
}
}

function _shl(call call_, uint512 a_, uint512 p_, uint512 r_) private view {
unchecked {
assembly {
let a1_ := mload(add(a_, 0x20))

mstore(r_, or(shl(1, mload(a_)), shr(255, a1_)))
mstore(add(r_, 0x20), shl(1, a1_))
}

_mod(call_, r_, p_, r_);
}
}

function _mod(call call_, uint512 a_, uint512 m_, uint512 r_) private view {
unchecked {
assembly {
@@ -479,9 +458,9 @@
}
}

function _add(uint512 a_, uint512 b_, uint512 r_) private pure {
unchecked {
assembly {

Check warning on line 463 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L461-L463

Added lines #L461 - L463 were not covered by tests
let aWord_ := mload(add(a_, 0x20))
let sum_ := add(aWord_, mload(add(b_, 0x20)))

@@ -566,9 +545,9 @@
}
}

function _mul(uint512 a_, uint512 b_, uint512 r_) private pure {
unchecked {
assembly {

Check warning on line 550 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L548-L550

Added lines #L548 - L550 were not covered by tests
let a0_ := shr(128, mload(a_))
let a1_ := and(mload(a_), 0xffffffffffffffffffffffffffffffff)
let a2_ := shr(128, mload(add(a_, 0x20)))
@@ -639,9 +618,9 @@
}
}

function _modmulOverflow(uint512 a_, uint512 b_, call call_) private pure {
unchecked {
assembly {

Check warning on line 623 in contracts/libs/crypto/bn/U512.sol

Codecov / codecov/patch

contracts/libs/crypto/bn/U512.sol#L621-L623

Added lines #L621 - L623 were not covered by tests
let a3_ := and(mload(add(a_, 0x20)), 0xffffffffffffffffffffffffffffffff)
let b3_ := and(mload(add(b_, 0x20)), 0xffffffffffffffffffffffffffffffff)

Loading