Download the PHP package l2iterative/bonsai-sdk-php without Composer
On this page you can find all versions of the php package l2iterative/bonsai-sdk-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package bonsai-sdk-php
PHP SDK for Bonsai API
The current Bonsai API implementation is written in Rust. Since Bonsai requires an API key and would consume the quotas to generate the zero-knowledge proof, web3 applications built with Bonsai would need a backend.
Like most web3 applications, the backend can be minimalistic, because most of the on-chain interactions can be completed through, for example, Metamask's Ethereum provider API, in the frontend.
How would the backend look like?
For the aforementioned reasons, backends often fall into the following six classes:
- Pure:
- A pure Typescript implementation, probably with the use of WebAssembly, often implemented through Rust (see wasm-bindgen)
- A pure PHP implementation
- A pure Python implementation, such as Django and Flask
- With foreign function interface (FFI):
- A Typescript implementation with FFI to high-performance code likely written in Rust, through ffi-rs or koffi
- A PHP implementation using FFI, probably through the FFI extension
- A Python implementation using FFI, probably through CFFI
Numerous factors will affect which one to go:
- Availability of web hosting providers.
- There are many affordable web hosting platforms for Typescript and PHP.
- To run Python, the tradition is to use a full-fledged cloud virtual machine (such as an AWS EC2 instance). There is a preference to avoid using a full-fledged VM due to cost and ability to auto-scale.
- Performance.
- Everyone would agree that Python is slow.
- PHP can be optimized through the use of OPcache to precompile the script, but it is not machine code. Typescript can be compiled (together with WebAssembly) into machine code through v8, which we can expect to have the best performance among the "pure" group.
- Nevertheless, it becomes less relevant when we can use FFI because one can also run machine code, and unlike code generated by just-in-time compilation (JIT), such code is ahead-of-time compilation (AOT), and is excepted to be more performant and should be the go-to whenever available.
- Developer friendliness.
- Probably most important: ideally with few learning cycles, no hacking around.
- Rust is useful here, as RISC Zero programs are now written in Rust, and WebAssembly from Rust has been used in web development, particularly for cryptography.
- The other programming language is better similar to Rust. Among all the three, PHP is most similar. PHP is known for very friendly to beginners.
- All there have available Web3 SDK implementation, but Typescript has best support nowadays.
It is important to know that this backend is going to be minimalistic. We have the backend because the API key cannot be exposed to the client. The backend may just consist of:
- access to a database, such as MySQL, for off-chain data storage
- integration with a spam prevention system, such as Cloudflare Turnstile or Google reCAPTCHA
- connecting to RISC Zero Bonsai API, which is the purpose of this repository
How to use
The documentation can be found in the docs folder. It is designed to resemble the Bonsai Rust API here:
https://github.com/risc0/risc0/blob/main/bonsai/sdk/src/alpha.rs
The integration tests can be served as tutorials. It can be as simple as follows:
Serializer
Another challenge when using a different programming language to interact with RISC Zero is about the types. This issue is particularly significant to PHP because PHP does not have separate types for different word sizes.
For example, in PHP, we have int
representing integers, nothing else. But this can correspond to i8, i16, i32, i64,
u8, u16, u32, u64 in Rust. There are also numbers that PHP cannot easily represent, such as i128 and u128.
Since the Rust type is the missing information here, there is no choice other than letting the developer explicitly provide the Rust type. To do so, the developer is asked to wrap all elements.
Below is a very comprehensive example that can served as a tutorial.
The corresponding Rust struct is defined as follows.
Note that we focus on String here in Rust rather than a binary string because the current serializer does have the trouble
to process Vec<u8>
in a different manner. If one's Rust code has to use Vec<u8>
and cannot afford a wrapper, then
the best solution so far is to let PHP break the string down to an array of bytes, through str_split($str)
.