Skip to content

Commit 59851c5

Browse files
committed
optimize tcbStatusToString() and add doc comments
Signed-off-by: Jun Kimura <[email protected]>
1 parent a579051 commit 59851c5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

contracts/DCAPValidator.sol

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ library DCAPValidator {
4545

4646
/**
4747
* @notice The output of the quote verification
48+
* @dev This struct corresponds to `QuoteVerificationOutput` in the dcap-quote-verifier library.
49+
* Note that some fields from the original output are omitted or parsed in greater detail in Solidity for our use case.
50+
* ref. https://github.com/datachainlab/zkdcap/blob/9616d7976a84e97a128fa02175ec994b95e3c137/crates/quote-verifier/src/verifier.rs#L19
4851
*/
4952
struct Output {
5053
string tcbStatus;
@@ -91,20 +94,23 @@ library DCAPValidator {
9194
* @return The string representation of the TCB status
9295
*/
9396
function tcbStatusToString(uint8 tcbStatus) internal pure returns (string memory) {
97+
// The if-else chain is ordered based on the expected frequency of allowed TCB statuses
98+
// (most common statuses first), rather than the order of the enum definition.
99+
// This ordering may result in minor gas savings by reducing the average number of comparisons in common cases.
94100
if (tcbStatus == TCB_STATUS_UP_TO_DATE) {
95101
return TCB_STATUS_UP_TO_DATE_STRING;
96-
} else if (tcbStatus == TCB_STATUS_OUT_OF_DATE) {
97-
return TCB_STATUS_OUT_OF_DATE_STRING;
98-
} else if (tcbStatus == TCB_STATUS_REVOKED) {
99-
return TCB_STATUS_REVOKED_STRING;
100-
} else if (tcbStatus == TCB_STATUS_CONFIGURATION_NEEDED) {
101-
return TCB_STATUS_CONFIGURATION_NEEDED_STRING;
102-
} else if (tcbStatus == TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED) {
103-
return TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED_STRING;
104102
} else if (tcbStatus == TCB_STATUS_SW_HARDENING_NEEDED) {
105103
return TCB_STATUS_SW_HARDENING_NEEDED_STRING;
104+
} else if (tcbStatus == TCB_STATUS_CONFIGURATION_NEEDED) {
105+
return TCB_STATUS_CONFIGURATION_NEEDED_STRING;
106106
} else if (tcbStatus == TCB_STATUS_CONFIGURATION_AND_SW_HARDENING_NEEDED) {
107107
return TCB_STATUS_CONFIGURATION_AND_SW_HARDENING_NEEDED_STRING;
108+
} else if (tcbStatus == TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED) {
109+
return TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED_STRING;
110+
} else if (tcbStatus == TCB_STATUS_OUT_OF_DATE) {
111+
return TCB_STATUS_OUT_OF_DATE_STRING;
112+
} else if (tcbStatus == TCB_STATUS_REVOKED) {
113+
return TCB_STATUS_REVOKED_STRING;
108114
} else {
109115
revert("unexpected TCB status");
110116
}

0 commit comments

Comments
 (0)