Repository navigation
Expand file tree
/
Copy pathcommitment.ts
More file actions
79 lines (76 loc) 路 2.5 KB
/
Copy pathcommitment.ts
File metadata and controls
79 lines (76 loc) 路 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Address } from "../crosschain/address";
import { ChainGroup } from "../crosschain/chains";
import { poseidon } from "../crypto/minaPoseidon";
import { keccak256Uint32 } from "../crypto/sha3";
import address from "../mina/address";
import base64 from "../util/base64";
import bigints from "../util/bigints";
import hex from "../util/hex";
import { HumanID, Section } from "./section.d";
const commit = (
chainGroup: ChainGroup,
ownerAddress: Address,
commitmentR: string
): string => {
switch (chainGroup) {
default:
case ChainGroup.EVM:
const buff = new Uint8Array(32 + 20);
base64.intoBytes(buff, commitmentR);
hex.intoBytes(buff.subarray(32), ownerAddress.slice(2));
return base64.from(new Uint8Array(
keccak256Uint32(new Uint32Array(buff.buffer)).buffer, 0, 32));
case ChainGroup.MINA:
const { x, yParity } = address.toPublicKey(ownerAddress);
const commitmentBytes = base64.toBytes(commitmentR);
const outBuff = new Uint8Array(32);
bigints.intoBytesLE(outBuff, poseidon([
bigints.fromBytesLE(commitmentBytes), yParity ? x + 1n : x
]));
return base64.from(outBuff);
}
}
/**
* Given a random array of length 2x32 Uint8Array, outputs a 72 bytes
* Uint8Array with the following layout:
*
* 32 bytes {@link Section.commitment}
* 32 bytes {@link HumanID.commitment}
* 8 bytes {@link Uint8Array(8)} padding for pow nonce
*
* @satisfies {PureFn}
*/
const commitDouble = (
chainGroup: ChainGroup,
ownerAddress: Address,
random: Uint8Array
): Uint8Array => {
switch (chainGroup) {
default:
case ChainGroup.EVM: {
const buff = new Uint8Array(32 + 20);
hex.intoBytes(buff.subarray(32), ownerAddress.slice(2));
buff.set(random.subarray(0, 32));
const commitment = new Uint32Array(18);
commitment.set(keccak256Uint32(new Uint32Array(buff.buffer)), 0);
buff.set(random.subarray(32, 64));
commitment.set(keccak256Uint32(new Uint32Array(buff.buffer)), 8);
return new Uint8Array(commitment.buffer, 0, 72);
}
case ChainGroup.MINA:
const commitment = new Uint8Array(72);
const { x, yParity } = address.toPublicKey(ownerAddress);
const h = yParity ? x + 1n : x;
bigints.intoBytesLE(
commitment,
poseidon([bigints.fromBytesLE(random.subarray(0, 32)), h]));
bigints.intoBytesLE(
commitment,
poseidon([bigints.fromBytesLE(random.subarray(32)), h]), 32);
return commitment;
}
}
export {
commit,
commitDouble
};