UNPKG

@nori-zk/o1js-zk-utils

Version:

o1js-zk-utils supporting Nori Bridge

53 lines 1.96 kB
import { Bytes, Field, Struct, UInt8 } from 'o1js'; import { wordToBytes } from '@nori-zk/proof-conversion/min'; export class Bytes32 extends Bytes(32) { static get zero() { return new this(new Array(32).fill(0).map(() => new UInt8(0))); } } export class Bytes20 extends Bytes(20) { static get zero() { return new this(new Array(20).fill(0).map(() => new UInt8(0))); } static fromHex(hex) { return super.fromHex(hex); } toField() { let result = new Field(0); for (let i = 0; i < 20; i++) { result = result.mul(256).add(this.bytes[i].value); } return result; } } export function bytes32FieldPairToBytes32(highByteField, lowerBytesField) { // wordToBytes returns little-endian (LSB first), so reverse to restore big-endian order. const highByte = wordToBytes(highByteField, 1)[0]; const lowerBytes = wordToBytes(lowerBytesField, 31).reverse(); return Bytes32.from([highByte, ...lowerBytes]); } export class Bytes32FieldPair extends Struct({ highByteField: Field, lowerBytesField: Field, }) { static fromBytes32(bytes32) { // Convert the store hash's higher byte into a provable field. let storeHashHighByteField = new Field(0); storeHashHighByteField = storeHashHighByteField.add(bytes32.bytes[0].value); // Convert the store hash's lower 31 bytes into a provable field. let storeHashLowerBytesField = new Field(0); for (let i = 1; i < 32; i++) { storeHashLowerBytesField = storeHashLowerBytesField .mul(256) .add(bytes32.bytes[i].value); } return new this({ highByteField: storeHashHighByteField, lowerBytesField: storeHashLowerBytesField, }); } toBytes32() { return bytes32FieldPairToBytes32(this.highByteField, this.lowerBytesField); } } //# sourceMappingURL=types.js.map