From df0eca9bae4e749b3eb4e304c4aed209255d4d56 Mon Sep 17 00:00:00 2001 From: Oleh Chausov <9oleg8@gmail.com> Date: Wed, 22 Mar 2023 21:35:01 +0200 Subject: [PATCH] data-types specification --- specs/README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/specs/README.md b/specs/README.md index fb6dcee6..7119195c 100644 --- a/specs/README.md +++ b/specs/README.md @@ -43,7 +43,68 @@ zkDatabase isn't just support ordinary Key-Value storage but also support BSON d ## Data Type -**HELP** We need to clarify which kind of data type will be supported and how could we make it compatible with zkApp (SnarkyJS) +### SnaryJS supported types +- **Built-in types** + - Field + - Bool + - UInt32 + - UInt64 + - PublicKey + - PrivateKey + - Signature + - Group + - Scalar + - CircuitString + - Character +- **Custom types** + - Struct +- **Trees** + - MerkleTree + - MerkleMap + +### Bson supported types +- Double +- String +- Object +- Array +- Binary data +- Undefined +- ObjectId +- Boolean +- Date +- Null +- Regular Expression +- DBPointer +- JavaScript +- Symbol +- 32-bit integer +- Timestamp +- 64-bit integer +- Decimal128 +- Min key +- Max key + +### Serialization/Deserialization +The provided code snippet demonstrates how to convert a zk-snark data type into a BSON-supported format by first converting the value into a Uint8Array and then serializing it using BSON. +```ts + const value = UInt64.from(12342); + const bytes: Uint8Array = Encoding.Bijective.Fp.toBytes(value.toFields()); + const bson = BSON.serialize({ bytes }); +``` +This code snippet demonstrates the process of converting BSON data back into a zk-SNARK data type. This is done by first deserializing the BSON data into a JavaScript object, then converting the Binary data into a Uint8Array, and finally using a built-in decoding method to reconstruct the original value from the byte array. +```ts + const deserializedBson = BSON.deserialize(bson); + const convertedResult = new Uint8Array(deserializedBson.bytes.buffer); + const initialField = Encoding.Bijective.Fp.fromBytes(convertedResult); +``` + +### Serializing Arbitrary Data into Field Elements +When serializing arbitrary data into field elements, it's important to note that field elements can hold a maximum of 254 arbitrary bits (not 255) due to the largest possible field element lying between 2^254 and 2^255. + +You can utilize the `Encoding.bytesToFields` method, which efficiently packs 31 bytes per field element for serialization. + +**HELP** We need to clarify which kind of data type will be supported. + ## Index BSON Document