-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commitment mode parameter and commitment type system
- Loading branch information
Showing
30 changed files
with
665 additions
and
461 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
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
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
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,67 @@ | ||
package commitments | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
) | ||
|
||
type DAServiceOPCommitmentType byte | ||
|
||
const ( | ||
EigenDAByte DAServiceOPCommitmentType = 0 | ||
) | ||
|
||
// DAServiceOPCommitment represents a value of one of two possible types (Keccak256Commitment or DAServiceCommitment). | ||
type DAServiceOPCommitment struct { | ||
eigendaCommitment *EigenDACommitment | ||
} | ||
|
||
var _ Commitment = (*DAServiceOPCommitment)(nil) | ||
|
||
func OptimismEigenDACommitment(value EigenDACommitment) DAServiceOPCommitment { | ||
return DAServiceOPCommitment{eigendaCommitment: &value} | ||
} | ||
|
||
func (e DAServiceOPCommitment) IsEigenDA() bool { | ||
return e.eigendaCommitment != nil | ||
} | ||
|
||
func (e DAServiceOPCommitment) MustEigenDAValue() EigenDACommitment { | ||
if e.eigendaCommitment != nil { | ||
return *e.eigendaCommitment | ||
} | ||
log.Panic("CommitmentEither does not contain a Keccak256Commitment value") | ||
return EigenDACommitment{} // This will never be reached, but is required for compilation. | ||
} | ||
|
||
func (e DAServiceOPCommitment) Marshal() ([]byte, error) { | ||
if e.IsEigenDA() { | ||
eigenDABytes, err := e.MustEigenDAValue().Marshal() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append([]byte{byte(EigenDAByte)}, eigenDABytes...), nil | ||
} else { | ||
return nil, fmt.Errorf("DAServiceOPCommitment is neither a keccak256 commitment or a DA service commitment") | ||
} | ||
} | ||
|
||
func (e *DAServiceOPCommitment) Unmarshal(bz []byte) error { | ||
if len(bz) < 1 { | ||
return fmt.Errorf("OP commitment does not contain generic commitment type prefix byte") | ||
} | ||
head := DAServiceOPCommitmentType(bz[0]) | ||
tail := bz[1:] | ||
switch head { | ||
case EigenDAByte: | ||
eigendaCommitment := EigenDACommitment{} | ||
err := eigendaCommitment.Unmarshal(tail) | ||
if err != nil { | ||
return err | ||
} | ||
e.eigendaCommitment = &eigendaCommitment | ||
default: | ||
return fmt.Errorf("unrecognized generic commitment type byte: %x", bz[0]) | ||
} | ||
return nil | ||
} |
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,58 @@ | ||
package commitments | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
) | ||
|
||
// Define the parent and child types | ||
type CertEncodingVersion byte | ||
|
||
const ( | ||
CertEncodingV0 CertEncodingVersion = 0 | ||
) | ||
|
||
type EigenDACommitment struct { | ||
certV0 []byte | ||
} | ||
|
||
var _ Commitment = (*EigenDACommitment)(nil) | ||
|
||
func EigenDACertV0(value []byte) EigenDACommitment { | ||
return EigenDACommitment{certV0: value} | ||
} | ||
|
||
func (e EigenDACommitment) IsCertV0() bool { | ||
return e.certV0 != nil | ||
} | ||
|
||
func (e EigenDACommitment) MustCertV0Value() []byte { | ||
if e.certV0 != nil { | ||
return e.certV0 | ||
} | ||
log.Panic("CommitmentEither does not contain a Keccak256Commitment value") | ||
return nil // This will never be reached, but is required for compilation. | ||
} | ||
|
||
func (e EigenDACommitment) Marshal() ([]byte, error) { | ||
if e.IsCertV0() { | ||
return append([]byte{byte(CertEncodingV0)}, e.certV0...), nil | ||
} else { | ||
return nil, fmt.Errorf("EigenDADAServiceOPCommitment is of unknown type") | ||
} | ||
} | ||
|
||
func (e *EigenDACommitment) Unmarshal(bz []byte) error { | ||
if len(bz) < 1 { | ||
return fmt.Errorf("OP commitment does not contain eigenda commitment encoding version prefix byte") | ||
} | ||
head := CertEncodingVersion(bz[0]) | ||
tail := bz[1:] | ||
switch head { | ||
case CertEncodingV0: | ||
e.certV0 = tail | ||
default: | ||
return fmt.Errorf("unrecognized EigenDA commitment encoding type byte: %x", bz[0]) | ||
} | ||
return nil | ||
} |
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,6 @@ | ||
package commitments | ||
|
||
type Commitment interface { | ||
Marshal() ([]byte, error) | ||
Unmarshal([]byte) error | ||
} |
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,90 @@ | ||
package commitments | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
) | ||
|
||
type OPCommitmentType byte | ||
|
||
const ( | ||
// Keccak256CommitmentTypeByte represents a commitment using Keccak256 hashing. | ||
Keccak256CommitmentTypeByte OPCommitmentType = 0 | ||
// GenericCommitmentTypeByte represents a commitment using a DA service. | ||
GenericCommitmentTypeByte OPCommitmentType = 1 | ||
) | ||
|
||
type OPCommitment struct { | ||
keccak256Commitment []byte | ||
genericCommitment *DAServiceOPCommitment | ||
} | ||
|
||
var _ Commitment = (*OPCommitment)(nil) | ||
|
||
func Keccak256Commitment(value []byte) OPCommitment { | ||
return OPCommitment{keccak256Commitment: value} | ||
} | ||
|
||
func GenericCommitment(value DAServiceOPCommitment) OPCommitment { | ||
return OPCommitment{genericCommitment: &value} | ||
} | ||
|
||
func (e OPCommitment) IsKeccak256Commitment() bool { | ||
return e.keccak256Commitment != nil | ||
} | ||
|
||
func (e OPCommitment) IsGenericCommitment() bool { | ||
return e.genericCommitment != nil | ||
} | ||
|
||
func (e OPCommitment) MustKeccak256CommitmentValue() []byte { | ||
if e.keccak256Commitment != nil { | ||
return e.keccak256Commitment | ||
} | ||
log.Panic("OPCommitment does not contain a Keccak256Commitment value") | ||
return nil // This will never be reached, but is required for compilation. | ||
} | ||
|
||
func (e OPCommitment) MustGenericCommitmentValue() DAServiceOPCommitment { | ||
if e.genericCommitment != nil { | ||
return *e.genericCommitment | ||
} | ||
log.Panic("OPCommitment does not contain a DAServiceCommitment value") | ||
return DAServiceOPCommitment{} // This will never be reached, but is required for compilation. | ||
} | ||
|
||
func (e OPCommitment) Marshal() ([]byte, error) { | ||
if e.IsGenericCommitment() { | ||
bytes, err := e.MustGenericCommitmentValue().Marshal() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append([]byte{byte(GenericCommitmentTypeByte)}, bytes...), nil | ||
} else if e.IsKeccak256Commitment() { | ||
return append([]byte{byte(Keccak256CommitmentTypeByte)}, e.MustKeccak256CommitmentValue()...), nil | ||
} else { | ||
return nil, fmt.Errorf("OPCommitment is neither a Keccak256 commitment nor a DA service commitment") | ||
} | ||
} | ||
|
||
func (e *OPCommitment) Unmarshal(bz []byte) error { | ||
if len(bz) < 1 { | ||
return fmt.Errorf("OPCommitment does not contain a commitment type prefix byte") | ||
} | ||
head := OPCommitmentType(bz[0]) | ||
tail := bz[1:] | ||
switch head { | ||
case Keccak256CommitmentTypeByte: | ||
e.keccak256Commitment = tail | ||
case GenericCommitmentTypeByte: | ||
daServiceCommitment := DAServiceOPCommitment{} | ||
err := daServiceCommitment.Unmarshal(tail) | ||
if err != nil { | ||
return err | ||
} | ||
e.genericCommitment = &daServiceCommitment | ||
default: | ||
return fmt.Errorf("unrecognized commitment type byte: %x", bz[0]) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.