@inco/lightning 0.8.0-devnet-8 → 0.8.0-devnet-10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/manifest.yaml +44 -0
- package/package.json +1 -1
- package/src/CreateXHelper.sol +1 -1
- package/src/IncoLightning.sol +9 -18
- package/src/IncoVerifier.sol +1 -1
- package/src/Lib.alphanet.sol +261 -2
- package/src/Lib.demonet.sol +261 -2
- package/src/Lib.devnet.sol +262 -3
- package/src/Lib.sol +262 -3
- package/src/Lib.template.sol +283 -2
- package/src/Lib.testnet.sol +261 -2
- package/src/Types.sol +13 -0
- package/src/interfaces/IIncoLightning.sol +2 -12
- package/src/interfaces/automata-interfaces/BELE.sol +1 -1
- package/src/interfaces/automata-interfaces/IPCCSRouter.sol +1 -1
- package/src/interfaces/automata-interfaces/IPcsDao.sol +1 -1
- package/src/interfaces/automata-interfaces/IQuoteVerifier.sol +1 -1
- package/src/interfaces/automata-interfaces/Types.sol +1 -1
- package/src/libs/incoLightning_alphanet_v0_297966649.sol +261 -2
- package/src/libs/incoLightning_alphanet_v1_725458969.sol +261 -2
- package/src/libs/incoLightning_alphanet_v2_976644394.sol +261 -2
- package/src/libs/incoLightning_demonet_v0_863421733.sol +261 -2
- package/src/libs/incoLightning_demonet_v2_467437523.sol +261 -2
- package/src/libs/incoLightning_devnet_v0_340846814.sol +261 -2
- package/src/libs/incoLightning_devnet_v1_904635675.sol +261 -2
- package/src/libs/incoLightning_devnet_v2_295237520.sol +261 -2
- package/src/libs/incoLightning_devnet_v3_976859633.sol +261 -2
- package/src/libs/incoLightning_devnet_v4_409204766.sol +261 -2
- package/src/libs/incoLightning_devnet_v5_203964628.sol +1201 -0
- package/src/libs/incoLightning_devnet_v6_281949651.sol +1201 -0
- package/src/libs/incoLightning_testnet_v0_183408998.sol +261 -2
- package/src/libs/incoLightning_testnet_v2_889158349.sol +261 -2
- package/src/lightning-parts/AccessControl/AdvancedAccessControl.sol +4 -0
- package/src/lightning-parts/AccessControl/interfaces/IBaseAccessControlList.sol +2 -1
- package/src/lightning-parts/AccessControl/test/TestAdvancedAccessControl.t.sol +15 -0
- package/src/lightning-parts/DecryptionAttester.sol +38 -2
- package/src/lightning-parts/DecryptionAttester.types.sol +14 -0
- package/src/lightning-parts/EList.sol +323 -0
- package/src/lightning-parts/TEELifecycle.sol +1 -1
- package/src/lightning-parts/TEELifecycle.types.sol +1 -1
- package/src/lightning-parts/TrivialEncryption.sol +1 -2
- package/src/lightning-parts/interfaces/IDecryptionAttester.sol +7 -1
- package/src/lightning-parts/interfaces/IEList.sol +35 -0
- package/src/lightning-parts/interfaces/IEncryptedInput.sol +3 -1
- package/src/lightning-parts/interfaces/IEncryptedOperations.sol +3 -1
- package/src/lightning-parts/interfaces/ITEELifecycle.sol +1 -1
- package/src/lightning-parts/interfaces/ITrivialEncryption.sol +3 -1
- package/src/lightning-parts/primitives/EListHandleGeneration.sol +63 -0
- package/src/lightning-parts/primitives/EListHandleMetadata.sol +60 -0
- package/src/lightning-parts/primitives/interfaces/IEListHandleMetadata.sol +8 -0
- package/src/lightning-parts/test/Elist.t.sol +67 -0
- package/src/misc/ABIHelper.sol +15 -0
- package/src/pasted-dependencies/ICreateX.sol +1 -1
- package/src/periphery/SessionVerifier.sol +4 -4
- package/src/shared/IOwnable.sol +1 -1
- package/src/shared/IUUPSUpgradable.sol +1 -1
- package/src/shared/TestUtils.sol +8 -1
- package/src/test/EListTester.sol +148 -0
- package/src/test/TEELifecycle/TEELifecycleMockTest.t.sol +1 -1
- package/src/test/TestUpgrade.t.sol +1 -1
- package/src/version/IncoLightningConfig.sol +1 -1
package/src/Lib.sol
CHANGED
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
pragma solidity ^0.8;
|
|
7
7
|
|
|
8
8
|
import { IncoLightning } from "./IncoLightning.sol";
|
|
9
|
-
import { ebool, euint256, eaddress, ETypes } from "./Types.sol";
|
|
10
|
-
import { DecryptionAttestation } from "./lightning-parts/DecryptionAttester.types.sol";
|
|
9
|
+
import { ebool, euint256, eaddress, ETypes, elist, IndexOutOfRange, InvalidRange, SliceOutOfRange, UnsupportedListType } from "./Types.sol";
|
|
10
|
+
import { DecryptionAttestation, ElementDecryptionProof } from "./lightning-parts/DecryptionAttester.types.sol";
|
|
11
11
|
|
|
12
|
-
IncoLightning constant inco = IncoLightning(
|
|
12
|
+
IncoLightning constant inco = IncoLightning(0xDF3830489208461f72Df6E45D0e6cbF9DBB74fe1);
|
|
13
13
|
address constant deployedBy = 0x8202D2D747784Cb7D48868E44C42C4bf162a70BC;
|
|
14
14
|
|
|
15
15
|
/// @notice Returns the ETypes enum value encoded in a handle
|
|
@@ -939,4 +939,263 @@ library e {
|
|
|
939
939
|
}
|
|
940
940
|
return abi.decode(result, (bytes32));
|
|
941
941
|
}
|
|
942
|
+
|
|
943
|
+
/// @notice Returns the type of the elements contained within the list.
|
|
944
|
+
/// @param handle EList handle to read container type from.
|
|
945
|
+
/// @return ret Type of each element in the list.
|
|
946
|
+
function listTypeOf(elist handle) internal pure returns (ETypes ret) {
|
|
947
|
+
return ETypes(uint8(uint256(elist.unwrap(handle)) >> 16));
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/// @notice Creates a new empty list. Type must be specified ahead of time and can not be changed.
|
|
951
|
+
/// @param listType Type of each element in the list.
|
|
952
|
+
/// @return ret A new elist handle
|
|
953
|
+
function newEList(ETypes listType) internal returns (elist ret) {
|
|
954
|
+
return inco.newEList(new bytes32[](0), listType);
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
/// @notice Creates a new list from existing array of handles.
|
|
958
|
+
/// @param handles An array of handles to create a new list from
|
|
959
|
+
/// @param listType Type of each element in the list.
|
|
960
|
+
/// @return ret A new elist handle
|
|
961
|
+
function newEList(bytes32[] memory handles, ETypes listType) internal returns (elist ret) {
|
|
962
|
+
return inco.newEList(handles, listType);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/// @notice Takes an array of user encrypted ciphertexts and returns a new elist handle.
|
|
966
|
+
/// @param ciphertexts An array of encrypted user inputs
|
|
967
|
+
/// @param listType Expected type of each element in the list.
|
|
968
|
+
/// @param user Address of the user encrypting the ciphertexts.
|
|
969
|
+
/// @return ret A new elist handle
|
|
970
|
+
/// @dev each ciphertext costs the inco fee
|
|
971
|
+
function newEList(bytes[] memory ciphertexts, ETypes listType, address user) internal returns (elist ret) {
|
|
972
|
+
return inco.newEList{value: inco.getFee() * ciphertexts.length}(ciphertexts, listType, user);
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
/// @notice Appends an ebool element at the end of a list.
|
|
976
|
+
/// @param a An elist handle to append to.
|
|
977
|
+
/// @param b Element value to be appended.
|
|
978
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
979
|
+
function append(elist a, ebool b) internal returns (elist ret) {
|
|
980
|
+
return inco.listAppend(a, ebool.unwrap(s(b)));
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/// @notice Appends an euint256 element at the end of a list.
|
|
984
|
+
/// @param a An elist handle to append to.
|
|
985
|
+
/// @param b Element value to be appended.
|
|
986
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
987
|
+
function append(elist a, euint256 b) internal returns (elist ret) {
|
|
988
|
+
return inco.listAppend(a, euint256.unwrap(s(b)));
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
/// @notice Replaces an element at some hidden index "i" (ebool variant).
|
|
992
|
+
/// @param a EList handle to modify element in.
|
|
993
|
+
/// @param i Hidden index position of an element to modify.
|
|
994
|
+
/// @param b Element value to be changed.
|
|
995
|
+
/// @return ret A new elist handle
|
|
996
|
+
function set(elist a, euint256 i, ebool b) internal returns (elist ret) {
|
|
997
|
+
return inco.listSet(a, euint256.unwrap(s(i)), ebool.unwrap(s(b)));
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/// @notice Replaces an element at some hidden index "i" (euint256 variant).
|
|
1001
|
+
/// @param a EList handle to modify element in.
|
|
1002
|
+
/// @param i Hidden index position of an element to modify.
|
|
1003
|
+
/// @param b Element value to be changed.
|
|
1004
|
+
/// @return ret A new elist handle
|
|
1005
|
+
function set(elist a, euint256 i, euint256 b) internal returns (elist ret) {
|
|
1006
|
+
return inco.listSet(a, euint256.unwrap(s(i)), euint256.unwrap(s(b)));
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
/// @notice Replaces an element at plaintext index "i" (ebool variant). Reverts if out of range.
|
|
1010
|
+
/// @param a EList handle to modify element in.
|
|
1011
|
+
/// @param i Index position of element to modify.
|
|
1012
|
+
/// @param b Element value to be changed.
|
|
1013
|
+
/// @return ret A new elist handle
|
|
1014
|
+
function set(elist a, uint16 i, ebool b) internal returns (elist ret) {
|
|
1015
|
+
require(i < length(a), IndexOutOfRange(i, length(a)));
|
|
1016
|
+
return inco.listSet(a, euint256.unwrap(asEuint256(uint256(i))), ebool.unwrap(s(b)));
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/// @notice Replaces an element at plaintext index "i" (euint256 variant). Reverts if out of range.
|
|
1020
|
+
/// @param a EList handle to modify element in.
|
|
1021
|
+
/// @param i Index position of element to modify.
|
|
1022
|
+
/// @param b Element value to be changed.
|
|
1023
|
+
/// @return ret A new elist handle
|
|
1024
|
+
function set(elist a, uint16 i, euint256 b) internal returns (elist ret) {
|
|
1025
|
+
require(i < length(a), IndexOutOfRange(i, length(a)));
|
|
1026
|
+
return inco.listSet(a, euint256.unwrap(asEuint256(uint256(i))), euint256.unwrap(s(b)));
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
/// @notice Return hidden element at some hidden position (ebool variant).
|
|
1030
|
+
/// @param a EList handle to get element from.
|
|
1031
|
+
/// @param i Hidden index position to get element at.
|
|
1032
|
+
/// @param defaultValue A default element value returned if index is out of range.
|
|
1033
|
+
/// @return ret A new handle for the element or the defaultValue
|
|
1034
|
+
function getOr(elist a, euint256 i, ebool defaultValue) internal returns (ebool ret) {
|
|
1035
|
+
return ebool.wrap(inco.listGetOr(a, euint256.unwrap(s(i)), ebool.unwrap(s(defaultValue))));
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/// @notice Return hidden element at some hidden position (euint256 variant).
|
|
1039
|
+
/// @param a EList handle to get element from.
|
|
1040
|
+
/// @param i Hidden index position to get element at.
|
|
1041
|
+
/// @param defaultValue A default element value returned if index is out of range.
|
|
1042
|
+
/// @return ret A new handle for the element or the defaultValue
|
|
1043
|
+
function getOr(elist a, euint256 i, euint256 defaultValue) internal returns (euint256 ret) {
|
|
1044
|
+
return euint256.wrap(inco.listGetOr(a, euint256.unwrap(s(i)), euint256.unwrap(s(defaultValue))));
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
/// @notice Return hidden element at a known position as ebool.
|
|
1048
|
+
/// @param a EList handle to get element from.
|
|
1049
|
+
/// @param i Index position to get element at.
|
|
1050
|
+
/// @return ret Returns a handle for the element.
|
|
1051
|
+
function getEbool(elist a, uint16 i) internal returns (ebool ret) {
|
|
1052
|
+
return ebool.wrap(inco.listGet(a, i));
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
/// @notice Return hidden element at a known position as euint256.
|
|
1056
|
+
/// @param a EList handle to get element from.
|
|
1057
|
+
/// @param i Index position to get element at.
|
|
1058
|
+
/// @return ret Returns a handle for the element.
|
|
1059
|
+
function getEuint256(elist a, uint16 i) internal returns (euint256 ret) {
|
|
1060
|
+
return euint256.wrap(inco.listGet(a, i));
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
/// @notice Inserts a hidden element at a desired hidden position (ebool, hidden index).
|
|
1064
|
+
/// @param a An elist handle to insert into.
|
|
1065
|
+
/// @param i Hidden index position to insert at.
|
|
1066
|
+
/// @param b Element value to be inserted.
|
|
1067
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1068
|
+
function insert(elist a, euint256 i, ebool b) internal returns (elist ret) {
|
|
1069
|
+
return inco.listInsert(a, euint256.unwrap(s(i)), ebool.unwrap(s(b)));
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/// @notice Inserts an ebool element at a known position. Reverts if out of range.
|
|
1073
|
+
/// @param a An elist handle to insert into.
|
|
1074
|
+
/// @param i Index position to insert at.
|
|
1075
|
+
/// @param b Element value to be inserted.
|
|
1076
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1077
|
+
function insert(elist a, uint16 i, ebool b) internal returns (elist ret) {
|
|
1078
|
+
require(i < length(a), IndexOutOfRange(i, length(a)));
|
|
1079
|
+
return inco.listInsert(a, euint256.unwrap(asEuint256(uint256(i))), ebool.unwrap(s(b)));
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
/// @notice Inserts a hidden element at a desired hidden position (euint256, hidden index).
|
|
1083
|
+
/// @param a An elist handle to insert into.
|
|
1084
|
+
/// @param i Hidden index position to insert at.
|
|
1085
|
+
/// @param b Element value to be inserted.
|
|
1086
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1087
|
+
function insert(elist a, euint256 i, euint256 b) internal returns (elist ret) {
|
|
1088
|
+
return inco.listInsert(a, euint256.unwrap(s(i)), euint256.unwrap(s(b)));
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/// @notice Inserts a euint256 element at a known position. Reverts if out of range.
|
|
1092
|
+
/// @param a An elist handle to insert into.
|
|
1093
|
+
/// @param i Index position to insert at.
|
|
1094
|
+
/// @param b Element value to be inserted.
|
|
1095
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1096
|
+
function insert(elist a, uint16 i, euint256 b) internal returns (elist ret) {
|
|
1097
|
+
require(i < length(a), IndexOutOfRange(i, length(a)));
|
|
1098
|
+
return inco.listInsert(a, euint256.unwrap(asEuint256(uint256(i))), euint256.unwrap(s(b)));
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/// @notice Concatenates two elists into one.
|
|
1102
|
+
/// @param a EList handle to be prepended
|
|
1103
|
+
/// @param b EList handle to be appended
|
|
1104
|
+
/// @return ret A new elist handle with length len(a)+len(b).
|
|
1105
|
+
function concat(elist a, elist b) internal returns (elist ret) {
|
|
1106
|
+
return inco.listConcat(a, b);
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/// @notice Slices a list from plaintext start to plaintext end.
|
|
1110
|
+
/// @param a EList handle to be sliced
|
|
1111
|
+
/// @param start Starting index (inclusive).
|
|
1112
|
+
/// @param end End index (exclusive). Must be >= start and within bounds.
|
|
1113
|
+
/// @return ret A new sliced list with length "end-start".
|
|
1114
|
+
function slice(elist a, uint16 start, uint16 end) internal returns (elist ret) {
|
|
1115
|
+
require(end >= start, InvalidRange(start, end));
|
|
1116
|
+
require(end <= length(a), SliceOutOfRange(start, end, length(a)));
|
|
1117
|
+
require(start < length(a), SliceOutOfRange(start, end, length(a)));
|
|
1118
|
+
bytes32 defaultValue;
|
|
1119
|
+
if (ETypes.Uint256 == listTypeOf(a)) {
|
|
1120
|
+
defaultValue = euint256.unwrap(asEuint256(0));
|
|
1121
|
+
} else if (ETypes.Bool == listTypeOf(a)) {
|
|
1122
|
+
defaultValue = ebool.unwrap(asEbool(false));
|
|
1123
|
+
} else {
|
|
1124
|
+
revert UnsupportedListType(listTypeOf(a));
|
|
1125
|
+
}
|
|
1126
|
+
return inco.listSlice(a, euint256.unwrap(asEuint256(start)), end - start, defaultValue);
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
/// @notice Slices at a hidden index with a specified length (euint256 default).
|
|
1130
|
+
/// @param a EList handle to be sliced.
|
|
1131
|
+
/// @param start Hidden starting index.
|
|
1132
|
+
/// @param len Length of the desired slice.
|
|
1133
|
+
/// @param defaultValue Default value if out of range.
|
|
1134
|
+
/// @return ret A new sliced list with length "len".
|
|
1135
|
+
function sliceLen(elist a, euint256 start, uint16 len, euint256 defaultValue) internal returns (elist ret) {
|
|
1136
|
+
return inco.listSlice(a, euint256.unwrap(s(start)), len, euint256.unwrap(s(defaultValue)));
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/// @notice Slices at a hidden index with a specified length (ebool default).
|
|
1140
|
+
/// @param a EList handle to be sliced.
|
|
1141
|
+
/// @param start Hidden starting index.
|
|
1142
|
+
/// @param len Length of the desired slice.
|
|
1143
|
+
/// @param defaultValue Default value if out of range.
|
|
1144
|
+
/// @return ret A new sliced list with length "len".
|
|
1145
|
+
function sliceLen(elist a, euint256 start, uint16 len, ebool defaultValue) internal returns (elist ret) {
|
|
1146
|
+
return inco.listSlice(a, euint256.unwrap(s(start)), len, ebool.unwrap(s(defaultValue)));
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/// @notice Creates a new list populated with ordered values from within range.
|
|
1150
|
+
/// @param start Start value of the range, inclusive.
|
|
1151
|
+
/// @param end End of the range, exclusive. Must be >= start.
|
|
1152
|
+
/// @return ret A new elist handle with length "end-start"
|
|
1153
|
+
function range(uint16 start, uint16 end) internal returns (elist ret) {
|
|
1154
|
+
return inco.listRange(start, end);
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/// @notice Deterministically shuffles elements within a list.
|
|
1158
|
+
/// @dev costs the inco fee
|
|
1159
|
+
/// @param a elist handle to be shuffled
|
|
1160
|
+
/// @return ret A new elist handle with elements shuffled
|
|
1161
|
+
function shuffle(elist a) internal returns (elist ret) {
|
|
1162
|
+
bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.listShuffle.selector, a));
|
|
1163
|
+
return elist.wrap(result);
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
/// @notice A convenience function equivalent to range() followed by shuffle().
|
|
1167
|
+
/// @dev costs the inco fee
|
|
1168
|
+
/// @param start Start value of the range, inclusive.
|
|
1169
|
+
/// @param end End of the range, exclusive. Must be >= start.
|
|
1170
|
+
/// @return ret A new elist handle in random order with length "end-start".
|
|
1171
|
+
function shuffledRange(uint16 start, uint16 end) internal returns (elist ret) {
|
|
1172
|
+
elist rangeList = inco.listRange(start, end);
|
|
1173
|
+
bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.listShuffle.selector, rangeList));
|
|
1174
|
+
return elist.wrap(result);
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
/// @notice Reverses the order of elements in a list.
|
|
1178
|
+
/// @param a Elist handle to be reversed
|
|
1179
|
+
/// @return ret A new elist handle with elements in reverse order
|
|
1180
|
+
function reverse(elist a) internal returns (elist ret) {
|
|
1181
|
+
return inco.listReverse(a);
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
/// @notice Returns the length of the list in plaintext. Pure function, no gas cost.
|
|
1185
|
+
/// @param a EList handle to read the length from
|
|
1186
|
+
/// @return len The length of the list
|
|
1187
|
+
function length(elist a) internal pure returns (uint16 len) {
|
|
1188
|
+
return inco.lengthOf(elist.unwrap(a));
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
/// @notice verifyEListDecryption verifies elist decryption attestation and commitment proof. Each element can be either a pre-computed hash or a commitment-value pair.
|
|
1192
|
+
/// @param elistHandle The elist handle that was decrypted
|
|
1193
|
+
/// @param proofElements Array of proof elements. Each can provide either pairHash directly, or commitment+value to compute the pairHash
|
|
1194
|
+
/// @param proof The expected proof that should match keccak256 of all concatenated pair hashes
|
|
1195
|
+
/// @param signatures Array of signatures from attesters
|
|
1196
|
+
/// @return bool True if verification succeeds
|
|
1197
|
+
/// @dev For privacy, publish only pairHash. For transparency, publish commitment+value. Can mix both in the same array.
|
|
1198
|
+
function verifyEListDecryption(elist elistHandle, ElementDecryptionProof[] memory proofElements, bytes32 proof, bytes[] memory signatures) internal view returns (bool) {
|
|
1199
|
+
return inco.incoVerifier().isValidEListDecryptionAttestation(elist.unwrap(elistHandle), proofElements, proof, signatures);
|
|
1200
|
+
}
|
|
942
1201
|
}
|
package/src/Lib.template.sol
CHANGED
|
@@ -3,8 +3,18 @@
|
|
|
3
3
|
pragma solidity ^0.8;
|
|
4
4
|
|
|
5
5
|
import {IncoLightning} from "./IncoLightning.sol";
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
ebool,
|
|
8
|
+
euint256,
|
|
9
|
+
eaddress,
|
|
10
|
+
ETypes,
|
|
11
|
+
elist,
|
|
12
|
+
IndexOutOfRange,
|
|
13
|
+
InvalidRange,
|
|
14
|
+
SliceOutOfRange,
|
|
15
|
+
UnsupportedListType
|
|
16
|
+
} from "./Types.sol";
|
|
17
|
+
import {DecryptionAttestation, ElementDecryptionProof} from "./lightning-parts/DecryptionAttester.types.sol";
|
|
8
18
|
|
|
9
19
|
// forge-lint: disable-next-line(screaming-snake-case-const)
|
|
10
20
|
IncoLightning constant inco = IncoLightning(0x000000000000000000000000000000000000baBe);
|
|
@@ -945,4 +955,275 @@ library e {
|
|
|
945
955
|
return abi.decode(result, (bytes32));
|
|
946
956
|
}
|
|
947
957
|
|
|
958
|
+
// ═══════════════════════════════════════════════════════════
|
|
959
|
+
// EList helpers
|
|
960
|
+
// ═══════════════════════════════════════════════════════════
|
|
961
|
+
|
|
962
|
+
/// @notice Returns the type of the elements contained within the list.
|
|
963
|
+
/// @param handle EList handle to read container type from.
|
|
964
|
+
/// @return ret Type of each element in the list.
|
|
965
|
+
function listTypeOf(elist handle) internal pure returns (ETypes ret) {
|
|
966
|
+
return ETypes(uint8(uint256(elist.unwrap(handle)) >> 16));
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/// @notice Creates a new empty list. Type must be specified ahead of time and can not be changed.
|
|
970
|
+
/// @param listType Type of each element in the list.
|
|
971
|
+
/// @return ret A new elist handle
|
|
972
|
+
function newEList(ETypes listType) internal returns (elist ret) {
|
|
973
|
+
return inco.newEList(new bytes32[](0), listType);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
/// @notice Creates a new list from existing array of handles.
|
|
977
|
+
/// @param handles An array of handles to create a new list from
|
|
978
|
+
/// @param listType Type of each element in the list.
|
|
979
|
+
/// @return ret A new elist handle
|
|
980
|
+
function newEList(bytes32[] memory handles, ETypes listType) internal returns (elist ret) {
|
|
981
|
+
return inco.newEList(handles, listType);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/// @notice Takes an array of user encrypted ciphertexts and returns a new elist handle.
|
|
985
|
+
/// @param ciphertexts An array of encrypted user inputs
|
|
986
|
+
/// @param listType Expected type of each element in the list.
|
|
987
|
+
/// @param user Address of the user encrypting the ciphertexts.
|
|
988
|
+
/// @return ret A new elist handle
|
|
989
|
+
/// @dev each ciphertext costs the inco fee
|
|
990
|
+
function newEList(bytes[] memory ciphertexts, ETypes listType, address user) internal returns (elist ret) {
|
|
991
|
+
return inco.newEList{value: inco.getFee() * ciphertexts.length}(ciphertexts, listType, user);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/// @notice Appends an ebool element at the end of a list.
|
|
995
|
+
/// @param a An elist handle to append to.
|
|
996
|
+
/// @param b Element value to be appended.
|
|
997
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
998
|
+
function append(elist a, ebool b) internal returns (elist ret) {
|
|
999
|
+
return inco.listAppend(a, ebool.unwrap(s(b)));
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/// @notice Appends an euint256 element at the end of a list.
|
|
1003
|
+
/// @param a An elist handle to append to.
|
|
1004
|
+
/// @param b Element value to be appended.
|
|
1005
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1006
|
+
function append(elist a, euint256 b) internal returns (elist ret) {
|
|
1007
|
+
return inco.listAppend(a, euint256.unwrap(s(b)));
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/// @notice Replaces an element at some hidden index "i" (ebool variant).
|
|
1011
|
+
/// @param a EList handle to modify element in.
|
|
1012
|
+
/// @param i Hidden index position of an element to modify.
|
|
1013
|
+
/// @param b Element value to be changed.
|
|
1014
|
+
/// @return ret A new elist handle
|
|
1015
|
+
function set(elist a, euint256 i, ebool b) internal returns (elist ret) {
|
|
1016
|
+
return inco.listSet(a, euint256.unwrap(s(i)), ebool.unwrap(s(b)));
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/// @notice Replaces an element at some hidden index "i" (euint256 variant).
|
|
1020
|
+
/// @param a EList handle to modify element in.
|
|
1021
|
+
/// @param i Hidden index position of an element to modify.
|
|
1022
|
+
/// @param b Element value to be changed.
|
|
1023
|
+
/// @return ret A new elist handle
|
|
1024
|
+
function set(elist a, euint256 i, euint256 b) internal returns (elist ret) {
|
|
1025
|
+
return inco.listSet(a, euint256.unwrap(s(i)), euint256.unwrap(s(b)));
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
/// @notice Replaces an element at plaintext index "i" (ebool variant). Reverts if out of range.
|
|
1029
|
+
/// @param a EList handle to modify element in.
|
|
1030
|
+
/// @param i Index position of element to modify.
|
|
1031
|
+
/// @param b Element value to be changed.
|
|
1032
|
+
/// @return ret A new elist handle
|
|
1033
|
+
function set(elist a, uint16 i, ebool b) internal returns (elist ret) {
|
|
1034
|
+
require(i < length(a), IndexOutOfRange(i, length(a)));
|
|
1035
|
+
return inco.listSet(a, euint256.unwrap(asEuint256(uint256(i))), ebool.unwrap(s(b)));
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/// @notice Replaces an element at plaintext index "i" (euint256 variant). Reverts if out of range.
|
|
1039
|
+
/// @param a EList handle to modify element in.
|
|
1040
|
+
/// @param i Index position of element to modify.
|
|
1041
|
+
/// @param b Element value to be changed.
|
|
1042
|
+
/// @return ret A new elist handle
|
|
1043
|
+
function set(elist a, uint16 i, euint256 b) internal returns (elist ret) {
|
|
1044
|
+
require(i < length(a), IndexOutOfRange(i, length(a)));
|
|
1045
|
+
return inco.listSet(a, euint256.unwrap(asEuint256(uint256(i))), euint256.unwrap(s(b)));
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/// @notice Return hidden element at some hidden position (ebool variant).
|
|
1049
|
+
/// @param a EList handle to get element from.
|
|
1050
|
+
/// @param i Hidden index position to get element at.
|
|
1051
|
+
/// @param defaultValue A default element value returned if index is out of range.
|
|
1052
|
+
/// @return ret A new handle for the element or the defaultValue
|
|
1053
|
+
function getOr(elist a, euint256 i, ebool defaultValue) internal returns (ebool ret) {
|
|
1054
|
+
return ebool.wrap(inco.listGetOr(a, euint256.unwrap(s(i)), ebool.unwrap(s(defaultValue))));
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/// @notice Return hidden element at some hidden position (euint256 variant).
|
|
1058
|
+
/// @param a EList handle to get element from.
|
|
1059
|
+
/// @param i Hidden index position to get element at.
|
|
1060
|
+
/// @param defaultValue A default element value returned if index is out of range.
|
|
1061
|
+
/// @return ret A new handle for the element or the defaultValue
|
|
1062
|
+
function getOr(elist a, euint256 i, euint256 defaultValue) internal returns (euint256 ret) {
|
|
1063
|
+
return euint256.wrap(inco.listGetOr(a, euint256.unwrap(s(i)), euint256.unwrap(s(defaultValue))));
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
/// @notice Return hidden element at a known position as ebool.
|
|
1067
|
+
/// @param a EList handle to get element from.
|
|
1068
|
+
/// @param i Index position to get element at.
|
|
1069
|
+
/// @return ret Returns a handle for the element.
|
|
1070
|
+
function getEbool(elist a, uint16 i) internal returns (ebool ret) {
|
|
1071
|
+
return ebool.wrap(inco.listGet(a, i));
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/// @notice Return hidden element at a known position as euint256.
|
|
1075
|
+
/// @param a EList handle to get element from.
|
|
1076
|
+
/// @param i Index position to get element at.
|
|
1077
|
+
/// @return ret Returns a handle for the element.
|
|
1078
|
+
function getEuint256(elist a, uint16 i) internal returns (euint256 ret) {
|
|
1079
|
+
return euint256.wrap(inco.listGet(a, i));
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
/// @notice Inserts a hidden element at a desired hidden position (ebool, hidden index).
|
|
1083
|
+
/// @param a An elist handle to insert into.
|
|
1084
|
+
/// @param i Hidden index position to insert at.
|
|
1085
|
+
/// @param b Element value to be inserted.
|
|
1086
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1087
|
+
function insert(elist a, euint256 i, ebool b) internal returns (elist ret) {
|
|
1088
|
+
return inco.listInsert(a, euint256.unwrap(s(i)), ebool.unwrap(s(b)));
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/// @notice Inserts an ebool element at a known position. Reverts if out of range.
|
|
1092
|
+
/// @param a An elist handle to insert into.
|
|
1093
|
+
/// @param i Index position to insert at.
|
|
1094
|
+
/// @param b Element value to be inserted.
|
|
1095
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1096
|
+
function insert(elist a, uint16 i, ebool b) internal returns (elist ret) {
|
|
1097
|
+
require(i < length(a), IndexOutOfRange(i, length(a)));
|
|
1098
|
+
return inco.listInsert(a, euint256.unwrap(asEuint256(uint256(i))), ebool.unwrap(s(b)));
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/// @notice Inserts a hidden element at a desired hidden position (euint256, hidden index).
|
|
1102
|
+
/// @param a An elist handle to insert into.
|
|
1103
|
+
/// @param i Hidden index position to insert at.
|
|
1104
|
+
/// @param b Element value to be inserted.
|
|
1105
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1106
|
+
function insert(elist a, euint256 i, euint256 b) internal returns (elist ret) {
|
|
1107
|
+
return inco.listInsert(a, euint256.unwrap(s(i)), euint256.unwrap(s(b)));
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
/// @notice Inserts a euint256 element at a known position. Reverts if out of range.
|
|
1111
|
+
/// @param a An elist handle to insert into.
|
|
1112
|
+
/// @param i Index position to insert at.
|
|
1113
|
+
/// @param b Element value to be inserted.
|
|
1114
|
+
/// @return ret A new elist handle with length len(a)+1.
|
|
1115
|
+
function insert(elist a, uint16 i, euint256 b) internal returns (elist ret) {
|
|
1116
|
+
require(i < length(a), IndexOutOfRange(i, length(a)));
|
|
1117
|
+
return inco.listInsert(a, euint256.unwrap(asEuint256(uint256(i))), euint256.unwrap(s(b)));
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
/// @notice Concatenates two elists into one.
|
|
1121
|
+
/// @param a EList handle to be prepended
|
|
1122
|
+
/// @param b EList handle to be appended
|
|
1123
|
+
/// @return ret A new elist handle with length len(a)+len(b).
|
|
1124
|
+
function concat(elist a, elist b) internal returns (elist ret) {
|
|
1125
|
+
return inco.listConcat(a, b);
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/// @notice Slices a list from plaintext start to plaintext end.
|
|
1129
|
+
/// @param a EList handle to be sliced
|
|
1130
|
+
/// @param start Starting index (inclusive).
|
|
1131
|
+
/// @param end End index (exclusive). Must be >= start and within bounds.
|
|
1132
|
+
/// @return ret A new sliced list with length "end-start".
|
|
1133
|
+
function slice(elist a, uint16 start, uint16 end) internal returns (elist ret) {
|
|
1134
|
+
require(end >= start, InvalidRange(start, end));
|
|
1135
|
+
require(end <= length(a), SliceOutOfRange(start, end, length(a)));
|
|
1136
|
+
require(start < length(a), SliceOutOfRange(start, end, length(a)));
|
|
1137
|
+
bytes32 defaultValue;
|
|
1138
|
+
if (ETypes.Uint256 == listTypeOf(a)) {
|
|
1139
|
+
defaultValue = euint256.unwrap(asEuint256(0));
|
|
1140
|
+
} else if (ETypes.Bool == listTypeOf(a)) {
|
|
1141
|
+
defaultValue = ebool.unwrap(asEbool(false));
|
|
1142
|
+
} else {
|
|
1143
|
+
revert UnsupportedListType(listTypeOf(a));
|
|
1144
|
+
}
|
|
1145
|
+
return inco.listSlice(a, euint256.unwrap(asEuint256(start)), end - start, defaultValue);
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/// @notice Slices at a hidden index with a specified length (euint256 default).
|
|
1149
|
+
/// @param a EList handle to be sliced.
|
|
1150
|
+
/// @param start Hidden starting index.
|
|
1151
|
+
/// @param len Length of the desired slice.
|
|
1152
|
+
/// @param defaultValue Default value if out of range.
|
|
1153
|
+
/// @return ret A new sliced list with length "len".
|
|
1154
|
+
function sliceLen(elist a, euint256 start, uint16 len, euint256 defaultValue) internal returns (elist ret) {
|
|
1155
|
+
return inco.listSlice(a, euint256.unwrap(s(start)), len, euint256.unwrap(s(defaultValue)));
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
/// @notice Slices at a hidden index with a specified length (ebool default).
|
|
1159
|
+
/// @param a EList handle to be sliced.
|
|
1160
|
+
/// @param start Hidden starting index.
|
|
1161
|
+
/// @param len Length of the desired slice.
|
|
1162
|
+
/// @param defaultValue Default value if out of range.
|
|
1163
|
+
/// @return ret A new sliced list with length "len".
|
|
1164
|
+
function sliceLen(elist a, euint256 start, uint16 len, ebool defaultValue) internal returns (elist ret) {
|
|
1165
|
+
return inco.listSlice(a, euint256.unwrap(s(start)), len, ebool.unwrap(s(defaultValue)));
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
/// @notice Creates a new list populated with ordered values from within range.
|
|
1169
|
+
/// @param start Start value of the range, inclusive.
|
|
1170
|
+
/// @param end End of the range, exclusive. Must be >= start.
|
|
1171
|
+
/// @return ret A new elist handle with length "end-start"
|
|
1172
|
+
function range(uint16 start, uint16 end) internal returns (elist ret) {
|
|
1173
|
+
return inco.listRange(start, end);
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
/// @notice Deterministically shuffles elements within a list.
|
|
1177
|
+
/// @dev costs the inco fee
|
|
1178
|
+
/// @param a elist handle to be shuffled
|
|
1179
|
+
/// @return ret A new elist handle with elements shuffled
|
|
1180
|
+
function shuffle(elist a) internal returns (elist ret) {
|
|
1181
|
+
bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.listShuffle.selector, a));
|
|
1182
|
+
return elist.wrap(result);
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/// @notice A convenience function equivalent to range() followed by shuffle().
|
|
1186
|
+
/// @dev costs the inco fee
|
|
1187
|
+
/// @param start Start value of the range, inclusive.
|
|
1188
|
+
/// @param end End of the range, exclusive. Must be >= start.
|
|
1189
|
+
/// @return ret A new elist handle in random order with length "end-start".
|
|
1190
|
+
function shuffledRange(uint16 start, uint16 end) internal returns (elist ret) {
|
|
1191
|
+
elist rangeList = inco.listRange(start, end);
|
|
1192
|
+
bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.listShuffle.selector, rangeList));
|
|
1193
|
+
return elist.wrap(result);
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
/// @notice Reverses the order of elements in a list.
|
|
1197
|
+
/// @param a Elist handle to be reversed
|
|
1198
|
+
/// @return ret A new elist handle with elements in reverse order
|
|
1199
|
+
function reverse(elist a) internal returns (elist ret) {
|
|
1200
|
+
return inco.listReverse(a);
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
/// @notice Returns the length of the list in plaintext. Pure function, no gas cost.
|
|
1204
|
+
/// @param a EList handle to read the length from
|
|
1205
|
+
/// @return len The length of the list
|
|
1206
|
+
function length(elist a) internal pure returns (uint16 len) {
|
|
1207
|
+
return inco.lengthOf(elist.unwrap(a));
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
/**
|
|
1211
|
+
* @notice verifyEListDecryption verifies elist decryption attestation and commitment proof. Each element can be either a pre-computed hash or a commitment-value pair.
|
|
1212
|
+
* @param elistHandle The elist handle that was decrypted
|
|
1213
|
+
* @param proofElements Array of proof elements. Each can provide either pairHash directly, or commitment+value to compute the pairHash
|
|
1214
|
+
* @param proof The expected proof that should match keccak256 of all concatenated pair hashes
|
|
1215
|
+
* @param signatures Array of signatures from attesters
|
|
1216
|
+
* @return bool True if verification succeeds
|
|
1217
|
+
* @dev For privacy, publish only pairHash. For transparency, publish commitment+value. Can mix both in the same array.
|
|
1218
|
+
*/
|
|
1219
|
+
function verifyEListDecryption(
|
|
1220
|
+
elist elistHandle,
|
|
1221
|
+
ElementDecryptionProof[] memory proofElements,
|
|
1222
|
+
bytes32 proof,
|
|
1223
|
+
bytes[] memory signatures
|
|
1224
|
+
) internal view returns (bool) {
|
|
1225
|
+
return inco.incoVerifier()
|
|
1226
|
+
.isValidEListDecryptionAttestation(elist.unwrap(elistHandle), proofElements, proof, signatures);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
948
1229
|
}
|