|
| 1 | +#include <math.h> |
| 2 | +#include <stdbool.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +typedef struct { |
| 7 | + const char *name; |
| 8 | + int canonicalTripleCount; |
| 9 | + int spoIndexTripleCount; |
| 10 | + int bloomBits; |
| 11 | + int hashFunctions; |
| 12 | + int negativeLookupsPerBatch; |
| 13 | + double fpRateBudget; |
| 14 | + double extraExactLookupsBudget; |
| 15 | + const char *exactTranscendentalSymbol; |
| 16 | + double certifiedLambda; |
| 17 | + double expMinusLambdaLower; |
| 18 | + double expMinusLambdaUpper; |
| 19 | + const char *maybePositivePolicy; |
| 20 | + const char *definiteNegativePolicy; |
| 21 | +} Artifact; |
| 22 | + |
| 23 | +typedef struct { |
| 24 | + bool parameterSanity; |
| 25 | + bool indexAgreement; |
| 26 | + double lambda; |
| 27 | + bool expIntervalCertificate; |
| 28 | + double fpRateLower; |
| 29 | + double fpRateUpper; |
| 30 | + bool withinFpRateBudget; |
| 31 | + double expectedExtraExactLookupsUpper; |
| 32 | + bool withinExactLookupBudget; |
| 33 | + bool accepted; |
| 34 | +} Evaluation; |
| 35 | + |
| 36 | +static bool same_policy(const char *lhs, const char *rhs) { |
| 37 | + return strcmp(lhs, rhs) == 0; |
| 38 | +} |
| 39 | + |
| 40 | +static Evaluation evaluate_artifact(const Artifact *a) { |
| 41 | + Evaluation ev; |
| 42 | + memset(&ev, 0, sizeof(ev)); |
| 43 | + |
| 44 | + ev.parameterSanity = |
| 45 | + a->canonicalTripleCount > 0 && |
| 46 | + a->spoIndexTripleCount > 0 && |
| 47 | + a->bloomBits > 0 && |
| 48 | + a->hashFunctions > 0 && |
| 49 | + a->negativeLookupsPerBatch > 0 && |
| 50 | + a->fpRateBudget > 0.0 && |
| 51 | + a->extraExactLookupsBudget > 0.0; |
| 52 | + |
| 53 | + ev.indexAgreement = (a->canonicalTripleCount == a->spoIndexTripleCount); |
| 54 | + |
| 55 | + if (ev.parameterSanity) { |
| 56 | + ev.lambda = ((double)a->hashFunctions * (double)a->canonicalTripleCount) / |
| 57 | + (double)a->bloomBits; |
| 58 | + } |
| 59 | + |
| 60 | + ev.expIntervalCertificate = |
| 61 | + ev.lambda > 0.0 && |
| 62 | + a->certifiedLambda == ev.lambda && |
| 63 | + a->expMinusLambdaLower < a->expMinusLambdaUpper && |
| 64 | + a->expMinusLambdaLower > 0.0 && |
| 65 | + a->expMinusLambdaUpper < 1.0; |
| 66 | + |
| 67 | + if (ev.expIntervalCertificate) { |
| 68 | + const double oneMinusLo = 1.0 - a->expMinusLambdaUpper; |
| 69 | + const double oneMinusHi = 1.0 - a->expMinusLambdaLower; |
| 70 | + ev.fpRateLower = pow(oneMinusLo, a->hashFunctions); |
| 71 | + ev.fpRateUpper = pow(oneMinusHi, a->hashFunctions); |
| 72 | + } |
| 73 | + |
| 74 | + ev.withinFpRateBudget = |
| 75 | + ev.fpRateUpper > 0.0 && |
| 76 | + ev.fpRateUpper < a->fpRateBudget; |
| 77 | + |
| 78 | + ev.expectedExtraExactLookupsUpper = |
| 79 | + (double)a->negativeLookupsPerBatch * ev.fpRateUpper; |
| 80 | + |
| 81 | + ev.withinExactLookupBudget = |
| 82 | + ev.expectedExtraExactLookupsUpper > 0.0 && |
| 83 | + ev.expectedExtraExactLookupsUpper < a->extraExactLookupsBudget; |
| 84 | + |
| 85 | + ev.accepted = |
| 86 | + ev.parameterSanity && |
| 87 | + ev.indexAgreement && |
| 88 | + ev.expIntervalCertificate && |
| 89 | + ev.withinFpRateBudget && |
| 90 | + ev.withinExactLookupBudget && |
| 91 | + same_policy(a->maybePositivePolicy, ":ConfirmAgainstCanonicalGraph") && |
| 92 | + same_policy(a->definiteNegativePolicy, ":ReturnAbsent"); |
| 93 | + |
| 94 | + return ev; |
| 95 | +} |
| 96 | + |
| 97 | +int main(void) { |
| 98 | + const Artifact artifact = { |
| 99 | + .name = ":artifact", |
| 100 | + .canonicalTripleCount = 1200, |
| 101 | + .spoIndexTripleCount = 1200, |
| 102 | + .bloomBits = 16384, |
| 103 | + .hashFunctions = 7, |
| 104 | + .negativeLookupsPerBatch = 50000, |
| 105 | + .fpRateBudget = 0.002, |
| 106 | + .extraExactLookupsBudget = 100.0, |
| 107 | + .exactTranscendentalSymbol = "exp(-k*n/m)", |
| 108 | + .certifiedLambda = 0.5126953125, |
| 109 | + .expMinusLambdaLower = 0.5988792348, |
| 110 | + .expMinusLambdaUpper = 0.5988792349, |
| 111 | + .maybePositivePolicy = ":ConfirmAgainstCanonicalGraph", |
| 112 | + .definiteNegativePolicy = ":ReturnAbsent" |
| 113 | + }; |
| 114 | + |
| 115 | + const Evaluation ev = evaluate_artifact(&artifact); |
| 116 | + |
| 117 | + printf("@prefix : <http://example.org/high-trust-rdf#> .\n"); |
| 118 | + printf("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n"); |
| 119 | + |
| 120 | + if (ev.expIntervalCertificate) { |
| 121 | + printf(":result :expIntervalCertificate :CertifiedDecimalInterval .\n"); |
| 122 | + } |
| 123 | + |
| 124 | + if (ev.accepted) { |
| 125 | + printf(":result :summary (\"parameter-sanity\" true "); |
| 126 | + printf("\"index-agreement\" true "); |
| 127 | + printf("\"transcendental\" \"%s\" ", artifact.exactTranscendentalSymbol); |
| 128 | + printf("\"lambda\" \"%.10f\"^^xsd:decimal ", ev.lambda); |
| 129 | + printf("\"certified-lambda\" %.10f ", artifact.certifiedLambda); |
| 130 | + printf("\"exp-lower\" %.10f ", artifact.expMinusLambdaLower); |
| 131 | + printf("\"exp-upper\" %.10f ", artifact.expMinusLambdaUpper); |
| 132 | + printf("\"fp-lower\" \"%.19f\"^^xsd:decimal ", ev.fpRateLower); |
| 133 | + printf("\"fp-upper\" \"%.19f\"^^xsd:decimal ", ev.fpRateUpper); |
| 134 | + printf("\"expected-extra-exact-lookups-upper\" \"%.14f\"^^xsd:decimal ", |
| 135 | + ev.expectedExtraExactLookupsUpper); |
| 136 | + printf("\"decision\" :AcceptForHighTrustUse) .\n"); |
| 137 | + } |
| 138 | + |
| 139 | + if (ev.withinExactLookupBudget) { |
| 140 | + printf(":result :withinExactLookupBudget true .\n"); |
| 141 | + } |
| 142 | + |
| 143 | + if (ev.withinFpRateBudget) { |
| 144 | + printf(":result :withinFpRateBudget true .\n"); |
| 145 | + } |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
0 commit comments