@@ -118,19 +118,38 @@ const OPERAND_SPEC = new Map<OperandType, [number, (offset: number) => OperandNa
118118 [ OperandType . UINT8 , [ 1 , Buffer . prototype . readUint8 , Buffer . prototype . writeUint8 ] ] ,
119119 [ OperandType . UINT16 , [ 2 , Buffer . prototype . readUint16BE , Buffer . prototype . writeUint16BE ] ] ,
120120 [ OperandType . UINT32 , [ 4 , Buffer . prototype . readUint32BE , Buffer . prototype . writeUint32BE ] ] ,
121- [ OperandType . UINT64 , [ 8 , Buffer . prototype . readBigInt64BE , Buffer . prototype . writeBigInt64BE ] ] ,
122- [ OperandType . UINT128 , [ 16 , readBigInt128BE , writeBigInt128BE ] ] ,
123- [ OperandType . FF , [ 32 , readBigInt254BE , writeBigInt254BE ] ] ,
121+ [ OperandType . UINT64 , [ 8 , readUint64BE , writeUint64BE ] ] ,
122+ [ OperandType . UINT128 , [ 16 , readUint128BE , writeUint128BE ] ] ,
123+ [ OperandType . FF , [ 32 , readUint254BE , writeUint254BE ] ] ,
124124 [ OperandType . TAG , [ 1 , Buffer . prototype . readUint8 , Buffer . prototype . writeUint8 ] ] ,
125125] ) ;
126126
127- function readBigInt254BE ( this : Buffer , offset : number ) : bigint {
128- const totalBytes = 32 ;
127+ function readUintBE ( buf : Buffer , offset : number , totalBytes : number ) : bigint {
129128 let value : bigint = 0n ;
130129 for ( let i = 0 ; i < totalBytes ; ++ i ) {
131130 value <<= 8n ;
132- value |= BigInt ( this . readUint8 ( i + offset ) ) ;
131+ value |= BigInt ( buf . readUint8 ( i + offset ) ) ;
133132 }
133+ return value ;
134+ }
135+
136+ function writeUintBE ( buf : Buffer , value : bigint , totalBytes : number ) : void {
137+ for ( let offset = totalBytes - 1 ; offset >= 0 ; -- offset ) {
138+ buf . writeUint8 ( Number ( value & 0xffn ) , offset ) ;
139+ value >>= 8n ;
140+ }
141+ }
142+
143+ function readUint64BE ( this : Buffer , offset : number ) : bigint {
144+ return readUintBE ( this , offset , 8 ) ;
145+ }
146+
147+ function writeUint64BE ( this : Buffer , value : bigint ) : void {
148+ writeUintBE ( this , value , 8 ) ;
149+ }
150+
151+ function readUint254BE ( this : Buffer , offset : number ) : bigint {
152+ let value = readUintBE ( this , offset , 32 ) ;
134153
135154 // In circuit, we only support values up to Fr.MODULUS and any deserialized value
136155 // would naturally undergo a modulus reduction.
@@ -141,30 +160,16 @@ function readBigInt254BE(this: Buffer, offset: number): bigint {
141160 return value ;
142161}
143162
144- function writeBigInt254BE ( this : Buffer , value : bigint ) : void {
145- const totalBytes = 32 ;
146- for ( let offset = totalBytes - 1 ; offset >= 0 ; -- offset ) {
147- this . writeUint8 ( Number ( value & 0xffn ) , offset ) ;
148- value >>= 8n ;
149- }
163+ function writeUint254BE ( this : Buffer , value : bigint ) : void {
164+ writeUintBE ( this , value , 32 ) ;
150165}
151166
152- function readBigInt128BE ( this : Buffer , offset : number ) : bigint {
153- const totalBytes = 16 ;
154- let ret : bigint = 0n ;
155- for ( let i = 0 ; i < totalBytes ; ++ i ) {
156- ret <<= 8n ;
157- ret |= BigInt ( this . readUint8 ( i + offset ) ) ;
158- }
159- return ret ;
167+ function readUint128BE ( this : Buffer , offset : number ) : bigint {
168+ return readUintBE ( this , offset , 16 ) ;
160169}
161170
162- function writeBigInt128BE ( this : Buffer , value : bigint ) : void {
163- const totalBytes = 16 ;
164- for ( let offset = totalBytes - 1 ; offset >= 0 ; -- offset ) {
165- this . writeUint8 ( Number ( value & 0xffn ) , offset ) ;
166- value >>= 8n ;
167- }
171+ function writeUint128BE ( this : Buffer , value : bigint ) : void {
172+ writeUintBE ( this , value , 16 ) ;
168173}
169174
170175/**
0 commit comments