Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
8d13b41
initial commit
May 24, 2022
063c299
Update README.md
smithnormform May 24, 2022
2c80493
Update README.md
smithnormform May 25, 2022
78ab4fc
Update README.md
smithnormform May 26, 2022
c1804b0
test files
May 26, 2022
3cba3c9
Merge branch 'saw-python-api' of github.com:weaversa/cryptol-course i…
May 26, 2022
6629e84
Update README.md
smithnormform May 26, 2022
ec00731
Moving saw-python to labs/SAW - to make it a legit lab
weaversa May 27, 2022
c136eee
Added some setup text and general polish
weaversa May 27, 2022
7aa9ca6
Rename README.md to SAW.md
weaversa May 27, 2022
e3ee069
Filled in SAW call and responses for rcs example
weaversa May 27, 2022
37d7ccd
Removing compiled file
weaversa May 27, 2022
8bc7895
Added new directory to hold struct example
djmorel May 27, 2022
3d4ce15
Added Makefile and header file for struct example
djmorel May 27, 2022
5db3456
Update SAW.md
smithnormform May 27, 2022
3aa38ff
Update SAW.md
smithnormform May 27, 2022
754f138
Update SAW.md
smithnormform May 27, 2022
0f907bb
Modified SAW Game example file layout
djmorel May 27, 2022
6942eb4
Added SAW contract setup for simple Player function & modified Game c…
djmorel May 27, 2022
da5dabb
Added working SAW struct example for the Python API
djmorel May 27, 2022
f8f2b1f
Added f-string comment
weaversa May 29, 2022
1bab983
Added comment about Coq and inductive reasoning
weaversa May 29, 2022
4675926
Added comment about EUF
weaversa May 29, 2022
fabdf85
ispell ftw
weaversa May 29, 2022
2de82aa
Added each refinement of rotate to make lab more streamlined
weaversa May 29, 2022
824fefd
Adding formatting directives to code fences
weaversa May 29, 2022
fe2375b
Added new player function for SAW verification
djmorel May 29, 2022
969c145
Renamed SAW Player.* example files to Game.*
djmorel May 29, 2022
65d9b12
Added initial SAW pointer field example
djmorel May 31, 2022
b67b268
Updated SAW pointer field example with fixed size rework
djmorel May 31, 2022
3690e53
Added incorrect alternatives for struct postconditions
djmorel May 31, 2022
2ba24d9
Update SAW.md
smithnormform May 31, 2022
3967104
Added simple SAW struct lemma example
djmorel May 31, 2022
5e8c1d6
Update SAW.md
smithnormform May 31, 2022
0297ee2
Update SAW.md
smithnormform May 31, 2022
b24a561
Added Memory Disjoint support example
djmorel May 31, 2022
9db3213
Merge branch 'saw-python-api' of https://github.com/weaversa/cryptol-…
djmorel May 31, 2022
df31122
Update SAW.md
smithnormform May 31, 2022
50639a2
Update SAW.md
smithnormform May 31, 2022
d0a81c0
Update SAW.md
smithnormform May 31, 2022
4e8294b
Added error documentation for resetInventoryItems contract & added ch…
djmorel May 31, 2022
646bce6
Enabled debug symbols in generated bitcode to support SAW field name …
djmorel May 31, 2022
0a4480e
Updated SAW Game example TODO list
djmorel May 31, 2022
ce3928c
Moved initByteSequence logic to its own Cryptol function
djmorel Jun 1, 2022
7d4d771
Added global variable function examples
djmorel Jun 1, 2022
2df89ea
Replaced the initByteSequence function with repeat
djmorel Jun 1, 2022
3ef065e
Modified setScreenTile function to be an extern SAW example
djmorel Jun 1, 2022
3eb2ef2
Update SAW.md
smithnormform Jun 1, 2022
0746144
Update SAW.md
smithnormform Jun 1, 2022
1b37a63
Create GameOutline.md
djmorel Jun 1, 2022
7ec8a08
Update SAW.md
smithnormform Jun 1, 2022
4807039
Updated initDefaultPlayer_Contract's incorrect alternatives with repeat
djmorel Jun 1, 2022
2e8e90b
Updated initDefaultPlayer_Contract's incorrect alternatives
djmorel Jun 1, 2022
0195bcd
Update GameOutline.md
djmorel Jun 1, 2022
8e46ea4
Update GameOutline.md
djmorel Jun 1, 2022
03de51d
added module files
Jun 1, 2022
bd449eb
Update GameOutline.md
djmorel Jun 1, 2022
539a5c6
Update SAW.md
smithnormform Jun 1, 2022
e14a7ad
added files used in examples
Jun 1, 2022
a73f7e1
added files used in course
Jun 1, 2022
f3008bb
Merge branch 'saw-python-api' of github.com:weaversa/cryptol-course i…
Jun 1, 2022
7bcddc7
Update GameOutline.md
djmorel Jun 1, 2022
1c4ba76
Update GameOutline.md
djmorel Jun 1, 2022
50a2fc3
Update GameOutline.md
djmorel Jun 1, 2022
b37d86d
Update GameOutline.md
djmorel Jun 1, 2022
b0463d5
Update SAW.md
smithnormform Jun 1, 2022
33fff23
Updated initDefaultPlayer_Contract example
djmorel Jun 2, 2022
c2f9589
Updated initDefaultPlayer_Contract example
djmorel Jun 2, 2022
f4dc11a
Modified function placemen to make their lesson order flow better
djmorel Jun 2, 2022
dd9e297
Removing generated files and adding some to .gitignore
weaversa Jun 2, 2022
8d4efca
removing generated file
weaversa Jun 2, 2022
07fd329
Added initial documentation for the resolveAttack example
djmorel Jun 2, 2022
530857f
Added an introduction and polished the rotate bits
weaversa Jun 2, 2022
3c18194
Merge branch 'saw-python-api' of github.com:weaversa/cryptol-course i…
weaversa Jun 2, 2022
2ee0473
spelling fix
weaversa Jun 2, 2022
f9554fa
Updated resolveAttack SAW example documentation
djmorel Jun 2, 2022
b0620f8
Merge branch 'saw-python-api' of https://github.com/weaversa/cryptol-…
djmorel Jun 2, 2022
6476840
Included checkStats discussion to resolveAttack example
djmorel Jun 2, 2022
4546585
RCS -> rotl to mesh better with Salsa20
weaversa Jun 2, 2022
2beb914
Updated course workflow diagram
weaversa Jun 2, 2022
354224f
Updated course workflow diagram
weaversa Jun 2, 2022
2ce1b06
First take at breadcrumbs for SAW lab
weaversa Jun 2, 2022
9bb766c
fixing breadcrumbs
weaversa Jun 2, 2022
448eeb8
reworking solution for more clarity
weaversa Jun 2, 2022
c400598
in an a -> in a
weaversa Jun 2, 2022
4ed59b7
Added explicit SAW struct example
djmorel Jun 2, 2022
dd3503f
Simplified initDefaultPlayer_Contract
djmorel Jun 2, 2022
65ab328
Added initial explicit struct example discussion
djmorel Jun 2, 2022
0ad4d08
Updated explicit structs example
djmorel Jun 2, 2022
e80e1c3
Updated SAW struct initialization & explicit structs section
djmorel Jun 2, 2022
076c079
Update SAW.md
smithnormform Jun 2, 2022
1aacca2
Testing a new workflow for the SAW python tutorial
weaversa Jun 2, 2022
cc0469a
Merge branch 'saw-python-api' of github.com:weaversa/cryptol-course i…
weaversa Jun 2, 2022
82b7e42
Updated Cryptol struct discussion
djmorel Jun 2, 2022
e4afe55
Merge branch 'saw-python-api' of https://github.com/weaversa/cryptol-…
djmorel Jun 2, 2022
a0b9aa5
fixed ci
weaversa Jun 2, 2022
79d6eaf
Update SAW.md
smithnormform Jun 2, 2022
2e2ecf4
Modified sprite example to contain a pointer field
djmorel Jun 2, 2022
e1a7da7
Updated initDefaultSprite example based on added pointer field
djmorel Jun 2, 2022
1e908cd
Reordered the Cryptol tuple and record discussion
djmorel Jun 2, 2022
af31bcc
Updated SAW.md
djmorel Jun 2, 2022
e205aa1
Updated SAW.md
djmorel Jun 2, 2022
0f2a20b
Update SAW.md
djmorel Jun 2, 2022
6ac58ae
Merge branch 'master' into saw-python-api
weaversa Jun 2, 2022
4c44e4a
Added a full tuple method to represent struct pointer fields
djmorel Jun 2, 2022
f17dd78
Merge branch 'saw-python-api' of https://github.com/weaversa/cryptol-…
djmorel Jun 2, 2022
2fa641b
Updated SAW.md
djmorel Jun 2, 2022
8d71126
Merge branch 'master' into saw-python-api
weaversa Jun 2, 2022
ecb01e2
Update SAW.md
smithnormform Jun 2, 2022
0c18cf1
Copied original Game artifacts into a subfolder, DLC
djmorel Jun 2, 2022
34df979
Update SAW.md
smithnormform Jun 2, 2022
1f551e5
fix?
weaversa Jun 2, 2022
e2e5da8
Merge branch 'saw-python-api' of github.com:weaversa/cryptol-course i…
weaversa Jun 2, 2022
5e90e51
Simplified base Game example proof, specs, and src files
djmorel Jun 2, 2022
773cdaa
Added basic documentation for Game.h
djmorel Jun 2, 2022
a51e796
Split Game.py into a worksheet and answers sheet
djmorel Jun 2, 2022
556924f
Added steps for running the Game lab
djmorel Jun 2, 2022
aa6dfd0
Updated DLC's GameOutline.md
djmorel Jun 2, 2022
fc04e8a
Removed DLC README since GameDLC covers its info
djmorel Jun 3, 2022
0eddae8
Added ceil(log2()) case study
djmorel Jun 3, 2022
d4e63e9
Updated findings for lg2 example
djmorel Jun 3, 2022
75f01bc
Updated lg2 example
djmorel Jun 3, 2022
ea40eb3
Updated SAW.md based on comments
djmorel Jun 3, 2022
0218c9c
Updated SAW.md based on comments
djmorel Jun 3, 2022
2867137
Adding missing code fence formatting directives
weaversa Jun 3, 2022
75dd879
Updated initDefaultSprite_Contract for consistency
djmorel Jun 3, 2022
241c626
Merge branch 'saw-python-api' of https://github.com/weaversa/cryptol-…
djmorel Jun 3, 2022
515fe11
Added CLZ suggestion for lg2 example
djmorel Jun 3, 2022
6226ce5
Added a missing reference
djmorel Jun 3, 2022
ac90032
added prereqs
weaversa Jun 3, 2022
6d02521
Merge branch 'saw-python-api' of github.com:weaversa/cryptol-course i…
weaversa Jun 3, 2022
0c78876
Adding Game to ci
weaversa Jun 3, 2022
e90489f
ci fix
weaversa Jun 3, 2022
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
19 changes: 19 additions & 0 deletions .github/workflows/python-saw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,22 @@ jobs:
python3 labs/Demos/SAW/xxHash/xxhash32-ref.py
python3 labs/Demos/SAW/xxHash/xxhash64-ref.py
python3 labs/Demos/SAW/Salsa20/proof/salsa20.py

