Skip to content

Commit

Permalink
update Bit255
Browse files Browse the repository at this point in the history
  • Loading branch information
Ho Nguyen Pham authored and Ho Nguyen Pham committed Nov 5, 2023
1 parent 057cbf8 commit 8ce5957
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@auxo-dev/auxo-libs",
"version": "0.2.2",
"version": "0.2.3",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand Down
10 changes: 5 additions & 5 deletions src/Bit255.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ describe('CustomScalar', () => {
expect(bitStringXor2.toBigInt()).toEqual(bigIntXor2);
});

// FIXME - not working
xit('Should convert to bigint correctly', async () => {
// let r = Scalar.random();
let r = Scalar.from(1n);
// FIXME - not preserving Scalar's constant value
it('Should convert between bigint correctly', async () => {
let r = Scalar.random();
let bitString = Bit255.fromScalar(r);
expect(bitString.toBigInt()).toEqual(r.toBigInt());
expect(Bit255.fromBigInt(r.toBigInt()).toBigInt()).toEqual(r.toBigInt());
expect(Bit255.fromBigInt(bitString.toBigInt())).toEqual(bitString);
});
});
19 changes: 19 additions & 0 deletions src/Bit255.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ export class Bit255 extends Struct({
});
}

static fromBits(bits: Bool[]): Bit255 {
if (bits.length !== 255) throw new Error('Invalid input length');
return new Bit255({
head: Field.fromBits(bits.slice(0, 127)),
tail: Field.fromBits(bits.slice(127)),
});
}

static fromBigInt(i: bigint): Bit255 {
let bits = new Array<Bool>(255).fill(Bool(false));
let index = 0;
while (i > 0) {
bits[index] = Bool(i % 2n == 1n);
i = i / 2n;
index += 1;
}
return Bit255.fromBits(bits);
}

static toBigInt(b: Bit255): bigint {
let bits = b.head
.toBits()
Expand Down

0 comments on commit 8ce5957

Please sign in to comment.