Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Reduce ressource count for a brittle method. #643

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions AwsEncryptionSDK/dafny/AwsEncryptionSdk/src/MessageBody.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -925,35 +925,45 @@ module MessageBody {
+ UInt64ToSeq(length)
}

function method WriteFramedMessageBody(
opaque function method WriteFramedMessageBody(
body: FramedMessage
)
:(ret: seq<uint8>)
//= compliance/data-format/message-body.txt#2.5.2.2
//= type=implication
//# The final frame
//# MUST be the last frame.
ensures ret == WriteMessageRegularFrames(body.regularFrames)
+ Frames.WriteFinalFrame(body.finalFrame)
//ensures ret == WriteMessageRegularFrames(body.regularFrames)
// + Frames.WriteFinalFrame(body.finalFrame)
{
WriteMessageRegularFrames(body.regularFrames) + Frames.WriteFinalFrame(body.finalFrame)
}
lemma AboutWriteFramedMessageBody(body: FramedMessage)
ensures WriteFramedMessageBody(body) == WriteMessageRegularFrames(body.regularFrames) + Frames.WriteFinalFrame(body.finalFrame)
{
reveal WriteFramedMessageBody();
}

function method WriteMessageRegularFrames(
opaque function method WriteMessageRegularFrames(
frames: MessageRegularFrames
)
:(ret: seq<uint8>)
ensures if |frames| == 0 then
ret == []
else
ret == WriteMessageRegularFrames(Seq.DropLast(frames))
+ Frames.WriteRegularFrame(Seq.Last(frames))
{
if |frames| == 0 then []
else
WriteMessageRegularFrames(Seq.DropLast(frames))
+ Frames.WriteRegularFrame(Seq.Last(frames))
}
// Unroll the function at most once
lemma AboutWriteMessageRegularFrames(frames: MessageRegularFrames)
ensures WriteMessageRegularFrames(frames) ==
if |frames| == 0 then []
else
WriteMessageRegularFrames(Seq.DropLast(frames))
+ Frames.WriteRegularFrame(Seq.Last(frames))
{
reveal WriteMessageRegularFrames();
}

function method {:recursive} {:vcs_split_on_every_assert} ReadFramedMessageBody(
buffer: ReadableBuffer,
Expand Down Expand Up @@ -1033,14 +1043,29 @@ module MessageBody {
//# following inputs:
assert CorrectlyRead(buffer, Success(SuccessfulRead(nextRegularFrames, regularFrame.tail)), WriteMessageRegularFrames) by {
reveal CorrectlyReadRange();
var tail := regularFrame.tail;
var f := WriteMessageRegularFrames;
var readRange := WriteMessageRegularFrames(nextRegularFrames);
assert readRange == f(nextRegularFrames);
AboutWriteMessageRegularFrames(nextRegularFrames);
assert && buffer.bytes == tail.bytes
&& buffer.start <= tail.start <= |buffer.bytes|
&& buffer.bytes[buffer.start..] == tail.bytes[buffer.start..]
&& readRange <= buffer.bytes[buffer.start..]
&& tail.start == buffer.start + |readRange|;
// Trivial evidence above but helps reduce brittleness by reducing the assertion below from 1.65MRU
// down to 550kRU
assert CorrectlyReadRange(buffer, regularFrame.tail, WriteMessageRegularFrames(nextRegularFrames));
}

ReadFramedMessageBody(
assert CorrectlyReadRange(buffer, regularFrame.tail, buffer.bytes[buffer.start..regularFrame.tail.start]);
var res := ReadFramedMessageBody(
buffer,
header,
nextRegularFrames,
regularFrame.tail
)
);
assert CorrectlyRead(buffer, res, WriteFramedMessageBody);
res
else
//= compliance/client-apis/decrypt.txt#2.7.4
//# If the first 4 bytes
Expand Down Expand Up @@ -1069,7 +1094,14 @@ module MessageBody {
assert {:split_here} true;
assert CorrectlyRead(continuation, Success(finalFrame), Frames.WriteFinalFrame);
assert {:split_here} true;
Success(SuccessfulRead(body, finalFrame.tail))
var res := Success(SuccessfulRead(body, finalFrame.tail));
assert CorrectlyRead(buffer, res, WriteFramedMessageBody) by {
reveal CorrectlyReadRange();
AboutWriteFramedMessageBody(body);
assert CorrectlyReadRange(buffer, res.value.tail, WriteFramedMessageBody(res.value.data));
assert CorrectlyReadRange(buffer, finalFrame.tail, WriteFramedMessageBody(body));
}
res
}

function WriteNonFramedMessageBody(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module Frames {

const SAFE_MAX_ENCRYPT := 0xFFFFFFFE0 // 2^36 - 32

function method WriteRegularFrame(
opaque function method WriteRegularFrame(
regularFrame: RegularFrame
)
:(ret: seq<uint8>)
Expand Down Expand Up @@ -191,14 +191,15 @@ module Frames {

assert CorrectlyReadRange(buffer, authTag.tail, WriteRegularFrame(regularFrame)) by {
CorrectlyReadByteRange(buffer, sequenceNumber.tail, WriteUint32(sequenceNumber.data));
reveal WriteRegularFrame();
AppendToCorrectlyReadByteRange(buffer, sequenceNumber.tail, iv.tail, Write(iv.data));
AppendToCorrectlyReadByteRange(buffer, iv.tail, encContent.tail, Write(encContent.data));
AppendToCorrectlyReadByteRange(buffer, encContent.tail, authTag.tail, Write(authTag.data));
}
Success(SuccessfulRead(regularFrame, authTag.tail))
}

function method WriteFinalFrame(
opaque function method WriteFinalFrame(
finalFrame: FinalFrame
)
:(ret: seq<uint8>)
Expand Down Expand Up @@ -276,6 +277,7 @@ module Frames {

assert CorrectlyReadRange(buffer, authTag.tail, WriteFinalFrame(finalFrame)) by {
reveal CorrectlyReadRange();
reveal WriteFinalFrame();
// It seems that Dafny can find a solution pretty fast.
// But I leave this here in case there is some problem later.
// CorrectlyReadByteRange(buffer, finalFrameSignal.tail, WriteUint32(finalFrameSignal.data));
Expand Down
Loading