Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit 799c2be

Browse files
author
Mark Erhardt
authored
Merge pull request #76 from BitGo/BG-16466.move-zcash-readwrite-to-bufferutils
refactor(zcash): move read/write to bufferutils
2 parents 5e171d9 + 06263f4 commit 799c2be

File tree

3 files changed

+178
-163
lines changed

3 files changed

+178
-163
lines changed

src/forks/zcash/bufferutils.js

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,156 @@
1-
const { BufferReader } = require('../../bufferutils')
1+
const typeforce = require('typeforce')
2+
const types = require('../../types')
3+
const version = require('./version')
4+
5+
const { BufferReader, BufferWriter } = require('../../bufferutils')
6+
7+
const NUM_JOINSPLITS_INPUTS = 2
8+
const NUM_JOINSPLITS_OUTPUTS = 2
9+
const NOTECIPHERTEXT_SIZE = 1 + 8 + 32 + 32 + 512 + 16
10+
11+
const G1_PREFIX_MASK = 0x02
12+
const G2_PREFIX_MASK = 0x0a
213

314
class ZcashBufferReader extends BufferReader {
15+
constructor (buffer, offset, txVersion) {
16+
super(buffer, offset)
17+
typeforce(types.maybe(types.Int32), txVersion)
18+
this.txVersion = txVersion
19+
}
20+
421
readInt64 () {
522
const a = this.buffer.readUInt32LE(this.offset)
623
let b = this.buffer.readInt32LE(this.offset + 4)
724
b *= 0x100000000
825
this.offset += 8
926
return b + a
1027
}
28+
29+
readCompressedG1 () {
30+
var yLsb = this.readUInt8() & 1
31+
var x = this.readSlice(32)
32+
return {
33+
x: x,
34+
yLsb: yLsb
35+
}
36+
}
37+
38+
readCompressedG2 () {
39+
var yLsb = this.readUInt8() & 1
40+
var x = this.readSlice(64)
41+
return {
42+
x: x,
43+
yLsb: yLsb
44+
}
45+
}
46+
47+
readZKProof () {
48+
var zkproof
49+
if (this.txVersion >= version.SAPLING) {
50+
zkproof = {
51+
sA: this.readSlice(48),
52+
sB: this.readSlice(96),
53+
sC: this.readSlice(48)
54+
}
55+
} else {
56+
zkproof = {
57+
gA: this.readCompressedG1(),
58+
gAPrime: this.readCompressedG1(),
59+
gB: this.readCompressedG2(),
60+
gBPrime: this.readCompressedG1(),
61+
gC: this.readCompressedG1(),
62+
gCPrime: this.readCompressedG1(),
63+
gK: this.readCompressedG1(),
64+
gH: this.readCompressedG1()
65+
}
66+
}
67+
return zkproof
68+
}
69+
70+
readJoinSplit () {
71+
var vpubOld = this.readUInt64()
72+
var vpubNew = this.readUInt64()
73+
var anchor = this.readSlice(32)
74+
var nullifiers = []
75+
for (var j = 0; j < NUM_JOINSPLITS_INPUTS; j++) {
76+
nullifiers.push(this.readSlice(32))
77+
}
78+
var commitments = []
79+
for (j = 0; j < NUM_JOINSPLITS_OUTPUTS; j++) {
80+
commitments.push(this.readSlice(32))
81+
}
82+
var ephemeralKey = this.readSlice(32)
83+
var randomSeed = this.readSlice(32)
84+
var macs = []
85+
for (j = 0; j < NUM_JOINSPLITS_INPUTS; j++) {
86+
macs.push(this.readSlice(32))
87+
}
88+
89+
var zkproof = this.readZKProof()
90+
var ciphertexts = []
91+
for (j = 0; j < NUM_JOINSPLITS_OUTPUTS; j++) {
92+
ciphertexts.push(this.readSlice(NOTECIPHERTEXT_SIZE))
93+
}
94+
return {
95+
vpubOld: vpubOld,
96+
vpubNew: vpubNew,
97+
anchor: anchor,
98+
nullifiers: nullifiers,
99+
commitments: commitments,
100+
ephemeralKey: ephemeralKey,
101+
randomSeed: randomSeed,
102+
macs: macs,
103+
zkproof: zkproof,
104+
ciphertexts: ciphertexts
105+
}
106+
}
107+
108+
readShieldedSpend () {
109+
var cv = this.readSlice(32)
110+
var anchor = this.readSlice(32)
111+
var nullifier = this.readSlice(32)
112+
var rk = this.readSlice(32)
113+
var zkproof = this.readZKProof()
114+
var spendAuthSig = this.readSlice(64)
115+
return {
116+
cv: cv,
117+
anchor: anchor,
118+
nullifier: nullifier,
119+
rk: rk,
120+
zkproof: zkproof,
121+
spendAuthSig: spendAuthSig
122+
}
123+
}
124+
125+
readShieldedOutput () {
126+
var cv = this.readSlice(32)
127+
var cmu = this.readSlice(32)
128+
var ephemeralKey = this.readSlice(32)
129+
var encCiphertext = this.readSlice(580)
130+
var outCiphertext = this.readSlice(80)
131+
var zkproof = this.readZKProof()
132+
133+
return {
134+
cv: cv,
135+
cmu: cmu,
136+
ephemeralKey: ephemeralKey,
137+
encCiphertext: encCiphertext,
138+
outCiphertext: outCiphertext,
139+
zkproof: zkproof
140+
}
141+
}
142+
}
143+
144+
class ZcashBufferWriter extends BufferWriter {
145+
writeCompressedG1 (i) {
146+
this.writeUInt8(G1_PREFIX_MASK | i.yLsb)
147+
this.writeSlice(i.x)
148+
}
149+
150+
writeCompressedG2 (i) {
151+
this.writeUInt8(G2_PREFIX_MASK | i.yLsb)
152+
this.writeSlice(i.x)
153+
}
11154
}
12155

13-
module.exports = { ZcashBufferReader }
156+
module.exports = { ZcashBufferReader, ZcashBufferWriter }

src/forks/zcash/version.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
JOINSPLITS_SUPPORT: 2,
3+
OVERWINTER: 3,
4+
SAPLING: 4
5+
}

0 commit comments

Comments
 (0)