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

Finished public and core API #1

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
5e5c79d
completed
BaldyAsh Jul 31, 2019
c943910
fix
BaldyAsh Jul 31, 2019
74963cc
renaming, api separation
BaldyAsh Jul 31, 2019
06e0e72
test fixes
BaldyAsh Jul 31, 2019
2e37c12
small test fix
BaldyAsh Jul 31, 2019
3613d93
input bytes array now have fixed size
BaldyAsh Jul 31, 2019
8b28c66
separated lengths verification
BaldyAsh Aug 1, 2019
a68604f
tested creating op_data
BaldyAsh Aug 1, 2019
c43a7ea
fixed a lot
BaldyAsh Aug 1, 2019
c177139
added g2 and pairing tests
BaldyAsh Aug 1, 2019
138de9b
fixed
BaldyAsh Aug 1, 2019
9a5a83d
added test for abi
BaldyAsh Aug 2, 2019
afaf533
added bn254 curve
BaldyAsh Aug 2, 2019
e9c0db8
replaced tests
BaldyAsh Aug 2, 2019
b36ba5a
fix
BaldyAsh Aug 2, 2019
04274c8
commented test for a while
BaldyAsh Aug 2, 2019
16dea36
fixed tests
BaldyAsh Aug 6, 2019
1fa6f8c
fix
BaldyAsh Aug 6, 2019
efc0704
fix
BaldyAsh Aug 7, 2019
f88d5dc
added tests for raw call precomp
BaldyAsh Aug 7, 2019
a21fcfc
fix
BaldyAsh Aug 7, 2019
cc8fbf8
some fixes
BaldyAsh Aug 7, 2019
3a0fb87
fix calling
shamatar Aug 7, 2019
41cea94
update for multiplication test example
shamatar Aug 7, 2019
792bcb2
more tests
BaldyAsh Aug 8, 2019
dffef91
raw tested mul g1&g2 and pairing for bls12-384
BaldyAsh Aug 8, 2019
90379db
tested g1 add raw
BaldyAsh Aug 8, 2019
1cdae9a
g2 add test passes
BaldyAsh Aug 8, 2019
b3c592c
added g1 multiexp raw test
BaldyAsh Aug 8, 2019
b49669d
completed raw tests
BaldyAsh Aug 8, 2019
83ced43
modified callEip1962
BaldyAsh Aug 8, 2019
0b2236c
added caller tests, pairing fails
BaldyAsh Aug 8, 2019
2d97124
separated a and b params for g1 and g2 operations due to different le…
BaldyAsh Aug 8, 2019
c2b7b5b
added check for bls12 type in pairings
BaldyAsh Aug 8, 2019
317a253
fixed pairing call
BaldyAsh Aug 11, 2019
11849f4
added g1add test for deployed bls curve
BaldyAsh Aug 11, 2019
6c7e152
added deploy and test with truffle on geth
BaldyAsh Aug 13, 2019
e65b337
added testing in readme
BaldyAsh Aug 13, 2019
6961cff
fixed readme
BaldyAsh Aug 13, 2019
8464058
Update README.md
BaldyAsh Aug 14, 2019
00a202b
Update README.md
BaldyAsh Aug 22, 2019
09b06fe
Update README.md
BaldyAsh Aug 22, 2019
c80e357
test contracts separation
BaldyAsh Oct 20, 2019
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
2 changes: 2 additions & 0 deletions .dev.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JSON_RPC_URL=http://localhost:8545
WALLET_PK=0x4d5db4107d237df6a3d58ee5f70ae63d73d7658d4026f2eefd2f204c81682cb7
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.vscode
build/*
.env
.DS_Store
182 changes: 182 additions & 0 deletions README.md

Large diffs are not rendered by default.

93 changes: 93 additions & 0 deletions contracts/Bytes.sol
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;
}

}
37 changes: 37 additions & 0 deletions contracts/CommonTypes.sol
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
}
}
166 changes: 166 additions & 0 deletions contracts/EllipticCurve.sol
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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

там bytes, я честно говоря даже не знаю как они сохраняются. надо померять газ.


// 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
);
}

}
Loading