Skip to content

Commit 8dd5823

Browse files
authored
add hash-poseidon alias of poseidon-hash-hack-a-chain (#1356)
1 parent 51b8191 commit 8dd5823

File tree

7 files changed

+91
-17
lines changed

7 files changed

+91
-17
lines changed

docs/en/pact-functions.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,38 @@ pact> (hash-keccak256 ["T73FllCNJKKgAQ4UCYC4CfucbVXsdRJYkd2YXTdmW9g", "D5vralAgd
18161816
```
18171817

18181818

1819+
### hash-poseidon {#hash-poseidon}
1820+
1821+
*i* `integer` *→* `integer`
1822+
1823+
*i* `integer` *j* `integer` *→* `integer`
1824+
1825+
*i* `integer` *j* `integer` *k* `integer` *→* `integer`
1826+
1827+
*i* `integer` *j* `integer` *k* `integer` *l* `integer` *→* `integer`
1828+
1829+
*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *→* `integer`
1830+
1831+
*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *n* `integer` *→* `integer`
1832+
1833+
*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *n* `integer` *o* `integer` *→* `integer`
1834+
1835+
*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *n* `integer` *o* `integer` *p* `integer` *→* `integer`
1836+
1837+
1838+
Poseidon Hash Function. This is an alias of 'poseidon-hash-hack-a-chain'.
1839+
```lisp
1840+
pact> (hash-poseidon 1)
1841+
18586133768512220936620570745912940619677854269274689475585506675881198879027
1842+
pact> (hash-poseidon 1 2)
1843+
7853200120776062878684798364095072458815029376092732009249414926327459813530
1844+
pact> (hash-poseidon 1 2 3 4 5 6)
1845+
20400040500897583745843009878988256314335038853985262692600694741116813247201
1846+
pact> (hash-poseidon 1 2 3 4 5 6 7 8)
1847+
18604317144381847857886385684060986177838410221561136253933256952257712543953
1848+
```
1849+
1850+
18191851
### poseidon-hash-hack-a-chain {#poseidon-hash-hack-a-chain}
18201852

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

golden/gas-model/golden

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@
114114
{ "balance": 0.0 }
115115
)
116116
- 130
117+
- - |-
118+
(poseidon-hash-hack-a-chain 1 2)
119+
(poseidon-hash-hack-a-chain 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
120+
(hash-poseidon 1 2)
121+
(hash-poseidon 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
122+
- 1504
117123
- - (hash longString)
118124
- 5
119125
- - (hash medString)
@@ -813,7 +819,9 @@
813819
- - |-
814820
(poseidon-hash-hack-a-chain 1 2)
815821
(poseidon-hash-hack-a-chain 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
816-
- 752
822+
(hash-poseidon 1 2)
823+
(hash-poseidon 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
824+
- 1504
817825
- - (enforce-pact-version "3.0")
818826
- 1
819827
- - |-

src/Pact/Gas/Table.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ defaultGasTable =
245245
,("pairing-check", 1)
246246

247247
,("poseidon-hash-hack-a-chain", 124)
248+
,("hash-poseidon", 124)
248249
,("hyperlane-message-id", 2)
249250
,("hyperlane-decode-token-message", 2)
250251
,("hash-keccak256",1)

src/Pact/GasModel/GasTests.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ allTests = HM.fromList
224224
, ("scalar-mult", scalarMulTests)
225225
, ("pairing-check", pairingCheckTests)
226226
, ("poseidon-hash-hack-a-chain", poseidonHashTests)
227+
, ("hash-poseidon", poseidonHashTests)
227228

228229
-- SPI/Hyperlane
229230
, ("hyperlane-message-id", hyperlaneMessageIdTests)
@@ -2015,6 +2016,8 @@ poseidonHashTests = defGasUnitTest $ PactExpression poseidonHashExprText Nothing
20152016
poseidonHashExprText = [text|
20162017
(poseidon-hash-hack-a-chain 1 2)
20172018
(poseidon-hash-hack-a-chain 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
2019+
(hash-poseidon 1 2)
2020+
(hash-poseidon 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888)
20182021
|]
20192022

20202023
enforceVerifierTests :: NativeDefName -> GasUnitTests

src/Pact/Interpreter.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ pact411Natives :: [Text]
282282
pact411Natives = ["enforce-verifier", "hyperlane-message-id", "hyperlane-decode-token-message"]
283283

284284
pact412Natives :: [Text]
285-
pact412Natives = ["hash-keccak256"]
285+
pact412Natives = ["hash-keccak256", "hash-poseidon"]
286286

287287
initRefStore :: RefStore
288288
initRefStore = RefStore nativeDefs

src/Pact/Native.hs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,28 +1566,49 @@ base64DecodeWithShimmedErrors i txt = do
15661566
hashDefs :: NativeModule
15671567
hashDefs = ("Hashes",)
15681568
[ poseidonHackAChainDef
1569+
, poseidonHashAliasDef
15691570
, keccak256Def
15701571
]
15711572

15721573
poseidonHackAChainDef :: NativeDef
15731574
poseidonHackAChainDef = defGasRNative
15741575
"poseidon-hash-hack-a-chain"
1575-
poseidon'
1576+
poseidonImpl
15761577
(funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger), ("o", tTyInteger), ("p", tTyInteger)])
1577-
["(poseidon-hash-hack-a-chain 1)"
1578-
,"(poseidon-hash-hack-a-chain 1 2)"
1579-
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6)"
1580-
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6 7 8)"
1581-
]
1582-
"Poseidon Hash Function. Note: This is a reference version of the Poseidon hash function used by Hack-a-Chain."
1583-
where
1584-
poseidon' :: RNativeFun e
1585-
poseidon' i as
1586-
| not (null as) && length as <= 8,
1587-
Just intArgs <- traverse (preview _TLitInteger) as
1588-
= computeGas' i (GPoseidonHashHackAChain $ length as) $
1589-
return $ toTerm $ poseidon intArgs
1590-
| otherwise = argsError i as
1578+
["(poseidon-hash-hack-a-chain 1)"
1579+
,"(poseidon-hash-hack-a-chain 1 2)"
1580+
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6)"
1581+
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6 7 8)"
1582+
]
1583+
"Poseidon Hash Function. Note: This is a reference version of the Poseidon hash function used by Hack-a-Chain."
1584+
1585+
poseidonHashAliasDef :: NativeDef
1586+
poseidonHashAliasDef = defGasRNative
1587+
"hash-poseidon"
1588+
poseidonImpl
1589+
( funType tTyInteger [("i", tTyInteger)]
1590+
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger)]
1591+
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger)]
1592+
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger)]
1593+
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger)]
1594+
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger)]
1595+
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger), ("o", tTyInteger)]
1596+
<> funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger), ("o", tTyInteger), ("p", tTyInteger)]
1597+
)
1598+
["(hash-poseidon 1)"
1599+
,"(hash-poseidon 1 2)"
1600+
,"(hash-poseidon 1 2 3 4 5 6)"
1601+
,"(hash-poseidon 1 2 3 4 5 6 7 8)"
1602+
]
1603+
"Poseidon Hash Function. This is an alias of 'poseidon-hash-hack-a-chain'."
1604+
1605+
poseidonImpl :: RNativeFun e
1606+
poseidonImpl i as
1607+
| not (null as) && length as <= 8,
1608+
Just intArgs <- traverse (preview _TLitInteger) as
1609+
= computeGas' i (GPoseidonHashHackAChain $ length as) $
1610+
return $ toTerm $ poseidon intArgs
1611+
| otherwise = argsError i as
15911612

15921613
keccak256Def :: NativeDef
15931614
keccak256Def = defGasRNative

tests/pact/poseidon-hash.repl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@
88
(expect "1 2 3 4 5 6 should equal 20400040500897583745843009878988256314335038853985262692600694741116813247201" 20400040500897583745843009878988256314335038853985262692600694741116813247201 (poseidon-hash-hack-a-chain 1 2 3 4 5 6))
99
(expect "1 2 3 4 5 6 7 should equal 12748163991115452309045839028154629052133952896122405799815156419278439301912" 12748163991115452309045839028154629052133952896122405799815156419278439301912 (poseidon-hash-hack-a-chain 1 2 3 4 5 6 7))
1010
(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))
11+
;; Alias
12+
(expect "1 should equal 18586133768512220936620570745912940619677854269274689475585506675881198879027" 18586133768512220936620570745912940619677854269274689475585506675881198879027 (hash-poseidon 1))
13+
(expect "1 2 should equal 7853200120776062878684798364095072458815029376092732009249414926327459813530" 7853200120776062878684798364095072458815029376092732009249414926327459813530 (hash-poseidon 1 2))
14+
(expect "1 2 3 should equal 6542985608222806190361240322586112750744169038454362455181422643027100751666" 6542985608222806190361240322586112750744169038454362455181422643027100751666 (hash-poseidon 1 2 3))
15+
(expect "1 2 3 4 should equal 18821383157269793795438455681495246036402687001665670618754263018637548127333" 18821383157269793795438455681495246036402687001665670618754263018637548127333 (hash-poseidon 1 2 3 4))
16+
(expect "1 2 3 4 5 should equal 6183221330272524995739186171720101788151706631170188140075976616310159254464" 6183221330272524995739186171720101788151706631170188140075976616310159254464 (hash-poseidon 1 2 3 4 5))
17+
(expect "1 2 3 4 5 6 should equal 20400040500897583745843009878988256314335038853985262692600694741116813247201" 20400040500897583745843009878988256314335038853985262692600694741116813247201 (hash-poseidon 1 2 3 4 5 6))
18+
(expect "1 2 3 4 5 6 7 should equal 12748163991115452309045839028154629052133952896122405799815156419278439301912" 12748163991115452309045839028154629052133952896122405799815156419278439301912 (hash-poseidon 1 2 3 4 5 6 7))
19+
(expect "1 2 3 4 5 6 7 8 should equal 18604317144381847857886385684060986177838410221561136253933256952257712543953" 18604317144381847857886385684060986177838410221561136253933256952257712543953 (hash-poseidon 1 2 3 4 5 6 7 8))

0 commit comments

Comments
 (0)