github.com/consensys/gnark-crypto@v0.14.0/ecc/stark-curve/pedersen-hash/pedersen_hash_test.go (about)

     1  package pedersenhash
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
     8  )
     9  
    10  func TestPedersen(t *testing.T) {
    11  	tests := []struct {
    12  		a, b string
    13  		want string
    14  	}{
    15  		{
    16  			"0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb",
    17  			"0x0208a0a10250e382e1e4bbe2880906c2791bf6275695e02fbbc6aeff9cd8b31a",
    18  			"0x030e480bed5fe53fa909cc0f8c4d99b8f9f2c016be4c41e13a4848797979c662",
    19  		},
    20  		{
    21  			"0x58f580910a6ca59b28927c08fe6c43e2e303ca384badc365795fc645d479d45",
    22  			"0x78734f65a067be9bdb39de18434d71e79f7b6466a4b66bbd979ab9e7515fe0b",
    23  			"0x68cc0b76cddd1dd4ed2301ada9b7c872b23875d5ff837b3a87993e0d9996b87",
    24  		},
    25  	}
    26  	for i, tt := range tests {
    27  		t.Run(fmt.Sprintf("TestHash %d", i), func(t *testing.T) {
    28  			a, err := new(fp.Element).SetString(tt.a)
    29  			if err != nil {
    30  				t.Errorf("expected no error but got %s", err)
    31  			}
    32  			b, err := new(fp.Element).SetString(tt.b)
    33  			if err != nil {
    34  				t.Errorf("expected no error but got %s", err)
    35  			}
    36  
    37  			want, err := new(fp.Element).SetString(tt.want)
    38  			if err != nil {
    39  				t.Errorf("expected no error but got %s", err)
    40  			}
    41  
    42  			ans := Pedersen(a, b)
    43  			if !ans.Equal(want) {
    44  				t.Errorf("TestHash got %s, want %s", ans.Text(16), want.Text(16))
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func TestPedersenArray(t *testing.T) {
    51  	tests := [...]struct {
    52  		input []string
    53  		want  string
    54  	}{
    55  		// Contract address calculation. See the following links for how the
    56  		// calculation is carried out and the result referenced.
    57  		//
    58  		// https://docs.starknet.io/documentation/develop/Contracts/contract-address/
    59  		// https://alpha4.starknet.io/feeder_gateway/get_transaction?transactionHash=0x1b50380d45ebd70876518203f131a12428b2ac1a3a75f1a74241a4abdd614e8
    60  		{
    61  			input: []string{
    62  				// Hex representation of []byte("STARKNET_CONTRACT_ADDRESS").
    63  				"0x535441524b4e45545f434f4e54524143545f41444452455353",
    64  				// caller_address.
    65  				"0x0",
    66  				// salt.
    67  				"0x5bebda1b28ba6daa824126577b9fbc984033e8b18360f5e1ef694cb172c7aa5",
    68  				// contract_hash. See the following for reference https://alpha4.starknet.io/feeder_gateway/get_block?blockHash=0x53e61cb9a53136ecb782e7396f7330e6bb3d069763d866612da3cf93cdf55b5.
    69  				"0x0439218681f9108b470d2379cf589ef47e60dc5888ee49ec70071671d74ca9c6",
    70  				// calldata_hash. (here h(0, 0) where h is the Pedersen hash
    71  				// function).
    72  				"0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804",
    73  			},
    74  			// contract_address.
    75  			want: "0x43c6817e70b3fd99a4f120790b2e82c6843df62b573fdadf9e2d677b60ac5eb",
    76  		},
    77  		// Transaction hash calculation. See the following for reference.
    78  		//
    79  		// https://alpha-mainnet.starknet.io/feeder_gateway/get_transaction?transactionHash=e0a2e45a80bb827967e096bcf58874f6c01c191e0a0530624cba66a508ae75.
    80  		{
    81  			input: []string{
    82  				// Hex representation of []byte("deploy").
    83  				"0x6465706c6f79",
    84  				// contract_address.
    85  				"0x20cfa74ee3564b4cd5435cdace0f9c4d43b939620e4a0bb5076105df0a626c6",
    86  				// Hex representation of keccak.Digest250([]byte("constructor")).
    87  				"0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
    88  				// calldata_hash.
    89  				"0x7885ba4f628b6cdcd0b5e6282d2a1b17fe7cd4dd536230c5db3eac890528b4d",
    90  				// chain_id. Hex representation of []byte("SN_MAIN").
    91  				"0x534e5f4d41494e",
    92  			},
    93  			want: "0xe0a2e45a80bb827967e096bcf58874f6c01c191e0a0530624cba66a508ae75",
    94  		},
    95  		// Hash of an empty array is defined to be h(0, 0).
    96  		{
    97  			input: make([]string, 0),
    98  			// The value below was found using the reference implementation. See:
    99  			// https://github.com/starkware-libs/cairo-lang/blob/de741b92657f245a50caab99cfaef093152fd8be/src/starkware/crypto/signature/fast_pedersen_hash.py#L34
   100  			want: "0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804",
   101  		},
   102  	}
   103  	for _, test := range tests {
   104  		var data []*fp.Element
   105  		for _, item := range test.input {
   106  			elem, _ := new(fp.Element).SetString(item)
   107  			data = append(data, elem)
   108  		}
   109  		want, _ := new(fp.Element).SetString(test.want)
   110  		got := PedersenArray(data...)
   111  		if !got.Equal(want) {
   112  			t.Errorf("PedersenArray(%x) = %x, want %x", data, got, want)
   113  		}
   114  	}
   115  }
   116  
   117  var feltBench fp.Element
   118  
   119  // go test -bench=. -run=^# -cpu=1,2,4,8,16
   120  func BenchmarkPedersenArray(b *testing.B) {
   121  	numOfElems := []int{3, 5, 10, 15, 20, 25, 30, 35, 40}
   122  	createRandomFelts := func(n int) []*fp.Element {
   123  		var felts []*fp.Element
   124  		for i := 0; i < n; i++ {
   125  			f, err := new(fp.Element).SetRandom()
   126  			if err != nil {
   127  				b.Fatalf("error while generating random felt: %x", err)
   128  			}
   129  			felts = append(felts, f)
   130  		}
   131  		return felts
   132  	}
   133  
   134  	for _, i := range numOfElems {
   135  		b.Run(fmt.Sprintf("Number of felts: %d", i), func(b *testing.B) {
   136  			var f fp.Element
   137  			randomFelts := createRandomFelts(i)
   138  			for n := 0; n < b.N; n++ {
   139  				f = PedersenArray(randomFelts...)
   140  			}
   141  			feltBench = f
   142  		})
   143  	}
   144  }
   145  
   146  func BenchmarkPedersen(b *testing.B) {
   147  	e0, err := new(fp.Element).SetString("0x3d937c035c878245caf64531a5756109c53068da139362728feb561405371cb")
   148  	if err != nil {
   149  		b.Errorf("Error occurred %s", err)
   150  	}
   151  
   152  	e1, err := new(fp.Element).SetString("0x208a0a10250e382e1e4bbe2880906c2791bf6275695e02fbbc6aeff9cd8b31a")
   153  	if err != nil {
   154  		b.Errorf("Error occurred %s", err)
   155  	}
   156  
   157  	var f fp.Element
   158  	for n := 0; n < b.N; n++ {
   159  		f = Pedersen(e0, e1)
   160  	}
   161  	feltBench = f
   162  }