Skip to content

Commit

Permalink
refactor(core): rename SigmaBuffer to SigmaWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
capt-nemo429 committed Dec 13, 2022
1 parent 40bffdc commit 255f116
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from "./builder";
export * from "./models";
export * from "./errors";
export * from "./serialization";
export { AddressType, Network } from "@fleet-sdk/common";
export { AddressType, Network, Box, Amount, TokenAmount } from "@fleet-sdk/common";
6 changes: 3 additions & 3 deletions packages/core/src/serialization/sigma/constantSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { HexString } from "@fleet-sdk/common";
import { DataSerializer } from "./dataSerializer";
import { SigmaBuffer } from "./sigmaBuffer";
import { ISigmaType } from "./sigmaTypes";
import { SigmaWriter } from "./sigmaWriter";
import { TypeSerializer } from "./typeSerializer";

export const MAX_CONSTANT_TYPES_LENGTH = 100;
export const MAX_CONSTANT_CONTENT_LENGTH = 4096;
export const MAX_CONSTANT_LENGTH = MAX_CONSTANT_TYPES_LENGTH + MAX_CONSTANT_CONTENT_LENGTH;

class SigmaConstant {
public static serialize(content: ISigmaType): SigmaBuffer {
const sigmaBuffer = new SigmaBuffer(MAX_CONSTANT_LENGTH);
public static serialize(content: ISigmaType): SigmaWriter {
const sigmaBuffer = new SigmaWriter(MAX_CONSTANT_LENGTH);
TypeSerializer.serialize(content, sigmaBuffer);
DataSerializer.serialize(content, sigmaBuffer);

Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/serialization/sigma/dataSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { VLQ } from "../vlq";
import { SigmaBuffer } from "./sigmaBuffer";
import { SigmaTypeCode } from "./sigmaTypeCode";
import { IPrimitiveSigmaType, ISigmaType } from "./sigmaTypes";
import { SigmaWriter } from "./sigmaWriter";
import { isColl, isPrimitiveType } from "./utils";

export class DataSerializer {
public static serialize(data: ISigmaType, buffer: SigmaBuffer) {
public static serialize(data: ISigmaType, buffer: SigmaWriter) {
if (isPrimitiveType(data)) {
switch (data.type) {
case SigmaTypeCode.Boolean:
buffer.putBoolean((data as IPrimitiveSigmaType<boolean>).value);
buffer.writeBoolean((data as IPrimitiveSigmaType<boolean>).value);
break;
case SigmaTypeCode.Byte:
buffer.put((data as IPrimitiveSigmaType<number>).value);
buffer.write((data as IPrimitiveSigmaType<number>).value);
break;
case SigmaTypeCode.Short:
case SigmaTypeCode.Int:
case SigmaTypeCode.Long:
buffer.putInt((data as IPrimitiveSigmaType<number>).value);
buffer.writeInt((data as IPrimitiveSigmaType<number>).value);
break;
case SigmaTypeCode.BigInt: {
buffer.putBigInt((data as IPrimitiveSigmaType<bigint>).value);
buffer.writeBigInt((data as IPrimitiveSigmaType<bigint>).value);
break;
}
case SigmaTypeCode.GroupElement:
buffer.putBytes((data as IPrimitiveSigmaType<Uint8Array>).value);
buffer.writeBytes((data as IPrimitiveSigmaType<Uint8Array>).value);
break;
case SigmaTypeCode.SigmaProp: {
const node = (data as IPrimitiveSigmaType<ISigmaType>).value;
if (node.type === SigmaTypeCode.GroupElement) {
buffer.put(0xcd); // CreateProveDlog operation
buffer.write(0xcd); // CreateProveDlog operation
DataSerializer.serialize(node, buffer);
} else {
throw Error("Not implemented");
Expand All @@ -43,14 +43,14 @@ export class DataSerializer {
throw Error("Not implemented");
}
} else if (isColl(data)) {
buffer.putBytes(VLQ.encode(data.value.length));
buffer.writeBytes(VLQ.encode(data.value.length));

switch (data.elementsType) {
case SigmaTypeCode.Boolean:
buffer.putBits(data.value as boolean[]);
buffer.writeBits(data.value as boolean[]);
break;
case SigmaTypeCode.Byte:
buffer.putBytes(Uint8Array.from(data.value as number[]));
buffer.writeBytes(Uint8Array.from(data.value as number[]));
break;
default:
for (let i = 0; i < data.value.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
import { MAX_CONSTANT_LENGTH } from "./constantSerializer";
import { SigmaBuffer } from "./sigmaBuffer";
import { SigmaWriter } from "./sigmaWriter";

describe("Sigma Buffer", () => {
describe("Sigma Writer", () => {
it("Should put a single byte at time", () => {
const sigmaBuffer = new SigmaBuffer(MAX_CONSTANT_LENGTH);
sigmaBuffer.put(0x10).put(0x0f).put(0x00);
const sigmaBuffer = new SigmaWriter(MAX_CONSTANT_LENGTH);
sigmaBuffer.write(0x10).write(0x0f).write(0x00);

expect(sigmaBuffer).toHaveLength(3);
expect(sigmaBuffer.toBytes()).toEqual(Uint8Array.from([0x10, 0x0f, 0x00]));
});

it("Should put multiple bytes at once", () => {
const sigmaBuffer = new SigmaBuffer(MAX_CONSTANT_LENGTH);
sigmaBuffer.putBytes(Uint8Array.from([0x00, 0x00, 0x21, 0xff]));
sigmaBuffer.putBytes(Uint8Array.from([0x15, 0x0c]));
const sigmaBuffer = new SigmaWriter(MAX_CONSTANT_LENGTH);
sigmaBuffer.writeBytes(Uint8Array.from([0x00, 0x00, 0x21, 0xff]));
sigmaBuffer.writeBytes(Uint8Array.from([0x15, 0x0c]));

expect(sigmaBuffer).toHaveLength(6);
expect(sigmaBuffer.toBytes()).toEqual(Uint8Array.from([0x00, 0x00, 0x21, 0xff, 0x15, 0x0c]));
});

it("Should put multiple hex string", () => {
const sigmaBuffer = new SigmaBuffer(MAX_CONSTANT_LENGTH);
const sigmaBuffer = new SigmaWriter(MAX_CONSTANT_LENGTH);

sigmaBuffer.putHex("000021ff");
sigmaBuffer.putHex("150c");
sigmaBuffer.writeHex("000021ff");
sigmaBuffer.writeHex("150c");

expect(sigmaBuffer).toHaveLength(6);
expect(sigmaBuffer.toBytes()).toEqual(Uint8Array.from([0x00, 0x00, 0x21, 0xff, 0x15, 0x0c]));
});

it("Should fail for invalid hex string", () => {
const sigmaBuffer = new SigmaBuffer(MAX_CONSTANT_LENGTH);
const sigmaBuffer = new SigmaWriter(MAX_CONSTANT_LENGTH);

expect(() => {
sigmaBuffer.putHex("000021f");
sigmaBuffer.writeHex("000021f");
}).toThrow(Error("Invalid hex padding"));

expect(() => {
sigmaBuffer.putHex("-1");
sigmaBuffer.writeHex("-1");
}).toThrow(Error("Invalid byte sequence"));

expect(() => {
sigmaBuffer.putHex("ka");
sigmaBuffer.writeHex("ka");
}).toThrow(Error("Invalid byte sequence"));
});

it("Should put a boolean", () => {
const sigmaBuffer = new SigmaBuffer(MAX_CONSTANT_LENGTH);
sigmaBuffer.putBoolean(true);
sigmaBuffer.putBoolean(false);
const sigmaBuffer = new SigmaWriter(MAX_CONSTANT_LENGTH);
sigmaBuffer.writeBoolean(true);
sigmaBuffer.writeBoolean(false);

expect(sigmaBuffer).toHaveLength(2);
expect(sigmaBuffer.toBytes()).toEqual(Uint8Array.from([0x01, 0x00]));
});

it("Should put multiple booleans", () => {
const sigmaBuffer = new SigmaBuffer(MAX_CONSTANT_LENGTH);
sigmaBuffer.putBooleans([true, false]);
const sigmaBuffer = new SigmaWriter(MAX_CONSTANT_LENGTH);
sigmaBuffer.writeBooleans([true, false]);

expect(sigmaBuffer).toHaveLength(2);
expect(sigmaBuffer.toBytes()).toEqual(Uint8Array.from([0x01, 0x00]));
Expand Down Expand Up @@ -86,10 +86,10 @@ describe("Sigma Buffer", () => {
{ int: 950, hex: "ec0e" }
];

const all = new SigmaBuffer(MAX_CONSTANT_LENGTH);
const all = new SigmaWriter(MAX_CONSTANT_LENGTH);
for (const tv of testVectors) {
all.putInt(tv.int);
expect(new SigmaBuffer(tv.hex.length).putInt(tv.int).toHex()).toBe(tv.hex);
all.writeInt(tv.int);
expect(new SigmaWriter(tv.hex.length).writeInt(tv.int).toHex()).toBe(tv.hex);
}

expect(all.toHex()).toEqual(testVectors.map((x) => x.hex).join(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { bytesToHex } from "@noble/hashes/utils";
import { VLQ } from "../vlq";
import { ZigZag } from "../zigZag";

export class SigmaBuffer {
export class SigmaWriter {
private _bytes!: Uint8Array;
private _cursor!: number;

Expand All @@ -16,40 +16,40 @@ export class SigmaBuffer {
this._cursor = 0;
}

public putBoolean(value: boolean): SigmaBuffer {
this.put(value === true ? 0x01 : 0x00);
public writeBoolean(value: boolean): SigmaWriter {
this.write(value === true ? 0x01 : 0x00);

return this;
}

public putBooleans(elements: boolean[]): SigmaBuffer {
public writeBooleans(elements: boolean[]): SigmaWriter {
for (let i = 0; i < elements.length; i++) {
this.putBoolean(elements[i]);
this.writeBoolean(elements[i]);
}

return this;
}

public putInt(value: number): SigmaBuffer {
this.putBytes(VLQ.encode(ZigZag.encode(ensureBigInt(value))));
public writeInt(value: number): SigmaWriter {
this.writeBytes(VLQ.encode(ZigZag.encode(ensureBigInt(value))));

return this;
}

public put(byte: number): SigmaBuffer {
public write(byte: number): SigmaWriter {
this._bytes[this._cursor++] = byte;

return this;
}

public putBytes(bytes: Uint8Array): SigmaBuffer {
public writeBytes(bytes: Uint8Array): SigmaWriter {
this._bytes.set(bytes, this._cursor);
this._cursor += bytes.length;

return this;
}

public putHex(hex: string): SigmaBuffer {
public writeHex(hex: string): SigmaWriter {
if (hex.length % 2) {
throw new Error("Invalid hex padding");
}
Expand All @@ -62,13 +62,13 @@ export class SigmaBuffer {
throw new Error("Invalid byte sequence");
}

this.put(byte);
this.write(byte);
}

return this;
}

public putBits(bits: ArrayLike<boolean>): SigmaBuffer {
public writeBits(bits: ArrayLike<boolean>): SigmaWriter {
let bitOffset = 0;

for (let i = 0; i < bits.length; i++) {
Expand All @@ -91,7 +91,7 @@ export class SigmaBuffer {
return this;
}

public putBigInt(number: bigint): SigmaBuffer {
public writeBigInt(number: bigint): SigmaWriter {
if (number < _0n) {
throw new Error("Negative BigInt values are not supported Fleet serializer.");
}
Expand All @@ -104,8 +104,8 @@ export class SigmaBuffer {
hex = "00" + hex;
}

this.putBytes(VLQ.encode(hex.length / 2));
this.putHex(hex);
this.writeBytes(VLQ.encode(hex.length / 2));
this.writeHex(hex);

return this;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/serialization/sigma/typeSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { SigmaBuffer } from "./sigmaBuffer";
import { ISigmaType } from "./sigmaTypes";
import { SigmaWriter } from "./sigmaWriter";
import { isColl, isEmbeddable, isPrimitiveType } from "./utils";

export class TypeSerializer {
public static serialize(value: ISigmaType, buffer: SigmaBuffer) {
public static serialize(value: ISigmaType, buffer: SigmaWriter) {
if (isPrimitiveType(value)) {
buffer.put(value.type);
buffer.write(value.type);
} else if (isColl(value)) {
if (isEmbeddable(value.elementsType)) {
buffer.put(value.type + value.elementsType);
buffer.write(value.type + value.elementsType);
}
}
}
Expand Down

0 comments on commit 255f116

Please sign in to comment.