Skip to content

Commit

Permalink
fix offset bug
Browse files Browse the repository at this point in the history
  • Loading branch information
epociask committed Jul 12, 2024
1 parent a4535fe commit e229225
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 106 deletions.
4 changes: 2 additions & 2 deletions arbitrator/prover/src/kzgbn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn prove_kzg_preimage_bn254(

let mut proving_offset = offset;

let length_usize = preimage.len() as usize;
let length_usize = preimage_polynomial.len() as usize;

// address proving past end edge case later
let proving_past_end = offset as usize >= length_usize;
Expand All @@ -103,7 +103,7 @@ pub fn prove_kzg_preimage_bn254(

let proven_y_fr = preimage_polynomial
.get_at_index(proving_offset as usize)
.ok_or_else(|| eyre::eyre!("Index out of bounds"))?;
.ok_or_else(|| eyre::eyre!("Index ({}) out of bounds for preimage of length {} with data of size {}", proving_offset, length_usize, preimage_polynomial.len()))?;

let z_fr = kzg
.get_nth_root_of_unity(proving_offset as usize)
Expand Down
202 changes: 99 additions & 103 deletions arbitrator/prover/test-cases/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ package main

import (
"bytes"
"crypto/sha512"
"encoding/hex"
"fmt"
"math/big"
"os"
"runtime"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
merkletree "github.com/wealdtech/go-merkletree"

"github.com/offchainlabs/nitro/arbcompress"
Expand Down Expand Up @@ -112,106 +108,106 @@ func main() {
runtime.GC()
time.Sleep(time.Second)

fmt.Printf("Stylus test\n")

callStylusProgram(5)

fmt.Printf("Stylus test done!\n")

// Data for the tree
data := [][]byte{
[]byte("Foo"),
[]byte("Bar"),
[]byte("Baz"),
}

var wg sync.WaitGroup

wg.Add(1)
go func() {
verified, err := MerkleSample(data, 0)
if err != nil {
panic(err)
}
if !verified {
panic("failed to verify proof for Baz")
}
wg.Done()
}()
wg.Add(1)
go func() {
verified, err := MerkleSample(data, 1)
if err != nil {
panic(err)
}
if !verified {
panic("failed to verify proof for Baz")
}
wg.Done()
}()
wg.Add(1)
go func() {
verified, err := MerkleSample(data, -1)
if err != nil {
if verified {
panic("succeeded to verify proof invalid")
}
}
wg.Done()
}()
wg.Wait()
println("verified proofs with waitgroup!\n")

doneChan1 := make(chan struct{})
doneChan2 := make(chan struct{})
go testCompression([]byte{}, doneChan1)
go testCompression([]byte("This is a test string la la la la la la la la la la"), doneChan2)
<-doneChan2
<-doneChan1

println("compression + chan test passed!\n")

if wavmio.GetInboxPosition() != 0 {
panic("unexpected inbox pos")
}
if wavmio.GetLastBlockHash() != (common.Hash{}) {
panic("unexpected lastblock hash")
}
println("wavmio test passed!\n")

checkPreimage := func(ty arbutil.PreimageType, hash common.Hash) {
preimage, err := wavmio.ResolveTypedPreimage(ty, hash)
if err != nil {
panic(fmt.Sprintf("failed to resolve preimage of type %v: %v", ty, err))
}
if !bytes.Equal(preimage, []byte("hello world")) {
panic(fmt.Sprintf("got wrong preimage of type %v: %v", ty, hex.EncodeToString(preimage)))
}
}

checkPreimage(arbutil.Keccak256PreimageType, common.HexToHash("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"))
checkPreimage(arbutil.Sha2_256PreimageType, common.HexToHash("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"))

kzgPreimage, err := wavmio.ResolveTypedPreimage(arbutil.EthVersionedHashPreimageType, common.HexToHash("01c277af4074155da57fd0f1065fc8b2e1d475e6639371b7300a2f1fb46296fa"))
if err != nil {
panic(fmt.Sprintf("failed to resolve eth versioned hash preimage: %v", err))
}
blobLength := FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT
if len(kzgPreimage) != blobLength {
panic(fmt.Sprintf("expected blob length to be %v but got %v", blobLength, len(kzgPreimage)))
}
for i := 0; i < FIELD_ELEMENTS_PER_BLOB; i++ {
hash := sha512.Sum512([]byte(fmt.Sprintf("%v", i)))
scalar := new(big.Int).SetBytes(hash[:])
scalar.Mod(scalar, BLS_MODULUS)
expectedElement := math.U256Bytes(scalar)
gotElement := kzgPreimage[i*BYTES_PER_FIELD_ELEMENT : (i+1)*BYTES_PER_FIELD_ELEMENT]
if !bytes.Equal(gotElement, expectedElement) {
panic(fmt.Sprintf("expected blob element %v to be %v but got %v", i, hex.EncodeToString(expectedElement), hex.EncodeToString(gotElement)))
}
}
// fmt.Printf("Stylus test\n")

// callStylusProgram(5)

// fmt.Printf("Stylus test done!\n")

// // Data for the tree
// data := [][]byte{
// []byte("Foo"),
// []byte("Bar"),
// []byte("Baz"),
// }

// var wg sync.WaitGroup

// wg.Add(1)
// go func() {
// verified, err := MerkleSample(data, 0)
// if err != nil {
// panic(err)
// }
// if !verified {
// panic("failed to verify proof for Baz")
// }
// wg.Done()
// }()
// wg.Add(1)
// go func() {
// verified, err := MerkleSample(data, 1)
// if err != nil {
// panic(err)
// }
// if !verified {
// panic("failed to verify proof for Baz")
// }
// wg.Done()
// }()
// wg.Add(1)
// go func() {
// verified, err := MerkleSample(data, -1)
// if err != nil {
// if verified {
// panic("succeeded to verify proof invalid")
// }
// }
// wg.Done()
// }()
// wg.Wait()
// println("verified proofs with waitgroup!\n")

// doneChan1 := make(chan struct{})
// doneChan2 := make(chan struct{})
// go testCompression([]byte{}, doneChan1)
// go testCompression([]byte("This is a test string la la la la la la la la la la"), doneChan2)
// <-doneChan2
// <-doneChan1

// println("compression + chan test passed!\n")

// if wavmio.GetInboxPosition() != 0 {
// panic("unexpected inbox pos")
// }
// if wavmio.GetLastBlockHash() != (common.Hash{}) {
// panic("unexpected lastblock hash")
// }
// println("wavmio test passed!\n")

// checkPreimage := func(ty arbutil.PreimageType, hash common.Hash) {
// preimage, err := wavmio.ResolveTypedPreimage(ty, hash)
// if err != nil {
// panic(fmt.Sprintf("failed to resolve preimage of type %v: %v", ty, err))
// }
// if !bytes.Equal(preimage, []byte("hello world")) {
// panic(fmt.Sprintf("got wrong preimage of type %v: %v", ty, hex.EncodeToString(preimage)))
// }
// }

// checkPreimage(arbutil.Keccak256PreimageType, common.HexToHash("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"))
// checkPreimage(arbutil.Sha2_256PreimageType, common.HexToHash("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"))

// kzgPreimage, err := wavmio.ResolveTypedPreimage(arbutil.EthVersionedHashPreimageType, common.HexToHash("01c277af4074155da57fd0f1065fc8b2e1d475e6639371b7300a2f1fb46296fa"))
// if err != nil {
// panic(fmt.Sprintf("failed to resolve eth versioned hash preimage: %v", err))
// }
// blobLength := FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT
// if len(kzgPreimage) != blobLength {
// panic(fmt.Sprintf("expected blob length to be %v but got %v", blobLength, len(kzgPreimage)))
// }
// for i := 0; i < FIELD_ELEMENTS_PER_BLOB; i++ {
// hash := sha512.Sum512([]byte(fmt.Sprintf("%v", i)))
// scalar := new(big.Int).SetBytes(hash[:])
// scalar.Mod(scalar, BLS_MODULUS)
// expectedElement := math.U256Bytes(scalar)
// gotElement := kzgPreimage[i*BYTES_PER_FIELD_ELEMENT : (i+1)*BYTES_PER_FIELD_ELEMENT]
// if !bytes.Equal(gotElement, expectedElement) {
// panic(fmt.Sprintf("expected blob element %v to be %v but got %v", i, hex.EncodeToString(expectedElement), hex.EncodeToString(gotElement)))
// }
// }
// EIGENDA COMMIT HASH
_, err = wavmio.ResolveTypedPreimage(arbutil.EigenDaPreimageType, common.HexToHash("011e229d75b13559dcb2d757ecae9b66fa579268e28e196789503322115c06e1"))
_, err := wavmio.ResolveTypedPreimage(arbutil.EigenDaPreimageType, common.HexToHash("011e229d75b13559dcb2d757ecae9b66fa579268e28e196789503322115c06e1"))
if err != nil {
panic(fmt.Sprintf("failed to resolve eigenda preimage: %v", err))
}
Expand Down

0 comments on commit e229225

Please sign in to comment.