refactoring the structure of the nimble package

This commit is contained in:
Balazs Komuves 2024-10-01 21:00:27 +02:00
parent 61bd510fd7
commit 69f1994eea
26 changed files with 62 additions and 60 deletions

7
goldilocks_hash.nim Normal file
View File

@ -0,0 +1,7 @@
import goldilocks_hash/types
import goldilocks_hash/goldilocks
export types
export goldilocks

5
goldilocks_hash.nimble Normal file
View File

@ -0,0 +1,5 @@
version = "0.0.1"
author = "Balazs Komuves"
description = "Airthmetic hash functions (Poseidon2, Monolith) over the Goldilocks field"
license = "MIT"

View File

@ -0,0 +1,14 @@
import goldilocks_hash/types
import goldilocks_hash/goldilocks
import goldilocks_hash/poseidon2/sponge
import goldilocks_hash/poseidon2/merkle
# import goldilocks_hash/poseidon2/permutation
# import goldilocks_hash/poseidon2/compress
export types
export goldilocks
export sponge
export merkle

View File

@ -1,5 +1,5 @@
import ./types import ../types
proc c_compress(a, b: var Digest, key: uint64, output: var Digest) {. header: "../cbits/goldilocks.h", importc: "goldilocks_poseidon2_keyed_compress", cdecl .} proc c_compress(a, b: var Digest, key: uint64, output: var Digest) {. header: "../cbits/goldilocks.h", importc: "goldilocks_poseidon2_keyed_compress", cdecl .}

View File

@ -7,7 +7,7 @@
# - there is no collision if you remove the bottommost layer (or several layers) # - there is no collision if you remove the bottommost layer (or several layers)
# - the merkle root of the singleton is not itself # - the merkle root of the singleton is not itself
import ./types import ../types
import ./compress import ./compress
#import ./io #import ./io

View File

@ -1,5 +1,5 @@
import ./types import ../types
# the Poseidon2 permutation (mutable, in-place version) # the Poseidon2 permutation (mutable, in-place version)
proc permInPlace* (state: var State) {. header: "../cbits/goldilocks.h", importc: "goldilocks_poseidon2_permutation", cdecl .} proc permInPlace* (state: var State) {. header: "../cbits/goldilocks.h", importc: "goldilocks_poseidon2_permutation", cdecl .}

View File

@ -10,8 +10,8 @@
#import std/assertions # on 1.6.18 with an M2 i got "cannot open file: std/assertions" .... #import std/assertions # on 1.6.18 with an M2 i got "cannot open file: std/assertions" ....
import ./types import ../types
import ./goldilocks import ../goldilocks
import ./permutation import ./permutation
#import ./io #import ./io

View File

@ -1,17 +0,0 @@
# import poseidon2/types
# import poseidon2/io
# import poseidon2/sponge
# import poseidon2/compress
# import poseidon2/merkle
# import poseidon2/spongemerkle
# export sponge
# export compress
# export merkle
# export spongemerkle
# export fromBytes
# export toBytes
# export toF
# export elements
# export types

View File

@ -1,5 +0,0 @@
version = "0.0.1"
author = "Balazs Komuves"
description = "Poseidon2 hash function over the goldilocks field"
license = "MIT"

View File

