Skip to content

Commit 7bf9ee3

Browse files
committed
refactor SigmaByteWriter and SigmaByteReader
1 parent 66d6fc9 commit 7bf9ee3

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

packages/serializer/src/coders/sigmaByteReader.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class SigmaByteReader {
1818
this.#cursor = 0;
1919
}
2020

21-
public readBoolean(): boolean {
21+
public readBool(): boolean {
2222
return this.readByte() === 0x01;
2323
}
2424

@@ -53,25 +53,24 @@ export class SigmaByteReader {
5353
return readVLQ(this);
5454
}
5555

56-
public readShort(): number {
57-
return zigZag32.decode(readBigVLQ(this));
58-
}
59-
6056
public readI8(): number {
6157
const byte = this.readByte();
62-
6358
return byte > MAX_I8 ? byte - (MAX_U8 + 1) : byte;
6459
}
6560

66-
public readInt(): number {
61+
public readI16(): number {
62+
return zigZag32.decode(readBigVLQ(this));
63+
}
64+
65+
public readI32(): number {
6766
return zigZag32.decode(readBigVLQ(this));
6867
}
6968

70-
public readLong(): bigint {
69+
public readI64(): bigint {
7170
return zigZag64.decode(readBigVLQ(this));
7271
}
7372

74-
public readBigInt(): bigint {
73+
public readI256(): bigint {
7574
const len = readVLQ(this);
7675
return hexToBigInt(hex.encode(this.readBytes(len)));
7776
}

packages/serializer/src/coders/sigmaByteWriter.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ describe("Sigma Writer", () => {
4848

4949
it("Should put a boolean", () => {
5050
const sigmaBuffer = new SigmaByteWriter(MAX_CONSTANT_LENGTH);
51-
sigmaBuffer.writeBoolean(true);
52-
sigmaBuffer.writeBoolean(false);
51+
sigmaBuffer.writeBool(true);
52+
sigmaBuffer.writeBool(false);
5353

5454
expect(sigmaBuffer).toHaveLength(2);
5555
expect(sigmaBuffer.toBytes()).toEqual(Uint8Array.from([0x01, 0x00]));
@@ -81,8 +81,8 @@ describe("Sigma Writer", () => {
8181

8282
const all = new SigmaByteWriter(MAX_CONSTANT_LENGTH);
8383
for (const tv of testVectors) {
84-
all.writeShort(tv.int);
85-
expect(new SigmaByteWriter(tv.hex.length).writeShort(tv.int).encode(hex)).toBe(
84+
all.writeI16(tv.int);
85+
expect(new SigmaByteWriter(tv.hex.length).writeI16(tv.int).encode(hex)).toBe(
8686
tv.hex
8787
);
8888
}
@@ -95,23 +95,23 @@ describe("Sigma Writer", () => {
9595

9696
// Short
9797
const i16Err = "out of range for a 16-bit integer";
98-
expect(() => sigmaBuffer.writeShort(MIN_I16 - 1)).to.throw(i16Err);
99-
expect(() => sigmaBuffer.writeShort(MAX_I16 + 1)).to.throw(i16Err);
98+
expect(() => sigmaBuffer.writeI16(MIN_I16 - 1)).to.throw(i16Err);
99+
expect(() => sigmaBuffer.writeI16(MAX_I16 + 1)).to.throw(i16Err);
100100

101101
// Int
102102
const i32Err = "out of range for a 32-bit integer";
103-
expect(() => sigmaBuffer.writeInt(MIN_I32 - 1)).to.throw(i32Err);
104-
expect(() => sigmaBuffer.writeInt(MAX_I32 + 1)).to.throw(i32Err);
103+
expect(() => sigmaBuffer.writeI32(MIN_I32 - 1)).to.throw(i32Err);
104+
expect(() => sigmaBuffer.writeI32(MAX_I32 + 1)).to.throw(i32Err);
105105

106106
// Long
107107
const i64Err = "out of range for a 64-bit integer";
108-
expect(() => sigmaBuffer.writeLong(MIN_I64 - 1n)).to.throw(i64Err);
109-
expect(() => sigmaBuffer.writeLong(MAX_I64 + 1n)).to.throw(i64Err);
108+
expect(() => sigmaBuffer.writeI64(MIN_I64 - 1n)).to.throw(i64Err);
109+
expect(() => sigmaBuffer.writeI64(MAX_I64 + 1n)).to.throw(i64Err);
110110

111111
// BigInt
112112
const i256Err = "out of range for a 256-bit integer";
113-
expect(() => sigmaBuffer.writeBigInt(MIN_I256 - 1n)).to.throw(i256Err);
114-
expect(() => sigmaBuffer.writeBigInt(MAX_I256 + 1n)).to.throw(i256Err);
113+
expect(() => sigmaBuffer.writeI256(MIN_I256 - 1n)).to.throw(i256Err);
114+
expect(() => sigmaBuffer.writeI256(MAX_I256 + 1n)).to.throw(i256Err);
115115
});
116116

117117
it("Should write a checksum based on the current stream content", () => {

packages/serializer/src/coders/sigmaByteWriter.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class SigmaByteWriter {
2626
this.#cursor = 0;
2727
}
2828

29-
public writeBoolean(value: boolean): SigmaByteWriter {
29+
public writeBool(value: boolean): SigmaByteWriter {
3030
this.write(value === true ? 0x01 : 0x00);
3131

3232
return this;
@@ -40,7 +40,7 @@ export class SigmaByteWriter {
4040
return writeBigVLQ(this, value);
4141
}
4242

43-
public writeShort(value: number): SigmaByteWriter {
43+
public writeI16(value: number): SigmaByteWriter {
4444
if (value < MIN_I16 || value > MAX_I16) {
4545
throw new RangeError(`Value ${value} is out of range for a 16-bit integer`);
4646
}
@@ -49,22 +49,31 @@ export class SigmaByteWriter {
4949
return this;
5050
}
5151

52-
public writeInt(value: number): SigmaByteWriter {
52+
public writeI32(value: number): SigmaByteWriter {
5353
if (value < MIN_I32 || value > MAX_I32) {
5454
throw new RangeError(`Value ${value} is out of range for a 32-bit integer`);
5555
}
5656

5757
return this.writeBigVLQ(zigZag32.encode(value));
5858
}
5959

60-
public writeLong(value: bigint): SigmaByteWriter {
60+
public writeI64(value: bigint): SigmaByteWriter {
6161
if (value < MIN_I64 || value > MAX_I64) {
6262
throw new RangeError(`Value ${value} is out of range for a 64-bit integer`);
6363
}
6464

6565
return this.writeBigVLQ(zigZag64.encode(value));
6666
}
6767

68+
public writeI256(value: bigint): SigmaByteWriter {
69+
if (value < MIN_I256 || value > MAX_I256) {
70+
throw new RangeError(`Value ${value} is out of range for a 256-bit integer`);
71+
}
72+
73+
const hex = bigIntToHex(value);
74+
return this.writeVLQ(hex.length / 2).writeHex(hex);
75+
}
76+
6877
public write(byte: number): SigmaByteWriter {
6978
this.#bytes[this.#cursor++] = byte;
7079
return this;
@@ -101,15 +110,6 @@ export class SigmaByteWriter {
101110
return this;
102111
}
103112

104-
public writeBigInt(value: bigint): SigmaByteWriter {
105-
if (value < MIN_I256 || value > MAX_I256) {
106-
throw new RangeError(`Value ${value} is out of range for a 256-bit integer`);
107-
}
108-
109-
const hex = bigIntToHex(value);
110-
return this.writeVLQ(hex.length / 2).writeHex(hex);
111-
}
112-
113113
public writeChecksum(length = 4, hashFn = blake2b256): SigmaByteWriter {
114114
const hash = hashFn(this.toBytes());
115115
return this.writeBytes(length ? hash.subarray(0, length) : hash);

packages/serializer/src/serializers/dataSerializer.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ export const dataSerializer = {
1212
if (type.embeddable) {
1313
switch (type.code) {
1414
case descriptors.bool.code:
15-
return writer.writeBoolean(data as boolean);
15+
return writer.writeBool(data as boolean);
1616
case descriptors.byte.code:
1717
return writer.write(data as number);
1818
case descriptors.short.code:
19-
return writer.writeShort(data as number);
19+
return writer.writeI16(data as number);
2020
case descriptors.int.code:
21-
return writer.writeInt(data as number);
21+
return writer.writeI32(data as number);
2222
case descriptors.long.code:
23-
return writer.writeLong(data as bigint);
23+
return writer.writeI64(data as bigint);
2424
case descriptors.bigInt.code:
25-
return writer.writeBigInt(data as bigint);
25+
return writer.writeI256(data as bigint);
2626
case descriptors.groupElement.code:
2727
return writer.writeBytes(data as Uint8Array);
2828
case descriptors.sigmaProp.code: {
@@ -90,17 +90,17 @@ export const dataSerializer = {
9090
if (type.embeddable) {
9191
switch (type.code) {
9292
case descriptors.bool.code:
93-
return reader.readBoolean();
93+
return reader.readBool();
9494
case descriptors.byte.code:
9595
return reader.readI8();
9696
case descriptors.short.code:
97-
return reader.readShort();
97+
return reader.readI16();
9898
case descriptors.int.code:
99-
return reader.readInt();
99+
return reader.readI32();
100100
case descriptors.long.code:
101-
return reader.readLong();
101+
return reader.readI64();
102102
case descriptors.bigInt.code:
103-
return reader.readBigInt();
103+
return reader.readI256();
104104
case descriptors.groupElement.code:
105105
return reader.readBytes(GROUP_ELEMENT_LENGTH);
106106
case descriptors.sigmaProp.code: {

0 commit comments

Comments
 (0)