Skip to content

Commit

Permalink
feat(core): add SShort, SInt, SLong, SByte and SBool sigma …
Browse files Browse the repository at this point in the history
…parsing support
  • Loading branch information
capt-nemo429 committed Dec 13, 2022
1 parent 1271bfd commit 93a03c9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
40 changes: 39 additions & 1 deletion packages/core/src/serialization/sigma/constantSerializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
sPositiveBigIntTestVectors,
sSigmaPropTestVectors
} from "../../tests/testVectors/constantsTestVectors";
import { SConstant } from "./constantSerializer";
import { SConstant, SParse } from "./constantSerializer";
import { SigmaTypeCode } from "./sigmaTypeCode";
import {
SBigInt,
Expand Down Expand Up @@ -136,3 +136,41 @@ describe("SColl serialization", () => {
}
});
});
``;

describe("Deserialization", () => {
it("Should deserialize SBoolean", () => {
expect(SParse("0101")).toBe(true);
expect(SParse("0100")).toBe(false);
});

it("Should deserialize SByte", () => {
expect(SParse("0201")).toBe(1);
expect(SParse("0202")).toBe(2);
expect(SParse("024c")).toBe(76);
});

it("Should deserialize SInt", () => {
for (const tv of sIntTestVectors) {
expect(SParse(tv.hex)).toBe(tv.value);
}
});

it("Should deserialize SLong", () => {
for (const tv of sLongTestVectors) {
expect(SParse(tv.hex)).toBe(tv.value);
}
});

it("Should deserialize SShort", () => {
expect(SParse("0302")).toBe(1);
expect(SParse("0303")).toBe(-2);
expect(SParse("0322")).toBe(17);
});

it("Should fail when trying to deserialize a not implemented type", () => {
expect(() => {
SParse("6122");
}).toThrow();
});
});
21 changes: 12 additions & 9 deletions packages/core/src/serialization/sigma/constantSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HexString } from "@fleet-sdk/common";
import { DataSerializer } from "./dataSerializer";
import { SigmaReader } from "./sigmaReader";
import { ISigmaType } from "./sigmaTypes";
import { SigmaWriter } from "./sigmaWriter";
import { TypeSerializer } from "./typeSerializer";
Expand All @@ -8,16 +9,18 @@ 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): SigmaWriter {
const sigmaBuffer = new SigmaWriter(MAX_CONSTANT_LENGTH);
TypeSerializer.serialize(content, sigmaBuffer);
DataSerializer.serialize(content, sigmaBuffer);
export function SConstant(content: ISigmaType): HexString {
const writer = new SigmaWriter(MAX_CONSTANT_LENGTH);

TypeSerializer.serialize(content, writer);
DataSerializer.serialize(content, writer);

return sigmaBuffer;
}
return writer.toHex();
}

export function SConstant(content: ISigmaType): HexString {
return SigmaConstant.serialize(content).toHex();
export function SParse<T>(content: HexString | Uint8Array): T {
const reader = new SigmaReader(content);
const type = reader.readType();

return DataSerializer.deserialize(type, reader) as T;
}
36 changes: 32 additions & 4 deletions packages/core/src/serialization/sigma/dataSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { VLQ } from "../vlq";
import { vlqEncode } from "../vlq";
import { SigmaReader } from "./sigmaReader";
import { SigmaTypeCode } from "./sigmaTypeCode";
import { IPrimitiveSigmaType, ISigmaType } from "./sigmaTypes";
import { SigmaWriter } from "./sigmaWriter";
import { isColl, isPrimitiveType } from "./utils";
import { isColl, isPrimitiveType, isPrimitiveTypeCode } from "./utils";

export class DataSerializer {
public static serialize(data: ISigmaType, buffer: SigmaWriter) {
Expand All @@ -16,8 +17,10 @@ export class DataSerializer {
break;
case SigmaTypeCode.Short:
case SigmaTypeCode.Int:
buffer.writeNumber((data as IPrimitiveSigmaType<number>).value);
break;
case SigmaTypeCode.Long:
buffer.writeInt((data as IPrimitiveSigmaType<number>).value);
buffer.writeLong((data as IPrimitiveSigmaType<bigint>).value);
break;
case SigmaTypeCode.BigInt: {
buffer.writeBigInt((data as IPrimitiveSigmaType<bigint>).value);
Expand All @@ -43,7 +46,7 @@ export class DataSerializer {
throw Error("Not implemented");
}
} else if (isColl(data)) {
buffer.writeBytes(VLQ.encode(data.value.length));
buffer.writeBytes(vlqEncode(data.value.length));

switch (data.elementsType) {
case SigmaTypeCode.Boolean:
Expand All @@ -64,4 +67,29 @@ export class DataSerializer {
throw Error("Not implemented");
}
}

static deserialize(typeCode: SigmaTypeCode, reader: SigmaReader): unknown {
if (isPrimitiveTypeCode(typeCode)) {
switch (typeCode) {
case SigmaTypeCode.Boolean:
return reader.readBoolean();
case SigmaTypeCode.Byte:
return reader.readByte();
case SigmaTypeCode.Short:
case SigmaTypeCode.Int:
return reader.readNumber();
case SigmaTypeCode.Long:
return reader.readLong();
// case SigmaTypeCode.BigInt:
// case SigmaTypeCode.GroupElement:
// case SigmaTypeCode.SigmaProp:
// case SigmaTypeCode.Unit:
// case SigmaTypeCode.Box:
// default:
// break;
}
}

throw new Error("Type parsing not yet implemented.");
}
}
4 changes: 4 additions & 0 deletions packages/core/src/serialization/sigma/sigmaReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class SigmaReader {
this._cursor = 0;
}

public readBoolean(): boolean {
return this.readByte() === 0x01;
}

public readByte(): number {
return this._bytes[this._cursor++];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/serialization/sigma/typeSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ISigmaType } from "./sigmaTypes";
import { SigmaWriter } from "./sigmaWriter";
import { isColl, isEmbeddable, isPrimitiveType } from "./utils";
import { isColl, isEmbeddableTypeCode, isPrimitiveType } from "./utils";

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

0 comments on commit 93a03c9

Please sign in to comment.