Skip to content

Commit

Permalink
create field class
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Mar 15, 2024
1 parent 3cbaa1d commit b195c5a
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
BigNumberish,
toBigInt,
toBeHex,
randomBytes,
zeroPadValue,
} from 'ethers';

const PRIME =
0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;

export class Field {
public value: bigint;
constructor(_value: BigNumberish) {
this.value = toBigInt(_value);
if (this.value < 0n) {
while (this.value < 0n) {
this.value = this.value + PRIME;
}
}
}

static from(value: BigNumberish | Field): Field {
if (value instanceof Field) {
return value;
}
return new Field(value);
}

static zero() {
return new Field(0n);
}

add(other: Field): Field {
return new Field((this.value + other.value) % PRIME);
}

sub(other: Field): Field {
return new Field((this.value - other.value + PRIME) % PRIME);
}

mul(other: Field): Field {
return new Field((this.value * other.value) % PRIME);
}
}

0 comments on commit b195c5a

Please sign in to comment.