diff --git a/Makefile b/Makefile index e97acbef23..36d2f48a0d 100644 --- a/Makefile +++ b/Makefile @@ -2,13 +2,18 @@ # with Go source code. If you know what GOPATH is then you probably # don't need to bother with make. -.PHONY: geth android ios evm all test clean +.PHONY: geth android ios evm all test clean mina GOBIN = ./build/bin GO ?= latest GORUN = env GO111MODULE=on go run -geth: +mina: + cd mina && cargo build --release + cp mina/target/mina.h mina/lib/mina.h + cp mina/target/release/libmina.a mina/lib/libmina.a + +geth: mina $(GORUN) build/ci.go install ./cmd/geth @echo "Done building." @echo "Run \"$(GOBIN)/geth\" to launch geth." diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 1b832b6386..04d4fdfab1 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -104,11 +104,17 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{ common.BytesToAddress([]byte{18}): &bls12381MapG2{}, } +var PrecompiledContractsMina = map[common.Address]PrecompiledContract{ + common.BytesToAddress([]byte{0x50}): &MinaHasher{}, + common.BytesToAddress([]byte{0x51}): &MinaSigner{}, +} + var ( PrecompiledAddressesBerlin []common.Address PrecompiledAddressesIstanbul []common.Address PrecompiledAddressesByzantium []common.Address PrecompiledAddressesHomestead []common.Address + PrecompiledAddressesMina []common.Address ) func init() { @@ -124,19 +130,24 @@ func init() { for k := range PrecompiledContractsBerlin { PrecompiledAddressesBerlin = append(PrecompiledAddressesBerlin, k) } + for k := range PrecompiledContractsMina { + PrecompiledAddressesMina = append(PrecompiledAddressesMina, k) + } } // ActivePrecompiles returns the precompiles enabled with the current configuration. func ActivePrecompiles(rules params.Rules) []common.Address { + result := PrecompiledAddressesMina + switch { case rules.IsBerlin: - return PrecompiledAddressesBerlin + return append(result, PrecompiledAddressesBerlin...) case rules.IsIstanbul: - return PrecompiledAddressesIstanbul + return append(result, PrecompiledAddressesIstanbul...) case rules.IsByzantium: - return PrecompiledAddressesByzantium + return append(result, PrecompiledAddressesByzantium...) default: - return PrecompiledAddressesHomestead + return append(result, PrecompiledAddressesHomestead...) } } diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go index b22d999e6c..d8c9dcf21e 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -65,6 +65,8 @@ var allPrecompiles = map[common.Address]PrecompiledContract{ common.BytesToAddress([]byte{16}): &bls12381Pairing{}, common.BytesToAddress([]byte{17}): &bls12381MapG1{}, common.BytesToAddress([]byte{18}): &bls12381MapG2{}, + common.BytesToAddress([]byte{0x50}): &MinaHasher{}, + common.BytesToAddress([]byte{0x51}): &MinaSigner{}, } // EIP-152 test vectors @@ -311,6 +313,8 @@ func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp", func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "10", t) } func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "11", t) } func TestPrecompiledBLS12381MapG2(t *testing.T) { testJson("blsMapG2", "12", t) } +func TestPrecompiledMinaHasher(t *testing.T) { testJson("minaHasher", "50", t) } +func TestPrecompiledMinaSigner(t *testing.T) { testJson("minaSigner", "51", t) } func BenchmarkPrecompiledBLS12381G1Add(b *testing.B) { benchJson("blsG1Add", "0a", b) } func BenchmarkPrecompiledBLS12381G1Mul(b *testing.B) { benchJson("blsG1Mul", "0b", b) } @@ -321,6 +325,8 @@ func BenchmarkPrecompiledBLS12381G2MultiExp(b *testing.B) { benchJson("blsG2Mult func BenchmarkPrecompiledBLS12381Pairing(b *testing.B) { benchJson("blsPairing", "10", b) } func BenchmarkPrecompiledBLS12381MapG1(b *testing.B) { benchJson("blsMapG1", "11", b) } func BenchmarkPrecompiledBLS12381MapG2(b *testing.B) { benchJson("blsMapG2", "12", b) } +func BenchmarkPrecompiledMinaHasher(b *testing.B) { benchJson("minaHasher", "50", b) } +func BenchmarkPrecompiledMinaSigner(b *testing.B) { benchJson("minaSigner", "51", b) } // Failure tests func TestPrecompiledBLS12381G1AddFail(t *testing.T) { testJsonFail("blsG1Add", "0a", t) } @@ -332,6 +338,8 @@ func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) { testJsonFail("blsG2Mu func TestPrecompiledBLS12381PairingFail(t *testing.T) { testJsonFail("blsPairing", "10", t) } func TestPrecompiledBLS12381MapG1Fail(t *testing.T) { testJsonFail("blsMapG1", "11", t) } func TestPrecompiledBLS12381MapG2Fail(t *testing.T) { testJsonFail("blsMapG2", "12", t) } +func TestPrecompiledMinaHasherFail(t *testing.T) { testJsonFail("minaHasher", "50", t) } +func TestPrecompiledMinaSignerFail(t *testing.T) { testJsonFail("minaSigner", "51", t) } func loadJson(name string) ([]precompiledTest, error) { data, err := os.ReadFile(fmt.Sprintf("testdata/precompiles/%v.json", name)) diff --git a/core/vm/evm.go b/core/vm/evm.go index dd55618bf8..dd873c4674 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -42,6 +42,10 @@ type ( ) func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { + if p, ok := PrecompiledContractsMina[addr]; ok { + return p, ok + } + var precompiles map[common.Address]PrecompiledContract switch { case evm.chainRules.IsBerlin: @@ -53,6 +57,7 @@ func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { default: precompiles = PrecompiledContractsHomestead } + p, ok := precompiles[addr] return p, ok } diff --git a/core/vm/mina_contracts.go b/core/vm/mina_contracts.go new file mode 100644 index 0000000000..084ad8ef1e --- /dev/null +++ b/core/vm/mina_contracts.go @@ -0,0 +1,175 @@ +package vm + +// Solidity interfaces for precompiles +// +// enum HashParameter { +// MAINNET, +// TESTNET, +// EMPTY +// } +// +// interface IHasher { +// function poseidonHash( +// HashParameter hashParameter, +// bytes32[] memory fields +// ) external view returns (bytes32); +// } +// +// interface ISigner { +// function verify( +// HashParameter hashParameter, +// bytes32 pubKeyX, +// bytes32 pubKeyY, +// bytes32 signatureRX, +// bytes32 signatureS, +// bytes32[] calldata fields +// ) external view returns (bool); +// } + +/* +#cgo LDFLAGS: ${SRCDIR}/../../mina/lib/libmina.a -ldl +#include "../../mina/lib/mina.h" +*/ +import "C" +import ( + "bytes" + "errors" + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/crypto" +) + +var sol_bool, _ = abi.NewType("bool", "", nil) +var sol_uint8, _ = abi.NewType("uint8", "", nil) +var sol_string, _ = abi.NewType("string", "", nil) +var sol_bytes32, _ = abi.NewType("bytes32", "", nil) +var sol_bytes32Arr, _ = abi.NewType("bytes32[]", "", nil) + +var revertSelector = crypto.Keccak256([]byte("Error(string)"))[:4] + +func packErr(message string) []byte { + bytes, err := abi.Arguments{{Type: sol_string}}.Pack(message) + + if err != nil { + return nil + } + + return append(revertSelector, bytes...) +} + +var ( + errMinaInvalidSignature = errors.New("invalid function signature") + errMinaCallingRustLibFailed = errors.New("calling rust library failed") +) + +type MinaHasher struct{} + +func (c *MinaHasher) RequiredGas(input []byte) uint64 { + return 1000 +} + +func poseidonHash(calldata []byte) ([]byte, error) { + unpacked, err := (abi.Arguments{{ + Type: sol_uint8}, // hashParameter + {Type: sol_bytes32Arr}, // fields + }).Unpack(calldata) + + if err != nil { + return packErr("Unable to unpack calldata"), err + } + + hashParameter := unpacked[0].(uint8) + fields := unpacked[1].([][32]uint8) + + output_buffer := [32]byte{} + + var fields_ptr *C.uint8_t + if len(fields) == 0 { + fields_ptr = (*C.uint8_t)(nil) + } else { + fields_ptr = (*C.uint8_t)(&fields[0][0]) + } + + if !C.poseidon( + C.uint8_t(hashParameter), + fields_ptr, + C.uintptr_t(len(fields)), + (*C.uint8_t)(&output_buffer[0]), + ) { + return packErr("Calling Poseidon hash failed"), errMinaCallingRustLibFailed + } + + return output_buffer[:], nil +} + +// 0x1f831f84 +var poseidonHashSignature = crypto.Keccak256([]byte("poseidonHash(uint8,bytes32[])"))[:4] + +func (c *MinaHasher) Run(input []byte) ([]byte, error) { + if len(input) < 4 || !bytes.Equal(input[:4], poseidonHashSignature) { + return packErr("Invalid signature"), errMinaInvalidSignature + } + + return poseidonHash(input[4:]) +} + +type MinaSigner struct{} + +func (c *MinaSigner) RequiredGas(input []byte) uint64 { + return 1000 +} + +// 0x462e39d6 +var verifySignature = crypto.Keccak256([]byte("verify(uint8,bytes32,bytes32,bytes32,bytes32,bytes32[])"))[:4] + +func (c *MinaSigner) Run(input []byte) ([]byte, error) { + if len(input) < 4 || !bytes.Equal(input[:4], verifySignature) { + return packErr("Invalid signature"), errMinaInvalidSignature + } + + calldata := input[4:] + + unpacked, err := (abi.Arguments{ + {Type: sol_uint8}, // hashParameter + {Type: sol_bytes32}, // pubKeyX + {Type: sol_bytes32}, // pubKeyY + {Type: sol_bytes32}, // signatureRX + {Type: sol_bytes32}, // signatureS + {Type: sol_bytes32Arr}, // fields + }).Unpack(calldata) + + if err != nil { + return packErr("Unable to unpack calldata"), err + } + + hashParameter := unpacked[0].(uint8) + pubKeyX := unpacked[1].([32]uint8) + pubKeyY := unpacked[2].([32]uint8) + signatureRX := unpacked[3].([32]uint8) + signatureS := unpacked[4].([32]uint8) + fields := unpacked[5].([][32]uint8) + + output_buffer := false + + var fields_ptr *C.uint8_t + if len(fields) == 0 { + fields_ptr = (*C.uint8_t)(nil) + } else { + fields_ptr = (*C.uint8_t)(&fields[0][0]) + } + + if !C.verify( + C.uint8_t(hashParameter), + (*C.uint8_t)(&pubKeyX[0]), + (*C.uint8_t)(&pubKeyY[0]), + (*C.uint8_t)(&signatureRX[0]), + (*C.uint8_t)(&signatureS[0]), + fields_ptr, + C.uintptr_t(len(fields)), + (*C.bool)(&output_buffer), + ) { + return packErr("Calling verify failed"), errMinaCallingRustLibFailed + } + + return abi.Arguments{{Type: sol_bool}}.Pack(output_buffer) +} diff --git a/core/vm/testdata/precompiles/fail-minaHasher.json b/core/vm/testdata/precompiles/fail-minaHasher.json new file mode 100644 index 0000000000..54f983b988 --- /dev/null +++ b/core/vm/testdata/precompiles/fail-minaHasher.json @@ -0,0 +1,22 @@ +[ + { + "Input": "aabbccdd", + "ExpectedError": "invalid function signature", + "Name": "mina_poseidon_invalid_signature" + }, + { + "Input": "1f831f84", + "ExpectedError": "abi: attempting to unmarshall an empty string while arguments are expected", + "Name": "mina_poseidon_invalid_calldata 1" + }, + { + "Input": "1f831f84aabbccdd", + "ExpectedError": "abi: cannot marshal in to go type: length insufficient 4 require 32", + "Name": "mina_poseidon_invalid_calldata 2" + }, + { + "Input": "1f831f84000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "ExpectedError": "calling rust library failed", + "Name": "mina_poseidon_invalid_field" + } +] diff --git a/core/vm/testdata/precompiles/fail-minaSigner.json b/core/vm/testdata/precompiles/fail-minaSigner.json new file mode 100644 index 0000000000..f6f0bcfd99 --- /dev/null +++ b/core/vm/testdata/precompiles/fail-minaSigner.json @@ -0,0 +1,22 @@ +[ + { + "Input": "aabbccdd", + "ExpectedError": "invalid function signature", + "Name": "mina_signer_invalid_signature" + }, + { + "Input": "462e39d6", + "ExpectedError": "abi: attempting to unmarshall an empty string while arguments are expected", + "Name": "mina_signer_invalid_calldata 1" + }, + { + "Input": "462e39d6aabbccdd", + "ExpectedError": "abi: cannot marshal in to go type: length insufficient 4 require 32", + "Name": "mina_signer_invalid_calldata 2" + }, + { + "Input": "462e39d60000000000000000000000000000000000000000000000000000000000000001ffb2abc6b0af14d8174f994590e37255f187c474ee385af4a399ad272e1eb60cf553e6e81fec4abc17cc21965c1991de96378b28754ab52dc758afe60afa1f0241197af9c449fcfe22fd770592997f157ef7c26eb33657897d077c71fdbb7f30bf2dc3c90732e136a85253dbb6db0c18823a5c573e70fa02c22eba7e04538c0100000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "ExpectedError": "calling rust library failed", + "Name": "mina_poseidon_invalid_field" + } +] diff --git a/core/vm/testdata/precompiles/minaHasher.json b/core/vm/testdata/precompiles/minaHasher.json new file mode 100644 index 0000000000..e812b8f92f --- /dev/null +++ b/core/vm/testdata/precompiles/minaHasher.json @@ -0,0 +1,44 @@ +[ + { + "Input": "1f831f84000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000", + "Expected": "a8eb9ee0f30046308abbfa5d20af73c81bbdabc25b459785024d045228bead2f", + "Name": "vector 1", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "1f831f84000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001f2eee8d8f6e5fb182c610cae6c5393fce69dc4d900e7b4923b074e54ad00fb36", + "Expected": "fb5992f65c07f9335995f43fd791d39012ad466717729e61045c297507054f3d", + "Name": "vector 2", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "1f831f84000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002bd3f1c8f183ceedea15080edbe79d30bd7d613b86bf2ba12007091c60ae3933765e4f04ab87706bab06d13c7eee0a7807d0b8ce268b4ece6aab1e0508ec9c42f", + "Expected": "fe2436f2027620a11233318b55d0a117086f09674826d1b7ce08d48ad0736c33", + "Name": "vector 3", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "1f831f84000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003f5ea61ce47773495363dc4f6a41c3e2da14b13d6dd173acf87c9ca7357fb2400f28573f49c658b4ba151e82ed0bd6aaab045311d1a72df58c21eed462bede01873cf45c39285f17ccea99e0daeb547430cf7921218fe3726010f608e682a841a", + "Expected": "9b1b94444a54af49a7623d1fe1ca72649f0a098daf5704925f024eb6ab0e4b3f", + "Name": "vector 4", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "1f831f840000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000044c28b87198e0012207f93cdbdaa35355ec8213fa97a60e62701f62602d4659200787a40fc046c4dd0ff3cad0e54006577fece871c774707494984f1c7d3347271504ffe48e4e6dfcc4ded439edd386cf271b69d94afae83079f3ee3e7c04d52d290b6506516fe7588b5100f8db2e871427c6d74e7a60ab656f43dd9bc687c312", + "Expected": "47ecd3bf2eed86dcf8d2cef3d7667104689dba4d9bcb54006e0c66f6ec8c5a16", + "Name": "vector 5", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "1f831f84000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000005da99182b35f2cd9f8a137052c4262576377a16deb83652db459a74893a0cf73c9805573990c4028292c9db171cd2b97902f9fc494983f6f7e0a0c184bc55df1b90ff1001b9dab21358aad1f6b7906a56d0c039502c1590c3ef9921a8951e440988b56238a0eda34576db959fecd1c3790bb5311fdb231753243c5085974a5b37896a7727e511a4c30d99082bf3542623fb702afab0b62ebbf301ed51e38f6812", + "Expected": "09a2d55277908b7c8214f745b3605f0f9055dcd4c9b594cdd759292c34c3a20c", + "Name": "vector 6", + "Gas": 1000, + "NoBenchmark": false + } +] diff --git a/core/vm/testdata/precompiles/minaSigner.json b/core/vm/testdata/precompiles/minaSigner.json new file mode 100644 index 0000000000..bcb134de67 --- /dev/null +++ b/core/vm/testdata/precompiles/minaSigner.json @@ -0,0 +1,44 @@ +[ + { + "Input": "462e39d60000000000000000000000000000000000000000000000000000000000000001ffb2abc6b0af14d8174f994590e37255f187c474ee385af4a399ad272e1eb60cf553e6e81fec4abc17cc21965c1991de96378b28754ab52dc758afe60afa1f0241197af9c449fcfe22fd770592997f157ef7c26eb33657897d077c71fdbb7f30bf2dc3c90732e136a85253dbb6db0c18823a5c573e70fa02c22eba7e04538c0100000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000147e64adc9759307d1472d1fe8b9340d67d56b79cea7cfafb04b5647229968809", + "Expected": "0000000000000000000000000000000000000000000000000000000000000001", + "Name": "vector 1", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "462e39d60000000000000000000000000000000000000000000000000000000000000001b4286e40a0cfd61dd7457433adfd13d229f8ea277faef68ad6a2ab15a1bf910fd090cf75ab20da968fc2642f5b1545b819c563d14106a943fcce4bab0b2458177e7f89d308cd9fe434621eef98ad4e3fa42461ab4acf39d6051a862793c88c239940148992baff75f41e1c4a1cc13596cfdbc71bc9043cc8deb4f731df3ba13a00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000041b856ed3cd4b1c194835c47d194ed90dc8cc18eb61825efc53ce72fe8451d006488f11b7917a37b2dc8d54c4aa8bb09ebcedeca1d9aa4bb790dd5710eedf061e5c2e11fac12b2dff35c8bda983880c18084f74b2bd150a6eaf88d9ea9492bd063ae94f5a90f0ccc35b40b91cc1770e97ff951679c9a497334521ea0ea48be32f", + "Expected": "0000000000000000000000000000000000000000000000000000000000000001", + "Name": "vector 2", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "462e39d60000000000000000000000000000000000000000000000000000000000000001bf2e7149abba9571f50fcb32c3486af9517dfcc8474909adc3b6cbd2245cf61cbb42e2b30e3e3b8c3cde48299bbd20195cc207e95fba1bb2d68fe79f8ff78931749b0b525a821c5d6393edace3c82a908e91c878a4264eea6710d6924b217a3c7a1df4449137deb4d21b766333358957676cb3dcad1a5a940a7d3eaf33cf1f1600000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000003a359b9b2c05816f11df3b83c11f67c7111bfc82297afcf1d419964a2afc06f3bba9bab58485eebaed5a8192c373a3967a46b70d5a9d141bb3d1ac61422045622af9f76a9f9237d3adb7fd7c4fd3bd3620060578395180c41079292e2b2e10621", + "Expected": "0000000000000000000000000000000000000000000000000000000000000001", + "Name": "vector 3", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "462e39d60000000000000000000000000000000000000000000000000000000000000001bf2e7149abba9571f50fcb32c3486af9517dfcc8474909adc3b6cbd2245cf61cbb42e2b30e3e3b8c3cde48299bbd20195cc207e95fba1bb2d68fe79f8ff78931749b0b525a821c5d6393edace3c82a908e91c878a4264eea6710d6924b217a3c7a1df4449137deb4d21b766333358957676cb3dcad1a5a940a7d3eaf33cf1f1600000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000002a359b9b2c05816f11df3b83c11f67c7111bfc82297afcf1d419964a2afc06f3bba9bab58485eebaed5a8192c373a3967a46b70d5a9d141bb3d1ac61422045622", + "Expected": "0000000000000000000000000000000000000000000000000000000000000000", + "Name": "vector 4", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "462e39d60000000000000000000000000000000000000000000000000000000000000001b4286e40a0cfd61dd7457433adfd13d229f8ea277faef68ad6a2ab15a1bf910fbb42e2b30e3e3b8c3cde48299bbd20195cc207e95fba1bb2d68fe79f8ff78931749b0b525a821c5d6393edace3c82a908e91c878a4264eea6710d6924b217a3c7a1df4449137deb4d21b766333358957676cb3dcad1a5a940a7d3eaf33cf1f1600000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000003a359b9b2c05816f11df3b83c11f67c7111bfc82297afcf1d419964a2afc06f3bba9bab58485eebaed5a8192c373a3967a46b70d5a9d141bb3d1ac61422045622af9f76a9f9237d3adb7fd7c4fd3bd3620060578395180c41079292e2b2e10621", + "Expected": "0000000000000000000000000000000000000000000000000000000000000000", + "Name": "vector 5", + "Gas": 1000, + "NoBenchmark": false + }, + { + "Input": "462e39d60000000000000000000000000000000000000000000000000000000000000001bf2e7149abba9571f50fcb32c3486af9517dfcc8474909adc3b6cbd2245cf61cbb42e2b30e3e3b8c3cde48299bbd20195cc207e95fba1bb2d68fe79f8ff78931749b0b525a821c5d6393edace3c82a908e91c878a4264eea6710d6924b217a3cbf2dc3c90732e136a85253dbb6db0c18823a5c573e70fa02c22eba7e04538c0100000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000003a359b9b2c05816f11df3b83c11f67c7111bfc82297afcf1d419964a2afc06f3bba9bab58485eebaed5a8192c373a3967a46b70d5a9d141bb3d1ac61422045622af9f76a9f9237d3adb7fd7c4fd3bd3620060578395180c41079292e2b2e10621", + "Expected": "0000000000000000000000000000000000000000000000000000000000000000", + "Name": "vector 6", + "Gas": 1000, + "NoBenchmark": false + } +] diff --git a/mina/.gitignore b/mina/.gitignore new file mode 100644 index 0000000000..064a109ecf --- /dev/null +++ b/mina/.gitignore @@ -0,0 +1,2 @@ +/target +/lib/libmina.a diff --git a/mina/Cargo.lock b/mina/Cargo.lock new file mode 100644 index 0000000000..7b1e9baa17 --- /dev/null +++ b/mina/Cargo.lock @@ -0,0 +1,1185 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + +[[package]] +name = "ark-ec" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dea978406c4b1ca13c2db2373b05cc55429c3575b8b21f1b9ee859aa5b03dd42" +dependencies = [ + "ark-ff", + "ark-serialize", + "ark-std", + "derivative", + "num-traits", + "rayon", + "zeroize", +] + +[[package]] +name = "ark-ff" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b3235cc41ee7a12aaaf2c575a2ad7b46713a8a50bda2fc3b003a04845c05dd6" +dependencies = [ + "ark-ff-asm", + "ark-ff-macros", + "ark-serialize", + "ark-std", + "derivative", + "num-bigint", + "num-traits", + "paste", + "rayon", + "rustc_version", + "zeroize", +] + +[[package]] +name = "ark-ff-asm" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db02d390bf6643fb404d3d22d31aee1c4bc4459600aef9113833d17e786c6e44" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-ff-macros" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fd794a08ccb318058009eefdf15bcaaaaf6f8161eb3345f907222bac38b20" +dependencies = [ + "num-bigint", + "num-traits", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-poly" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b0f78f47537c2f15706db7e98fe64cc1711dbf9def81218194e17239e53e5aa" +dependencies = [ + "ark-ff", + "ark-serialize", + "ark-std", + "derivative", + "hashbrown 0.11.2", + "rayon", +] + +[[package]] +name = "ark-serialize" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6c2b318ee6e10f8c2853e73a83adc0ccb88995aa978d8a3408d492ab2ee671" +dependencies = [ + "ark-serialize-derive", + "ark-std", + "digest 0.9.0", +] + +[[package]] +name = "ark-serialize-derive" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dd4e5f0bf8285d5ed538d27fab7411f3e297908fd93c62195de8bee3f199e82" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-std" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df2c09229cbc5a028b1d70e00fdb2acee28b1055dfb5ca73eea49c5a25c4e7c" +dependencies = [ + "num-traits", + "rand", + "rayon", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bcs" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bd3ffe8b19a604421a5d461d4a70346223e535903fbc3067138bddbebddcf77" +dependencies = [ + "serde", + "thiserror", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest 0.10.6", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bs58" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" + +[[package]] +name = "cbindgen" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6358dedf60f4d9b8db43ad187391afe959746101346fe51bb978126bec61dfb" +dependencies = [ + "clap", + "heck", + "indexmap", + "log", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn 1.0.109", + "tempfile", + "toml", +] + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "3.2.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" +dependencies = [ + "atty", + "bitflags", + "clap_lex", + "indexmap", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "cpufeatures" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "280a9f2d8b3a38871a3c8a46fb80db65e5e5ed97da80c4d08bf27fb63e35e181" +dependencies = [ + "libc", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf2b3e8478797446514c91ef04bafcb59faba183e621ad488df88983cc14128c" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46bd5f3f85273295a9d14aedfb86f6aadbff6d8f5295c4a9edb08e819dcf5695" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "darling" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" +dependencies = [ + "darling_core", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "digest" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + +[[package]] +name = "errno" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "fastrand" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +dependencies = [ + "instant", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] + +[[package]] +name = "hermit-abi" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "io-lifetimes" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" +dependencies = [ + "hermit-abi 0.3.1", + "libc", + "windows-sys", +] + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + +[[package]] +name = "libc" +version = "0.2.141" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" + +[[package]] +name = "linux-raw-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memoffset" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" +dependencies = [ + "autocfg", +] + +[[package]] +name = "mina" +version = "0.1.0" +dependencies = [ + "cbindgen", + "mina-hasher", + "mina-signer", + "num-bigint", + "o1-utils", + "serde", + "serde_json", +] + +[[package]] +name = "mina-curves" +version = "0.1.0" +source = "git+https://github.com/o1-labs/proof-systems?tag=0.1.0#2e7f353719bdcb90f1f346f796b8ea69a3c30ec5" +dependencies = [ + "ark-ec", + "ark-ff", +] + +[[package]] +name = "mina-hasher" +version = "0.1.0" +source = "git+https://github.com/o1-labs/proof-systems?tag=0.1.0#2e7f353719bdcb90f1f346f796b8ea69a3c30ec5" +dependencies = [ + "ark-ff", + "bitvec", + "mina-curves", + "mina-poseidon", + "o1-utils", + "serde", +] + +[[package]] +name = "mina-poseidon" +version = "0.1.0" +source = "git+https://github.com/o1-labs/proof-systems?tag=0.1.0#2e7f353719bdcb90f1f346f796b8ea69a3c30ec5" +dependencies = [ + "ark-ec", + "ark-ff", + "ark-poly", + "mina-curves", + "o1-utils", + "once_cell", + "rand", + "rayon", + "serde", + "serde_with", +] + +[[package]] +name = "mina-signer" +version = "0.1.0" +source = "git+https://github.com/o1-labs/proof-systems?tag=0.1.0#2e7f353719bdcb90f1f346f796b8ea69a3c30ec5" +dependencies = [ + "ark-ec", + "ark-ff", + "bitvec", + "blake2", + "bs58", + "hex", + "mina-curves", + "mina-hasher", + "o1-utils", + "rand", + "sha2", + "thiserror", +] + +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", + "rand", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" +dependencies = [ + "hermit-abi 0.2.6", + "libc", +] + +[[package]] +name = "o1-utils" +version = "0.1.0" +source = "git+https://github.com/o1-labs/proof-systems?tag=0.1.0#2e7f353719bdcb90f1f346f796b8ea69a3c30ec5" +dependencies = [ + "ark-ec", + "ark-ff", + "ark-poly", + "ark-serialize", + "bcs", + "hex", + "num-bigint", + "num-integer", + "num-traits", + "rand", + "rand_core", + "rayon", + "serde", + "serde_with", + "sha2", + "thiserror", +] + +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + +[[package]] +name = "os_str_bytes" +version = "6.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" + +[[package]] +name = "paste" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" + +[[package]] +name = "pest" +version = "2.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1403e8401ad5dedea73c626b99758535b342502f8d1e361f4a2dd952749122" +dependencies = [ + "thiserror", + "ucd-trie", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rayon" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + +[[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustc_version" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "0.37.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aae838e49b3d63e9274e1c01833cc8139d3fec468c3b84688c628f44b1ae11d" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "semver" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" +dependencies = [ + "pest", +] + +[[package]] +name = "serde" +version = "1.0.160" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.160" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.13", +] + +[[package]] +name = "serde_json" +version = "1.0.96" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_with" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678b5a069e50bf00ecd22d0cd8ddf7c236f68581b03db652061ed5eb13a312ff" +dependencies = [ + "serde", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest 0.10.6", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "tempfile" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" +dependencies = [ + "cfg-if", + "fastrand", + "redox_syscall", + "rustix", + "windows-sys", +] + +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + +[[package]] +name = "thiserror" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.13", +] + +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + +[[package]] +name = "ucd-trie" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" + +[[package]] +name = "unicode-ident" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + +[[package]] +name = "zeroize" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.13", +] diff --git a/mina/Cargo.toml b/mina/Cargo.toml new file mode 100644 index 0000000000..4049088cdc --- /dev/null +++ b/mina/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "mina" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["staticlib"] + +[dependencies] +mina-hasher = { git = "https://github.com/o1-labs/proof-systems", tag = "0.1.0", version = "0.1.0" } +mina-signer = { git = "https://github.com/o1-labs/proof-systems", tag = "0.1.0", version = "0.1.0" } +o1-utils = { git = "https://github.com/o1-labs/proof-systems", tag = "0.1.0", version = "0.1.0" } + +[build-dependencies] +cbindgen = "0.24.3" + +[dev-dependencies] +num-bigint = "0.4.3" +serde = "1.0.160" +serde_json = "1.0.96" diff --git a/mina/build.rs b/mina/build.rs new file mode 100644 index 0000000000..6677fa7921 --- /dev/null +++ b/mina/build.rs @@ -0,0 +1,35 @@ +extern crate cbindgen; + +use cbindgen::Config; +use std::env; +use std::path::PathBuf; + +fn main() { + let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + + let package_name = env::var("CARGO_PKG_NAME").unwrap(); + let output_file = target_dir() + .join(format!("{}.h", package_name)) + .display() + .to_string(); + + let config = Config { + language: cbindgen::Language::C, + ..Default::default() + }; + + cbindgen::generate_with_config(crate_dir, config) + .unwrap() + .write_to_file(output_file); +} + +/// Find the location of the `target/` directory. Note that this may be +/// overridden by `cmake`, so we also need to check the `CARGO_TARGET_DIR` +/// variable. +fn target_dir() -> PathBuf { + if let Ok(target) = env::var("CARGO_TARGET_DIR") { + PathBuf::from(target) + } else { + PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target") + } +} diff --git a/mina/lib/mina.h b/mina/lib/mina.h new file mode 100644 index 0000000000..84b5f591f9 --- /dev/null +++ b/mina/lib/mina.h @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +#define FIELD_SIZE 32 + +/** + * * # Safety * this functions accepts raw pointer from golang + */ +bool poseidon(uint8_t network_id, + const uint8_t *field_ptr, + uintptr_t field_len, + uint8_t *output_ptr); + +/** + * * # Safety * this functions accepts raw pointer from golang + */ +bool verify(uint8_t network_id, + const uint8_t *pubkey_x, + const uint8_t *pubkey_y, + const uint8_t *sig_rx, + const uint8_t *sig_s, + const uint8_t *field_ptr, + uintptr_t field_len, + bool *output_ptr); diff --git a/mina/src/lib.rs b/mina/src/lib.rs new file mode 100644 index 0000000000..967c596afd --- /dev/null +++ b/mina/src/lib.rs @@ -0,0 +1,271 @@ +mod mina; + +use std::array::TryFromSliceError; + +use mina::{HashParameter, Message}; +use mina_signer::{BaseField, CurvePoint, PubKey, ScalarField, Signature}; +use o1_utils::FieldHelpers; + +pub const FIELD_SIZE: usize = 32; + +/** + * # Safety + * this functions accepts raw pointer from golang + */ +#[no_mangle] +pub unsafe extern "C" fn poseidon( + network_id: u8, + field_ptr: *const u8, + field_len: usize, + output_ptr: *mut u8, // 32 bytes +) -> bool { + if (field_ptr.is_null() && field_len != 0) || output_ptr.is_null() { + return false; + } + + let network_id = match network_id { + 0x00 => HashParameter::Mainnet, + 0x01 => HashParameter::Testnet, + 0x02 => HashParameter::Empty, + _ => return false, + }; + + let fields = unsafe { std::slice::from_raw_parts(field_ptr, field_len * FIELD_SIZE) }; + + let fields = match fields + .chunks(FIELD_SIZE) + .map(|chunk| chunk[..32].try_into()) + .collect::, TryFromSliceError>>() + { + Ok(fields) => fields, + Err(_) => return false, + }; + + let msg = match Message::from_bytes_slice(&fields) { + Ok(msg) => msg, + Err(_) => return false, + }; + + let hash = mina::poseidon(&msg, network_id); + + let output = unsafe { std::slice::from_raw_parts_mut(output_ptr, FIELD_SIZE) }; + + output.copy_from_slice(&hash.to_bytes()); + + true +} + +/** + * # Safety + * this functions accepts raw pointer from golang + */ +#[no_mangle] +pub unsafe extern "C" fn verify( + network_id: u8, + pubkey_x: *const u8, + pubkey_y: *const u8, + sig_rx: *const u8, + sig_s: *const u8, + field_ptr: *const u8, + field_len: usize, + output_ptr: *mut bool, +) -> bool { + if pubkey_x.is_null() + || pubkey_y.is_null() + || sig_rx.is_null() + || sig_s.is_null() + || (field_ptr.is_null() && field_len != 0) + || output_ptr.is_null() + { + return false; + } + + let network_id = match network_id { + 0x00 => HashParameter::Mainnet, + 0x01 => HashParameter::Testnet, + 0x02 => HashParameter::Empty, + _ => return false, + }; + + let pubkey_x = unsafe { std::slice::from_raw_parts(pubkey_x, FIELD_SIZE) }; + let pubkey_y = unsafe { std::slice::from_raw_parts(pubkey_y, FIELD_SIZE) }; + + let pubkey = PubKey::from_point_unsafe(CurvePoint::new( + match BaseField::from_bytes(pubkey_x) { + Ok(x) => x, + Err(_) => return false, + }, + match BaseField::from_bytes(pubkey_y) { + Ok(y) => y, + Err(_) => return false, + }, + false, + )); + + let sig_rx = unsafe { std::slice::from_raw_parts(sig_rx, FIELD_SIZE) }; + let sig_s = unsafe { std::slice::from_raw_parts(sig_s, FIELD_SIZE) }; + + let signature = Signature::new( + match BaseField::from_bytes(sig_rx) { + Ok(rx) => rx, + Err(_) => return false, + }, + match ScalarField::from_bytes(sig_s) { + Ok(s) => s, + Err(_) => return false, + }, + ); + + let fields = unsafe { std::slice::from_raw_parts(field_ptr, field_len * FIELD_SIZE) }; + + let fields = match fields + .chunks(FIELD_SIZE) + .map(|chunk| chunk[..32].try_into()) + .collect::, TryFromSliceError>>() + { + Ok(fields) => fields, + Err(_) => return false, + }; + + let msg = match Message::from_bytes_slice(&fields) { + Ok(msg) => msg, + Err(_) => return false, + }; + + let result = mina::verify(&signature, &pubkey, &msg, network_id); + + unsafe { *output_ptr = result }; + + true +} + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use super::*; + use num_bigint::BigUint; + use serde::Deserialize; + + #[derive(Debug, Deserialize)] + struct PoseidonTestVector { + input: Vec, + output: String, + } + + #[derive(Debug, Deserialize)] + struct PoseidonTestVectors { + test_vectors: Vec, + } + + #[test] + fn poseidon_test_vectors() { + let test_vectors: PoseidonTestVectors = + serde_json::from_str(include_str!("test/poseidon_test_vectors.json")).unwrap(); + + for test_vector in test_vectors.test_vectors { + let mut output = [0u8; 32]; + + let input = test_vector + .input + .iter() + .flat_map(|input| BaseField::from_hex(input).unwrap().to_bytes()) + .collect::>(); + + unsafe { + assert!(poseidon( + 0x02, + input.as_ptr(), + test_vector.input.len(), + output.as_mut_ptr() + )) + }; + + assert_eq!( + BaseField::from_bytes(&output).unwrap().to_hex(), + test_vector.output + ); + } + } + + #[derive(Debug, Deserialize)] + struct SignerTestVector { + pub_key_x: String, + pub_key_y: String, + sig_rx: String, + sig_s: String, + fields: Vec, + output: bool, + } + + #[derive(Debug, Deserialize)] + struct SignerTestVectors { + test_vectors: Vec, + } + + #[test] + fn test_signer() { + let test_vectors: SignerTestVectors = + serde_json::from_str(include_str!("test/signer_test_vectors.json")).unwrap(); + + for test_vector in test_vectors.test_vectors { + let mut output = false; + + let pub_key_x = + BaseField::from_biguint(&BigUint::from_str(&test_vector.pub_key_x).unwrap()) + .unwrap() + .to_bytes(); + let pub_key_y = + BaseField::from_biguint(&BigUint::from_str(&test_vector.pub_key_y).unwrap()) + .unwrap() + .to_bytes(); + let sig_rx = BaseField::from_biguint(&BigUint::from_str(&test_vector.sig_rx).unwrap()) + .unwrap() + .to_bytes(); + let sig_s = ScalarField::from_biguint(&BigUint::from_str(&test_vector.sig_s).unwrap()) + .unwrap() + .to_bytes(); + let fields = test_vector + .fields + .iter() + .flat_map(|input| { + BaseField::from_biguint(&BigUint::from_str(input).unwrap()) + .unwrap() + .to_bytes() + }) + .collect::>(); + + unsafe { + assert!(verify( + 0x01, + pub_key_x.as_ptr(), + pub_key_y.as_ptr(), + sig_rx.as_ptr(), + sig_s.as_ptr(), + fields.as_ptr(), + test_vector.fields.len(), + &mut output + )) + }; + + assert_eq!(output, test_vector.output); + } + } + + #[test] + fn null_pointer() { + unsafe { + assert!(!poseidon(0x00, std::ptr::null(), 1, std::ptr::null_mut())); + assert!(!verify( + 0x00, + std::ptr::null(), + std::ptr::null(), + std::ptr::null(), + std::ptr::null(), + std::ptr::null(), + 0, + std::ptr::null_mut() + )); + } + } +} diff --git a/mina/src/mina.rs b/mina/src/mina.rs new file mode 100644 index 0000000000..2883bd893b --- /dev/null +++ b/mina/src/mina.rs @@ -0,0 +1,76 @@ +use mina_hasher::{DomainParameter, Hashable, Hasher, ROInput}; +use mina_signer::{BaseField, PubKey, Signature, Signer}; +use o1_utils::{field_helpers::FieldHelpersError, FieldHelpers}; + +#[derive(Debug, Clone)] +#[repr(C)] +pub enum HashParameter { + Mainnet = 0x00, + Testnet = 0x01, + Empty = 0x02, + TransactionCommitment = 0x03, +} + +impl From for u8 { + fn from(id: HashParameter) -> u8 { + id as u8 + } +} + +impl DomainParameter for HashParameter { + fn into_bytes(self) -> Vec { + vec![self as u8] + } +} + +#[derive(Clone)] +pub struct Message { + pub fields: Vec, +} + +impl Message { + pub fn from_bytes_slice(fields_bytes: &[[u8; 32]]) -> Result { + Ok(Self { + fields: fields_bytes + .iter() + .map(|bytes| BaseField::from_bytes(bytes)) + .collect::, FieldHelpersError>>()?, + }) + } +} + +impl Hashable for Message { + type D = HashParameter; + + fn to_roinput(&self) -> ROInput { + self.fields + .iter() + .fold(ROInput::new(), |roi, field| roi.append_field(*field)) + } + + fn domain_string(network_id: HashParameter) -> Option { + match network_id { + HashParameter::Mainnet => "MinaSignatureMainnet".to_string().into(), + HashParameter::Testnet => "CodaSignature".to_string().into(), + HashParameter::TransactionCommitment => "MinaAcctUpdateCons".to_string().into(), + HashParameter::Empty => None, + } + } +} + +pub fn poseidon(msg: &Message, network_id: HashParameter) -> BaseField { + let mut hasher = mina_hasher::create_kimchi::(network_id); + + hasher.hash(msg) +} + +pub fn verify( + signature: &Signature, + pubkey: &PubKey, + msg: &Message, + network_id: HashParameter, +) -> bool { + let mut signer = mina_signer::create_kimchi::(network_id); + + signer.verify(signature, pubkey, msg) +} diff --git a/mina/src/test/poseidon_test_vectors.json b/mina/src/test/poseidon_test_vectors.json new file mode 100644 index 0000000000..1212c72421 --- /dev/null +++ b/mina/src/test/poseidon_test_vectors.json @@ -0,0 +1,47 @@ +{ + "name": "kimchi", + "test_vectors": [ + { + "input": [], + "output": "a8eb9ee0f30046308abbfa5d20af73c81bbdabc25b459785024d045228bead2f" + }, + { + "input": ["f2eee8d8f6e5fb182c610cae6c5393fce69dc4d900e7b4923b074e54ad00fb36"], + "output": "fb5992f65c07f9335995f43fd791d39012ad466717729e61045c297507054f3d" + }, + { + "input": [ + "bd3f1c8f183ceedea15080edbe79d30bd7d613b86bf2ba12007091c60ae39337", + "65e4f04ab87706bab06d13c7eee0a7807d0b8ce268b4ece6aab1e0508ec9c42f" + ], + "output": "fe2436f2027620a11233318b55d0a117086f09674826d1b7ce08d48ad0736c33" + }, + { + "input": [ + "f5ea61ce47773495363dc4f6a41c3e2da14b13d6dd173acf87c9ca7357fb2400", + "f28573f49c658b4ba151e82ed0bd6aaab045311d1a72df58c21eed462bede018", + "73cf45c39285f17ccea99e0daeb547430cf7921218fe3726010f608e682a841a" + ], + "output": "9b1b94444a54af49a7623d1fe1ca72649f0a098daf5704925f024eb6ab0e4b3f" + }, + { + "input": [ + "4c28b87198e0012207f93cdbdaa35355ec8213fa97a60e62701f62602d465920", + "0787a40fc046c4dd0ff3cad0e54006577fece871c774707494984f1c7d334727", + "1504ffe48e4e6dfcc4ded439edd386cf271b69d94afae83079f3ee3e7c04d52d", + "290b6506516fe7588b5100f8db2e871427c6d74e7a60ab656f43dd9bc687c312" + ], + "output": "47ecd3bf2eed86dcf8d2cef3d7667104689dba4d9bcb54006e0c66f6ec8c5a16" + }, + { + "input": [ + "da99182b35f2cd9f8a137052c4262576377a16deb83652db459a74893a0cf73c", + "9805573990c4028292c9db171cd2b97902f9fc494983f6f7e0a0c184bc55df1b", + "90ff1001b9dab21358aad1f6b7906a56d0c039502c1590c3ef9921a8951e4409", + "88b56238a0eda34576db959fecd1c3790bb5311fdb231753243c5085974a5b37", + "896a7727e511a4c30d99082bf3542623fb702afab0b62ebbf301ed51e38f6812" + ], + "output": "09a2d55277908b7c8214f745b3605f0f9055dcd4c9b594cdd759292c34c3a20c" + } + ] +} diff --git a/mina/src/test/signer_test_vectors.json b/mina/src/test/signer_test_vectors.json new file mode 100644 index 0000000000..0411762e22 --- /dev/null +++ b/mina/src/test/signer_test_vectors.json @@ -0,0 +1,72 @@ +{ + "test_vectors": [ + { + "pub_key_x": "5749528645515407352164834780408435992604865487588794690727207227268864389887", + "pub_key_y": "961123686654787572379844019641929632619233203965678030693512890434590233589", + "sig_rx": "21936703768608309712712286678384212721667043165509057046476743727602317531457", + "sig_s": "700244403784750329046343352585969728434513601583367350022979202621496307135", + "fields": ["4312143217416119453149054021680295703515035791870532431847656061436158469703"], + "output": true + }, + { + "pub_key_x": "7042208129527677077861348888492606461956846197792817644260213199747289065652", + "pub_key_y": "10558926836583110251042002010080745706359112066577139056638753279976447906000", + "sig_rx": "16079692606027161177880995922609222716277493656983184336617842262008786419582", + "sig_s": "26519020815623540666020017924931679489835794248279750040849654719218653085849", + "fields": [ + "3081943907937102890466321349086512568672738229687175831440974884688704210203", + "13581532047510445467277570462258108561396477004644819735095763123534482739016", + "3048822856514015646580881400214404144660523787797871598232610379373617426012", + "21660741932862358360736636215811102042484175487489560833468895032615023667514" + ], + "output": true + }, + { + "pub_key_x": "13100040091688310466504835329210079551770129473582830476827447762924491321023", + "pub_key_y": "22407096231914635758535978488910840638440068134091954456802730574700708250299", + "sig_rx": "27354556051988045756232951201922658662417395660561896688145248213905143733108", + "sig_s": "10007084982746413426116958072671171608413225743556423925802350318023121051002", + "fields": [ + "26883907960994343248721672123600111927912732275633851101666934083556914846115", + "15530614225213312579256518680413347727953796927384451546410690799125628951482", + "14938482801295869243888529448769259929444998690971476813418451846255531696047" + ], + "output": true + }, + { + "pub_key_x": "13100040091688310466504835329210079551770129473582830476827447762924491321023", + "pub_key_y": "22407096231914635758535978488910840638440068134091954456802730574700708250299", + "sig_rx": "27354556051988045756232951201922658662417395660561896688145248213905143733108", + "sig_s": "10007084982746413426116958072671171608413225743556423925802350318023121051002", + "fields": [ + "26883907960994343248721672123600111927912732275633851101666934083556914846115", + "15530614225213312579256518680413347727953796927384451546410690799125628951482" + ], + "output": false + }, + { + "pub_key_x": "7042208129527677077861348888492606461956846197792817644260213199747289065652", + "pub_key_y": "22407096231914635758535978488910840638440068134091954456802730574700708250299", + "sig_rx": "27354556051988045756232951201922658662417395660561896688145248213905143733108", + "sig_s": "10007084982746413426116958072671171608413225743556423925802350318023121051002", + "fields": [ + "26883907960994343248721672123600111927912732275633851101666934083556914846115", + "15530614225213312579256518680413347727953796927384451546410690799125628951482", + "14938482801295869243888529448769259929444998690971476813418451846255531696047" + ], + "output": false + }, + { + "pub_key_x": "13100040091688310466504835329210079551770129473582830476827447762924491321023", + "pub_key_y": "22407096231914635758535978488910840638440068134091954456802730574700708250299", + "sig_rx": "27354556051988045756232951201922658662417395660561896688145248213905143733108", + "sig_s": "700244403784750329046343352585969728434513601583367350022979202621496307135", + "fields": [ + "26883907960994343248721672123600111927912732275633851101666934083556914846115", + "15530614225213312579256518680413347727953796927384451546410690799125628951482", + "14938482801295869243888529448769259929444998690971476813418451846255531696047" + ], + "output": false + } + ] +}