-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chen Kai <281165273grape@gmail.com>
- Loading branch information
Showing
10 changed files
with
440 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/RLPEncodingHelpers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.optimism.utilities.derive.stages; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.MutableBytes; | ||
|
||
/** | ||
* Helper static methods to facilitate RLP encoding <b>within this package</b>. Neither this class | ||
* nor any of its method are meant to be exposed publicly, they are too low level. | ||
*/ | ||
class RLPEncodingHelpers { | ||
private RLPEncodingHelpers() {} | ||
|
||
static boolean isSingleRLPByte(final Bytes value) { | ||
return value.size() == 1 && value.get(0) >= 0; | ||
} | ||
|
||
static boolean isShortElement(final Bytes value) { | ||
return value.size() <= 55; | ||
} | ||
|
||
static boolean isShortList(final int payloadSize) { | ||
return payloadSize <= 55; | ||
} | ||
|
||
/** The encoded size of the provided value. */ | ||
static int elementSize(final Bytes value) { | ||
if (isSingleRLPByte(value)) return 1; | ||
|
||
if (isShortElement(value)) return 1 + value.size(); | ||
|
||
return 1 + sizeLength(value.size()) + value.size(); | ||
} | ||
|
||
/** The encoded size of a list given the encoded size of its payload. */ | ||
static int listSize(final int payloadSize) { | ||
int size = 1 + payloadSize; | ||
if (!isShortList(payloadSize)) size += sizeLength(payloadSize); | ||
return size; | ||
} | ||
|
||
/** | ||
* Writes the result of encoding the provided value to the provided destination (which must be big | ||
* enough). | ||
*/ | ||
static int writeElement(final Bytes value, final MutableBytes dest, final int destOffset) { | ||
final int size = value.size(); | ||
if (isSingleRLPByte(value)) { | ||
dest.set(destOffset, value.get(0)); | ||
return destOffset + 1; | ||
} | ||
|
||
if (isShortElement(value)) { | ||
dest.set(destOffset, (byte) (0x80 + size)); | ||
value.copyTo(dest, destOffset + 1); | ||
return destOffset + 1 + size; | ||
} | ||
|
||
final int offset = writeLongMetadata(0xb7, size, dest, destOffset); | ||
value.copyTo(dest, offset); | ||
return offset + size; | ||
} | ||
|
||
/** | ||
* Writes the encoded header of a list provided its encoded payload size to the provided | ||
* destination (which must be big enough). | ||
*/ | ||
static int writeListHeader(final int payloadSize, final MutableBytes dest, final int destOffset) { | ||
if (isShortList(payloadSize)) { | ||
dest.set(destOffset, (byte) (0xc0 + payloadSize)); | ||
return destOffset + 1; | ||
} | ||
|
||
return writeLongMetadata(0xf7, payloadSize, dest, destOffset); | ||
} | ||
|
||
private static int writeLongMetadata( | ||
final int baseCode, final int size, final MutableBytes dest, final int destOffset) { | ||
final int sizeLength = sizeLength(size); | ||
dest.set(destOffset, (byte) (baseCode + sizeLength)); | ||
int shift = 0; | ||
for (int i = 0; i < sizeLength; i++) { | ||
dest.set(destOffset + sizeLength - i, (byte) (size >> shift)); | ||
shift += 8; | ||
} | ||
return destOffset + 1 + sizeLength; | ||
} | ||
|
||
private static int sizeLength(final int size) { | ||
final int zeros = Integer.numberOfLeadingZeros(size); | ||
return 4 - (zeros / 8); | ||
} | ||
} |
55 changes: 54 additions & 1 deletion
55
hildr-utilities/src/main/java/io/optimism/utilities/derive/stages/SpanBatchSignature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,58 @@ | ||
package io.optimism.utilities.derive.stages; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Objects; | ||
|
||
public record SpanBatchSignature(BigInteger v, BigInteger r, BigInteger s) {} | ||
public class SpanBatchSignature { | ||
private BigInteger v; | ||
private BigInteger r; | ||
private BigInteger s; | ||
|
||
public SpanBatchSignature(BigInteger v, BigInteger r, BigInteger s) { | ||
this.v = v; | ||
this.r = r; | ||
this.s = s; | ||
} | ||
|
||
public BigInteger v() { | ||
return v; | ||
} | ||
|
||
public BigInteger r() { | ||
return r; | ||
} | ||
|
||
public BigInteger s() { | ||
return s; | ||
} | ||
|
||
public void setV(BigInteger v) { | ||
this.v = v; | ||
} | ||
|
||
public void setR(BigInteger r) { | ||
this.r = r; | ||
} | ||
|
||
public void setS(BigInteger s) { | ||
this.s = s; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) return true; | ||
if (obj == null || obj.getClass() != this.getClass()) return false; | ||
var that = (SpanBatchSignature) obj; | ||
return Objects.equals(this.v, that.v) && Objects.equals(this.r, that.r) && Objects.equals(this.s, that.s); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(v, r, s); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SpanBatchSignature[" + "v=" + v + ", " + "r=" + r + ", " + "s=" + s + ']'; | ||
} | ||
} |
Oops, something went wrong.