-
Notifications
You must be signed in to change notification settings - Fork 29
Feat/u512 #128
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
Merged
Feat/u512 #128
Changes from 23 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
f3f6b79
init
dovgopoly 9d6d09c
rm comments
dovgopoly c8c57db
wip
dovgopoly ba239ee
wip
dovgopoly 3720814
fixed add & added test
dovgopoly a8a79a5
wip (passed ecdsa check) 17.8kk
dovgopoly c698a36
fix
dovgopoly 85a2c36
opt
dovgopoly de6382b
-500k
Arvolear 31a36eb
15.8kk 2p
dovgopoly 4e00dc7
15.3kk
dovgopoly 7facbab
rm shl 15.48kk
dovgopoly 32a9716
wip
dovgopoly 9efa52b
wip
dovgopoly 2c5dd2d
added test vectors, tests are failed
dovgopoly ce83623
fixed ecdsa512 impl 22.2kk
dovgopoly 8f7deb4
20.4kk
dovgopoly 007dc1c
added assert
dovgopoly e650ce3
small fixes
dovgopoly 85684f1
refactored
dovgopoly 57f0f8d
typo
dovgopoly 6db5141
added crazy optimization with bit skipping 20.1kk
dovgopoly b9462a7
13.86kk & typos
dovgopoly 288f0c1
remove opt 384 libs
mllwchrry b3cffd6
add tests for U512
mllwchrry 5df79f0
add natspec
mllwchrry 82a88c0
add operator overloading
mllwchrry 936c966
modify moddiv test
mllwchrry 5b188af
rm ops and fixed tests
dovgopoly cfc730b
added assign & call & bitwise ops
dovgopoly c1c7cd8
added modexpU256 & tested gas
dovgopoly 0aaa9fe
typo
dovgopoly 52672ed
small adjustments
dovgopoly d87784e
add U512 usage example and fix tests
mllwchrry bbca148
fix natspec
mllwchrry 4b65a75
fixed comment
dovgopoly bf130be
add toBytes to natspec
mllwchrry 66ba3e1
typos
dovgopoly d5a813c
typos
dovgopoly 0a427dc
typos
dovgopoly 8dd88d9
small adjustments
dovgopoly d053dd7
update readme
Arvolear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
import {call} from "../../../libs/bn/U512.sol"; | ||
import {uint512} from "../../../libs/bn/U512.sol"; | ||
import {U512} from "../../../libs/bn/U512.sol"; | ||
|
||
contract U512Mock { | ||
using U512 for *; | ||
|
||
function modadd( | ||
bytes memory aBytes_, | ||
bytes memory bBytes_, | ||
bytes memory mBytes_ | ||
) external view returns (bytes memory rBytes_) { | ||
call call_ = U512.initCall(); | ||
|
||
uint512 a_ = U512.fromBytes(aBytes_); | ||
uint512 b_ = U512.fromBytes(bBytes_); | ||
uint512 m_ = U512.fromBytes(mBytes_); | ||
|
||
return U512.modadd(call_, a_, b_, m_).toBytes(); | ||
} | ||
|
||
function modsub( | ||
bytes memory aBytes_, | ||
bytes memory bBytes_, | ||
bytes memory mBytes_ | ||
) external view returns (bytes memory rBytes_) { | ||
call call_ = U512.initCall(); | ||
|
||
uint512 a_ = U512.fromBytes(aBytes_); | ||
uint512 b_ = U512.fromBytes(bBytes_); | ||
uint512 m_ = U512.fromBytes(mBytes_); | ||
|
||
return U512.modsub(call_, a_, b_, m_).toBytes(); | ||
} | ||
|
||
function modmul( | ||
bytes memory aBytes_, | ||
bytes memory bBytes_, | ||
bytes memory mBytes_ | ||
) external view returns (bytes memory rBytes_) { | ||
call call_ = U512.initCall(); | ||
|
||
uint512 a_ = U512.fromBytes(aBytes_); | ||
uint512 b_ = U512.fromBytes(bBytes_); | ||
uint512 m_ = U512.fromBytes(mBytes_); | ||
|
||
return U512.modmul(call_, a_, b_, m_).toBytes(); | ||
} | ||
|
||
function modexp( | ||
bytes memory aBytes_, | ||
bytes memory bBytes_, | ||
bytes memory mBytes_ | ||
) external view returns (bytes memory rBytes_) { | ||
call call_ = U512.initCall(); | ||
|
||
uint512 a_ = U512.fromBytes(aBytes_); | ||
uint512 b_ = U512.fromBytes(bBytes_); | ||
uint512 m_ = U512.fromBytes(mBytes_); | ||
|
||
return U512.modexp(call_, a_, b_, m_).toBytes(); | ||
} | ||
|
||
function moddiv( | ||
bytes memory aBytes_, | ||
bytes memory bBytes_, | ||
bytes memory mBytes_ | ||
) external view returns (bytes memory rBytes_) { | ||
call call_ = U512.initCall(); | ||
|
||
uint512 a_ = U512.fromBytes(aBytes_); | ||
uint512 b_ = U512.fromBytes(bBytes_); | ||
uint512 m_ = U512.fromBytes(mBytes_); | ||
|
||
return U512.moddiv(call_, a_, b_, m_).toBytes(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
import {ECDSA512} from "../../../libs/crypto/ECDSA512.sol"; | ||
|
||
contract ECDSA512Mock { | ||
using ECDSA512 for *; | ||
|
||
ECDSA512.Parameters private _brainpoolP512r1CurveParams = | ||
ECDSA512.Parameters({ | ||
a: hex"7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca", | ||
b: hex"3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723", | ||
gx: hex"81aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d0098eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f822", | ||
gy: hex"7dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f8111b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad80892", | ||
p: hex"aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca703308717d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3", | ||
n: hex"aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069", | ||
lowSmax: hex"556ecedc6df4e2459fea735719e4fe03e59846d9d9e4e9076b31ce65381984382a9f2e20a654930ca0c3308cbfd608238ed8e9c0842eed6edac3cb414e548034" | ||
}); | ||
|
||
function verifyBrainpoolP512r1WithoutHashing( | ||
bytes calldata message_, | ||
bytes calldata signature_, | ||
bytes calldata pubKey_ | ||
) external view returns (bool) { | ||
return _brainpoolP512r1CurveParams.verify(abi.encodePacked(message_), signature_, pubKey_); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { ethers } from "hardhat"; | ||
import { expect } from "chai"; | ||
import { Reverter } from "@/test/helpers/reverter"; | ||
|
||
import { U512Mock } from "@ethers-v6"; | ||
|
||
describe("U512", () => { | ||
const reverter = new Reverter(); | ||
|
||
let u512: U512Mock; | ||
|
||
function randomU512(): string { | ||
return "0x" + ethers.toBigInt(ethers.randomBytes(64)).toString(16).padStart(128, "0"); | ||
} | ||
|
||
function toBytes(value: bigint): string { | ||
return "0x" + value.toString(16).padStart(128, "0"); | ||
} | ||
|
||
function modadd(a: string, b: string, m: string): string { | ||
return toBytes((ethers.toBigInt(a) + ethers.toBigInt(b)) % ethers.toBigInt(m)); | ||
} | ||
|
||
function modmul(a: string, b: string, m: string): string { | ||
return toBytes((ethers.toBigInt(a) * ethers.toBigInt(b)) % ethers.toBigInt(m)); | ||
} | ||
|
||
function modexp(a: string, b: string, m: string): string { | ||
return toBytes(ethers.toBigInt(a) ** ethers.toBigInt(b) % ethers.toBigInt(m)); | ||
} | ||
|
||
function modsub(a: string, b: string, m: string): string { | ||
const aBn = ethers.toBigInt(a); | ||
const bBn = ethers.toBigInt(b); | ||
const mBn = ethers.toBigInt(m); | ||
|
||
return toBytes((((aBn - bBn) % mBn) + mBn) % mBn); | ||
} | ||
|
||
before(async () => { | ||
const U512Mock = await ethers.getContractFactory("U512Mock"); | ||
|
||
u512 = await U512Mock.deploy(); | ||
|
||
await reverter.snapshot(); | ||
}); | ||
|
||
afterEach(reverter.revert); | ||
|
||
it("modadd test", async () => { | ||
for (let i = 0; i < 100; ++i) { | ||
const a = randomU512(); | ||
const b = randomU512(); | ||
const m = randomU512(); | ||
|
||
expect(await u512.modadd(a, b, m)).to.equal(modadd(a, b, m)); | ||
} | ||
}); | ||
|
||
it("modmul test", async () => { | ||
for (let i = 0; i < 100; ++i) { | ||
const a = randomU512(); | ||
const b = randomU512(); | ||
const m = randomU512(); | ||
|
||
expect(await u512.modmul(a, b, m)).to.equal(modmul(a, b, m)); | ||
} | ||
}); | ||
|
||
it("modsub test", async () => { | ||
for (let i = 0; i < 100; ++i) { | ||
const a = randomU512(); | ||
const b = randomU512(); | ||
const m = randomU512(); | ||
|
||
expect(await u512.modsub(a, b, m)).to.equal(modsub(a, b, m)); | ||
} | ||
}); | ||
|
||
it("modexp test", async () => { | ||
for (let i = 0; i < 100; ++i) { | ||
const a = randomU512(); | ||
const b = toBytes(100n); | ||
const m = randomU512(); | ||
|
||
expect(await u512.modexp(a, b, m)).to.equal(modexp(a, b, m)); | ||
} | ||
}); | ||
|
||
it("moddiv test", async () => { | ||
const a = toBytes(779149564533142355434093157610126726613246737199n); | ||
const b = toBytes(29118654464229156312755475164902924590603964377702716942232927993582928167089n); | ||
const m = toBytes(76884956397045344220809746629001649092737531784414529538755519063063536359079n); | ||
|
||
const expected = toBytes(30823410400962253491978005949535646087432096635784775122170630924100507445065n); | ||
|
||
expect(await u512.moddiv(a, b, m)).to.equal(expected); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ethers } from "hardhat"; | ||
import { expect } from "chai"; | ||
import { Reverter } from "@/test/helpers/reverter"; | ||
|
||
import { ECDSA512Mock } from "@ethers-v6"; | ||
|
||
describe("ECDSA512", () => { | ||
const reverter = new Reverter(); | ||
|
||
let ecdsa512: ECDSA512Mock; | ||
|
||
before(async () => { | ||
const ECDSA512Mock = await ethers.getContractFactory("ECDSA512Mock"); | ||
|
||
ecdsa512 = await ECDSA512Mock.deploy(); | ||
|
||
await reverter.snapshot(); | ||
}); | ||
|
||
afterEach(reverter.revert); | ||
|
||
describe.only("brainpoolP512r1", () => { | ||
const signature = | ||
"0x0bd2593447cc6c02caf99d60418dd42e9a194c910e6755ed0c7059acac656b04ccfe1e8348462ee43066823aee2fed7ca012e9890dfb69866d7ae88b6506f9c744b42304e693796618d090dbcb2a2551c3cb78534611e61fd9d1a5c0938b5b8ec6ed53d2d28999eabbd8e7792d167fcf582492403a6a0f7cc94c73a28fb76b71"; | ||
const pubKey = | ||
"0x67cea1bedf84cbdcba69a05bb2ce3a2d1c9d911d236c480929a16ad697b45a6ca127079fe8d7868671e28ef33bdf9319e2e51c84b190ac5c91b51baf0a980ba500a7e79006194b5378f65cbe625ef2c47c64e56040d873b995b5b1ebaa4a6ce971da164391ff619af3bcfc71c5e1ad27ee0e859c2943e2de8ef7c43d3c976e9b"; | ||
const message = | ||
"0x43f800fbeaf9238c58af795bcdad04bc49cd850c394d3382953356b023210281757b30e19218a37cbd612086fbc158caa8b4e1acb2ec00837e5d941f342fb3cc"; | ||
|
||
it("should verify the signature", async () => { | ||
expect(await ecdsa512.verifyBrainpoolP512r1WithoutHashing(message, signature, pubKey)).to.be.true; | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.