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

refactor deriver #7

Merged
merged 4 commits into from
May 23, 2024
Merged
Changes from all commits
Commits
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
24 changes: 12 additions & 12 deletions src/Deriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {EllipticCurve} from "../lib/elliptic-curve-solidity/contracts/EllipticCu
import {Bech32m} from "./Bech32m.sol";

library Deriver {

// BEGIN SECP256k1 CONSTANTS
uint256 public constant GX =
0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798;
Expand All @@ -15,54 +16,52 @@ library Deriver {
uint256 public constant BB = 7;
uint256 public constant PP =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;

// END SECP256k1 CONSTANTS

// https://ethereum.stackexchange.com/questions/884/how-to-convert-an-address-to-bytes-in-solidity
function toBytes(address a) public pure returns (bytes memory) {
return abi.encodePacked(a);
}

// TODO(mkl): use tagged hashes
function getCoefficient(
uint256 x1,
uint256 y1,
address a
) public pure returns (uint256) {
) internal pure returns (uint256) {
uint256 c = uint256(sha256(abi.encode(x1, y1, a)));
return c;
}

// pubkey add operation
function addPubkeys(
uint256 x1,
uint256 y1,
uint256 x2,
uint256 y2
) public pure returns (uint256, uint256) {
) internal pure returns (uint256, uint256) {
return EllipticCurve.ecAdd(x1, y1, x2, y2, AA, PP);
}

// pubkey multiplication by scalar operation
function mulPubkey(
uint256 x,
uint256 y,
uint256 scalar
) public pure returns (uint256, uint256) {
) internal pure returns (uint256, uint256) {
return EllipticCurve.ecMul(scalar, x, y, AA, PP);
}

// linear combination of two pubkeys
function getCombinedPubkey(
uint256 p1x,
uint256 p1y,
uint256 p2x,
uint256 p2y,
uint256 c1,
uint256 c2
) public pure returns (uint256, uint256) {
) internal pure returns (uint256, uint256) {
(uint256 x1, uint256 y1) = mulPubkey(p1x, p1y, c1);
(uint256 x2, uint256 y2) = mulPubkey(p2x, p2y, c2);
return addPubkeys(x1, y1, x2, y2);
}

// derive pubkey from Validators' pubkeys and user's Ethereum address
function getPubkeyFromAddress(
uint256 p1x,
uint256 p1y,
Expand All @@ -75,6 +74,7 @@ library Deriver {
return getCombinedPubkey(p1x, p1y, p2x, p2y, c1, c2);
}

// derive Bitcoin address from user's Ethereum address and validators' pubkeys
function getBtcAddressFromEth(
uint256 p1x,
uint256 p1y,
Expand All @@ -93,8 +93,8 @@ library Deriver {
return string(Bech32m.encodeSegwitAddress(hrp, 1, abi.encodePacked(x)));
}

// calculate y coordinate from x coordinate
function liftX(uint256 x) public pure returns (uint256) {
uint256 y = EllipticCurve.deriveY(0x02, x, AA, BB, PP);
return y;
return EllipticCurve.deriveY(0x02, x, AA, BB, PP);
}
}