saw-tutorial:
runs-on: ubuntu-latest
container:
image: ghcr.io/weaversa/cryptol-course:2.12
options: --user root
steps:
- name: Checkout
uses: actions/checkout@v2
# Start saw-remote-api server
- run: start-saw-remote-api-read-only
# Run final rotl SAW Script
- run: cd labs/SAW/proof && clang ../src/rotl3.c -o ../src/rotl.bc -c -emit-llvm && python3 rotl.py
# Run addRow SAW Script
- run: cd labs/SAW/proof && clang ../src/addRow.c -o ../src/addRow.bc -c -emit-llvm && python3 addRow.py
# Run null SAW Script
- run: cd labs/SAW/proof && clang ../src/null.c -o ../src/null.bc -c -emit-llvm && python3 null.py
# Run Game SAW Script
- run: cd labs/SAW/Game/DLC && mkdir artifacts && clang -c -g -emit-llvm -o artifacts/Game.bc src/Game.c && python3 proof/Game.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/.DS_Store
**/*.bc
**/*~
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ first lab walks you through [installing and running Cryptol](INSTALL.md).
enjoyed the last lab, go ahead and try your hand at using
Cryptol's connection to automated provers (SMT solvers) to solve
some classic computational puzzles.
* [Continuous Reasoning with SAW](./labs/SAW/SAW.md): Learn how to
use Python to drive SAW and enforce formal invariants on
cryptographic implementations at every check-in to a repository.
8. [Methods for Key Wrapping](./labs/KeyWrapping/KeyWrapping.md):
Create a Cryptol specification of NIST's [SP800-38F key wrap
standard](https://csrc.nist.gov/publications/detail/sp/800-38f/final).
Expand Down
3 changes: 2 additions & 1 deletion labs/CryptoProofs/CryptoProofs.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ plaintext inputs.

**EXERCISE**: 2.5.2 DES Parity Bits

Having equivalent keys is often considered a weakness in an a
Having equivalent keys is often considered a weakness in a
cipher. However, in the case of DES, it turns out that this is a
result of a design choice. The lowest bit of each byte of a DES key is
actually a [parity bit](https://en.wikipedia.org/wiki/Parity_bit) that
Expand Down Expand Up @@ -383,3 +383,4 @@ https://github.com/weaversa/cryptol-course/issues
|| [+ Salsa20 Properties](../Salsa20/Salsa20Props.md) ||
|| [+ Transposition Ciphers](../Transposition/Contents.md) ||
|| [+ Project Euler](../ProjectEuler/ProjectEuler.md) ||
|| [+ Continuous Reasoning with SAW](../SAW/SAW.md) ||
9 changes: 5 additions & 4 deletions labs/CryptoProofs/CryptoProofsAnswers.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ matched_pt = join "tootough"
matched_ct = 0x95d07f8a72707733
```

To make this solvable, try it again with the first six bytes of key
provided: `0x1234567890ab`.

```Xcryptol-session-ci-none
labs::CryptoProofs::CryptoProofsAnswers> :sat \key -> DES.encrypt key matched_pt == matched_ct
```
Expand All @@ -242,6 +239,9 @@ DES keys have been broken using specialized algorithms
and large amounts of compute power, but not by a single computer
running a SAT solver.

To make this solvable, try it again with the first six bytes of key
provided: `0x1234567890ab`.

```Xcryptol-session
labs::CryptoProofs::CryptoProofsAnswers> :sat \key -> DES.encrypt key matched_pt == matched_ct /\ take key == 0x1234567890ab
Satisfiable
Expand Down Expand Up @@ -419,7 +419,7 @@ Q.E.D.

**EXERCISE**: 2.5.2 DES Parity Bits

Having equivalent keys is often considered a weakness in an a
Having equivalent keys is often considered a weakness in a
cipher. However, in the case of DES, it turns out that this is a
result of a design choice. The lowest bit of each byte of a DES key is
actually a [parity bit](https://en.wikipedia.org/wiki/Parity_bit) that
Expand Down Expand Up @@ -477,3 +477,4 @@ https://github.com/weaversa/cryptol-course/issues
|| [+ Salsa20 Properties](../Salsa20/Salsa20Props.md) ||
|| [+ Transposition Ciphers](../Transposition/Contents.md) ||
|| [+ Project Euler](../ProjectEuler/ProjectEuler.md) ||
|| [+ Continuous Reasoning with SAW](../SAW/SAW.md) ||
117 changes: 117 additions & 0 deletions labs/SAW/Game/DLC/GameDLC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Game DLC

This directory contains additional Game functions and SAW contracts for you to reference. You will notice:
- `Makefile`: Provides the necessary steps to generate our bitcode and run our SAW Python scripts.
- `src/`: Contains the source code we'll be analyzing
- `proof/`: Contains our Python scripts to run our SAW contracts.
- `specs/`: Contains our Cryptol specs that our SAW contracts can call.

# DLC Functions

Below is a list of functions included in Game/.

## levelUp(uint32_t level)
**Goal:** Determine how to set up and verify a very simple function in a SAW contract.

**Lessons Learned**
- How to declare a fresh variable in SAW
- How to call a Cryptol function
- How to pass SAW types to Cryptol function (curly braces)


## getDefaultLevel()
**Goal:** Determine how to handle global variables in a SAW contract.

**Lessons Learned:**
- How and when to use `global_initializer`
- When the source code already initializes the value and SAW should just use that value
- How to declare a global variable in SAW (`global_var`)
- How to set a global variable to an initialized value in a SAW contract (`points_to`)
- When to pass no inputs to `execute_func`


## initDefaultPlayer(player_t* player)
**Goal:** Determine how to setup a struct variable in a SAW contract.

**Lessons Learned:**
- How and when to use `alias_ty`
- How and when to use `alloc`
- Use `alloc` when you only consider the pointer
- Use `ptr_to_fresh` when you care about the values being referenced (i.e. for pre/postconditions) and the pointer
- SAW only recognizes the base struct name (`player_t` vs `character_t`)
- Passing global variables/parameters/defines defined in SAW to contracts
- Values copied from the source code's defines
- Examples: `MAX_NAME_LENGTH`, `SUCCESS`
- How to assert postconditions using `points_to`
- Compiling clang with the `-g` flag provides debug symbols, so you can reference fields names rather than just indices


## checkStats(character_t* character)
**Goal:** Determine how to use parameterized contracts to set preconditions and postconditions.

**Lessons Learned:**
- Understand that when a function's behavior depends on specific input value ranges, it may be necessary to define multiple cases for a given SAW contract
- Determine how to break a function into its fundamental case behaviors (i.e. `SUCCESS` and `FAILURE`)
- How to parameterize a SAW contract
- Understand which parameter type is best for the job (i.e. `int`, `bool`, `string`, etc)
- How to pass and use parameters within a SAW contract
- Note that SAW's Python API takes advantage of if-statements, which reduces the number of SAW contracts/specs that we need to write
- Understand that `ptr_to_fresh` is useful for setting preconditions for a struct's fields
- How to use contract parameters to assert two possible postconditions
- How to represent multiple Unit Tests on the same contract with different input parameters

**Additional Notes:**
- Note that the `checkStats` function would be used in the Game library to check character stats every instance before such stats would be referenced/used for gameplay.
- In terms of the example, `checkStats` provides gameplay balancing by limiting how well characters can perform.
- Given this policy, all other functions included in the library assume that `checkStats` is called before them.
- This means we will use the preconditions from `checkStats_Contract` in their contracts
- We will show an example in `resolveAttack` about what happens when the preconditions aren't used
- Discuss the coding & security tradeoffs between checks made in callers vs callees


## resolveAttack(character_t* target, uint32_t atk)
**Goal:** Expand upon the lessons learned with `checkStats` to get a contract ready for overrides/lemmas.

**Lessons Learned:**
- All of the points referenced in `checkStats` **Lessons Learned**
- Understand how to write cases for a function with more than 2 possible behaviors
- Understand when it is appropriate to include preconditions for functions that lack input checks
- Goes back to the caller vs callee tradeoffs mentioned in `checkStats` **Additional Notes**


## selfDamage(player_t* player)
**Goal:** To provide a case study where SAW should have complained about its memory disjoint assertion being violated. Note that SAW's Python API silently resolves this issue.


## resetInventoryItems(inventory_t* inventory)
**Goal:** To show the problems SAW faces when verifying structs with pointer fields.

**Lessons Learned:**
- Consider rewriting the source code to avoid unallocated pointers as it makes SAW happy and reduces assumptions code makes (improves security)


## initScreen(screen_t* screen, uint8_t assetID)
**Goal:** To provide a case study where SAW should have complained about not knowing what nested defines resolve to. Note that SAW's Python API silently resolves this issue.


## setScreenTile(screen_t* screen, uint32_t screenIdx, uint32_t tableIdx)
**Goal:** Demonstrate how to initialize an extern global array.

**Lessons Learned:**
- How to handle extern global variables when they aren't linked into the generated bitcode
- Copy extern definition to Cryptol spec file
- Could be possible to ignore the initialization if the bitcode links the file that implements the extern global (testing required)
- Understand when `ptr_to_fresh` vs `self.alloc` is needed
- Repointing a pointer to a SAW variable (`screen_post`) in order to use that variable for postconditions


## quickBattle(player_t* player, character_t* opponent)
**Goal:** To show how to pass overrides (lemmas) to a Unit test.

**Lessons Learned:**
- Must pass preconditions that match the preconditions included in the passed overrides

**Additional Notes:**
- The function assumes that both the player and opponent have non-zero HP values, otherwise there wouldn't be a battle!
- Explains why the `> 0` preconditions exist

20 changes: 20 additions & 0 deletions labs/SAW/Game/DLC/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Note: Makefile assumes user already started the SAW remote API
# $ start-saw-remote-api

# Variables to hold relative file paths
SRC=src
PROOF=proof
SPECS=specs
ARTIFACTS=artifacts

# Note: -g flag compiles the code with debug symbols enabled.
# The flag lets us pass field names in addition to indices for structs.
all: artifacts
clang -c -g -emit-llvm -o $(ARTIFACTS)/Game.bc $(SRC)/Game.c
python3 $(PROOF)/Game.py

artifacts:
mkdir $(ARTIFACTS)

clean:
rm -r $(ARTIFACTS)
Loading