Skip to content

Commit 15af347

Browse files
committed
WIP that will not work b/c there is no MapWithResult for maps
1 parent f7e0513 commit 15af347

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

AwsCryptographicMaterialProviders/dafny/AwsCryptographyKeyStore/src/HierarchicalVersionUtils.dfy

+14-27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module {:options "/functionSyntax:4" } HierarchicalVersionUtils {
1919
const BKC_DIGEST_LENGTH: uint8 := 48
2020
type PlainTextTuple = s: seq<uint8> | |s| == 80 witness *
2121
type BKCDigestError = e: Types.Error | (e.KeyStoreException? ) witness *
22+
datatype Utf8KeyValue = Utf8KeyValue(key: UTF8.ValidUTF8Bytes, value: UTF8.ValidUTF8Bytes)
2223

2324
method ProvideCryptoClient(
2425
Crypto?: Option<AtomicPrimitives.AtomicPrimitivesClient> := None
@@ -156,31 +157,23 @@ module {:options "/functionSyntax:4" } HierarchicalVersionUtils {
156157
input: Types.EncryptionContextString
157158
): (output: Result<Types.EncryptionContext, string>)
158159
ensures output.Success? ==> |output.value| == |input| // Output map size equals input map size
159-
ensures output.Failure? ==> output.error == "Unable to encode string"
160160
{
161-
var encodedEncryptionContext
162-
:= set k <- input
163-
::
164-
(UTF8.Encode(k), UTF8.Encode(input[k]), k);
165-
166-
if (forall i <- encodedEncryptionContext
167-
::
168-
&& i.0.Success?
169-
&& i.1.Success?)
170-
then
171-
var resultMap := map i <- encodedEncryptionContext :: i.0.value := i.1.value;
172-
if |resultMap| == |input| then
173-
Success(resultMap)
174-
else
175-
Failure("Unable to encode string")
176-
else
177-
Failure("Unable to encode string")
161+
var encodedInputResult: seq<(Utf8KeyValue)> :- Seq.MapWithResult(
162+
// Dafny requires the type of the element being mapped over, or it feaks out.
163+
(strKey: string)
164+
=>
165+
var keyValueUtf8 :- Utf8EncodeKeyValue(strKey, input[strKey]);
166+
Success(keyValueUtf8),
167+
input.Keys
168+
);
169+
Success(map r | r in encodedInputResult :: r.key := r.value)
178170
}
179171

180-
function method Utf8EncodeKeyValue(
172+
173+
function Utf8EncodeKeyValue(
181174
strKey: string,
182175
strValue: string
183-
) : (res: Result<(UTF8.ValidUTF8Bytes, UTF8.ValidUTF8Bytes), Types.Error>)
176+
) : (res: Result<Utf8KeyValue, Types.Error>)
184177
ensures (UTF8.Encode(strKey).Success? && UTF8.Encode(strValue).Success?) <==> res.Success?
185178
{
186179
var key :- UTF8
@@ -196,13 +189,7 @@ module {:options "/functionSyntax:4" } HierarchicalVersionUtils {
196189
=>
197190
WrapStringToError("Could not UTF8 Encode: " + strValue + " due to: " + eStr ));
198191

199-
Success((key, value))
200-
}
201-
202-
function method WrapStringToError(e: string)
203-
:(ret: Types.Error)
204-
{
205-
Types.KeyStoreException( message := e )
192+
Success(Utf8KeyValue(key := key, value := value))
206193
}
207194

208195
// Helper function to decode encryption context from UTF8 bytes map to string map

0 commit comments

Comments
 (0)