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. #644

Open
wants to merge 4 commits into
base: mainline
Choose a base branch
from
Open
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
66 changes: 45 additions & 21 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 @@ -1034,22 +1044,29 @@ module MessageBody {
var res := Success(SuccessfulRead(nextRegularFrames, regularFrame.tail));
assert CorrectlyRead(buffer, res, WriteMessageRegularFrames) by {
reveal CorrectlyReadRange();

var tail := res.value.tail;
var readRange := WriteMessageRegularFrames(res.value.data);
assert buffer.bytes == tail.bytes;
assert buffer.start <= tail.start <= |buffer.bytes|;
assert buffer.bytes[buffer.start..] == tail.bytes[buffer.start..];
assert readRange <= buffer.bytes[buffer.start..];
assert tail.start == buffer.start + |readRange|;
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 @@ -1078,7 +1095,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