@ -2,5 +2,7 @@
Haskell reference implementation Haskell reference implementation
-------------------------------- --------------------------------
Used for generating test cases (as Nim doesn't support integers larger than 64 bit) This is a very inefficient, but easy to read (and check) Haskell implementation
of Poseidon2.
We use it for generating test cases for the real implementation.

View File

@ -64,7 +64,7 @@ hPrintTests h = hPutStrLn h $ unlines
writeTests :: IO () writeTests :: IO ()
writeTests = withFile "fieldTestCases.nim" WriteMode $ \h -> do writeTests = withFile "fieldTestCases.nim" WriteMode $ \h -> do
hPutStrLn h "# generated by TestGen/TestGoldilocks.hs\n" hPutStrLn h "# generated by TestGen/TestGoldilocks.hs\n"
-- hPutStrLn h "import poseidon2/types\n" hPutStrLn h "import goldilocks_hash/types\n"
hPrintTests h hPrintTests h
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -37,7 +37,7 @@ hPrintTests h = hPutStrLn h $ unlines $
writeTests :: IO () writeTests :: IO ()
writeTests = withFile "merkleTestCases.nim" WriteMode $ \h -> do writeTests = withFile "merkleTestCases.nim" WriteMode $ \h -> do
hPutStrLn h "# generated by TestGen/TestMerkle.hs\n" hPutStrLn h "# generated by TestGen/TestMerkle.hs\n"
hPutStrLn h "import poseidon2/types\n" hPutStrLn h "import goldilocks_hash/types\n"
hPrintTests h hPrintTests h
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -46,7 +46,7 @@ hPrintTests h = hPutStrLn h $ unlines
writeTests :: IO () writeTests :: IO ()
writeTests = withFile "permTestCases.nim" WriteMode $ \h -> do writeTests = withFile "permTestCases.nim" WriteMode $ \h -> do
hPutStrLn h "# generated by TestGen/TestPermutation.hs\n" hPutStrLn h "# generated by TestGen/TestPermutation.hs\n"
-- hPutStrLn h "import poseidon2/types\n" hPutStrLn h "import goldilocks_hash/types\n"
hPrintTests h hPrintTests h
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -38,7 +38,7 @@ hPrintTests h = hPutStrLn h $ unlines $
writeTests :: IO () writeTests :: IO ()
writeTests = withFile "spongeTestCases.nim" WriteMode $ \h -> do writeTests = withFile "spongeTestCases.nim" WriteMode $ \h -> do
hPutStrLn h "# generated by TestGen/TestSponge.hs\n" hPutStrLn h "# generated by TestGen/TestSponge.hs\n"
hPutStrLn h "import poseidon2/types\n" hPutStrLn h "import goldilocks_hash/types\n"
hPrintTests h hPrintTests h
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -2,14 +2,14 @@
import std/unittest import std/unittest
# import std/sequtils # import std/sequtils
import poseidon2/types import goldilocks_hash/types
import poseidon2/goldilocks import goldilocks_hash/goldilocks
import ./fieldTestCases import ./fieldTestCases
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
suite "field": suite "the Goldilocks field":
test "negation": test "negation":
var ok = true var ok = true

View File

@ -1,6 +1,6 @@
# generated by TestGen/TestMerkle.hs # generated by TestGen/TestMerkle.hs
import poseidon2/types import goldilocks_hash/types
const testcases_merkleroot* : array[175, tuple[n:int,digest:F4]] = const testcases_merkleroot* : array[175, tuple[n:int,digest:F4]] =
[ ( 1 , [ toF( 0x232f21acc9d346d8'u64 ), toF( 0x2eba96d3a73822c1'u64 ), toF( 0x4163308f6d0eff64'u64 ), toF( 0x5190c2b759734aff'u64 ) ] ) [ ( 1 , [ toF( 0x232f21acc9d346d8'u64 ), toF( 0x2eba96d3a73822c1'u64 ), toF( 0x4163308f6d0eff64'u64 ), toF( 0x5190c2b759734aff'u64 ) ] )

View File

@ -1,6 +1,6 @@
# generated by TestGen/TestPermutation.hs # generated by TestGen/TestPermutation.hs
import poseidon2/types import goldilocks_hash/types
const testcases_perm* : array[16, tuple[xs:F12, ys:F12]] = const testcases_perm* : array[16, tuple[xs:F12, ys:F12]] =
[ ( [ toF( 0x0000000000000000'u64 ), toF( 0x0000000000000001'u64 ), toF( 0x0000000000000002'u64 ), toF( 0x0000000000000003'u64 ), toF( 0x0000000000000004'u64 ), toF( 0x0000000000000005'u64 ), toF( 0x0000000000000006'u64 ), toF( 0x0000000000000007'u64 ), toF( 0x0000000000000008'u64 ), toF( 0x0000000000000009'u64 ), toF( 0x000000000000000a'u64 ), toF( 0x000000000000000b'u64 ) ] , [ toF( 0x01eaef96bdf1c0c1'u64 ), toF( 0x1f0d2cc525b2540c'u64 ), toF( 0x6282c1dfe1e0358d'u64 ), toF( 0xe780d721f698e1e6'u64 ), toF( 0x280c0b6f753d833b'u64 ), toF( 0x1b942dd5023156ab'u64 ), toF( 0x43f0df3fcccb8398'u64 ), toF( 0xe8e8190585489025'u64 ), toF( 0x56bdbf72f77ada22'u64 ), toF( 0x7911c32bf9dcd705'u64 ), toF( 0xec467926508fbe67'u64 ), toF( 0x6a50450ddf85a6ed'u64 ) ] ) [ ( [ toF( 0x0000000000000000'u64 ), toF( 0x0000000000000001'u64 ), toF( 0x0000000000000002'u64 ), toF( 0x0000000000000003'u64 ), toF( 0x0000000000000004'u64 ), toF( 0x0000000000000005'u64 ), toF( 0x0000000000000006'u64 ), toF( 0x0000000000000007'u64 ), toF( 0x0000000000000008'u64 ), toF( 0x0000000000000009'u64 ), toF( 0x000000000000000a'u64 ), toF( 0x000000000000000b'u64 ) ] , [ toF( 0x01eaef96bdf1c0c1'u64 ), toF( 0x1f0d2cc525b2540c'u64 ), toF( 0x6282c1dfe1e0358d'u64 ), toF( 0xe780d721f698e1e6'u64 ), toF( 0x280c0b6f753d833b'u64 ), toF( 0x1b942dd5023156ab'u64 ), toF( 0x43f0df3fcccb8398'u64 ), toF( 0xe8e8190585489025'u64 ), toF( 0x56bdbf72f77ada22'u64 ), toF( 0x7911c32bf9dcd705'u64 ), toF( 0xec467926508fbe67'u64 ), toF( 0x6a50450ddf85a6ed'u64 ) ] )

View File

@ -1,6 +1,6 @@
# generated by TestGen/TestSponge.hs # generated by TestGen/TestSponge.hs
import poseidon2/types import goldilocks_hash/types
const testcases_field_rate1* : array[81, tuple[n:int,digest:F4]] = const testcases_field_rate1* : array[81, tuple[n:int,digest:F4]] =
[ ( 0 , [ toF( 0x823867639a2e2e42'u64 ), toF( 0x5a49f311c9c6aed5'u64 ), toF( 0x0755816884b47697'u64 ), toF( 0x5ebb5981aa75ab5c'u64 ) ] ) [ ( 0 , [ toF( 0x823867639a2e2e42'u64 ), toF( 0x5a49f311c9c6aed5'u64 ), toF( 0x0755816884b47697'u64 ), toF( 0x5ebb5981aa75ab5c'u64 ) ] )

View File

@ -1,9 +1,8 @@
import std/unittest import std/unittest
# import std/sequtils
import poseidon2/types import goldilocks_hash/types
import poseidon2/compress import goldilocks_hash/poseidon2/compress
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@ -53,7 +52,7 @@ const refOutKey3: array[4,F] =
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
suite "compression": suite "poseidon2 compression":
test "compression of [1..4] and [5..8] with key=0": test "compression of [1..4] and [5..8] with key=0":
let input1 : Digest = toDigest(refInp1) let input1 : Digest = toDigest(refInp1)

View File

@ -2,8 +2,8 @@
import std/unittest import std/unittest
# import std/sequtils # import std/sequtils
import poseidon2/types import goldilocks_hash/types
import poseidon2/merkle import goldilocks_hash/poseidon2/merkle
import ./merkleTestCases import ./merkleTestCases
@ -24,7 +24,7 @@ func isOkDigestNim( testcases: openarray[tuple[n:int,digest:F4]] ): bool =
ok = false ok = false
return ok return ok
suite "merkle/Nim": suite "poseidon2 merkle tree /Nim":
test "merkle root of digest sequences": check isOkDigestNim( testcases_merkleroot ) test "merkle root of digest sequences": check isOkDigestNim( testcases_merkleroot )

View File

@ -2,8 +2,8 @@
import std/unittest import std/unittest
# import std/sequtils # import std/sequtils
import poseidon2/types import goldilocks_hash/types
import poseidon2/permutation import goldilocks_hash/poseidon2/permutation
import ./permTestCases import ./permTestCases
@ -41,7 +41,7 @@ const refOut: F12 =
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
suite "permutation": suite "poseidon2 permutation":
test "permutation of [0..11]": test "permutation of [0..11]":
var input = toState(refInp) var input = toState(refInp)

View File

@ -2,8 +2,8 @@
import std/unittest import std/unittest
# import std/sequtils # import std/sequtils
import poseidon2/types import goldilocks_hash/types
import poseidon2/sponge import goldilocks_hash/poseidon2/sponge
import ./spongeTestCases import ./spongeTestCases
@ -29,7 +29,7 @@ func isOkFeltNim(r: static int, testcases: openarray[tuple[n:int,digest:F4]] ):
ok = false ok = false
return ok return ok
suite "sponge/Nim": suite "poseidon2 sponge /Nim":
test "sponge for field elements w/ rate = 1": check isOkFeltNim( 1 , testcases_field_rate1 ) test "sponge for field elements w/ rate = 1": check isOkFeltNim( 1 , testcases_field_rate1 )
test "sponge for field elements w/ rate = 2": check isOkFeltNim( 2 , testcases_field_rate2 ) test "sponge for field elements w/ rate = 2": check isOkFeltNim( 2 , testcases_field_rate2 )
@ -58,7 +58,7 @@ func isOkBytesC(r: static int, testcases: openarray[tuple[n:int,digest:F4]] ): b
ok = false ok = false
return ok return ok
suite "sponge/C": suite "poseidon2 sponge /C":
test "sponge for field elements w/ rate = 1": check isOkFeltC( 1 , testcases_field_rate1 ) test "sponge for field elements w/ rate = 1": check isOkFeltC( 1 , testcases_field_rate1 )
test "sponge for field elements w/ rate = 2": check isOkFeltC( 2 , testcases_field_rate2 ) test "sponge for field elements w/ rate = 2": check isOkFeltC( 2 , testcases_field_rate2 )

View File

@ -1,11 +1,8 @@
#import ./poseidon2/testSpongeMerkle
#import ./poseidon2/testIo
#import ./poseidon2/testReadme
import ./poseidon2/testField import ./goldilocks_hash/field/testField
import ./poseidon2/testPermutation import ./goldilocks_hash/poseidon2/testPermutation
import ./poseidon2/testCompress import ./goldilocks_hash/poseidon2/testCompress
import ./poseidon2/testSponge import ./goldilocks_hash/poseidon2/testSponge
import ./poseidon2/testMerkle import ./goldilocks_hash/poseidon2/testMerkle
{.warning[UnusedImport]: off.} {.warning[UnusedImport]: off.}