-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rotation - Adds input encoding in Solidity and compatibility tests (#24)
* adds tests for encoding input. Failing to encode public keys * Fix clearing of bits * Fix Endieness of pubkeys * adds using spec for committee size and correctly handling compressed and uncompressed keys * add draft of encoding. Something weird is going on though * rotate input encoding tests passing * refactor endian conversions to own lib * fix endieness * ok fixed it for real * rename rotation input encoding function to toPublicInputs --------- Co-authored-by: ec2 <[email protected]>
- Loading branch information
1 parent
2f4bbc7
commit bfab59a
Showing
8 changed files
with
208 additions
and
37 deletions.
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,24 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
library EndianConversions { | ||
function toLittleEndian64(uint64 v) internal pure returns (bytes8) { | ||
v = ((v & 0xFF00FF00FF00FF00) >> 8) | ((v & 0x00FF00FF00FF00FF) << 8); | ||
v = ((v & 0xFFFF0000FFFF0000) >> 16) | ((v & 0x0000FFFF0000FFFF) << 16); | ||
v = ((v & 0xFFFFFFFF00000000) >> 32) | ((v & 0x00000000FFFFFFFF) << 32); | ||
return bytes8(v); | ||
} | ||
|
||
function toLittleEndian(uint256 v) internal pure returns (bytes32) { | ||
v = ((v & 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >> 8) | ||
| ((v & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8); | ||
v = ((v & 0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >> 16) | ||
| ((v & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16); | ||
v = ((v & 0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >> 32) | ||
| ((v & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32); | ||
v = ((v & 0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >> 64) | ||
| ((v & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64); | ||
v = (v >> 128) | (v << 128); | ||
return bytes32(v); | ||
} | ||
} |
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,38 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import { EndianConversions } from "./EndianConversions.sol"; | ||
|
||
library RotateLib { | ||
|
||
struct RotateInput { | ||
bytes32 syncCommitteeSSZ; | ||
bytes32 syncCommitteePoseidon; | ||
} | ||
|
||
/** | ||
* @notice Compute the public input commitment for the rotation | ||
* This must always match the method used in lightclient-circuits/src/committee_udate_circuit.rs - CommitteeUpdateCircuit::instance() | ||
* @param args The arguments for the sync step | ||
* @return The public input commitment that can be sent to the verifier contract. | ||
*/ | ||
function toPublicInputs(RotateInput memory args, bytes32 finalizedHeaderRoot) internal pure returns (uint256[65] memory) { | ||
uint256[65] memory inputs; | ||
|
||
inputs[0] = uint256(EndianConversions.toLittleEndian(uint256(args.syncCommitteePoseidon))); | ||
|
||
uint256 syncCommitteeSSZNumeric = uint256(args.syncCommitteeSSZ); | ||
for (uint256 i = 0; i < 32; i++) { | ||
inputs[32 - i] = syncCommitteeSSZNumeric % 2 ** 8; | ||
syncCommitteeSSZNumeric = syncCommitteeSSZNumeric / 2 ** 8; | ||
} | ||
|
||
uint256 finalizedHeaderRootNumeric = uint256(finalizedHeaderRoot); | ||
for (uint256 j = 0; j < 32; j++) { | ||
inputs[64 - j] = finalizedHeaderRootNumeric % 2 ** 8; | ||
finalizedHeaderRootNumeric = finalizedHeaderRootNumeric / 2 ** 8; | ||
} | ||
|
||
return inputs; | ||
} | ||
} |
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
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,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.19; | ||
|
||
import { RotateLib } from "../src/RotateLib.sol"; | ||
|
||
/** | ||
* @title RotateExternal | ||
* @dev This contract exists solely for the purpose of exposing the RotateLib functions | ||
* so they can be used in the Rust test suite. It should not be part of a production deployment | ||
*/ | ||
contract RotateExternal { | ||
using RotateLib for RotateLib.RotateInput; | ||
|
||
function toPublicInputs(RotateLib.RotateInput calldata args, bytes32 finalizedHeaderRoot) public pure returns (uint256[] memory) { | ||
uint256[65] memory commitment = args.toPublicInputs(finalizedHeaderRoot); | ||
// copy all elements into a dynamic array. We need to do this because ethers-rs has a bug that can't support uint256[65] return types | ||
uint256[] memory result = new uint256[](65); | ||
for (uint256 i = 0; i < commitment.length; i++) { | ||
result[i] = commitment[i]; | ||
} | ||
return result; | ||
} | ||
} |
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
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
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
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