Skip to content

Commit

Permalink
fix bug in Bit255
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Sep 30, 2024
1 parent 58684c3 commit e69ee1d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
18 changes: 2 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 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.6.1",
"version": "0.6.3",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand All @@ -17,6 +17,7 @@
"types": "./build/types/src/index.d.ts",
"scripts": {
"build": "tsc --build tsconfig.esm.json tsconfig.cjs.json tsconfig.types.json && ./fix-export.sh",
"build:clean": "rm -rf build && rm package-lock.json && tsc --build tsconfig.esm.json tsconfig.cjs.json tsconfig.types.json && ./fix-export.sh",
"buildw": "tsc --watch",
"scripts": "tsc --build tsconfig.json tsconfig.cjs.json tsconfig.types.json && ./fix-export.sh && node",
"coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
Expand Down
22 changes: 21 additions & 1 deletion src/Bit255.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Provable, Reducer, Scalar, SmartContract, method } from 'o1js';
import {
Bool,
Field,
Provable,
Reducer,
Scalar,
SmartContract,
method,
} from 'o1js';
import { Bit255 } from './Bit255.js';

describe('Bit255', () => {
Expand Down Expand Up @@ -74,4 +82,16 @@ describe('Bit255', () => {
let converted = Bit255.fromBits(b.toBits());
expect(b.toBigInt()).toEqual(converted.toBigInt());
});

it('Should preserve correctness between bigint and bits', async () => {
let r = Field.random();
let bi = r.toBigInt();
let bits = r.toBits();
expect(Bit255.fromBits(bits.concat([Bool(false)])).toBigInt()).toEqual(
bi
);
// let b2 = Bit255.fromBigInt(b.toBigInt());
// expect(b.toBigInt()).toEqual(b1.toBigInt());
// expect(b.toBigInt()).toEqual(b2.toBigInt());
});
});
10 changes: 5 additions & 5 deletions src/Bit255.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ 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, 128)),
tail: Field.fromBits(bits.slice(128)),
head: Field.fromBits(bits.slice(127)),
tail: Field.fromBits(bits.slice(0, 127)),
});
}

static toBits(b: Bit255): Bool[] {
return b.head
return b.tail
.toBits()
.slice(0, 128)
.concat(b.tail.toBits().slice(0, 127));
.slice(0, 127)
.concat(b.head.toBits().slice(0, 128));
}

static fromBigInt(i: bigint): Bit255 {
Expand Down

0 comments on commit e69ee1d

Please sign in to comment.