Skip to content

Commit

Permalink
update CustomScalar and Bit255
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Nov 20, 2023
1 parent 23ac35d commit abfedca
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 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.3.1",
"version": "0.3.3",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand Down
29 changes: 28 additions & 1 deletion src/Bit255.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { Scalar } from 'o1js';
import { Provable, Reducer, Scalar, SmartContract, method } from 'o1js';
import { Bit255 } from './Bit255.js';

describe('CustomScalar', () => {
xit('Should be provable', async () => {
class TestBit255 extends SmartContract {
reducer = Reducer({ actionType: Bit255 });

@method
xor(a: Bit255, b: Bit255, c: Bit255) {
let res = Bit255.xor(a, b);
res.assertEquals(c);
res.assertEquals(
Bit255.fromScalar(
Provable.witness(Scalar, () =>
Scalar.from(Scalar.random().toBigInt())
)
)
);
}
}

await TestBit255.compile();
});

it('Should xor correctly', async () => {
let r1 = Scalar.random();
let r2 = Scalar.random();
Expand Down Expand Up @@ -32,4 +53,10 @@ describe('CustomScalar', () => {
expect(Bit255.fromBigInt(r.toBigInt()).toBigInt()).toEqual(r.toBigInt());
expect(Bit255.fromBigInt(bitString.toBigInt())).toEqual(bitString);
});

it('Should convert between Scalar correctly', async () => {
let r = Scalar.random();
let converted = Bit255.fromScalar(r).toScalar();
expect(r.toBigInt()).toEqual(converted.toBigInt());
});
});
31 changes: 23 additions & 8 deletions src/Bit255.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { Bool, Field, Gadgets, Poseidon, Scalar, Struct } from 'o1js';
import { Bool, Field, Gadgets, Poseidon, Provable, Scalar, Struct } from 'o1js';
import { CustomScalar } from './CustomScalar.js';

// FIXME - Convert between Scalar and Bit255 does not preserve bigint value
export class Bit255 extends Struct({
head: Field,
tail: Field,
}) {
static fromScalar(scalar: Scalar): Bit255 {
let bits = scalar.toFields();
let bits = scalar.toFields().map((e) => Bool.fromFields([e]));
return new Bit255({
head: Field.fromFields(bits.slice(0, 127)),
tail: Field.fromFields(bits.slice(127)),
head: Field.fromBits(bits.slice(0, 127)),
tail: Field.fromBits(bits.slice(127)),
});
}

static toScalar(b: Bit255): Scalar {
return new CustomScalar({
head: b.head,
tail: b.tail,
}).toScalar();
}

static fromFields(fields: Field[]): Bit255 {
return new Bit255({
head: fields[0],
Expand All @@ -29,10 +37,13 @@ export class Bit255 extends Struct({
}

static xor(a: Bit255, b: Bit255): Bit255 {
return new Bit255({
head: Gadgets.xor(a.head, b.head, 127),
tail: Gadgets.xor(a.tail, b.tail, 128),
});
return Provable.witness(Bit255, () =>
Bit255.fromBigInt(a.toBigInt() ^ b.toBigInt())
);
// return new Bit255({
// head: Gadgets.xor(a.head, b.head, 127),
// tail: Gadgets.xor(a.tail, b.tail, 128),
// });
}

static fromBits(bits: Bool[]): Bit255 {
Expand Down Expand Up @@ -87,6 +98,10 @@ export class Bit255 extends Struct({
return Bit255.toFields(this);
}

toScalar(): Scalar {
return Bit255.toScalar(this);
}

xor(b: Bit255): Bit255 {
return Bit255.xor(this, b);
}
Expand Down
2 changes: 1 addition & 1 deletion src/CustomScalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class CustomScalar extends Struct({
tail: Field,
}) {
static fromScalar(scalar: Scalar): CustomScalar {
let bits = scalar.toFields().map((e) => e.toBits()[0]);
let bits = scalar.toFields().map((e) => Bool.fromFields([e]));
return new CustomScalar({
head: Field.fromBits(bits.slice(0, 127)),
tail: Field.fromBits(bits.slice(127)),
Expand Down

0 comments on commit abfedca

Please sign in to comment.