Skip to content

Commit

Permalink
add hash-poseidon alias of poseidon-hash-hack-a-chain (#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
chessai authored May 15, 2024
1 parent 51b8191 commit 8dd5823
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 17 deletions.
32 changes: 32 additions & 0 deletions docs/en/pact-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,38 @@ pact> (hash-keccak256 ["T73FllCNJKKgAQ4UCYC4CfucbVXsdRJYkd2YXTdmW9g", "D5vralAgd
```


### hash-poseidon {#hash-poseidon}

*i* `integer` *→* `integer`

*i* `integer` *j* `integer` *→* `integer`

*i* `integer` *j* `integer` *k* `integer` *→* `integer`

*i* `integer` *j* `integer` *k* `integer` *l* `integer` *→* `integer`

*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *→* `integer`

*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *n* `integer` *→* `integer`

*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *n* `integer` *o* `integer` *→* `integer`

*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *n* `integer` *o* `integer` *p* `integer` *→* `integer`


Poseidon Hash Function. This is an alias of 'poseidon-hash-hack-a-chain'.
```lisp
pact> (hash-poseidon 1)
18586133768512220936620570745912940619677854269274689475585506675881198879027
pact> (hash-poseidon 1 2)
7853200120776062878684798364095072458815029376092732009249414926327459813530
pact> (hash-poseidon 1 2 3 4 5 6)
20400040500897583745843009878988256314335038853985262692600694741116813247201
pact> (hash-poseidon 1 2 3 4 5 6 7 8)
18604317144381847857886385684060986177838410221561136253933256952257712543953
```


### poseidon-hash-hack-a-chain {#poseidon-hash-hack-a-chain}

*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *n* `integer` *o* `integer` *p* `integer` *→* `integer`
Expand Down
10 changes: 9 additions & 1 deletion golden/gas-model/golden
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@
{ "balance": 0.0 }
)
- 130
- - |-
(poseidon-hash-hack-a-chain 1 2)
(poseidon-hash-hack-a-chain 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
(hash-poseidon 1 2)
(hash-poseidon 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
- 1504
- - (hash longString)
- 5
- - (hash medString)
Expand Down Expand Up @@ -813,7 +819,9 @@
- - |-
(poseidon-hash-hack-a-chain 1 2)
(poseidon-hash-hack-a-chain 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
- 752
(hash-poseidon 1 2)
(hash-poseidon 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
- 1504
- - (enforce-pact-version "3.0")
- 1
- - |-
Expand Down
1 change: 1 addition & 0 deletions src/Pact/Gas/Table.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ defaultGasTable =
,("pairing-check", 1)

,("poseidon-hash-hack-a-chain", 124)
,("hash-poseidon", 124)
,("hyperlane-message-id", 2)
,("hyperlane-decode-token-message", 2)
,("hash-keccak256",1)
Expand Down
3 changes: 3 additions & 0 deletions src/Pact/GasModel/GasTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ allTests = HM.fromList
, ("scalar-mult", scalarMulTests)
, ("pairing-check", pairingCheckTests)
, ("poseidon-hash-hack-a-chain", poseidonHashTests)
, ("hash-poseidon", poseidonHashTests)

-- SPI/Hyperlane
, ("hyperlane-message-id", hyperlaneMessageIdTests)
Expand Down Expand Up @@ -2015,6 +2016,8 @@ poseidonHashTests = defGasUnitTest $ PactExpression poseidonHashExprText Nothing
poseidonHashExprText = [text|
(poseidon-hash-hack-a-chain 1 2)
(poseidon-hash-hack-a-chain 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
(hash-poseidon 1 2)
(hash-poseidon 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
|]

enforceVerifierTests :: NativeDefName -> GasUnitTests
Expand Down
2 changes: 1 addition & 1 deletion src/Pact/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pact411Natives :: [Text]
pact411Natives = ["enforce-verifier", "hyperlane-message-id", "hyperlane-decode-token-message"]

pact412Natives :: [Text]
pact412Natives = ["hash-keccak256"]
pact412Natives = ["hash-keccak256", "hash-poseidon"]

initRefStore :: RefStore
initRefStore = RefStore nativeDefs
Expand Down
51 changes: 36 additions & 15 deletions src/Pact/Native.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,28 +1566,49 @@ base64DecodeWithShimmedErrors i txt = do
hashDefs :: NativeModule
hashDefs = ("Hashes",)
[ poseidonHackAChainDef
, poseidonHashAliasDef
, keccak256Def
]

poseidonHackAChainDef :: NativeDef
poseidonHackAChainDef = defGasRNative
"poseidon-hash-hack-a-chain"
poseidon'
poseidonImpl
(funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger), ("o", tTyInteger), ("p", tTyInteger)])
["(poseidon-hash-hack-a-chain 1)"
,"(poseidon-hash-hack-a-chain 1 2)"
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6)"
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6 7 8)"
]
"Poseidon Hash Function. Note: This is a reference version of the Poseidon hash function used by Hack-a-Chain."
where
poseidon' :: RNativeFun e
poseidon' i as
| not (null as) && length as <= 8,
Just intArgs <- traverse (preview _TLitInteger) as
= computeGas' i (GPoseidonHashHackAChain $ length as) $
return $ toTerm $ poseidon intArgs
| otherwise = argsError i as
["(poseidon-hash-hack-a-chain 1)"
,"(poseidon-hash-hack-a-chain 1 2)"
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6)"
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6 7 8)"
]
"Poseidon Hash Function. Note: This is a reference version of the Poseidon hash function used by Hack-a-Chain."

poseidonHashAliasDef :: NativeDef
poseidonHashAliasDef = defGasRNative
"hash-poseidon"
poseidonImpl
( funType tTyInteger [("i", tTyInteger)]
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger)]
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger)]
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger)]
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger)]
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger)]
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger), ("o", tTyInteger)]
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger), ("o", tTyInteger), ("p", tTyInteger)]
)
["(hash-poseidon 1)"
,"(hash-poseidon 1 2)"
,"(hash-poseidon 1 2 3 4 5 6)"
,"(hash-poseidon 1 2 3 4 5 6 7 8)"
]
"Poseidon Hash Function. This is an alias of 'poseidon-hash-hack-a-chain'."

poseidonImpl :: RNativeFun e
poseidonImpl i as
| not (null as) && length as <= 8,
Just intArgs <- traverse (preview _TLitInteger) as
= computeGas' i (GPoseidonHashHackAChain $ length as) $
return $ toTerm $ poseidon intArgs
| otherwise = argsError i as

keccak256Def :: NativeDef
keccak256Def = defGasRNative
Expand Down
9 changes: 9 additions & 0 deletions tests/pact/poseidon-hash.repl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@
(expect "1 2 3 4 5 6 should equal 20400040500897583745843009878988256314335038853985262692600694741116813247201" 20400040500897583745843009878988256314335038853985262692600694741116813247201 (poseidon-hash-hack-a-chain 1 2 3 4 5 6))
(expect "1 2 3 4 5 6 7 should equal 12748163991115452309045839028154629052133952896122405799815156419278439301912" 12748163991115452309045839028154629052133952896122405799815156419278439301912 (poseidon-hash-hack-a-chain 1 2 3 4 5 6 7))
(expect "1 2 3 4 5 6 7 8 should equal 18604317144381847857886385684060986177838410221561136253933256952257712543953" 18604317144381847857886385684060986177838410221561136253933256952257712543953 (poseidon-hash-hack-a-chain 1 2 3 4 5 6 7 8))
;; Alias
(expect "1 should equal 18586133768512220936620570745912940619677854269274689475585506675881198879027" 18586133768512220936620570745912940619677854269274689475585506675881198879027 (hash-poseidon 1))
(expect "1 2 should equal 7853200120776062878684798364095072458815029376092732009249414926327459813530" 7853200120776062878684798364095072458815029376092732009249414926327459813530 (hash-poseidon 1 2))
(expect "1 2 3 should equal 6542985608222806190361240322586112750744169038454362455181422643027100751666" 6542985608222806190361240322586112750744169038454362455181422643027100751666 (hash-poseidon 1 2 3))
(expect "1 2 3 4 should equal 18821383157269793795438455681495246036402687001665670618754263018637548127333" 18821383157269793795438455681495246036402687001665670618754263018637548127333 (hash-poseidon 1 2 3 4))
(expect "1 2 3 4 5 should equal 6183221330272524995739186171720101788151706631170188140075976616310159254464" 6183221330272524995739186171720101788151706631170188140075976616310159254464 (hash-poseidon 1 2 3 4 5))
(expect "1 2 3 4 5 6 should equal 20400040500897583745843009878988256314335038853985262692600694741116813247201" 20400040500897583745843009878988256314335038853985262692600694741116813247201 (hash-poseidon 1 2 3 4 5 6))
(expect "1 2 3 4 5 6 7 should equal 12748163991115452309045839028154629052133952896122405799815156419278439301912" 12748163991115452309045839028154629052133952896122405799815156419278439301912 (hash-poseidon 1 2 3 4 5 6 7))
(expect "1 2 3 4 5 6 7 8 should equal 18604317144381847857886385684060986177838410221561136253933256952257712543953" 18604317144381847857886385684060986177838410221561136253933256952257712543953 (hash-poseidon 1 2 3 4 5 6 7 8))

0 comments on commit 8dd5823

Please sign in to comment.