-
Notifications
You must be signed in to change notification settings - Fork 2
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
Finished public and core API #1
Open
BaldyAsh
wants to merge
43
commits into
master
Choose a base branch
from
v0.9
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
5e5c79d
completed
BaldyAsh c943910
fix
BaldyAsh 74963cc
renaming, api separation
BaldyAsh 06e0e72
test fixes
BaldyAsh 2e37c12
small test fix
BaldyAsh 3613d93
input bytes array now have fixed size
BaldyAsh 8b28c66
separated lengths verification
BaldyAsh a68604f
tested creating op_data
BaldyAsh c43a7ea
fixed a lot
BaldyAsh c177139
added g2 and pairing tests
BaldyAsh 138de9b
fixed
BaldyAsh 9a5a83d
added test for abi
BaldyAsh afaf533
added bn254 curve
BaldyAsh e9c0db8
replaced tests
BaldyAsh b36ba5a
fix
BaldyAsh 04274c8
commented test for a while
BaldyAsh 16dea36
fixed tests
BaldyAsh 1fa6f8c
fix
BaldyAsh efc0704
fix
BaldyAsh f88d5dc
added tests for raw call precomp
BaldyAsh a21fcfc
fix
BaldyAsh cc8fbf8
some fixes
BaldyAsh 3a0fb87
fix calling
shamatar 41cea94
update for multiplication test example
shamatar 792bcb2
more tests
BaldyAsh dffef91
raw tested mul g1&g2 and pairing for bls12-384
BaldyAsh 90379db
tested g1 add raw
BaldyAsh 1cdae9a
g2 add test passes
BaldyAsh b3c592c
added g1 multiexp raw test
BaldyAsh b49669d
completed raw tests
BaldyAsh 83ced43
modified callEip1962
BaldyAsh 0b2236c
added caller tests, pairing fails
BaldyAsh 2d97124
separated a and b params for g1 and g2 operations due to different le…
BaldyAsh c2b7b5b
added check for bls12 type in pairings
BaldyAsh 317a253
fixed pairing call
BaldyAsh 11849f4
added g1add test for deployed bls curve
BaldyAsh 6c7e152
added deploy and test with truffle on geth
BaldyAsh e65b337
added testing in readme
BaldyAsh 6961cff
fixed readme
BaldyAsh 8464058
Update README.md
BaldyAsh 00a202b
Update README.md
BaldyAsh 09b06fe
Update README.md
BaldyAsh c80e357
test contracts separation
BaldyAsh 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
This file contains 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,2 @@ | ||
JSON_RPC_URL=http://localhost:8545 | ||
WALLET_PK=0x4d5db4107d237df6a3d58ee5f70ae63d73d7658d4026f2eefd2f204c81682cb7 |
This file contains 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,5 @@ | ||
node_modules | ||
.vscode | ||
build/* | ||
.env | ||
.DS_Store |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,93 @@ | ||
pragma solidity ^0.5.8; | ||
|
||
library Bytes { | ||
|
||
// Compies uint8 'self' into a new 'bytes memory'. | ||
// Returns the newly created 'bytes memory'. | ||
function toBytesFromUInt8(uint8 self) internal pure returns (bytes memory bts) { | ||
bts = new bytes(1); | ||
bts[0] = byte(self); | ||
} | ||
|
||
// Original source code: https://github.com/GNSPS/solidity-bytes-utils/blob/master/contracts/BytesLib.sol#L13 | ||
// Concatenate bytes arrays in memory | ||
// Returns the newly created 'bytes memory'. | ||
function concat( | ||
bytes memory _preBytes, | ||
bytes memory _postBytes | ||
) | ||
internal | ||
pure | ||
returns (bytes memory) | ||
{ | ||
bytes memory tempBytes; | ||
|
||
assembly { | ||
// Get a location of some free memory and store it in tempBytes as | ||
// Solidity does for memory variables. | ||
tempBytes := mload(0x40) | ||
|
||
// Store the length of the first bytes array at the beginning of | ||
// the memory for tempBytes. | ||
let length := mload(_preBytes) | ||
mstore(tempBytes, length) | ||
|
||
// Maintain a memory counter for the current write location in the | ||
// temp bytes array by adding the 32 bytes for the array length to | ||
// the starting location. | ||
let mc := add(tempBytes, 0x20) | ||
// Stop copying when the memory counter reaches the length of the | ||
// first bytes array. | ||
let end := add(mc, length) | ||
|
||
for { | ||
// Initialize a copy counter to the start of the _preBytes data, | ||
// 32 bytes into its memory. | ||
let cc := add(_preBytes, 0x20) | ||
} lt(mc, end) { | ||
// Increase both counters by 32 bytes each iteration. | ||
mc := add(mc, 0x20) | ||
cc := add(cc, 0x20) | ||
} { | ||
// Write the _preBytes data into the tempBytes memory 32 bytes | ||
// at a time. | ||
mstore(mc, mload(cc)) | ||
} | ||
|
||
// Add the length of _postBytes to the current length of tempBytes | ||
// and store it as the new length in the first 32 bytes of the | ||
// tempBytes memory. | ||
length := mload(_postBytes) | ||
mstore(tempBytes, add(length, mload(tempBytes))) | ||
|
||
// Move the memory counter back from a multiple of 0x20 to the | ||
// actual end of the _preBytes data. | ||
mc := end | ||
// Stop copying when the memory counter reaches the new combined | ||
// length of the arrays. | ||
end := add(mc, length) | ||
|
||
for { | ||
let cc := add(_postBytes, 0x20) | ||
} lt(mc, end) { | ||
mc := add(mc, 0x20) | ||
cc := add(cc, 0x20) | ||
} { | ||
mstore(mc, mload(cc)) | ||
} | ||
|
||
// Update the free-memory pointer by padding our last write location | ||
// to 32 bytes: add 31 bytes to the end of tempBytes to move to the | ||
// next 32 byte block, then round down to the nearest multiple of | ||
// 32. If the sum of the length of the two arrays is zero then add | ||
// one before rounding down to leave a blank 32 bytes (the length block with 0). | ||
mstore(0x40, and( | ||
add(add(end, iszero(add(length, mload(_preBytes)))), 31), | ||
not(31) // Round down to the nearest 32 bytes. | ||
)) | ||
} | ||
|
||
return tempBytes; | ||
} | ||
|
||
} |
This file contains 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,37 @@ | ||
pragma solidity ^0.5.8; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import {Bytes} from "../contracts/Bytes.sol"; | ||
|
||
library CommonTypes { | ||
// Curve parameters struct | ||
struct CurveParams { | ||
uint8 curveType; | ||
uint8 fieldLength; | ||
bytes baseFieldModulus; | ||
uint8 extensionDegree; | ||
bytes aG1; | ||
bytes bG1; | ||
bytes aG2; | ||
bytes bG2; | ||
uint8 groupOrderLength; | ||
bytes groupOrder; | ||
bytes fpNonResidue; | ||
bytes fp2NonResidue; | ||
bytes fp6NonResidue; | ||
uint8 twistType; | ||
uint8 xLength; | ||
bytes x; | ||
uint8 sign; | ||
} | ||
|
||
// Enum describes possible curves. | ||
// 'Custom' is user defined curve. | ||
// 'Undefined' curve is undefined; | ||
enum PrebuildCurveTypes { | ||
BLS12_384_m, | ||
BLS12_384_d, | ||
BLS12_381_m, | ||
BLS12_381_d | ||
} | ||
} |
This file contains 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,166 @@ | ||
pragma solidity ^0.5.8; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import {GenericEllipticCurve} from "../contracts/GenericEllipticCurve.sol"; | ||
import {CommonTypes} from "../contracts/CommonTypes.sol"; | ||
|
||
contract EllipticCurve { | ||
|
||
// Current curve parameters | ||
CommonTypes.CurveParams private curveParams; | ||
|
||
// Contract creator | ||
address creator; | ||
|
||
// Constructor input is curve params. | ||
// If _curveType is Custom then curveType will be setted to Undefined. | ||
constructor(CommonTypes.CurveParams memory _curveParams) public { | ||
curveParams = _curveParams; | ||
creator = msg.sender; | ||
} | ||
|
||
// Only the elliptic curve contract creator can change curve params. | ||
function changeCurveParams(CommonTypes.CurveParams memory _curveParams) public { | ||
if (msg.sender != creator) return; | ||
|
||
curveParams = _curveParams; | ||
} | ||
|
||
// Get curve params | ||
function getCurveParams() public view returns (CommonTypes.CurveParams memory) { | ||
return curveParams; | ||
} | ||
|
||
// Compies the G1 Add operation result. | ||
// Params: | ||
// - lhs - first point's X and Y coordinates in G1Point struct representation | ||
// - rhs - second point's X and Y coordinates in G1Point struct representation | ||
// Returns the newly created bytes memory. | ||
function g1Add( | ||
bytes memory lhs, | ||
bytes memory rhs | ||
) public view returns (bytes memory result) { | ||
(bytes memory input, uint256 outputLength) = GenericEllipticCurve.formG1AddInput(curveParams, lhs, rhs); | ||
result = GenericEllipticCurve.callEip1962( | ||
9, | ||
input, | ||
input.length, | ||
outputLength | ||
); | ||
} | ||
|
||
// Compies the G1 Mul operation result. | ||
// Params: | ||
// - lhs - first point's X and Y coordinates in G1Point struct representation | ||
// - rhs - sсalar multiplication factor in bytes | ||
// Returns the newly created bytes memory. | ||
function g1Mul( | ||
bytes memory lhs, | ||
bytes memory rhs | ||
) public view returns (bytes memory result) { | ||
(bytes memory input, uint256 outputLength) = GenericEllipticCurve.formG1MulInput(curveParams, lhs, rhs); | ||
result = GenericEllipticCurve.callEip1962( | ||
9, | ||
input, | ||
input.length, | ||
outputLength | ||
); | ||
} | ||
|
||
// Compies the G1 Multiexponentiation operation result. | ||
// Params: | ||
// - numPairs - number of (point, scalar) pairs for multiexponentiation | ||
// - pointScalarPairs - (point, scalar) pairs for multiexponentiation | ||
// Returns the newly created bytes memory. | ||
function g1MultiExp( | ||
uint8 numPairs, | ||
bytes memory pointScalarPairs | ||
) public view returns (bytes memory result) { | ||
(bytes memory input, uint256 outputLength) = GenericEllipticCurve.formG1MultiExpInput(curveParams, numPairs, pointScalarPairs); | ||
result = GenericEllipticCurve.callEip1962( | ||
9, | ||
input, | ||
input.length, | ||
outputLength | ||
); | ||
} | ||
|
||
// Compies the G2 Add operation result. | ||
// Params: | ||
// - lhs - first point's X and Y coordinates in G2Point struct representation | ||
// - rhs - second point's X and Y coordinates in G2Point struct representation | ||
// Returns the newly created bytes memory. | ||
function g2Add( | ||
bytes memory lhs, | ||
bytes memory rhs | ||
) public view returns (bytes memory result) { | ||
(bytes memory input, uint256 outputLength) = GenericEllipticCurve.formG2AddInput(curveParams, lhs, rhs); | ||
result = GenericEllipticCurve.callEip1962( | ||
9, | ||
input, | ||
input.length, | ||
outputLength | ||
); | ||
} | ||
|
||
// Compies the G2 Mul operation result. | ||
// Params: | ||
// - lhs - first point's X and Y coordinates in G2Point struct representation | ||
// - rhs - sсalar multiplication factor in bytes | ||
// Returns the newly created bytes memory. | ||
function g2Mul( | ||
bytes memory lhs, | ||
bytes memory rhs | ||
) public view returns (bytes memory result) { | ||
(bytes memory input, uint256 outputLength) = GenericEllipticCurve.formG2MulInput(curveParams, lhs, rhs); | ||
result = GenericEllipticCurve.callEip1962( | ||
9, | ||
input, | ||
input.length, | ||
outputLength | ||
); | ||
} | ||
|
||
// Compies the G2 Multiexponentiation operation result. | ||
// Params: | ||
// - numPairs - number of (point, scalar) pairs for multiexponentiation | ||
// - pointScalarPairs - (point, scalar) pairs for multiexponentiation | ||
// Returns the newly created bytes memory. | ||
function g2MultiExp( | ||
uint8 numPairs, | ||
bytes memory pointScalarPairs | ||
) public view returns (bytes memory result) { | ||
(bytes memory input, uint256 outputLength) = GenericEllipticCurve.formG2MultiExpInput(curveParams, numPairs, pointScalarPairs); | ||
result = GenericEllipticCurve.callEip1962( | ||
9, | ||
input, | ||
input.length, | ||
outputLength | ||
); | ||
} | ||
|
||
// Verifies the correctness of the pairing operation parameters. | ||
// Params: | ||
// - pairs - point pairs array encoded as (G1 point, G2 point) in bytes | ||
// - numPairs - number of pairs as uint8 | ||
// Returns: | ||
// If result of a pairing (element of Fp12) is equal to identity - return single byte 0x01, otherwise return 0x00 following the existing ABI for BN254 precompile. | ||
function pairing( | ||
bytes memory pairs, | ||
uint8 numPairs | ||
) public view returns (bytes memory result) { | ||
bytes memory input; | ||
uint256 outputLength; | ||
// Currently pairing is available only for BLS12 curve family | ||
if (curveParams.curveType == 0x01) { | ||
(input, outputLength) = GenericEllipticCurve.formBLS12PairingInput(curveParams, pairs, numPairs); | ||
} | ||
result = GenericEllipticCurve.callEip1962( | ||
9, | ||
input, | ||
input.length, | ||
outputLength | ||
); | ||
} | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
там bytes, я честно говоря даже не знаю как они сохраняются. надо померять газ.