Skip to content

Commit 8e3d1f4

Browse files
committed
add hash-poseidon alias of poseidon-hash-hack-a-chain
1 parent 3751854 commit 8e3d1f4

File tree

7 files changed

+63
-11
lines changed

7 files changed

+63
-11
lines changed

docs/en/pact-functions.md

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

18181818

1819+
### hash-poseidon {#hash-poseidon}
1820+
1821+
*i* `integer` *j* `integer` *k* `integer` *l* `integer` *m* `integer` *n* `integer` *o* `integer` *p* `integer` *→* `integer`
1822+
1823+
1824+
Poseidon Hash Function. This is an alias of 'poseidon-hash-hack-a-chain'.
1825+
```lisp
1826+
pact> (hash-poseidon 1)
1827+
18586133768512220936620570745912940619677854269274689475585506675881198879027
1828+
pact> (hash-poseidon 1 2)
1829+
7853200120776062878684798364095072458815029376092732009249414926327459813530
1830+
pact> (hash-poseidon 1 2 3 4 5 6)
1831+
20400040500897583745843009878988256314335038853985262692600694741116813247201
1832+
pact> (hash-poseidon 1 2 3 4 5 6 7 8)
1833+
18604317144381847857886385684060986177838410221561136253933256952257712543953
1834+
```
1835+
1836+
18191837
### poseidon-hash-hack-a-chain {#poseidon-hash-hack-a-chain}
18201838

18211839
*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
@@ -244,6 +244,7 @@ defaultGasTable =
244244
,("pairing-check", 1)
245245

246246
,("poseidon-hash-hack-a-chain", 124)
247+
,("hash-poseidon", 124)
247248
,("hyperlane-message-id", 2)
248249
,("hyperlane-decode-token-message", 2)
249250
,("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: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,28 +1566,41 @@ 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)])
15771578
["(poseidon-hash-hack-a-chain 1)"
15781579
,"(poseidon-hash-hack-a-chain 1 2)"
15791580
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6)"
15801581
,"(poseidon-hash-hack-a-chain 1 2 3 4 5 6 7 8)"
15811582
]
15821583
"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
1584+
1585+
poseidonHashAliasDef :: NativeDef
1586+
poseidonHashAliasDef = defGasRNative
1587+
"hash-poseidon"
1588+
poseidonImpl
1589+
(funType tTyInteger [("i", tTyInteger), ("j", tTyInteger), ("k", tTyInteger), ("l", tTyInteger), ("m", tTyInteger), ("n", tTyInteger), ("o", tTyInteger), ("p", tTyInteger)])
1590+
["(hash-poseidon 1)"
1591+
,"(hash-poseidon 1 2)"
1592+
,"(hash-poseidon 1 2 3 4 5 6)"
1593+
,"(hash-poseidon 1 2 3 4 5 6 7 8)"
1594+
]
1595+
"Poseidon Hash Function. This is an alias of 'poseidon-hash-hack-a-chain'."
1596+
1597+
poseidonImpl :: RNativeFun e
1598+
poseidonImpl i as
1599+
| not (null as) && length as <= 8,
1600+
Just intArgs <- traverse (preview _TLitInteger) as
1601+
= computeGas' i (GPoseidonHashHackAChain $ length as) $
1602+
return $ toTerm $ poseidon intArgs
1603+
| otherwise = argsError i as
15911604

15921605
keccak256Def :: NativeDef
15931606
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)