Skip to content

Commit

Permalink
rotate input encoding tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
willemolding committed Oct 13, 2023
1 parent a9ed3a6 commit 135a961
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
36 changes: 25 additions & 11 deletions contracts/src/RotateLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ pragma solidity ^0.8.0;


library RotateLib {

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

struct RotateInput {
bytes32 syncCommitteeSSZ;
bytes32 syncCommitteePoseidon;
Expand All @@ -17,19 +31,19 @@ library RotateLib {
function toInputCommitment(RotateInput memory args, bytes32 finalizedHeaderRoot) internal pure returns (uint256[65] memory) {
uint256[65] memory inputs;

inputs[0] = uint256(args.syncCommitteePoseidon);
inputs[0] = uint256(toLittleEndian(uint256(args.syncCommitteePoseidon)));

// uint256 syncCommitteeSSZNumeric = uint256(args.syncCommitteeSSZ);
// for (uint256 i = 0; i < 32; i++) {
// inputs[32 - 1 - i] = syncCommitteeSSZNumeric % 2 ** 8;
// syncCommitteeSSZNumeric = syncCommitteeSSZNumeric / 2 ** 8;
// }
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;
// }
uint256 finalizedHeaderRootNumeric = uint256(finalizedHeaderRoot);
for (uint256 j = 0; j < 32; j++) {
inputs[64 - j] = finalizedHeaderRootNumeric % 2 ** 8;
finalizedHeaderRootNumeric = finalizedHeaderRootNumeric / 2 ** 8;
}

return inputs;
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/test/RotateExternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RotateLib } from "../src/RotateLib.sol";

/**
* @title SyncStepLibTest
* @dev This contract exists solely for the purpose of exposing the SyncStepLib functions
* @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 {
Expand All @@ -15,9 +15,9 @@ contract RotateExternal {
uint256[65] memory commitment = args.toInputCommitment(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];
// }
for (uint256 i = 0; i < commitment.length; i++) {
result[i] = commitment[i];
}
return result;
}
}
1 change: 1 addition & 0 deletions lightclient-circuits/tests/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ mod solidity_tests {
})
.collect();

assert_eq!(result_decoded.len(), instance[0].len());
assert_eq!(vec![result_decoded], instance);
Ok(())
}
Expand Down

0 comments on commit 135a961

Please sign in to comment.