From 49c14be71e1fc1e07e55562509598300ddf01be5 Mon Sep 17 00:00:00 2001 From: Paul Cadman Date: Thu, 14 Nov 2024 17:54:39 +0000 Subject: [PATCH] Fix nock compilation and builtin evaluation of sha256 stdilb (#3175) This PR: 1. Fixes the compilation of the sha256 builtin anoma lib call A sha256 hash is 32 bytes long, not 64 bytes. This number is used when constructing the ByteArray representation (i.e `[length payload]` cell) of the output of Anoma stdlib sha256 call. The Anoma stdlib sha256 call just returns the atom payload. 2. Fixes the evaluation of the sha256 stdlib call Previously we were converting the sha256 hash bytestring to Base16 format. This is convenient when displaying the ByteString hash. However the Anoma nock interpreter outputs the raw bytes so we must change the builtin evaluator to match this behaviour. After these fixes we can re-enable the test084 anoma compilation test. --- src/Juvix/Compiler/Nockma/Encoding/ByteString.hs | 6 +----- src/Juvix/Compiler/Nockma/Translation/FromTree.hs | 2 +- test/Anoma/Compilation/Positive.hs | 8 ++++---- test/Nockma/Encoding.hs | 2 +- tests/Core/positive/out/test062.out | 2 +- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Juvix/Compiler/Nockma/Encoding/ByteString.hs b/src/Juvix/Compiler/Nockma/Encoding/ByteString.hs index 821c2ef11..a506116f7 100644 --- a/src/Juvix/Compiler/Nockma/Encoding/ByteString.hs +++ b/src/Juvix/Compiler/Nockma/Encoding/ByteString.hs @@ -5,7 +5,6 @@ import Data.Bit (Bit) import Data.Bit qualified as Bit import Data.Bits import Data.ByteString qualified as BS -import Data.ByteString.Base16 qualified as Base16 import Data.ByteString.Builder qualified as BS import Juvix.Compiler.Nockma.Encoding.Base import Juvix.Compiler.Nockma.Encoding.Effect.BitReader @@ -120,7 +119,4 @@ decodeByteStringWithDefault :: ByteString -> Integer -> ByteString decodeByteStringWithDefault d = fromRight d . run . runErrorNoCallStack @BitReadError . decodeByteString sha256Natural :: Natural -> ByteString -sha256Natural = - Base16.encode - . SHA256.hash - . naturalToByteStringLE +sha256Natural = SHA256.hash . naturalToByteStringLE diff --git a/src/Juvix/Compiler/Nockma/Translation/FromTree.hs b/src/Juvix/Compiler/Nockma/Translation/FromTree.hs index 13798aa85..b6eb4a477 100644 --- a/src/Juvix/Compiler/Nockma/Translation/FromTree.hs +++ b/src/Juvix/Compiler/Nockma/Translation/FromTree.hs @@ -712,7 +712,7 @@ compile = \case return $ mkByteArray (nockNatLiteral (integerToNatural sha256HashLength)) stdcall where sha256HashLength :: Integer - sha256HashLength = 64 + sha256HashLength = 32 goAnomaRandomNextBytes :: [Term Natural] -> Sem r (Term Natural) goAnomaRandomNextBytes args = case args of diff --git a/test/Anoma/Compilation/Positive.hs b/test/Anoma/Compilation/Positive.hs index bc2705849..637fe5589 100644 --- a/test/Anoma/Compilation/Positive.hs +++ b/test/Anoma/Compilation/Positive.hs @@ -240,7 +240,7 @@ classify AnomaTest {..} = case _anomaTestNum of 81 -> ClassWorking 82 -> ClassWorking 83 -> ClassWorking - 84 -> ClassWrong + 84 -> ClassWorking 85 -> ClassWorking 86 -> ClassExpectedFail _ -> error "non-exhaustive test classification" @@ -1024,11 +1024,11 @@ allTests = $(mkRelFile "test084.juvix") [] $ checkOutput - [ [nock| 64 |], + [ [nock| 32 |], [nock| [ - 64 - 5092006196359674779938793937035252249221936503860319648757996882954518215195609232852607160812968472040491493412050369557521935588220586883008001462395444 + 32 + 69779012276202546540741613998220636891790827476075440677599814057037833368907 ] |] ], mkAnomaTest diff --git a/test/Nockma/Encoding.hs b/test/Nockma/Encoding.hs index b880fe74e..439d94560 100644 --- a/test/Nockma/Encoding.hs +++ b/test/Nockma/Encoding.hs @@ -17,7 +17,7 @@ propEncodingRoundtrip = property $ do propSha256Length :: Property propSha256Length = property $ do n <- forAll (Gen.integral (Range.linear 0 1000000)) - BS.length (Encoding.sha256Natural n) === 64 + BS.length (Encoding.sha256Natural n) === 32 propSha256HandlesLargeIntegers :: Property propSha256HandlesLargeIntegers = property $ do diff --git a/tests/Core/positive/out/test062.out b/tests/Core/positive/out/test062.out index dad530bba..43836fce8 100644 --- a/tests/Core/positive/out/test062.out +++ b/tests/Core/positive/out/test062.out @@ -4,4 +4,4 @@ true true true false -bytearray-from-list-byte (builtinListCons UInt8 52 (builtinListCons UInt8 98 (builtinListCons UInt8 102 (builtinListCons UInt8 53 (builtinListCons UInt8 49 (builtinListCons UInt8 50 (builtinListCons UInt8 50 (builtinListCons UInt8 102 (builtinListCons UInt8 51 (builtinListCons UInt8 52 (builtinListCons UInt8 52 (builtinListCons UInt8 53 (builtinListCons UInt8 53 (builtinListCons UInt8 52 (builtinListCons UInt8 99 (builtinListCons UInt8 53 (builtinListCons UInt8 51 (builtinListCons UInt8 98 (builtinListCons UInt8 100 (builtinListCons UInt8 101 (builtinListCons UInt8 50 (builtinListCons UInt8 101 (builtinListCons UInt8 98 (builtinListCons UInt8 98 (builtinListCons UInt8 56 (builtinListCons UInt8 99 (builtinListCons UInt8 100 (builtinListCons UInt8 50 (builtinListCons UInt8 98 (builtinListCons UInt8 55 (builtinListCons UInt8 101 (builtinListCons UInt8 51 (builtinListCons UInt8 100 (builtinListCons UInt8 49 (builtinListCons UInt8 54 (builtinListCons UInt8 48 (builtinListCons UInt8 48 (builtinListCons UInt8 97 (builtinListCons UInt8 100 (builtinListCons UInt8 54 (builtinListCons UInt8 51 (builtinListCons UInt8 49 (builtinListCons UInt8 99 (builtinListCons UInt8 51 (builtinListCons UInt8 56 (builtinListCons UInt8 53 (builtinListCons UInt8 97 (builtinListCons UInt8 53 (builtinListCons UInt8 100 (builtinListCons UInt8 55 (builtinListCons UInt8 99 (builtinListCons UInt8 99 (builtinListCons UInt8 101 (builtinListCons UInt8 50 (builtinListCons UInt8 51 (builtinListCons UInt8 99 (builtinListCons UInt8 55 (builtinListCons UInt8 55 (builtinListCons UInt8 56 (builtinListCons UInt8 53 (builtinListCons UInt8 52 (builtinListCons UInt8 53 (builtinListCons UInt8 57 (builtinListCons UInt8 97 (builtinListNil UInt8))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) +bytearray-from-list-byte (builtinListCons UInt8 75 (builtinListCons UInt8 245 (builtinListCons UInt8 18 (builtinListCons UInt8 47 (builtinListCons UInt8 52 (builtinListCons UInt8 69 (builtinListCons UInt8 84 (builtinListCons UInt8 197 (builtinListCons UInt8 59 (builtinListCons UInt8 222 (builtinListCons UInt8 46 (builtinListCons UInt8 187 (builtinListCons UInt8 140 (builtinListCons UInt8 210 (builtinListCons UInt8 183 (builtinListCons UInt8 227 (builtinListCons UInt8 209 (builtinListCons UInt8 96 (builtinListCons UInt8 10 (builtinListCons UInt8 214 (builtinListCons UInt8 49 (builtinListCons UInt8 195 (builtinListCons UInt8 133 (builtinListCons UInt8 165 (builtinListCons UInt8 215 (builtinListCons UInt8 204 (builtinListCons UInt8 226 (builtinListCons UInt8 60 (builtinListCons UInt8 119 (builtinListCons UInt8 133 (builtinListCons UInt8 69 (builtinListCons UInt8 154 (builtinListNil UInt8)))))))))))))))))))))))))))))))))