@thesingularitynetwork/darkswap-sdk 0.1.18 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/test/utils/helpers.d.ts +9 -0
- package/package.json +56 -53
- package/src/abis/DarkSwapAssetManager.json +0 -1162
- package/src/abis/DarkSwapFeeAssetManager.json +0 -282
- package/src/abis/IERC20.json +0 -194
- package/src/abis/IERC20_USDT.json +0 -188
- package/src/abis/MerkleTreeOperator.json +0 -533
- package/src/aztec/bigint-buffer/index.ts +0 -87
- package/src/aztec/collection/object.ts +0 -76
- package/src/aztec/crypto/poseidon/index.ts +0 -18
- package/src/aztec/crypto/schnorr/index.ts +0 -40
- package/src/aztec/crypto/schnorr/signature.ts +0 -107
- package/src/aztec/crypto/serialize.ts +0 -85
- package/src/aztec/crypto/signature/index.ts +0 -17
- package/src/aztec/fields/fields.ts +0 -361
- package/src/aztec/fields/point.ts +0 -208
- package/src/aztec/serialize/buffer_reader.ts +0 -402
- package/src/aztec/serialize/field_reader.ts +0 -154
- package/src/aztec/serialize/free_funcs.ts +0 -193
- package/src/aztec/serialize/serialize.ts +0 -336
- package/src/aztec/serialize/types.ts +0 -19
- package/src/aztec/string/index.ts +0 -39
- package/src/circuits/pro/dark_swap_cancel_order_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_deposit_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_join_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_pro_create_order_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_pro_swap_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_triple_join_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_withdraw_compiled_circuit.json +0 -1
- package/src/circuits/retail/dark_swap_cancel_order_withdraw_compiled_circuit.json +0 -1
- package/src/circuits/retail/dark_swap_retail_deposit_create_order_compiled_circuit.json +0 -1
- package/src/circuits/retail/dark_swap_retail_swap_compiled_circuit.json +0 -1
- package/src/config/chain.ts +0 -9
- package/src/config/config.ts +0 -9
- package/src/config/contractConfig.ts +0 -61
- package/src/config/index.ts +0 -3
- package/src/darkSwap.ts +0 -31
- package/src/entities/error.ts +0 -6
- package/src/entities/index.ts +0 -3
- package/src/entities/token.ts +0 -9
- package/src/entities/types.ts +0 -6
- package/src/index.ts +0 -9
- package/src/proof/baseProofService.ts +0 -34
- package/src/proof/basic/depositProof.ts +0 -101
- package/src/proof/basic/joinProof.ts +0 -112
- package/src/proof/basic/tripleJoinProof.ts +0 -130
- package/src/proof/basic/withdrawProof.ts +0 -97
- package/src/proof/keyService.ts +0 -9
- package/src/proof/noteService.ts +0 -114
- package/src/proof/pro/orders/cancelOrderProof.ts +0 -126
- package/src/proof/pro/orders/createOrderProof.ts +0 -136
- package/src/proof/pro/orders/swapProof.ts +0 -189
- package/src/proof/retail/cancelOrderProof.ts +0 -78
- package/src/proof/retail/depositOrderProof.ts +0 -133
- package/src/proof/retail/swapProof.ts +0 -158
- package/src/services/BaseService.ts +0 -49
- package/src/services/EventService.ts +0 -17
- package/src/services/agent/index.ts +0 -1
- package/src/services/agent/retailSwap.ts +0 -116
- package/src/services/base/deposit.ts +0 -174
- package/src/services/base/index.ts +0 -4
- package/src/services/base/join.ts +0 -135
- package/src/services/base/tripleJoin.ts +0 -161
- package/src/services/base/withdraw.ts +0 -123
- package/src/services/feeRatioService.ts +0 -13
- package/src/services/index.ts +0 -7
- package/src/services/merkletree.ts +0 -46
- package/src/services/noteService.ts +0 -75
- package/src/services/pro/cancelOrder.ts +0 -142
- package/src/services/pro/createOrder.ts +0 -183
- package/src/services/pro/index.ts +0 -3
- package/src/services/pro/proSwap.ts +0 -199
- package/src/services/retail/cancelAndWithdrawOrder.ts +0 -95
- package/src/services/retail/depositAndCreateOrder.ts +0 -150
- package/src/services/retail/index.ts +0 -2
- package/src/types.ts +0 -76
- package/src/utils/constants.ts +0 -3
- package/src/utils/encoders.ts +0 -10
- package/src/utils/formatters.ts +0 -11
- package/src/utils/mimc.ts +0 -143
- package/src/utils/proofUtils.ts +0 -18
- package/src/utils/swapUtils.ts +0 -55
- package/src/utils/util.ts +0 -21
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
|
-
|
|
3
|
-
import { concatenateUint8Arrays } from '../serialize';
|
|
4
|
-
import { SchnorrSignature } from './signature';
|
|
5
|
-
import { GrumpkinScalar } from '../../fields/fields';
|
|
6
|
-
import { Point } from '../../fields/point';
|
|
7
|
-
import { numToInt32BE } from '../../serialize/free_funcs';
|
|
8
|
-
|
|
9
|
-
export * from './signature';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Schnorr signature construction and helper operations.
|
|
13
|
-
*/
|
|
14
|
-
export class Schnorr {
|
|
15
|
-
/**
|
|
16
|
-
* Computes a grumpkin public key from a private key.
|
|
17
|
-
* @param privateKey - The private key.
|
|
18
|
-
* @returns A grumpkin public key.
|
|
19
|
-
*/
|
|
20
|
-
public async computePublicKey(privateKey: GrumpkinScalar): Promise<Point> {
|
|
21
|
-
const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
|
|
22
|
-
const [result] = api.getWasm().callWasmExport('schnorr_compute_public_key', [privateKey.toBuffer()], [64]);
|
|
23
|
-
return Point.fromBuffer(Buffer.from(result));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Constructs a Schnorr signature given a msg and a private key.
|
|
28
|
-
* @param msg - Message over which the signature is constructed.
|
|
29
|
-
* @param privateKey - The private key of the signer.
|
|
30
|
-
* @returns A Schnorr signature of the form (s, e).
|
|
31
|
-
*/
|
|
32
|
-
public async constructSignature(msg: Uint8Array, privateKey: GrumpkinScalar) {
|
|
33
|
-
const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
|
|
34
|
-
const messageArray = concatenateUint8Arrays([numToInt32BE(msg.length), msg]);
|
|
35
|
-
const [s, e] = api
|
|
36
|
-
.getWasm()
|
|
37
|
-
.callWasmExport('schnorr_construct_signature', [messageArray, privateKey.toBuffer()], [32, 32]);
|
|
38
|
-
return new SchnorrSignature(Buffer.from([...s, ...e]));
|
|
39
|
-
}
|
|
40
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import type { Signature } from '../signature';
|
|
2
|
-
import { Fr } from '../../fields/fields';
|
|
3
|
-
import { BufferReader } from '../../serialize/buffer_reader';
|
|
4
|
-
import { mapTuple } from '../../serialize/types';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Schnorr signature used for transactions.
|
|
8
|
-
* @see cpp/barretenberg/cpp/src/barretenberg/crypto/schnorr/schnorr.hpp
|
|
9
|
-
*/
|
|
10
|
-
export class SchnorrSignature implements Signature {
|
|
11
|
-
/**
|
|
12
|
-
* The size of the signature in bytes.
|
|
13
|
-
*/
|
|
14
|
-
public static SIZE = 64;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* An empty signature.
|
|
18
|
-
*/
|
|
19
|
-
public static EMPTY = new SchnorrSignature(Buffer.alloc(64));
|
|
20
|
-
|
|
21
|
-
constructor(private buffer: Buffer) {
|
|
22
|
-
if (buffer.length !== SchnorrSignature.SIZE) {
|
|
23
|
-
throw new Error(`Invalid signature buffer of length ${buffer.length}.`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Determines if the provided signature is valid or not.
|
|
29
|
-
* @param signature - The data to be checked.
|
|
30
|
-
* @returns Boolean indicating if the provided data is a valid schnorr signature.
|
|
31
|
-
*/
|
|
32
|
-
public static isSignature(signature: string) {
|
|
33
|
-
return /^(0x)?[0-9a-f]{128}$/i.test(signature);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Constructs a SchnorrSignature from the provided string.
|
|
38
|
-
* @param signature - The string to be converted to a schnorr signature.
|
|
39
|
-
* @returns The constructed schnorr signature.
|
|
40
|
-
*/
|
|
41
|
-
public static fromString(signature: string) {
|
|
42
|
-
if (!SchnorrSignature.isSignature(signature)) {
|
|
43
|
-
throw new Error(`Invalid signature string: ${signature}`);
|
|
44
|
-
}
|
|
45
|
-
return new SchnorrSignature(Buffer.from(signature.replace(/^0x/i, ''), 'hex'));
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Returns the 's' component of the signature.
|
|
50
|
-
* @returns A buffer containing the signature's 's' component.
|
|
51
|
-
*/
|
|
52
|
-
get s() {
|
|
53
|
-
return this.buffer.subarray(0, 32);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Returns the 'e' component of the signature.
|
|
58
|
-
* @returns A buffer containing the signature's 'e' component.
|
|
59
|
-
*/
|
|
60
|
-
get e() {
|
|
61
|
-
return this.buffer.subarray(32);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Returns the full signature as a buffer.
|
|
66
|
-
* @returns A buffer containing the signature.
|
|
67
|
-
*/
|
|
68
|
-
toBuffer() {
|
|
69
|
-
return this.buffer;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Deserializes from a buffer.
|
|
74
|
-
* @param buffer - The buffer representation of the object.
|
|
75
|
-
* @returns The new object.
|
|
76
|
-
*/
|
|
77
|
-
static fromBuffer(buffer: Buffer | BufferReader): SchnorrSignature {
|
|
78
|
-
const reader = BufferReader.asReader(buffer);
|
|
79
|
-
return new SchnorrSignature(reader.readBytes(SchnorrSignature.SIZE));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Returns the full signature as a hex string.
|
|
84
|
-
* @returns A string containing the signature in hex format.
|
|
85
|
-
*/
|
|
86
|
-
toString() {
|
|
87
|
-
return `0x${this.buffer.toString('hex')}`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Converts the signature to an array of three fields.
|
|
92
|
-
* @returns The signature components as an array of three fields
|
|
93
|
-
*/
|
|
94
|
-
toFields(): Fr[] {
|
|
95
|
-
const sig = this.toBuffer();
|
|
96
|
-
|
|
97
|
-
const buf1 = Buffer.alloc(32);
|
|
98
|
-
const buf2 = Buffer.alloc(32);
|
|
99
|
-
const buf3 = Buffer.alloc(32);
|
|
100
|
-
|
|
101
|
-
sig.copy(buf1, 1, 0, 31);
|
|
102
|
-
sig.copy(buf2, 1, 31, 62);
|
|
103
|
-
sig.copy(buf3, 1, 62, 64);
|
|
104
|
-
|
|
105
|
-
return mapTuple([buf1, buf2, buf3], Fr.fromBuffer);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
// TODO find a new home for this as we move to external bb.js
|
|
2
|
-
// See https://github.com/AztecProtocol/aztec-packages/issues/782
|
|
3
|
-
import { Buffer } from 'buffer';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* For serializing an array of fixed length buffers.
|
|
7
|
-
* TODO move to foundation pkg.
|
|
8
|
-
* @param arr - Array of bufffers.
|
|
9
|
-
* @returns The serialized buffers.
|
|
10
|
-
*/
|
|
11
|
-
export function serializeBufferArrayToVector(arr: Buffer[]) {
|
|
12
|
-
const lengthBuf = Buffer.alloc(4);
|
|
13
|
-
lengthBuf.writeUInt32BE(arr.length, 0);
|
|
14
|
-
return Buffer.concat([lengthBuf, ...arr]);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Helper function for deserializeArrayFromVector.
|
|
19
|
-
*/
|
|
20
|
-
type DeserializeFn<T> = (
|
|
21
|
-
buf: Buffer,
|
|
22
|
-
offset: number,
|
|
23
|
-
) => {
|
|
24
|
-
/**
|
|
25
|
-
* The deserialized type.
|
|
26
|
-
*/
|
|
27
|
-
elem: T;
|
|
28
|
-
/**
|
|
29
|
-
* How many bytes to advance by.
|
|
30
|
-
*/
|
|
31
|
-
adv: number;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* For deserializing numbers to 32-bit little-endian form.
|
|
36
|
-
* TODO move to foundation pkg.
|
|
37
|
-
* @param n - The number.
|
|
38
|
-
* @returns The endian-corrected number.
|
|
39
|
-
*/
|
|
40
|
-
export function deserializeArrayFromVector<T>(deserialize: DeserializeFn<T>, vector: Buffer, offset = 0) {
|
|
41
|
-
let pos = offset;
|
|
42
|
-
const size = vector.readUInt32BE(pos);
|
|
43
|
-
pos += 4;
|
|
44
|
-
const arr = new Array<T>(size);
|
|
45
|
-
for (let i = 0; i < size; ++i) {
|
|
46
|
-
const { elem, adv } = deserialize(vector, pos);
|
|
47
|
-
pos += adv;
|
|
48
|
-
arr[i] = elem;
|
|
49
|
-
}
|
|
50
|
-
return { elem: arr, adv: pos - offset };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* For serializing numbers to 32 bit little-endian form.
|
|
55
|
-
* TODO move to foundation pkg.
|
|
56
|
-
* @param n - The number.
|
|
57
|
-
* @returns The endian-corrected number.
|
|
58
|
-
*/
|
|
59
|
-
export function numToUInt32LE(n: number, bufferSize = 4) {
|
|
60
|
-
const buf = Buffer.alloc(bufferSize);
|
|
61
|
-
buf.writeUInt32LE(n, bufferSize - 4);
|
|
62
|
-
return buf;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Deserialize the 256-bit number at address `offset`.
|
|
67
|
-
* @param buf - The buffer.
|
|
68
|
-
* @param offset - The address.
|
|
69
|
-
* @returns The derserialized 256-bit field.
|
|
70
|
-
*/
|
|
71
|
-
export function deserializeField(buf: Buffer, offset = 0) {
|
|
72
|
-
const adv = 32;
|
|
73
|
-
return { elem: buf.slice(offset, offset + adv), adv };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function concatenateUint8Arrays(arrayOfUint8Arrays: Uint8Array[]) {
|
|
77
|
-
const totalLength = arrayOfUint8Arrays.reduce((prev, curr) => prev + curr.length, 0);
|
|
78
|
-
const result = new Uint8Array(totalLength);
|
|
79
|
-
let length = 0;
|
|
80
|
-
for (const array of arrayOfUint8Arrays) {
|
|
81
|
-
result.set(array, length);
|
|
82
|
-
length += array.length;
|
|
83
|
-
}
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Fr } from "../../fields/fields";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Interface to represent a signature.
|
|
5
|
-
*/
|
|
6
|
-
export interface Signature {
|
|
7
|
-
/**
|
|
8
|
-
* Serializes to a buffer.
|
|
9
|
-
* @returns A buffer.
|
|
10
|
-
*/
|
|
11
|
-
toBuffer(): Buffer;
|
|
12
|
-
/**
|
|
13
|
-
* Serializes to an array of fields.
|
|
14
|
-
* @returns Fields.
|
|
15
|
-
*/
|
|
16
|
-
toFields(): Fr[];
|
|
17
|
-
}
|
|
@@ -1,361 +0,0 @@
|
|
|
1
|
-
import { toBigIntBE, toBufferBE } from '../bigint-buffer';
|
|
2
|
-
import { BufferReader } from '../serialize/buffer_reader';
|
|
3
|
-
|
|
4
|
-
const ZERO_BUFFER = Buffer.alloc(32);
|
|
5
|
-
|
|
6
|
-
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Represents a field derived from BaseField.
|
|
10
|
-
*/
|
|
11
|
-
type DerivedField<T extends BaseField> = {
|
|
12
|
-
new (value: any): T;
|
|
13
|
-
/**
|
|
14
|
-
* All derived fields will specify a MODULUS.
|
|
15
|
-
*/
|
|
16
|
-
MODULUS: bigint;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Base field class.
|
|
21
|
-
* Conversions from Buffer to BigInt and vice-versa are not cheap.
|
|
22
|
-
* We allow construction with either form and lazily convert to other as needed.
|
|
23
|
-
* We only check we are within the field modulus when initializing with bigint.
|
|
24
|
-
*/
|
|
25
|
-
abstract class BaseField {
|
|
26
|
-
static SIZE_IN_BYTES = 32;
|
|
27
|
-
private asBuffer?: Buffer;
|
|
28
|
-
private asBigInt?: bigint;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Return bigint representation.
|
|
32
|
-
* @deprecated Just to get things compiling. Use toBigInt().
|
|
33
|
-
* */
|
|
34
|
-
get value(): bigint {
|
|
35
|
-
return this.toBigInt();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Returns the size in bytes. */
|
|
39
|
-
get size(): number {
|
|
40
|
-
return BaseField.SIZE_IN_BYTES;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
protected constructor(value: number | bigint | boolean | BaseField | Buffer) {
|
|
44
|
-
if (Buffer.isBuffer(value)) {
|
|
45
|
-
if (value.length > BaseField.SIZE_IN_BYTES) {
|
|
46
|
-
throw new Error(`Value length ${value.length} exceeds ${BaseField.SIZE_IN_BYTES}`);
|
|
47
|
-
}
|
|
48
|
-
this.asBuffer =
|
|
49
|
-
value.length === BaseField.SIZE_IN_BYTES
|
|
50
|
-
? value
|
|
51
|
-
: Buffer.concat([Buffer.alloc(BaseField.SIZE_IN_BYTES - value.length), value]);
|
|
52
|
-
} else if (typeof value === 'bigint' || typeof value === 'number' || typeof value === 'boolean') {
|
|
53
|
-
this.asBigInt = BigInt(value);
|
|
54
|
-
if (this.asBigInt >= this.modulus()) {
|
|
55
|
-
throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
|
|
56
|
-
}
|
|
57
|
-
} else if (value instanceof BaseField) {
|
|
58
|
-
this.asBuffer = value.asBuffer;
|
|
59
|
-
this.asBigInt = value.asBigInt;
|
|
60
|
-
} else {
|
|
61
|
-
throw new Error(`Type '${typeof value}' with value '${value}' passed to BaseField ctor.`);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
protected abstract modulus(): bigint;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* We return a copy of the Buffer to ensure this remains immutable.
|
|
69
|
-
*/
|
|
70
|
-
toBuffer(): Buffer {
|
|
71
|
-
if (!this.asBuffer) {
|
|
72
|
-
this.asBuffer = toBufferBE(this.asBigInt!, 32);
|
|
73
|
-
}
|
|
74
|
-
return Buffer.from(this.asBuffer);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
toString(): string {
|
|
78
|
-
return `0x${this.toBuffer().toString('hex')}`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
toBigInt(): bigint {
|
|
82
|
-
if (this.asBigInt === undefined) {
|
|
83
|
-
this.asBigInt = toBigIntBE(this.asBuffer!);
|
|
84
|
-
if (this.asBigInt >= this.modulus()) {
|
|
85
|
-
throw new Error(`Value 0x${this.asBigInt.toString(16)} is greater or equal to field modulus.`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return this.asBigInt;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
toBool(): boolean {
|
|
92
|
-
return Boolean(this.toBigInt());
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Converts this field to a number.
|
|
97
|
-
* Throws if the underlying value is greater than MAX_SAFE_INTEGER.
|
|
98
|
-
*/
|
|
99
|
-
toNumber(): number {
|
|
100
|
-
const value = this.toBigInt();
|
|
101
|
-
if (value > Number.MAX_SAFE_INTEGER) {
|
|
102
|
-
throw new Error(`Value ${value.toString(16)} greater than than max safe integer`);
|
|
103
|
-
}
|
|
104
|
-
return Number(value);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Converts this field to a number.
|
|
109
|
-
* May cause loss of precision if the underlying value is greater than MAX_SAFE_INTEGER.
|
|
110
|
-
*/
|
|
111
|
-
toNumberUnsafe(): number {
|
|
112
|
-
const value = this.toBigInt();
|
|
113
|
-
return Number(value);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
toShortString(): string {
|
|
117
|
-
const str = this.toString();
|
|
118
|
-
return `${str.slice(0, 10)}...${str.slice(-4)}`;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
equals(rhs: BaseField): boolean {
|
|
122
|
-
return this.toBuffer().equals(rhs.toBuffer());
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
lt(rhs: BaseField): boolean {
|
|
126
|
-
return this.toBigInt() < rhs.toBigInt();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
cmp(rhs: BaseField): -1 | 0 | 1 {
|
|
130
|
-
const lhsBigInt = this.toBigInt();
|
|
131
|
-
const rhsBigInt = rhs.toBigInt();
|
|
132
|
-
return lhsBigInt === rhsBigInt ? 0 : lhsBigInt < rhsBigInt ? -1 : 1;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
isZero(): boolean {
|
|
136
|
-
return this.toBuffer().equals(ZERO_BUFFER);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
isEmpty(): boolean {
|
|
140
|
-
return this.isZero();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
toFriendlyJSON(): string {
|
|
144
|
-
return this.toString();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
toField() {
|
|
148
|
-
return this;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Constructs a field from a Buffer of BufferReader.
|
|
154
|
-
* It maybe not read the full 32 bytes if the Buffer is shorter, but it will padded in BaseField constructor.
|
|
155
|
-
*/
|
|
156
|
-
export function fromBuffer<T extends BaseField>(buffer: Buffer | BufferReader, f: DerivedField<T>) {
|
|
157
|
-
const reader = BufferReader.asReader(buffer);
|
|
158
|
-
return new f(reader.readBytes(BaseField.SIZE_IN_BYTES));
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Constructs a field from a Buffer, but reduces it first, modulo the field modulus.
|
|
163
|
-
* This requires a conversion to a bigint first so the initial underlying representation will be a bigint.
|
|
164
|
-
*/
|
|
165
|
-
function fromBufferReduce<T extends BaseField>(buffer: Buffer, f: DerivedField<T>) {
|
|
166
|
-
return new f(toBigIntBE(buffer) % f.MODULUS);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Constructs a field from a 0x prefixed hex string.
|
|
171
|
-
*/
|
|
172
|
-
function fromHexString<T extends BaseField>(buf: string, f: DerivedField<T>) {
|
|
173
|
-
const withoutPrefix = buf.replace(/^0x/i, '');
|
|
174
|
-
const checked = withoutPrefix.match(/^[0-9A-F]+$/i)?.[0];
|
|
175
|
-
if (checked === undefined) {
|
|
176
|
-
throw new Error(`Invalid hex-encoded string: "${buf}"`);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const buffer = Buffer.from(checked.length % 2 === 1 ? '0' + checked : checked, 'hex');
|
|
180
|
-
|
|
181
|
-
return new f(buffer);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/** Branding to ensure fields are not interchangeable types. */
|
|
185
|
-
export interface Fr {
|
|
186
|
-
/** Brand. */
|
|
187
|
-
_branding: 'Fr';
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Fr field class.
|
|
192
|
-
* @dev This class is used to represent elements of BN254 scalar field or elements in the base field of Grumpkin.
|
|
193
|
-
* (Grumpkin's scalar field corresponds to BN254's base field and vice versa.)
|
|
194
|
-
*/
|
|
195
|
-
export class Fr extends BaseField {
|
|
196
|
-
static ZERO = new Fr(0n);
|
|
197
|
-
static ONE = new Fr(1n);
|
|
198
|
-
static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
199
|
-
static MAX_FIELD_VALUE = new Fr(Fr.MODULUS - 1n);
|
|
200
|
-
|
|
201
|
-
constructor(value: number | bigint | boolean | Fr | Buffer) {
|
|
202
|
-
super(value);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
protected modulus() {
|
|
206
|
-
return Fr.MODULUS;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
static zero() {
|
|
210
|
-
return Fr.ZERO;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
static isZero(value: Fr) {
|
|
214
|
-
return value.isZero();
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
218
|
-
return fromBuffer(buffer, Fr);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
static fromBufferReduce(buffer: Buffer) {
|
|
222
|
-
return fromBufferReduce(buffer, Fr);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Creates a Fr instance from a string.
|
|
227
|
-
* @param buf - the string to create a Fr from.
|
|
228
|
-
* @returns the Fr instance
|
|
229
|
-
* @remarks if the string only consists of numbers, we assume we are parsing a bigint,
|
|
230
|
-
* otherwise we require the hex string to be prepended with "0x", to ensure there is no misunderstanding
|
|
231
|
-
* as to what is being parsed.
|
|
232
|
-
*/
|
|
233
|
-
static fromString(buf: string) {
|
|
234
|
-
if (buf.match(/^\d+$/) !== null) {
|
|
235
|
-
return new Fr(toBufferBE(BigInt(buf), 32));
|
|
236
|
-
}
|
|
237
|
-
if (buf.match(/^0x/i) !== null) {
|
|
238
|
-
return fromHexString(buf, Fr);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
throw new Error(`Tried to create a Fr from an invalid string: ${buf}`);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Creates a Fr instance from a hex string.
|
|
246
|
-
* @param buf - a hex encoded string.
|
|
247
|
-
* @returns the Fr instance
|
|
248
|
-
*/
|
|
249
|
-
static fromHexString(buf: string) {
|
|
250
|
-
return fromHexString(buf, Fr);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Branding to ensure fields are not interchangeable types.
|
|
257
|
-
*/
|
|
258
|
-
export interface Fq {
|
|
259
|
-
/** Brand. */
|
|
260
|
-
_branding: 'Fq';
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Fq field class.
|
|
265
|
-
* @dev This class is used to represent elements of BN254 base field or elements in the scalar field of Grumpkin.
|
|
266
|
-
* (Grumpkin's scalar field corresponds to BN254's base field and vice versa.)
|
|
267
|
-
*/
|
|
268
|
-
export class Fq extends BaseField {
|
|
269
|
-
static ZERO = new Fq(0n);
|
|
270
|
-
static MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
|
|
271
|
-
private static HIGH_SHIFT = BigInt((BaseField.SIZE_IN_BYTES / 2) * 8);
|
|
272
|
-
private static LOW_MASK = (1n << Fq.HIGH_SHIFT) - 1n;
|
|
273
|
-
|
|
274
|
-
get lo(): Fr {
|
|
275
|
-
return new Fr(this.toBigInt() & Fq.LOW_MASK);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
get hi(): Fr {
|
|
279
|
-
return new Fr(this.toBigInt() >> Fq.HIGH_SHIFT);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
constructor(value: number | bigint | boolean | Fq | Buffer) {
|
|
283
|
-
super(value);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
protected modulus() {
|
|
287
|
-
return Fq.MODULUS;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
static zero() {
|
|
291
|
-
return Fq.ZERO;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
295
|
-
return fromBuffer(buffer, Fq);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
static fromBufferReduce(buffer: Buffer) {
|
|
299
|
-
return fromBufferReduce(buffer, Fq);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Creates a Fq instance from a string.
|
|
304
|
-
* @param buf - the string to create a Fq from.
|
|
305
|
-
* @returns the Fq instance
|
|
306
|
-
* @remarks if the string only consists of numbers, we assume we are parsing a bigint,
|
|
307
|
-
* otherwise we require the hex string to be prepended with "0x", to ensure there is no misunderstanding
|
|
308
|
-
* as to what is being parsed.
|
|
309
|
-
*/
|
|
310
|
-
static fromString(buf: string) {
|
|
311
|
-
if (buf.match(/^\d+$/) !== null) {
|
|
312
|
-
return new Fq(toBufferBE(BigInt(buf), 32));
|
|
313
|
-
}
|
|
314
|
-
if (buf.match(/^0x/i) !== null) {
|
|
315
|
-
return fromHexString(buf, Fq);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
throw new Error(`Tried to create a Fq from an invalid string: ${buf}`);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Creates a Fq instance from a hex string.
|
|
323
|
-
* @param buf - a hex encoded string.
|
|
324
|
-
* @returns the Fq instance
|
|
325
|
-
*/
|
|
326
|
-
static fromHexString(buf: string) {
|
|
327
|
-
return fromHexString(buf, Fq);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
static fromHighLow(high: Fr, low: Fr): Fq {
|
|
331
|
-
return new Fq((high.toBigInt() << Fq.HIGH_SHIFT) + low.toBigInt());
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
add(rhs: Fq) {
|
|
335
|
-
return new Fq((this.toBigInt() + rhs.toBigInt()) % Fq.MODULUS);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
toJSON() {
|
|
339
|
-
return this.toString();
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
toFields() {
|
|
343
|
-
// The following has to match the order of the limbs in EmbeddedCurveScalar struct in noir::std. This is because
|
|
344
|
-
// this function is used when returning Scalar from the getAddressSecret oracle and in Noir the values get deserialized
|
|
345
|
-
// using the intrinsic serialization of Noir (which follows the order of the fields/members in the struct).
|
|
346
|
-
return [this.lo, this.hi];
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* GrumpkinScalar is an Fq.
|
|
352
|
-
* @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
|
|
353
|
-
* the Aztec Protocol Specs.
|
|
354
|
-
*/
|
|
355
|
-
export type GrumpkinScalar = Fq;
|
|
356
|
-
export const GrumpkinScalar = Fq;
|
|
357
|
-
|
|
358
|
-
/** Wraps a function that returns a buffer so that all results are reduced into a field of the given type. */
|
|
359
|
-
export function reduceFn<TInput, TField extends BaseField>(fn: (input: TInput) => Buffer, field: DerivedField<TField>) {
|
|
360
|
-
return (input: TInput) => fromBufferReduce(fn(input), field);
|
|
361
|
-
}
|