Skip to content

Commit 708bbe8

Browse files
committed
Merge remote-tracking branch 'origin/master' into release
2 parents 2838542 + 58caec5 commit 708bbe8

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

kore/src/Kore/Builtin/Krypto.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Kore.Builtin.Krypto (
2121

2222
-- * Constants
2323
keccak256Key,
24+
keccak256RawKey,
2425
sha256Key,
2526
hashSha256Key,
2627
sha3256Key,
@@ -79,6 +80,7 @@ import Prelude.Kore
7980
import System.IO.Unsafe (unsafePerformIO)
8081

8182
keccak256Key
83+
, keccak256RawKey
8284
, ecdsaRecoverKey
8385
, ecdsaPubKey
8486
, sha256Key
@@ -87,6 +89,7 @@ keccak256Key
8789
, ripemd160Key ::
8890
IsString s => s
8991
keccak256Key = "KRYPTO.keccak256"
92+
keccak256RawKey = "KRYPTO.keccak256raw"
9093
ecdsaRecoverKey = "KRYPTO.ecdsaRecover"
9194
ecdsaPubKey = "KRYPTO.ecdsaPubKey"
9295
sha256Key = "KRYPTO.sha256"
@@ -123,6 +126,7 @@ symbolVerifiers :: Builtin.SymbolVerifiers
123126
symbolVerifiers =
124127
HashMap.fromList
125128
[ (keccak256Key, verifyHashFunction)
129+
, (keccak256RawKey, verifyHashFunctionRaw)
126130
, (hashKeccak256Key, verifyHashFunction)
127131
, (sha256Key, verifyHashFunction)
128132
, (hashSha256Key, verifyHashFunction)
@@ -158,6 +162,7 @@ symbolVerifiers =
158162
builtinFunctions :: Text -> Maybe BuiltinAndAxiomSimplifier
159163
builtinFunctions key
160164
| key == keccak256Key = Just evalKeccak
165+
| key == keccak256RawKey = Just evalKeccakRaw
161166
| key == hashKeccak256Key = Just evalKeccak
162167
| key == sha256Key = Just evalSha256
163168
| key == hashSha256Key = Just evalSha256
@@ -227,6 +232,9 @@ evalHashFunctionRaw context algorithm =
227232
evalKeccak :: BuiltinAndAxiomSimplifier
228233
evalKeccak = evalHashFunction keccak256Key Keccak_256
229234

235+
evalKeccakRaw :: BuiltinAndAxiomSimplifier
236+
evalKeccakRaw = evalHashFunctionRaw keccak256RawKey Keccak_256
237+
230238
evalSha256 :: BuiltinAndAxiomSimplifier
231239
evalSha256 = evalHashFunction sha256Key SHA256
232240

kore/test/Test/Kore/Builtin/Definition.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@ keccak256Symbol :: Internal.Symbol
852852
keccak256Symbol =
853853
builtinSymbol "keccak256Krypto" stringSort [bytesSort]
854854
& hook "KRYPTO.keccak256"
855+
keccak256RawSymbol :: Internal.Symbol
856+
keccak256RawSymbol =
857+
builtinSymbol "keccak256RawKrypto" bytesSort [bytesSort]
858+
& hook "KRYPTO.keccak256raw"
855859
sha256Symbol :: Internal.Symbol
856860
sha256Symbol =
857861
builtinSymbol "sha256Krypto" stringSort [bytesSort]
@@ -874,6 +878,9 @@ ecdsaRecoverKrypto m v r s = mkApplySymbol ecdsaRecoverSymbol [m, v, r, s]
874878
keccak256Krypto ::
875879
TermLike RewritingVariableName -> TermLike RewritingVariableName
876880
keccak256Krypto message = mkApplySymbol keccak256Symbol [message]
881+
keccak256RawKrypto ::
882+
TermLike RewritingVariableName -> TermLike RewritingVariableName
883+
keccak256RawKrypto message = mkApplySymbol keccak256RawSymbol [message]
877884
sha256Krypto ::
878885
TermLike RewritingVariableName -> TermLike RewritingVariableName
879886
sha256Krypto message = mkApplySymbol sha256Symbol [message]

kore/test/Test/Kore/Builtin/Krypto.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Test.Kore.Builtin.Krypto (
55
test_ecdsaRecover,
66
test_secp256k1EcdsaRecover,
77
test_keccak256,
8+
test_keccak256Raw,
89
test_hashKeccak256,
910
test_sha256,
1011
test_hashSha256,
@@ -16,12 +17,14 @@ module Test.Kore.Builtin.Krypto (
1617

1718
import Control.Lens qualified as Lens
1819
import Data.ByteString.Char8 qualified as BS
20+
import Data.Char (chr)
1921
import Data.Generics.Sum.Constructors
2022
import Data.Proxy
2123
import Data.Text (
2224
Text,
2325
)
2426
import Data.Text qualified as Text
27+
import Data.Text.Encoding qualified as Text
2528
import GHC.TypeLits qualified as TypeLits
2629
import Kore.Attribute.Symbol qualified as Attribute
2730
import Kore.Builtin.InternalBytes qualified as InternalBytes
@@ -137,6 +140,36 @@ test_keccak256 =
137140
& evaluate "KRYPTO.keccak256"
138141
assertEqual "" expect actual
139142

143+
test_keccak256Raw :: [TestTree]
144+
test_keccak256Raw =
145+
[ -- from the blockchain-k-plugin tests:
146+
test
147+
"foo"
148+
"A\xb1\xa0\&d\x97R\xaf\x1b(\xb3\xdc)\xa1Un\xeex\x1eJL:\x1f\x7fS\xf9\x0f\xa8\&4\xde\t\x8cM"
149+
, -- from above:
150+
test
151+
"\249\SOH\245\160\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\160\GS\204M\232\222\199]z\171\133\181g\182\204\212\SUB\211\DC2E\ESC\148\138t\DC3\240\161B\253@\212\147G\148*\220%fP\CAN\170\US\224\230\188fm\172\143\194i\DEL\249\186\160h\172|e\163\163\ESC}\139\230!\217\157/\"w\SOHX]\232s\219\218\162\181\248\215K\225Z\241\178\160V\232\US\ETB\ESC\204U\166\255\131E\230\146\192\248n[H\224\ESC\153l\173\192\SOHb/\181\227c\180!\160V\232\US\ETB\ESC\204U\166\255\131E\230\146\192\248n[H\224\ESC\153l\173\192\SOHb/\181\227c\180!\185\SOH\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\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e4175ae7f1bf6b8ed90b4ea22206353a7450aa10bdd5e2ba3cb2bd953")
153+
, -- from above:
154+
test
155+
"testing"
156+
(asBytes "5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02")
157+
]
158+
where
159+
asBytes = Text.decodeLatin1 . BS.pack . map (chr . read . ("0x" <>)) . groupEach2
160+
where
161+
groupEach2 [] = []
162+
groupEach2 [_] = error "odd number of characters in input"
163+
groupEach2 (a : b : rest) = [a, b] : groupEach2 rest
164+
165+
test input result =
166+
testCase (show input) $ do
167+
let expect = String.asPattern bytesSort result
168+
actual <-
169+
keccak256RawKrypto (InternalBytes.asInternal bytesSort (BS.pack input))
170+
& evaluate "KRYPTO.keccak256raw"
171+
assertEqual "" expect actual
172+
140173
test_hashKeccak256 :: [TestTree]
141174
test_hashKeccak256 =
142175
[ test

0 commit comments

Comments
 (0)