mirror of
https://github.com/logos-storage/nim-poseidon2.git
synced 2026-01-02 21:53:07 +00:00
Move merkleRoot() into its own module
This commit is contained in:
parent
af67374929
commit
7e18c0f46b
@ -1,47 +1,12 @@
|
||||
import std/sequtils
|
||||
import constantine/math/arithmetic
|
||||
|
||||
import poseidon2/types
|
||||
import poseidon2/permutation
|
||||
import poseidon2/io
|
||||
import poseidon2/sponge
|
||||
import poseidon2/compress
|
||||
import poseidon2/merkle
|
||||
|
||||
export sponge
|
||||
export compress
|
||||
export merkle
|
||||
export toBytes
|
||||
export elements
|
||||
export types
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
func merkleRoot*(xs: openArray[F]) : F =
|
||||
let a = low(xs)
|
||||
let b = high(xs)
|
||||
let m = b-a+1
|
||||
|
||||
if m==1:
|
||||
return xs[a]
|
||||
|
||||
else:
|
||||
let halfn : int = m div 2
|
||||
let n : int = 2*halfn
|
||||
let isOdd : bool = (n != m)
|
||||
|
||||
var ys : seq[F]
|
||||
if not isOdd:
|
||||
ys = newSeq[F](halfn)
|
||||
else:
|
||||
ys = newSeq[F](halfn+1)
|
||||
|
||||
for i in 0..<halfn:
|
||||
ys[i] = compress( xs[a+2*i], xs[a+2*i+1] )
|
||||
if isOdd:
|
||||
ys[halfn] = compress( xs[n], zero )
|
||||
|
||||
return merkleRoot(ys)
|
||||
|
||||
func merkleRoot*(bytes: openArray[byte]): F =
|
||||
merkleRoot(toSeq bytes.elements(F))
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
34
poseidon2/merkle.nim
Normal file
34
poseidon2/merkle.nim
Normal file
@ -0,0 +1,34 @@
|
||||
import std/sequtils
|
||||
import constantine/math/arithmetic
|
||||
import ./types
|
||||
import ./io
|
||||
import ./compress
|
||||
|
||||
func merkleRoot*(xs: openArray[F]) : F =
|
||||
let a = low(xs)
|
||||
let b = high(xs)
|
||||
let m = b-a+1
|
||||
|
||||
if m==1:
|
||||
return xs[a]
|
||||
|
||||
else:
|
||||
let halfn : int = m div 2
|
||||
let n : int = 2*halfn
|
||||
let isOdd : bool = (n != m)
|
||||
|
||||
var ys : seq[F]
|
||||
if not isOdd:
|
||||
ys = newSeq[F](halfn)
|
||||
else:
|
||||
ys = newSeq[F](halfn+1)
|
||||
|
||||
for i in 0..<halfn:
|
||||
ys[i] = compress( xs[a+2*i], xs[a+2*i+1] )
|
||||
if isOdd:
|
||||
ys[halfn] = compress( xs[n], zero )
|
||||
|
||||
return merkleRoot(ys)
|
||||
|
||||
func merkleRoot*(bytes: openArray[byte]): F =
|
||||
merkleRoot(toSeq bytes.elements(F))
|
||||
@ -8,9 +8,11 @@ import constantine/math/io/io_bigints
|
||||
import constantine/serialization/codecs
|
||||
|
||||
import poseidon2/types
|
||||
import poseidon2
|
||||
import poseidon2/io
|
||||
import poseidon2/compress
|
||||
import poseidon2/merkle
|
||||
|
||||
suite "poseidon2":
|
||||
suite "merkle root":
|
||||
|
||||
test "merkle root of field elements":
|
||||
let m = 17
|
||||
@ -1,7 +1,7 @@
|
||||
import ./poseidon2/testPermutation
|
||||
import ./poseidon2/testSponge
|
||||
import ./poseidon2/testCompress
|
||||
import ./poseidon2/testPoseidon2
|
||||
import ./poseidon2/testMerkle
|
||||
import ./poseidon2/testIo
|
||||
import ./poseidon2/testReadme
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user