Skip to content

Commit 68ea568

Browse files
authored
Merge pull request #21 from functionally/cli
API & CLI
2 parents 1991acb + 2d24b05 commit 68ea568

6 files changed

Lines changed: 137 additions & 10 deletions

File tree

lake-manifest.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
"inputRev": "v0.0.30",
3838
"inherited": true,
3939
"configFile": "lakefile.lean"},
40-
{"url": "https://github.com/leanprover/lean4-cli",
40+
{"url": "https://github.com/leanprover/lean4-cli.git",
4141
"type": "git",
4242
"subDir": null,
43-
"rev": "be8fa79a28b8b6897dce0713ef50e89c4a0f6ef5",
43+
"rev": "a11566029bd9ec4f68a65394e8c3ff1af74c1a29",
4444
"name": "Cli",
4545
"manifestFile": "lake-manifest.json",
46-
"inputRev": "main",
47-
"inherited": true,
46+
"inputRev": "v2.2.0-lv4.7.0",
47+
"inherited": false,
4848
"configFile": "lakefile.lean"},
4949
{"url": "https://github.com/leanprover-community/import-graph.git",
5050
"type": "git",

lakefile.lean

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ require mathlib from git
1919
require LSpec from git
2020
"https://github.com/argumentcomputer/LSpec.git"
2121

22+
require Cli from git
23+
"https://github.com/leanprover/lean4-cli.git"
24+
@ "v2.2.0-lv4.7.0"
25+
2226
@[default_target]
2327
lean_exe «crypto» where
2428
root := `Main
2529
srcDir := "src"
2630
-- Enables the use of the Lean interpreter by the executable (e.g.,
2731
-- `runFrontend`) at the expense of increased binary size on Linux.
2832
-- Remove this line if you do not need such functionality.
29-
supportInterpreter := true
33+
supportInterpreter := false

src/Crypto.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import Crypto.API
3+
24
import Crypto.Field.Fp
35
import Crypto.Field.Fp.Test
46

src/Crypto/API.lean

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
import Crypto.Hash
3+
import Crypto.Serial
4+
5+
open Crypto
6+
7+
8+
namespace Crypto.API
9+
10+
11+
def bytesToHex (inFile : System.FilePath) (outFile : System.FilePath) : IO Unit :=
12+
do
13+
let src ← IO.FS.readBinFile inFile
14+
let dst := Serial.bytesToHex src
15+
IO.FS.writeFile outFile $ dst ++ "\n"
16+
17+
18+
def bytesToNat (inFile : System.FilePath) (outFile : System.FilePath) : IO Unit :=
19+
do
20+
let src ← IO.FS.readBinFile inFile
21+
let dst : Nat := Serial.Words.fromWords src.toList.toArray
22+
IO.FS.writeFile outFile $ toString dst ++ "\n"
23+
24+
25+
def parseHashAlgorithm : String → Option (CHash.Algorithm)
26+
| "SHA2_224" => some CHash.Algorithm.SHA2_224
27+
| "SHA2_256" => some CHash.Algorithm.SHA2_256
28+
| "SHA2_384" => some CHash.Algorithm.SHA2_384
29+
| "SHA2_512" => some CHash.Algorithm.SHA2_512
30+
| _ => none
31+
32+
def hash (alg : CHash.Algorithm) (inFile : System.FilePath) (outFile : System.FilePath) : IO Unit :=
33+
do
34+
let src ← IO.FS.readBinFile inFile
35+
let dst :=
36+
Serial.bytesToHex
37+
∘ CHash.data
38+
∘ alg.chash
39+
$ src
40+
IO.FS.writeFile outFile $ dst ++ "\n"
41+
42+
43+
end Crypto.API

src/Crypto/Hash.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ namespace CHash
3232
| SHA2_384 => 56
3333
| SHA2_512 => 64
3434

35-
def chash [Serializable a] : a → (alg : Algorithm) → CHash alg.length
36-
| x , SHA2_224 => CHash.mk ∘ SHA2.sha224 $ Serializable.encode x
37-
| x , SHA2_256 => CHash.mk ∘ SHA2.sha256 $ Serializable.encode x
38-
| x , SHA2_384 => CHash.mk ∘ SHA2.sha384 $ Serializable.encode x
39-
| x , SHA2_512 => CHash.mk ∘ SHA2.sha512 $ Serializable.encode x
35+
def chash [Serializable a] : a → (alg : Algorithm) → CHash alg.length
36+
| x , SHA2_224 => CHash.mk ∘ SHA2.sha224 $ Serializable.encode x
37+
| x , SHA2_256 => CHash.mk ∘ SHA2.sha256 $ Serializable.encode x
38+
| x , SHA2_384 => CHash.mk ∘ SHA2.sha384 $ Serializable.encode x
39+
| x , SHA2_512 => CHash.mk ∘ SHA2.sha512 $ Serializable.encode x
4040

4141
end Algorithm
4242

src/Main.lean

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Cli
2+
import Crypto.API
3+
4+
open Cli
5+
open Crypto
6+
7+
8+
def getInFile (p : Parsed) : System.FilePath :=
9+
if p.hasFlag "input" then (p.flag! "input").value else "/dev/stdin"
10+
11+
def getOutFile (p : Parsed) : System.FilePath :=
12+
if p.hasFlag "output" then (p.flag! "output").value else "/dev/stdout"
13+
14+
15+
def bytesToHexExec (p : Parsed) : IO UInt32 :=
16+
do
17+
API.bytesToHex (getInFile p) (getOutFile p)
18+
pure 0
19+
20+
def bytesToHex : Cmd := `[Cli|
21+
bytesToHex VIA bytesToHexExec;
22+
"Convert bytes to hexadecimal."
23+
FLAGS:
24+
i, input : String ; "Input file."
25+
o, output : String ; "Output file."
26+
]
27+
28+
29+
def bytesToNatExec (p : Parsed) : IO UInt32 :=
30+
do
31+
API.bytesToNat (getInFile p) (getOutFile p)
32+
pure 0
33+
34+
def bytesToNat : Cmd := `[Cli|
35+
bytesToNat VIA bytesToNatExec;
36+
"Convert bytes to a natural number."
37+
FLAGS:
38+
i, input : String ; "Input file."
39+
o, output : String ; "Output file."
40+
]
41+
42+
43+
def HashAlg : ParamType :=
44+
ParamType.mk
45+
"Hash algorithm"
46+
$ not ∘ BEq.beq none ∘ API.parseHashAlgorithm
47+
48+
def hashExec (p : Parsed) : IO UInt32 :=
49+
match API.parseHashAlgorithm (p.positionalArg! "alg").value with
50+
| some alg =>
51+
do
52+
API.hash alg (getInFile p) (getOutFile p)
53+
pure 0
54+
| none => pure 0
55+
56+
def hash : Cmd := `[Cli|
57+
hash VIA hashExec;
58+
"Hash bytes."
59+
FLAGS:
60+
i, input : String ; "Input file."
61+
o, output : String ; "Output file."
62+
ARGS:
63+
alg : HashAlg; "SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512"
64+
]
65+
66+
67+
def crypto : Cmd := `[Cli|
68+
crypto NOOP;
69+
"Cryptographic operations."
70+
SUBCOMMANDS:
71+
bytesToHex;
72+
bytesToNat;
73+
hash
74+
]
75+
76+
77+
def main (args : List String) : IO UInt32 :=
78+
crypto.validate args

0 commit comments

Comments
 (0)