Skip to content

Commit

Permalink
Merge pull request #13 from abstraction-hq/12-test-passkey
Browse files Browse the repository at this point in the history
test passkey
  • Loading branch information
imduchuyyy authored Jun 21, 2024
2 parents ab589e1 + a85abaf commit 94db221
Show file tree
Hide file tree
Showing 10 changed files with 602 additions and 246 deletions.
12 changes: 3 additions & 9 deletions src/libraries/Base64.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ library Base64 {
mstore(afterPtr, 0x00)

// Run over the input, 3 bytes at a time
for {

} lt(dataPtr, endPtr) {

} {
for {} lt(dataPtr, endPtr) {} {
// Advance 3 bytes
dataPtr := add(dataPtr, 3)
let input := mload(dataPtr)
Expand Down Expand Up @@ -109,12 +105,10 @@ library Base64 {
mstore8(sub(resultPtr, 1), 0x3d)
mstore8(sub(resultPtr, 2), 0x3d)
}
case 2 {
mstore8(sub(resultPtr, 1), 0x3d)
}
case 2 { mstore8(sub(resultPtr, 1), 0x3d) }
}
}

return result;
}
}
}
64 changes: 64 additions & 0 deletions src/libraries/Base64Url.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

/**
* @dev Encode (without '=' padding)
* @author evmbrahmin, adapted from hiromin's Base64URL libraries
*/
library Base64Url {
/**
* @dev Base64Url Encoding Table
*/
string internal constant ENCODING_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

function encode(bytes memory data) internal pure returns (string memory) {
if (data.length == 0) return "";

// Load the table into memory
string memory table = ENCODING_TABLE;

string memory result = new string(4 * ((data.length + 2) / 3));

// @solidity memory-safe-assembly
assembly {
let tablePtr := add(table, 1)
let resultPtr := add(result, 32)

for {
let dataPtr := data
let endPtr := add(data, mload(data))
} lt(dataPtr, endPtr) {} {
dataPtr := add(dataPtr, 3)
let input := mload(dataPtr)

mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
resultPtr := add(resultPtr, 1)

mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
resultPtr := add(resultPtr, 1)

mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
resultPtr := add(resultPtr, 1)

mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
resultPtr := add(resultPtr, 1)
}

// Remove the padding adjustment logic
switch mod(mload(data), 3)
case 1 {
// Adjust for the last byte of data
resultPtr := sub(resultPtr, 2)
}
case 2 {
// Adjust for the last two bytes of data
resultPtr := sub(resultPtr, 1)
}

// Set the correct length of the result string
mstore(result, sub(resultPtr, add(result, 32)))
}

return result;
}
}
47 changes: 21 additions & 26 deletions src/libraries/FCL_ecdsa.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,25 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;


import {FCL_Elliptic_ZZ} from "./FCL_elliptic.sol";



library FCL_ecdsa {
// Set parameters for curve sec256r1.public
//curve order (number of points)
//curve order (number of points)
uint256 constant n = FCL_Elliptic_ZZ.n;

/**
* @dev ECDSA verification, given , signature, and public key.
*/

/**
* @dev ECDSA verification, given , signature, and public key, no calldata version
*/
function ecdsa_verify(bytes32 message, uint256 r, uint256 s, uint256 Qx, uint256 Qy) internal view returns (bool){

function ecdsa_verify(bytes32 message, uint256 r, uint256 s, uint256 Qx, uint256 Qy) internal view returns (bool) {
if (r == 0 || r >= FCL_Elliptic_ZZ.n || s == 0 || s >= FCL_Elliptic_ZZ.n) {
return false;
}

if (!FCL_Elliptic_ZZ.ecAff_isOnCurve(Qx, Qy)) {
return false;
}
Expand All @@ -55,33 +51,32 @@ library FCL_ecdsa {

x1 = FCL_Elliptic_ZZ.ecZZ_mulmuladd_S_asm(Qx, Qy, scalar_u, scalar_v);

x1= addmod(x1, n-r,n );
x1 = addmod(x1, n - r, n);

return x1 == 0;
}

function ec_recover_r1(uint256 h, uint256 v, uint256 r, uint256 s) internal view returns (address)
{
if (r == 0 || r >= FCL_Elliptic_ZZ.n || s == 0 || s >= FCL_Elliptic_ZZ.n) {
function ec_recover_r1(uint256 h, uint256 v, uint256 r, uint256 s) internal view returns (address) {
if (r == 0 || r >= FCL_Elliptic_ZZ.n || s == 0 || s >= FCL_Elliptic_ZZ.n) {
return address(0);
}
uint256 y=FCL_Elliptic_ZZ.ec_Decompress(r, v-27);
uint256 rinv=FCL_Elliptic_ZZ.FCL_nModInv(r);
uint256 u1=mulmod(FCL_Elliptic_ZZ.n-addmod(0,h,FCL_Elliptic_ZZ.n), rinv,FCL_Elliptic_ZZ.n);//-hr^-1
uint256 u2=mulmod(s, rinv,FCL_Elliptic_ZZ.n);//sr^-1
uint256 y = FCL_Elliptic_ZZ.ec_Decompress(r, v - 27);
uint256 rinv = FCL_Elliptic_ZZ.FCL_nModInv(r);
uint256 u1 = mulmod(FCL_Elliptic_ZZ.n - addmod(0, h, FCL_Elliptic_ZZ.n), rinv, FCL_Elliptic_ZZ.n); //-hr^-1
uint256 u2 = mulmod(s, rinv, FCL_Elliptic_ZZ.n); //sr^-1

uint256 Qx;
uint256 Qy;
(Qx,Qy)=FCL_Elliptic_ZZ.ecZZ_mulmuladd(r,y, u1, u2);
(Qx, Qy) = FCL_Elliptic_ZZ.ecZZ_mulmuladd(r, y, u1, u2);

return address(uint160(uint256(keccak256(abi.encodePacked(Qx, Qy)))));
}

function ecdsa_precomputed_verify(bytes32 message, uint256 r, uint256 s, address Shamir8)
internal view
internal
view
returns (bool)
{

if (r == 0 || r >= n || s == 0 || s >= n) {
return false;
}
Expand All @@ -97,13 +92,14 @@ library FCL_ecdsa {
//Shamir 8 dimensions
X = FCL_Elliptic_ZZ.ecZZ_mulmuladd_S8_extcode(mulmod(uint256(message), sInv, n), mulmod(r, sInv, n), Shamir8);

X= addmod(X, n-r,n );
X = addmod(X, n - r, n);

return X == 0;
} //end ecdsa_precomputed_verify()

function ecdsa_precomputed_verify(bytes32 message, uint256[2] calldata rs, address Shamir8)
internal view
function ecdsa_precomputed_verify(bytes32 message, uint256[2] calldata rs, address Shamir8)
internal
view
returns (bool)
{
uint256 r = rs[0];
Expand All @@ -123,9 +119,8 @@ library FCL_ecdsa {
//Shamir 8 dimensions
X = FCL_Elliptic_ZZ.ecZZ_mulmuladd_S8_extcode(mulmod(uint256(message), sInv, n), mulmod(r, sInv, n), Shamir8);

X= addmod(X, n-r,n );
X = addmod(X, n - r, n);

return X == 0;
} //end ecdsa_precomputed_verify()

}
}
Loading

0 comments on commit 94db221

Please sign in to comment.